Class: Kitchen::Provisioner::Dsc

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/provisioner/dsc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tmp_dirObject

Returns the value of attribute tmp_dir.



20
21
22
# File 'lib/kitchen/provisioner/dsc.rb', line 20

def tmp_dir
  @tmp_dir
end

Instance Method Details

#create_sandboxObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kitchen/provisioner/dsc.rb', line 64

def create_sandbox
  super
  info("Staging DSC Resource Modules for copy to the SUT")
  if powershell_module?
    prepare_resource_style_directory
  else
    prepare_repo_style_directory
  end
  info("Staging DSC configuration script for copy to the SUT")
  prepare_configuration_script
end

#finalize_config!(instance) ⇒ Object



40
41
42
43
# File 'lib/kitchen/provisioner/dsc.rb', line 40

def finalize_config!(instance)
  config[:dsc_local_configuration_manager] = lcm.lcm_config
  super(instance)
end

#init_commandObject



56
57
58
59
60
61
62
# File 'lib/kitchen/provisioner/dsc.rb', line 56

def init_command
  script = <<~EOH
    #{setup_config_directory_script}
    #{install_module_script if install_modules?}
  EOH
  wrap_powershell_code(script)
end

#install_commandObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/kitchen/provisioner/dsc.rb', line 45

def install_command
  full_lcm_configuration_script = <<-EOH
  #{lcm.lcm_configuration_script}

  $null = SetupLCM
  Set-DscLocalConfigurationManager -Path ./SetupLCM | out-null
  EOH

  wrap_powershell_code(full_lcm_configuration_script)
end

#prepare_commandObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/kitchen/provisioner/dsc.rb', line 76

def prepare_command
  info("Moving DSC Resources onto PSModulePath")
  scripts = <<-EOH

  if (Test-Path (join-path #{config[:root_path]} 'modules'))
  {
    dir ( join-path #{config[:root_path]} 'modules/*') -directory |
    copy-item -destination $env:programfiles/windowspowershell/modules/ -recurse -force
  }

  $ConfigurationScriptPath = Join-path #{config[:root_path]} #{sandboxed_configuration_script}
  if (-not (test-path $ConfigurationScriptPath))
  {
    throw "Failed to find $ConfigurationScriptPath"
  }
  invoke-expression (get-content $ConfigurationScriptPath -raw)

  EOH
  ensure_array(config[:configuration_name]).each do |configuration|
    info("Generating the MOF script for the configuration #{configuration}")
    stage_resources_and_generate_mof_script = <<-EOH

      if(Test-Path c:/configurations/#{configuration})
      {
          Remove-Item -Recurse -Force c:/configurations/#{configuration}
      }

      $Error.clear()

      if (-not (test-path 'c:/configurations'))
      {
        mkdir 'c:/configurations' | out-null
      }

      if (-not (get-command #{configuration}))
      {
        throw "Failed to create a configuration command #{configuration}"
      }

      #{configuration_data_assignment unless config[:configuration_data].nil?}

      try{
        $null = #{configuration} -outputpath c:/configurations/#{configuration} #{"-configurationdata $" + configuration_data_variable}
      }
      catch{
      }

      if($Error -ne $null)
      {
        $Error[-1]
        exit 1
      }

    EOH
    scripts << stage_resources_and_generate_mof_script
  end
  debug("Shelling out: #{scripts}")
  wrap_powershell_code(scripts)
end

#run_commandObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/kitchen/provisioner/dsc.rb', line 136

def run_command
  config[:retry_on_exit_code] = [35] if config[:retry_on_exit_code].empty?
  config[:max_retries] = 3 if config[:max_retries] == 1
  scripts = ""
  ensure_array(config[:configuration_name]).each do |configuration|
    info("Running the configuration #{configuration}")
    run_configuration_script = <<-EOH
      $job = start-dscconfiguration -Path c:/configurations/#{configuration} -force
      $job | wait-job
      $verbose_output = $job.childjobs[0].verbose
      $verbose_output
      if ($verbose_output -match 'A reboot is required to progress further. Please reboot the system.') {
        "A reboot is required to continue."
        shutdown /r /t 15
        exit 35
      }
      $dsc_errors = $job.childjobs[0].Error
      if ($dsc_errors -ne $null) {
        $dsc_errors
        exit 1
      }
    EOH
    scripts << run_configuration_script
  end
  debug("Shelling out: #{scripts}")
  wrap_powershell_code(scripts)
end