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

Added uninstall_chef_client flag which determines whether to uninstall chef-client or not. #103

4 changes: 2 additions & 2 deletions ChefExtensionHandler/bin/chef-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down Expand Up @@ -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:"
Expand Down
43 changes: 40 additions & 3 deletions ChefExtensionHandler/bin/chef-uninstall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
9 changes: 7 additions & 2 deletions ChefExtensionHandler/bin/chef-uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
29 changes: 22 additions & 7 deletions ChefExtensionHandler/bin/chef-update.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions ChefExtensionHandler/bin/chef-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ 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
# We refer this file inside uninstall.sh, install.sh and enable.sh so that waagent doesn't update
# 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`"
Expand All @@ -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

Expand Down
8 changes: 8 additions & 0 deletions ChefExtensionHandler/bin/shared.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
22 changes: 12 additions & 10 deletions ChefExtensionHandler/uninstall.cmd
Original file line number Diff line number Diff line change
@@ -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
)

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%
20 changes: 19 additions & 1 deletion ChefExtensionHandler/uninstall.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down