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

Decouple chef-client installation for windows - using omnitruck API #76

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions ChefExtensionHandler/bin/chef-install.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function Get-SharedHelper {
"$chefExtensionRoot\\bin\\shared.ps1"
}

function Get-LocalDestinationMsiPath($chefExtensionRoot) {
[System.IO.Path]::GetFullPath("$chefExtensionRoot\\installer\\chef-client-latest.msi")
function Get-LocalDestinationMsiPath {
[System.IO.Path]::GetFullPath("$env:temp\\chef-client-latest.msi")
}

function Install-ChefClient {
Expand All @@ -65,14 +65,31 @@ function Install-ChefClient {
Archive-ChefClientLog $chefClientMsiLogPath
}

$localDestinationMsiPath = Get-LocalDestinationMsiPath $chefExtensionRoot
Download-ChefClient

$localDestinationMsiPath = Get-LocalDestinationMsiPath

Run-ChefInstaller $localDestinationMsiPath $chefClientMsiLogPath

$env:Path += ";C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin"

Install-AzureChefExtensionGem $chefExtensionRoot
}

function Download-ChefClient {
# TODO: Add functionality to accept version for chef-client from user and update $remoteUrl accordingly
$remoteUrl = "http://www.chef.io/chef/download?p=windows&pv=2012&m=x86_64&v=latest&prerelease=false"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think next step will be take a chef-client version from user and form this url? Please put todo note here!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if possible form pv=2012&m=x86_64&v with the help of PS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddheshwar-more I don't think there is the need for this since its going to download the same chef-client for different windows platform. Look at https://downloads.chef.io/chef-client/windows/. @adamedx Please make me correct if I am wrong here .

$localPath = "$env:temp\\chef-client-latest.msi"
$webClient = new-object System.Net.WebClient
echo "Downloading Chef Client ..."
Try {
$webClient.DownloadFile($remoteUrl, $localPath)
}
Catch{
$ErrorMessage = $_.Exception.Message
# log to CommandExecution log:
echo "Error running install: $ErrorMessage"
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add pester tests?


Export-ModuleMember -Function Install-ChefClient
Export-ModuleMember -Function Install-ChefClient
39 changes: 20 additions & 19 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,23 @@ def load_build_environment(platform, version)
version = major_minor_version.join('.')
end

if platform == "windows"
url = URI.parse('http://opscode.com/chef/metadata?v=' + version + "&prerelease=false&nightlies=false&p=windows&pv=7&m=x86_64")
elsif platform == "ubuntu"
if platform == "ubuntu"
url = URI.parse('http://opscode.com/chef/metadata?v=' + version + '&prerelease=false&nightlies=false&p=ubuntu&pv=14.04&m=x86_64')
elsif platform == "centos"
url = URI.parse('http://opscode.com/chef/metadata?v=' + version + '&prerelease=false&nightlies=false&p=centos&pv=7&m=x86_64')
end
request = Net::HTTP::Get.new(url.to_s)
response = Net::HTTP.start(url.host, url.port) {|http|
http.request(request)
}
if response.kind_of? Net::HTTPOK
download_url = response.body.split(' ')[1]
download_url
else
error_and_exit! "ERROR: Invalid chef-client version"

if url
request = Net::HTTP::Get.new(url.to_s)
response = Net::HTTP.start(url.host, url.port) {|http|
http.request(request)
}
if response.kind_of? Net::HTTPOK
download_url = response.body.split(' ')[1]
download_url
else
error_and_exit! "ERROR: Invalid chef-client version"
end
end
end

Expand Down Expand Up @@ -267,9 +268,10 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem
**********************************************
CONFIRMATION

# Get user confirmation if we are downloading correct version.
if args.confirmation_required == "true"
confirm!("build")
# Get user confirmation if we are downloading correct version.
if args.confirmation_required == "true"
confirm!("build")
end
end

puts "Building #{args.target_type} package..."
Expand Down Expand Up @@ -304,8 +306,6 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem
"#{CHEF_BUILD_DIR}/installer/chef-client-latest.deb"
when "centos"
"#{CHEF_BUILD_DIR}/installer/chef-client-latest.rpm"
else
"#{CHEF_BUILD_DIR}/installer/chef-client-latest.msi"
end

date_tag = Date.today.strftime("%Y%m%d")
Expand All @@ -314,7 +314,9 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem
# to check if package was synced in PIR.
FileUtils.touch "#{CHEF_BUILD_DIR}/version_#{args.extension_version}_#{date_tag}_#{args.target_type}"

download_chef(download_url, target_chef_pkg)
unless args.target_type == "windows"
download_chef(download_url, target_chef_pkg)
end

puts "\nCreating a zip package..."
puts "#{PACKAGE_NAME}_#{args.extension_version}_#{date_tag}_#{args.target_type}.zip\n\n"
Expand All @@ -324,7 +326,6 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem
zipfile.add(file.sub("#{CHEF_BUILD_DIR}/", ''), file)
end
end
end
end

desc "Cleans up the package sandbox"
Expand Down
17 changes: 11 additions & 6 deletions spec/ps_specs/chef-install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ describe "#Install-ChefClient" {
$extensionRoot = "C:\Packages\Plugin\ChefExtensionHandler"
mock Chef-GetExtensionRoot {return $extensionRoot}

$localMsiPath = "C:\Packages\Plugin\ChefExtensionHandler\installer\chef-client-latest.msi"
mock Download-ChefClient

$env:temp = "C:\AppData\Temp"
$localMsiPath = "$env:temp\\chef-client-latest.msi"
mock Get-LocalDestinationMsiPath {return $localMsiPath}

$chefMsiLogPath = $env:tmp
Expand All @@ -45,10 +48,13 @@ describe "#Install-ChefClient" {
# Delete temp file created for Get-SharedHelper
Remove-Item $tempPS

# Download-ChefClient should called atleast 1 time
Assert-MockCalled Download-ChefClient -Times 1

# Archive-ChefClientLog should called with $chefMsiLogPath params atleast 1 time
Assert-MockCalled Archive-ChefClientLog -Times 1 -ParameterFilter{$chefClientMsiLogPath -eq $chefMsiLogPath}

Assert-MockCalled Get-LocalDestinationMsiPath -Times 1 -ParameterFilter{$chefExtensionRoot -eq $extensionRoot}
Assert-MockCalled Get-LocalDestinationMsiPath -Times 1

Assert-MockCalled Run-ChefInstaller -Times 1 -ParameterFilter{$localDestinationMsiPath -eq $localMsiPath -and $chefClientMsiLogPath -eq $chefMsiLogPath}

Expand Down Expand Up @@ -89,10 +95,9 @@ describe "#Get-SharedHelper" {

describe "#Get-LocalDestinationMsiPath" {
it "contains chef-client-latest.msi path" {
$extensionRoot = "C:\Users\azure\azure-chef-extension\ChefExtensionHandler"
$result = Get-LocalDestinationMsiPath($extensionRoot)

$result | should Match("\\installer\\chef-client-latest.msi")
$env:temp = "C:\AppData\Temp"
$result = Get-LocalDestinationMsiPath
$result | should Match("\\chef-client-latest.msi")
}
}

Expand Down