Skip to content

Commit

Permalink
Added support for environment_variables in windows extension. (#193)
Browse files Browse the repository at this point in the history
* Added support for environment_variables in windows extension

Signed-off-by: Piyush <[email protected]>
  • Loading branch information
piyushawasthi authored and btm committed Mar 8, 2017
1 parent 7288af8 commit 983990b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 34 deletions.
12 changes: 11 additions & 1 deletion ChefExtensionHandler/bin/chef-install.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@ function Get-ChefPackage {
Get-WmiObject -Class Win32_Product | Where-Object { $_.Name.contains("Chef Client") }
}

function Read-Environment-Variables {
$powershellVersion = Get-PowershellVersion
$environment_variables = Get-PublicSettings-From-Config-Json "environment_variables" $powershellVersion
if ( $environment_variables ){
Chef-SetCustomEnvVariables $environment_variables $powershellVersion
} else {
echo "Environment variables not passed."
}
}

function Install-ChefClient {
# Source the shared PS
. $(Get-SharedHelper)
$powershellVersion = Get-PowershellVersion

Read-Environment-Variables
# Install Chef Client
$retries = 3
$retrycount = 0
Expand Down
21 changes: 21 additions & 0 deletions ChefExtensionHandler/bin/shared.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ function Chef-AddToPath($folderPath)
[Environment]::SetEnvironmentVariable("Path", "$folderPath;$currentPath", "Process")
}

function Chef-SetCustomEnvVariables($envHash, $powershellVersion)
{
if ( $powershellVersion -ge 3 ) {
$props=Get-Member -InputObject $envHash -MemberType NoteProperty
foreach($prop in $props) {
$propValue=$envHash | Select-Object -ExpandProperty $prop.Name
if (-not (Test-Path Env:$prop.Name)){
[Environment]::SetEnvironmentVariable($prop.Name, $propValue, "Machine")
[Environment]::SetEnvironmentVariable($prop.Name, $propValue, "User")
[Environment]::SetEnvironmentVariable($prop.Name, $propValue, "Process")
}
}
} else {
foreach($prop in $envHash.GetEnumerator()) {
[Environment]::SetEnvironmentVariable($prop.Key, $prop.Value, "Machine")
[Environment]::SetEnvironmentVariable($prop.Key, $prop.Value, "User")
[Environment]::SetEnvironmentVariable($prop.Key, $prop.Value, "Process")
}
}
}

function Get-PowershellVersion {
$PSVersionTable.PSVersion.Major
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ It is an ordered list of roles and/or recipes that are run in the exact order de
`service` configures chef-client as a service.
`task` configures chef-client as a scheduled task for defined interval. Default value is 30 mins.

`environment_variables`: Specifies the list of environment variables (like the environment variables for proxy server configuration) to be available to the Chef Extension scripts. This option is currently supported only for `Linux` platforms.
`environment_variables`: Specifies the list of environment variables (like the environment variables for proxy server configuration) to be available to the Chef Extension scripts.

`chef_service_interval`: Specifies the frequency (in minutes) at which the `chef-client` runs as `service` or as `scheduled task`. If in case you don't want the `chef-service` or `scheduled task` to be installed on the Azure VM then set value as `0` in this field. At any time you can change the interval value using the `Set-AzureVMExtension` command with the new interval passed in the `publicconfig.config` file (pass `0` if you want to delete the already installed chef-service on the Azure VM). Default value is `30` minutes.

Expand Down Expand Up @@ -148,7 +148,7 @@ Update-AzureVM -VM $vmOb.VM -Name "<vm-name>" -ServiceName "<cloud-service-name>
1. Please refer https://github.com/Azure/azure-quickstart-templates/tree/master/chef-json-parameters-linux-vm of creating the ARM template files.

2. Find below some advanced options that can be set in the Azure ARM template file `azuredeploy.json`:
- `environment_variables`: Specifies the list of environment variables (like the environment variables for proxy server configuration) to be available to the Chef Extension scripts. This option is currently supported only for `Linux` platforms.
- `environment_variables`: Specifies the list of environment variables (like the environment variables for proxy server configuration) to be available to the Chef Extension scripts.
- `hints`: Specifies the Ohai Hints to be set in the Ohai configuration of the target node.

***Note***: Set both these options under `properties` --> `settings` section of the `Microsoft.Compute/virtualMachines/extensions` resource type as shown in the below example:
Expand Down
54 changes: 25 additions & 29 deletions lib/chef/azure/helpers/parse_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,32 @@ def is_numeric(sequence)
end

def escape_unescaped_content(file_content)
lines = file_content.lines.to_a
# convert tabs to spaces -- technically invalidates content, but
# if we know the content in question treats tabs and spaces the
# same, we can do this.
untabified_lines = lines.map { | line | line.gsub(/\t/," ") }

# remove whitespace and trailing newline
stripped_lines = untabified_lines.map { | line | line.strip }
escaped_content = ""
line_index = 0

stripped_lines.each do | line |
escaped_line = line

# assume lines ending in json delimiters are not content,
# and that lines followed by a line that starts with ','
# are not content
if !!(line[line.length - 1] =~ /[\,\}\]]/) ||
(line_index < (lines.length - 1) && lines[line_index + 1][0] == ',')
escaped_line += "\n"
else
escaped_line += "\\n"
end

