diff --git a/ChefExtensionHandler/bin/chef-install.sh b/ChefExtensionHandler/bin/chef-install.sh index e7879f30..88fe1428 100755 --- a/ChefExtensionHandler/bin/chef-install.sh +++ b/ChefExtensionHandler/bin/chef-install.sh @@ -107,7 +107,7 @@ get_chef_version() { echo "No config file found !!" else if cat $config_file_name 2>/dev/null | grep -q "bootstrap_version"; then - chef_version=`sed 's/.*bootstrap_version":"\(.*\)".*/\1/' $config_file_name 2>/dev/null` + chef_version=`sed 's/.*bootstrap_version" *: *"\(.*\)/\1/' $config_file_name 2>/dev/null | awk -F\" '{ print $1 }'` echo $chef_version else echo "" @@ -147,7 +147,7 @@ install_from_repo_centos(){ install_from_repo_ubuntu() { #check if chef-client is already installed - dpkg-query -l chef + dpkg-query -s chef if [ $? -ne 0 ]; then echo "Starting installation:" diff --git a/ChefExtensionHandler/bin/chef-uninstall.psm1 b/ChefExtensionHandler/bin/chef-uninstall.psm1 index 428be7ff..7c58cc6f 100644 --- a/ChefExtensionHandler/bin/chef-uninstall.psm1 +++ b/ChefExtensionHandler/bin/chef-uninstall.psm1 @@ -65,15 +65,52 @@ function Delete-ChefConfig($deleteChefConfig) { } } +function Chef-GetScriptDirectory +{ + $Invocation = (Get-Variable MyInvocation -Scope 1).Value + Split-Path $Invocation.MyCommand.Path +} + +$scriptDir = Chef-GetScriptDirectory + +function Chef-GetExtensionRoot { + $chefExtensionRoot = [System.IO.Path]::GetFullPath("$scriptDir\\..") + $chefExtensionRoot +} + +function Get-SharedHelper { + $chefExtensionRoot = Chef-GetExtensionRoot + "$chefExtensionRoot\\bin\\shared.ps1" +} + function Uninstall-ChefClient { - param([boolean]$calledFromUpdate = $False) + param([boolean]$calledFromUpdate = $False, [string]$configFilePath) trap [Exception] {echo $_.Exception.Message;exit 1} - $env:Path += ";C:\opscode\chef\bin;C:\opscode\chef\embedded\bin" - # Source the shared PS . $(Get-SharedHelper) + $env:Path += ";C:\opscode\chef\bin;C:\opscode\chef\embedded\bin" + + $powershellVersion = Get-PowershellVersion + + if ($calledFromUpdate -eq $False) { + if ($powershellVersion -ge 3) { + $settingsData = Get-Content $configFilePath -Raw | ConvertFrom-Json + $uninstallChefClientFlag = $settingsData.runtimesettings.handlersettings.publicsettings.uninstallChefClient + } else { + # $calledFromUninstall = $True + # $uninstallChefClientFlag = Get-uninstallChefClientSetting $calledFromUninstall #$configFilePath + + $uninstallChefClientFlag = Get-JsonValueUsingRuby "$configFilePath" "runtimeSettings" 0 "handlerSettings" "publicSettings" "uninstallChefClient" + } + + if ($uninstallChefClientFlag -eq "false") { + Write-Host("[$(Get-Date)] Not doing Chef uninstall, as the uninstall_chef_client flag is false.") + exit 1 + } + } + # powershell has in built cmdlets: ConvertFrom-Json and ConvertTo-Json which are supported above PS v 3.0 # so the hack - use ruby json parsing for versions lower than 3.0 if ( $(Get-PowershellVersion) -ge 3 ) { diff --git a/ChefExtensionHandler/bin/chef-uninstall.sh b/ChefExtensionHandler/bin/chef-uninstall.sh index 195ae902..87fdd6d9 100755 --- a/ChefExtensionHandler/bin/chef-uninstall.sh +++ b/ChefExtensionHandler/bin/chef-uninstall.sh @@ -52,8 +52,14 @@ linux_distributor=$(get_linux_distributor) auto_update_false=/etc/chef/.auto_update_false +export PATH=$PATH:/opt/chef/embedded/bin:/opt/chef/bin + if [ -f $auto_update_false ]; then - return + uninstall_chef_client=`ruby -e "require 'chef/azure/helpers/parse_json';value_from_json_file_for_ps '$handler_settings_file','runtimeSettings','0','handlerSettings','publicSettings','uninstallChefClient'"` + if [ "$uninstall_chef_client" = "true" ]; then + echo "Invalid config specified...uninstallChefClient flag cannot be true when autoUpdateClient flag is false." >> /var/log/azure/custom.log + fi + exit 1 fi update_process_descriptor=/etc/chef/.updating_chef_extension @@ -64,7 +70,6 @@ if [ -f $update_process_descriptor ]; then echo "[$(date)] Not doing uninstall, as the update process is running" rm $update_process_descriptor else - export PATH=$PATH:/opt/chef/embedded/bin:/opt/chef/bin get_script_dir(){ SCRIPT=$(readlink -f "$0") diff --git a/ChefExtensionHandler/bin/chef-update.psm1 b/ChefExtensionHandler/bin/chef-update.psm1 index d0e997d2..89a41e83 100644 --- a/ChefExtensionHandler/bin/chef-update.psm1 +++ b/ChefExtensionHandler/bin/chef-update.psm1 @@ -51,17 +51,30 @@ function Update-ChefClient { $autoUpdateClient = Get-autoUpdateClientSetting } + # Import Chef Install and Chef Uninstall PS modules + Import-Module "$(Chef-GetExtensionRoot)\\bin\\chef-install.psm1" + + if ($powershellVersion -ge 3) { + $json_handlerSettings = Get-PreviousVersionHandlerSettings + $uninstallChefClient = $json_handlerSettings.publicSettings.uninstallChefClient + } else { + $uninstallChefClient = Get-uninstallChefClientSetting + } + Write-Host "[$(Get-Date)] AutoUpdateClient: $autoUpdateClient" # Auto update flag in Runtime Settings allows the user to opt for automatic chef-client update. # Default value is false if($autoUpdateClient -ne "true"){ Write-Host "[$(Get-Date)] Auto update disabled" - return + if ($uninstallChefClient -eq "true"){ + Write-Host "Invalid config specified...uninstallChefClient flag cannot be true when autoUpdateClient flag is false." + } + exit 1 } - # Import Chef Install and Chef Uninstall PS modules - Import-Module "$(Chef-GetExtensionRoot)\\bin\\chef-install.psm1" - #Import-Module "$(Chef-GetExtensionRoot)\\bin\\chef-uninstall.psm1" + if ($uninstallChefClient -eq "true"){ + Import-Module "$(Chef-GetExtensionRoot)\\bin\\chef-uninstall.psm1" + } Try { @@ -75,9 +88,11 @@ function Update-ChefClient { Write-Host "[$(Get-Date)] Configuration saved to $backupLocation" # uninstall chef. this will work since the uninstall script is idempotent. - #echo "Calling Uninstall-ChefClient from $scriptDir\chef-uninstall.psm1" - #Uninstall-ChefClient $calledFromUpdate - #Write-Host "[$(Get-Date)] Uninstall completed" + if ($uninstallChefClient -eq "true"){ + echo "Calling Uninstall-ChefClient from $scriptDir\chef-uninstall.psm1" + Uninstall-ChefClient $calledFromUpdate + Write-Host "[$(Get-Date)] Uninstall completed" + } # Restore Chef Configuration Copy-Item $backupLocation $bootstrapDirectory -recurse diff --git a/ChefExtensionHandler/bin/chef-update.sh b/ChefExtensionHandler/bin/chef-update.sh index bf48969c..15ccbf8f 100755 --- a/ChefExtensionHandler/bin/chef-update.sh +++ b/ChefExtensionHandler/bin/chef-update.sh @@ -33,6 +33,7 @@ previous_extension="$waagentdir/$previous_extension" handler_settings_file=`ls $previous_extension/config/*.settings -S -r | head -1` auto_update_client=`ruby -e "require 'chef/azure/helpers/parse_json';value_from_json_file_for_ps '$handler_settings_file','runtimeSettings','0','handlerSettings','publicSettings','autoUpdateClient'"` +uninstall_chef_client=`ruby -e "require 'chef/azure/helpers/parse_json';value_from_json_file_for_ps '$handler_settings_file','runtimeSettings','0','handlerSettings','publicSettings','uninstallChefClient'"` if [ "$auto_update_client" != "true" ] then # touch the auto_update_false @@ -40,7 +41,10 @@ then # even if autoUpdateClient=false. # Waagent itself spawns processes for uninstall, install and enable otherwise. touch /etc/chef/.auto_update_false - return + if [ "$uninstall_chef_client" = "true" ]; then + echo "Invalid config specified...uninstallChefClient flag cannot be true when autoUpdateClient flag is false." >> /var/log/azure/custom.log + fi + exit 1 fi BACKUP_FOLDER="etc_chef_extn_update_`date +%s`" @@ -51,7 +55,9 @@ called_from_update="update" # Save chef configuration. mv /etc/chef /tmp/$BACKUP_FOLDER # uninstall chef. -#sh $commands_script_path/chef-uninstall.sh "$called_from_update" +if [ "$uninstall_chef_client" = "true" ]; then + sh $commands_script_path/chef-uninstall.sh "$called_from_update" +fi # Restore Chef Configuration mv /tmp/$BACKUP_FOLDER /etc/chef diff --git a/ChefExtensionHandler/bin/shared.ps1 b/ChefExtensionHandler/bin/shared.ps1 index 9df75b45..349b9da6 100644 --- a/ChefExtensionHandler/bin/shared.ps1 +++ b/ChefExtensionHandler/bin/shared.ps1 @@ -194,3 +194,11 @@ function Get-deleteChefConfigSetting { } return $deleteChefConfig } + +# Get the uninstall chef client setting for powershell 2 +function Get-uninstallChefClientSetting{ + $extensionPreviousVersion = Get-PreviousExtensionVersion + $latestSettingFile = Get-HandlerSettingsFileName "$chefExtensionParent\\$extensionPreviousVersion" + + Get-JsonValueUsingRuby "$chefExtensionParent\\$extensionPreviousVersion\\RuntimeSettings\\$latestSettingFile" "runtimeSettings" 0 "handlerSettings" "publicSettings" "uninstallChefClient" +} diff --git a/ChefExtensionHandler/uninstall.cmd b/ChefExtensionHandler/uninstall.cmd index f8fcd3ae..0b27af02 100644 --- a/ChefExtensionHandler/uninstall.cmd +++ b/ChefExtensionHandler/uninstall.cmd @@ -1,10 +1,12 @@ - -set CHEF_EXT_DIR=%~dp0 - -echo %CHEF_EXT_DIR% - -set uninstall_chef_client=false - -if "%uninstall_chef_client%"=="true" ( - powershell -nologo -noprofile -executionpolicy unrestricted Import-Module %CHEF_EXT_DIR%bin\chef-uninstall.psm1;Uninstall-ChefClient -) \ No newline at end of file + +set CHEF_EXT_DIR=%~dp0 + +echo %CHEF_EXT_DIR% + +mode con:cols=150 lines=70 + +set get_config_file_path_cmd=powershell -nologo -noprofile -executionpolicy unrestricted -Command ". %CHEF_EXT_DIR%bin\shared.ps1;Get-HandlerSettingsFilePath" + +for /f "delims=" %%I in ('%get_config_file_path_cmd%') do set "config_file_path=%%I" + +powershell -nologo -noprofile -executionpolicy unrestricted Import-Module %CHEF_EXT_DIR%bin\chef-uninstall.psm1;Uninstall-ChefClient 0 %config_file_path% diff --git a/ChefExtensionHandler/uninstall.sh b/ChefExtensionHandler/uninstall.sh index 85b7b3be..b1a6e35d 100755 --- a/ChefExtensionHandler/uninstall.sh +++ b/ChefExtensionHandler/uninstall.sh @@ -1,12 +1,30 @@ #!/bin/sh +get_config_settings_file() { + config_files_path="$1/config/*.settings" + config_file_name=`ls $config_files_path 2>/dev/null | sort -V | tail -1` + + echo $config_file_name +} + +get_uninstall_chef_client_flag() { + if [ -z "$1" ]; then + echo "false" + else + export PATH=$PATH:/opt/chef/bin/:/opt/chef/embedded/bin + uninstall_chef_client_flag=`ruby -e "require 'chef/azure/helpers/parse_json';value_from_json_file_for_ps '$1','runtimeSettings','0','handlerSettings','publicSettings','uninstallChefClient'"` + echo $uninstall_chef_client_flag + fi +} + SCRIPT=$(readlink -f "$0") CHEF_EXT_DIR=$(dirname "$SCRIPT") echo $CHEF_EXT_DIR >> /var/log/azure/custom.log -uninstall_chef_client="false" +config_file_name=$(get_config_settings_file $CHEF_EXT_DIR) +uninstall_chef_client=$(get_uninstall_chef_client_flag $config_file_name) if [ "$uninstall_chef_client" = "true" ]; then $CHEF_EXT_DIR/bin/chef-uninstall.sh >> /var/log/azure/custom.log