Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy config folder #34

Closed
wants to merge 9 commits into from
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ You will see a delay in the return of the run details due to an difference in ho
* configuration_script_folder
* Defaults to 'examples'.
* The location of a PowerShell script(s) containing the DSC configuration command(s).
* The whole content of the folder will be copied to the node

* configuration_script
* Defaults to 'dsc_configuration.ps1'
* The name of the PowerShell script containing the DSC configuration command(s) (and possibly configuration data)
* can be a relative path from the configuration_script_folder
* the configuration script is dot sourced
* if the configuration_script is set to `MOF` it will not execute a configuration script but copy
the content of configuration_script_folder as-is, so that MOF from those directories can be applied.

* configuration_name
* Name of the configuration to run, defaults to the suite name.
* Name of the configurations to run, defaults to the suite name.
* This can be an array of configuration from the same configuration script.
* When applying MOFs directly, the path expected is `configuration_script_folder\configuration_name\*.mof`

* configuration_data
* A YAML representation of what should be passed to the configuration.
Expand Down Expand Up @@ -73,11 +80,11 @@ You will see a delay in the return of the run details due to an difference in ho
* gallery_uri
* URI for a custom PowerShell gallery feed.

### Specific to repository style testing
* modules_path
* Defaults to 'modules'.
* Points to the location of modules containing DSC resources to upload
* This path is relative to the root of the repository (the location of the .kitchen.yml).
* this works for both Repository and Resource style testing

## Example

Expand Down Expand Up @@ -105,4 +112,24 @@ suite:
AllNodes:
- nodename: localhost
role: webserver
```
```

Now you can also:
- apply MOF(s) directly
- copy the whole content of the `configuration_script_folder`,
allowing more complex configs
- Copy modules from module_path to the node in Resource Repo
- store your kitchen.yml file in the parent folder of a Module where
the structure follows this:
```
MODULENAME
│ .kitchen.yml
├───ModuleName
│ │ ModuleName.psd1
│ │
│ └───DscResources
└───Modules
└───xNetworking
```

100 changes: 61 additions & 39 deletions lib/kitchen/provisioner/dsc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,30 @@ def create_sandbox
end

def prepare_command
sandboxed_mof_path = File.join("configuration", config[:configuration_script_folder])

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

$configuration_name = '#{config[:configuration_name]}'
#Disabling Execution Policy for current session to allow Dot Sourcing
($ctx = $executioncontext.gettype().getfield("_context","nonpublic,instance").getvalue(
$executioncontext)).gettype().getfield("_authorizationManager","nonpublic,instance").setvalue(
$ctx, (new-object System.Management.Automation.AuthorizationManager "Microsoft.PowerShell"))

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"
$ConfigurationScriptPath = Join-path '#{config[:root_path]}' #{sandboxed_configuration_script}
if('#{config[:configuration_script]}' -ne 'MOF') {
if (-not (test-path $ConfigurationScriptPath))
{
throw "Failed to find $ConfigurationScriptPath"
}
. $ConfigurationScriptPath
}
invoke-expression (get-content $ConfigurationScriptPath -raw)

EOH
ensure_array(config[:configuration_name]).each do |configuration|
Expand All @@ -108,19 +117,25 @@ def prepare_command
mkdir 'c:/configurations' | out-null
}

if (-not (get-command #{configuration}))
if('#{config[:configuration_script]}' -eq 'MOF')
{
throw "Failed to create a configuration command #{configuration}"
$SourceMof = '#{sandboxed_mof_path}\*'
Copy-Item -force -recurse $SourceMof 'c:/configurations/'
}

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

try{
$null = #{configuration} -outputpath c:/configurations/#{configuration} #{"-configurationdata $" + configuration_data_variable}
elseif (-not (get-command #{configuration}))
{
throw "Failed to create a configuration command #{configuration}"
}
catch{
else
{
#{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]
Expand Down Expand Up @@ -241,8 +256,8 @@ def wrap_powershell_code(code)
end

def powershell_module?
module_metadata_file = File.join(config[:kitchen_root], "#{module_name}.psd1")
File.exist?(module_metadata_file)
File.exist?(File.join(config[:kitchen_root], "#{module_name}.psd1")) ||
File.exist?(File.join(config[:kitchen_root], module_name, "#{module_name}.psd1"))
end

def list_files(path)
Expand All @@ -262,32 +277,38 @@ def module_name
end

def prepare_resource_style_directory
sandbox_base_module_path = File.join(sandbox_path, "modules/#{module_name}")

sandbox_module_path = File.join(sandbox_path, "modules")
base = config[:kitchen_root]
list_files(base).each do |src|
dest = File.join(sandbox_base_module_path, src.sub("#{base}/", ""))
FileUtils.mkdir_p(File.dirname(dest))
debug("Staging #{src} ")
debug(" at #{dest}")
FileUtils.cp(src, dest, :preserve => true)
FileUtils.mkdir_p(sandbox_module_path)

if File.exist?(File.join(base, module_name, "#{module_name}.psd1"))
module_dir = File.join(base, module_name)
info("Staging Resource Module from #{module_dir}")
copy_if_dir_exists(module_dir, sandbox_module_path)
else
debug("Staging Resource Module from #{base} to #{sandbox_module_path}")
copy_if_dir_exists(base, sandbox_module_path)
end
prepare_repo_style_directory
end

def copy_if_dir_exists(src_to_validate, destination)
if Dir.exist?(src_to_validate)
debug("Moving #{src_to_validate} to #{destination}")
FileUtils.cp_r(src_to_validate, destination)
else
debug("The modules path #{src_to_validate} was not found. Not moving to #{destination}.")
end
end

def prepare_repo_style_directory
module_path = File.join(config[:kitchen_root], config[:modules_path])
sandbox_module_path = File.join(sandbox_path, "modules")

if Dir.exist?(module_path)
debug("Moving #{module_path} to #{sandbox_module_path}")
FileUtils.cp_r(module_path, sandbox_module_path)
else
debug("The modules path #{module_path} was not found. Not moving to #{sandbox_module_path}.")
end
copy_if_dir_exists("#{module_path}/.", sandbox_module_path)
end

def sandboxed_configuration_script
File.join("configuration", config[:configuration_script])
File.join("configuration", config[:configuration_script_folder], config[:configuration_script])
end

def pad(depth = 0)
Expand All @@ -308,12 +329,13 @@ def ps_hash(obj, depth = 0)
end

def prepare_configuration_script
configuration_script_file = File.join(config[:configuration_script_folder], config[:configuration_script])
configuration_script_path = File.join(config[:kitchen_root], configuration_script_file)
sandbox_configuration_script_path = File.join(sandbox_path, sandboxed_configuration_script)
FileUtils.mkdir_p(File.dirname(sandbox_configuration_script_path))
debug("Moving #{configuration_script_path} to #{sandbox_configuration_script_path}")
FileUtils.cp(configuration_script_path, sandbox_configuration_script_path)
sandbox_configuration_path = File.join(sandbox_path, 'configuration',config[:configuration_script_folder])
debug("Local sandbox folder: #{sandbox_configuration_path}")
configuration_path = File.join(config[:kitchen_root], config[:configuration_script_folder])
info("Configuration Source folder to copy: #{configuration_path}")
FileUtils.mkdir_p(sandbox_configuration_path)
debug("Copying #{configuration_path} to #{sandbox_configuration_path}")
FileUtils.cp_r("#{configuration_path}/.", sandbox_configuration_path)
end

def ensure_array(thing)
Expand Down