escaped_content += escaped_line
line_index += 1
end
lines = file_content.lines.to_a
# convert tabs to spaces -- technically invalidates content, but
# if we know the content in question treats tabs and spaces the
# same, we can do this.
untabified_lines = lines.map { | line | line.gsub(/\t/," ") }

# remove whitespace and trailing newline
stripped_lines = untabified_lines.map { | line | line.strip }
escaped_content = ""
line_index = 0

stripped_lines.each do | line |
escaped_line = line

# assume lines ending in json delimiters are not content,
# and that lines followed by a line that starts with ','
# are not content
if !!(line[line.length - 1] =~ /[\,\}\]]/) || (line_index < (lines.length - 1) && lines[line_index + 1][0] == ',')
escaped_line += "\n"
end

escaped_content
end
escaped_content += escaped_line
line_index += 1
end
escaped_content
end

def get_jsonreader_object(file_name, *keys)
file = file_name
Expand Down
1 change: 1 addition & 0 deletions spec/functional/chefserver_ssl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@decrypted_protected_settings = mock_data('ssl_certs/decrypted_protected_settings.txt')
chefserver_ssl_cert = mock_data('ssl_certs/chefserver_ssl_cert.txt')
@actual_ssl_cert = OpenSSL::X509::Certificate.new(chefserver_ssl_cert.squeeze("\n")).to_pem
@actual_ssl_cert.delete!("\n")
end

let(:extension_root) { "./" }
Expand Down
2 changes: 2 additions & 0 deletions spec/functional/validation_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@decrypted_settings = mock_data('correct_decrypted_json.txt')
validation_key = mock_data('validation_key.txt')
@key = OpenSSL::PKey::RSA.new(validation_key.squeeze("\n")).to_pem
@key.delete!("\n")
allow(instance).to receive(:windows?).and_return(true)
allow(instance).to receive(:handler_settings_file).and_return(mock_data("handler_settings.settings"))
end
Expand All @@ -42,6 +43,7 @@
@decrypted_settings = mock_data('correct_decrypted_json.txt')
validation_key = mock_data('validation_key.txt')
@key = OpenSSL::PKey::RSA.new(validation_key.squeeze("\n")).to_pem
@key.delete!("\n")
allow(instance).to receive(:windows?).and_return(false)
allow(instance).to receive(:handler_settings_file).and_return(mock_data("handler_settings.settings"))
end
Expand Down
9 changes: 7 additions & 2 deletions spec/unit/parse_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
end

describe "value_from_json_file" do
before do
@multi_lined = "first line\nsecond line\nthird line\n"
@multi_lined.delete!("\n")
end

it "returns json value from the supplied json file_content." do
file_content = '[{ "name": "Test", "version": "1.0" }]'
expect(value_from_json_file(file_content, "name")).to eq "Test"
Expand All @@ -61,11 +66,11 @@
second line
third line
" }]'
expect(value_from_json_file(file_content, "data")).to eq "first line\nsecond line\nthird line\n"
expect(value_from_json_file(file_content, "data")).to eq @multi_lined
end

it "returns json multi-lined value from the supplied json file." do
file = File.expand_path(File.dirname("spec/assets/*"))+"/test_json_file.json"
expect(value_from_json_file(file, "data")).to eq "first line\nsecond line\nthird line\n"
expect(value_from_json_file(file, "data")).to eq @multi_lined
end
end

0 comments on commit 983990b

Please sign in to comment.