Skip to content

Commit d4b3fe2

Browse files
author
Nimisha Sharad
authored
Merge pull request #188 from MsysTechnologiesllc/ali/add_support_for_sch_task
Add support for chef-client scheduled task.
2 parents 64465e5 + eb1dfef commit d4b3fe2

File tree

14 files changed

+754
-255
lines changed

14 files changed

+754
-255
lines changed

ChefExtensionHandler/bin/chef-install.psm1

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -30,73 +30,6 @@ function Get-ChefPackage {
3030
Get-WmiObject -Class Win32_Product | Where-Object { $_.Name.contains("Chef Client") }
3131
}
3232

33-
function Get-PublicSettings-From-Config-Json($key, $powershellVersion) {
34-
Try
35-
{
36-
$azure_config_file = Get-Azure-Config-Path($powershellVersion)
37-
$json_contents = Get-Content $azure_config_file
38-
$normalized_json = normalize_json($json_contents)
39-
40-
if ( $powershellVersion -ge 3 ) {
41-
$value = ($normalized_json | ConvertFrom-Json | Select -expand runtimeSettings | Select -expand handlerSettings | Select -expand publicSettings).$key
42-
}
43-
else {
44-
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
45-
$value = $ser.DeserializeObject($normalized_json).runtimeSettings[0].handlerSettings.publicSettings.$key
46-
}
47-
$value
48-
}
49-
Catch
50-
{
51-
$ErrorMessage = $_.Exception.Message
52-
$FailedItem = $_.Exception.ItemName
53-
echo "Failed to read file: $FailedItem. The error message was $ErrorMessage"
54-
throw "Error in Get-PublicSettings-From-Config-Json. Couldn't parse $azure_config_file"
55-
}
56-
}
57-
58-
function normalize_json($json) {
59-
$json -Join " "
60-
}
61-
62-
function Get-Azure-Config-Path($powershellVersion) {
63-
$chefExtensionRoot = Chef-GetExtensionRoot
64-
65-
Try
66-
{
67-
# Reading chef_extension_root/HandlerEnvironment.json file
68-
$handler_file = "$chefExtensionRoot\\HandlerEnvironment.json"
69-
70-
if ( $powershellVersion -ge 3 ) {
71-
$config_folder = (((Get-Content $handler_file) | ConvertFrom-Json)[0] | Select -expand handlerEnvironment).configFolder
72-
}
73-
else {
74-
add-type -assembly system.web.extensions
75-
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
76-
$config_folder = ($ser.DeserializeObject($(Get-Content $handler_file)))[0].handlerEnvironment.configFolder
77-
}
78-
79-
# Get the last .settings file
80-
$config_files = get-childitem $config_folder -recurse | where {$_.extension -eq ".settings"}
81-
82-
if($config_files -is [system.array]) {
83-
$config_file_name = $config_files[-1].Name
84-
}
85-
else {
86-
$config_file_name = $config_files.Name
87-
}
88-
89-
"$config_folder\$config_file_name"
90-
}
91-
Catch
92-
{
93-
$ErrorMessage = $_.Exception.Message
94-
$FailedItem = $_.Exception.ItemName
95-
echo "Failed to read file: $FailedItem. The error message was $ErrorMessage"
96-
throw "Error in Get-Azure-Config-Path. Couldn't parse the HandlerEnvironment.json file"
97-
}
98-
}
99-
10033
function Install-ChefClient {
10134
# Source the shared PS
10235
. $(Get-SharedHelper)
@@ -155,19 +88,4 @@ function Get-SharedHelper {
15588
"$chefExtensionRoot\\bin\\shared.ps1"
15689
}
15790

158-
# This method is called separetely from enable.cmd before calling Install-ChefClient
159-
# Sourcing the script again refreshes the powershell console and the changes
160-
# of registry key become available
161-
function Run-Powershell2-With-Dot-Net4 {
162-
# Source the shared PS
163-
. $(Get-SharedHelper)
164-
$powershellVersion = Get-PowershellVersion
165-
166-
if ( $powershellVersion -lt 3 ) {
167-
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
168-
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
169-
}
170-
}
171-
17291
Export-ModuleMember -Function Install-ChefClient
173-
Export-ModuleMember -Function Run-Powershell2-With-Dot-Net4

ChefExtensionHandler/bin/chef-uninstall.psm1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ function Uninstall-ChefService {
2323
Write-Host("[$(Get-Date)] $result")
2424
}
2525

26+
function Uninstall-ChefSchTask {
27+
Write-Host("[$(Get-Date)] Uninstalling chef scheduled task...")
28+
$result = schtasks /delete /tn "chef-client" /f
29+
Write-Host("[$(Get-Date)] $result")
30+
}
31+
2632
function Uninstall-AzureChefExtensionGem {
2733
Write-Host("[$(Get-Date)] Uninstalling Azure-Chef-Extension gem...")
2834
# Uninstall the custom gem
@@ -61,7 +67,14 @@ function Uninstall-ChefClient {
6167
if (!(Test-ChefExtensionRegistry)) {
6268
if ($logStatus) { Write-ChefStatus "uninstalling-chef-extension" "transitioning" "Uninstalling Chef Extension" }
6369

64-
Uninstall-ChefService
70+
$daemon = Get-PublicSettings-From-Config-Json "daemon" $powershellVersion
71+
72+
if ( -Not $daemon -Or $daemon -eq "service") {
73+
Uninstall-ChefService
74+
}
75+
if ( $daemon -eq "task" ) {
76+
Uninstall-ChefSchTask
77+
}
6578

6679
Uninstall-AzureChefExtensionGem
6780

ChefExtensionHandler/bin/shared.ps1

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,80 @@ function Get-autoUpdateClientSetting{
176176

177177
Get-JsonValueUsingRuby "$chefExtensionParent\\$extensionPreviousVersion\\RuntimeSettings\\$latestSettingFile" "runtimeSettings" 0 "handlerSettings" "publicSettings" "autoUpdateClient"
178178
}
179+
180+
function Get-PublicSettings-From-Config-Json($key, $powershellVersion) {
181+
Try
182+
{
183+
$azure_config_file = Get-Azure-Config-Path($powershellVersion)
184+
$json_contents = Get-Content $azure_config_file
185+
$normalized_json = normalize_json($json_contents)
186+
187+
if ( $powershellVersion -ge 3 ) {
188+
$value = ($normalized_json | ConvertFrom-Json | Select -expand runtimeSettings | Select -expand handlerSettings | Select -expand publicSettings).$key
189+
}
190+
else {
191+
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
192+
$value = $ser.DeserializeObject($normalized_json).runtimeSettings[0].handlerSettings.publicSettings.$key
193+
}
194+
$value
195+
}
196+
Catch
197+
{
198+
$ErrorMessage = $_.Exception.Message
199+
$FailedItem = $_.Exception.ItemName
200+
echo "Failed to read file: $FailedItem. The error message was $ErrorMessage"
201+
throw "Error in Get-PublicSettings-From-Config-Json. Couldn't parse $azure_config_file"
202+
}
203+
}
204+
205+
function normalize_json($json) {
206+
$json -Join " "
207+
}
208+
209+
function Get-Azure-Config-Path($powershellVersion) {
210+
Try
211+
{
212+
# Reading chef_extension_root/HandlerEnvironment.json file
213+
$handler_file = "$chefExtensionRoot\\HandlerEnvironment.json"
214+
215+
if ( $powershellVersion -ge 3 ) {
216+
$config_folder = (((Get-Content $handler_file) | ConvertFrom-Json)[0] | Select -expand handlerEnvironment).configFolder
217+
}
218+
else {
219+
add-type -assembly system.web.extensions
220+
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
221+
$config_folder = ($ser.DeserializeObject($(Get-Content $handler_file)))[0].handlerEnvironment.configFolder
222+
}
223+
224+
# Get the last .settings file
225+
$config_files = get-childitem $config_folder -recurse | where {$_.extension -eq ".settings"}
226+
227+
if($config_files -is [system.array]) {
228+
$config_file_name = $config_files[-1].Name
229+
}
230+
else {
231+
$config_file_name = $config_files.Name
232+
}
233+
234+
"$config_folder\$config_file_name"
235+
}
236+
Catch
237+
{
238+
$ErrorMessage = $_.Exception.Message
239+
$FailedItem = $_.Exception.ItemName
240+
echo "Failed to read file: $FailedItem. The error message was $ErrorMessage"
241+
throw "Error in Get-Azure-Config-Path. Couldn't parse the HandlerEnvironment.json file"
242+
}
243+
}
244+
245+
# This method is called separetely from enable.cmd before calling Install-ChefClient
246+
# Sourcing the script again refreshes the powershell console and the changes
247+
# of registry key become available
248+
function Run-Powershell2-With-Dot-Net4 {
249+
$powershellVersion = Get-PowershellVersion
250+
251+
if ( $powershellVersion -lt 3 ) {
252+
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
253+
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
254+
}
255+
}

ChefExtensionHandler/enable.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set CHEF_EXT_DIR=%~dp0
44
echo %CHEF_EXT_DIR%
55

66
REM Doing required settings if Powershell Version is less than 3
7-
powershell -nologo -noprofile -executionpolicy unrestricted Import-Module %CHEF_EXT_DIR%bin\chef-install.psm1;Run-Powershell2-With-Dot-Net4
7+
powershell -nologo -noprofile -executionpolicy unrestricted Import-Module %CHEF_EXT_DIR%bin\shared.ps1;Run-Powershell2-With-Dot-Net4
88

99
REM Installing chef-client
1010
powershell -nologo -noprofile -executionpolicy unrestricted Import-Module %CHEF_EXT_DIR%bin\chef-install.psm1;Install-ChefClient

ChefExtensionHandler/uninstall.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ echo %CHEF_EXT_DIR%
55

66
mode con:cols=150 lines=70
77

8+
REM Doing required settings if Powershell Version is less than 3
9+
powershell -nologo -noprofile -executionpolicy unrestricted Import-Module %CHEF_EXT_DIR%bin\shared.ps1;Run-Powershell2-With-Dot-Net4
10+
811
set get_config_file_path_cmd=powershell -nologo -noprofile -executionpolicy unrestricted -Command ". %CHEF_EXT_DIR%bin\shared.ps1;Get-HandlerSettingsFilePath"
912

1013
for /f "delims=" %%I in ('%get_config_file_path_cmd%') do set "config_file_path=%%I"

lib/chef/azure/commands/disable.rb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require 'chef'
55
require 'chef/azure/helpers/shared'
66
require 'chef/azure/service'
7+
require 'chef/azure/task'
8+
require 'chef/azure/helpers/parse_json'
79

810
class DisableChef
911
include ChefAzure::Shared
@@ -40,16 +42,20 @@ def load_env
4042

4143
def disable_chef
4244
# Disabling Chef involves following steps:
43-
# - Stop the Chef service
45+
# - Stop the Chef service OR
46+
# - Disable the Chef scheduled task
4447
begin
45-
@exit_code, error_message = ChefService.new.disable(@azure_plugin_log_location)
46-
if @exit_code == 0
47-
report_status_to_azure "chef-service disabled", "success"
48-
else
49-
report_status_to_azure "chef-service disable failed - #{error_message}", "error"
48+
daemon = value_from_json_file(handler_settings_file, 'runtimeSettings', '0', 'handlerSettings', 'publicSettings', 'daemon')
49+
daemon = "service" if (daemon.nil? || daemon.empty?)
50+
if(daemon == "service" || !windows?)
51+
@exit_code, @error_message = ChefService.new.disable(@azure_plugin_log_location)
52+
update_chef_status("service")
53+
elsif daemon == "task" && windows?
54+
@exit_code, @error_message = ChefTask.new.disable
55+
update_chef_status("sch-task")
56+
elsif daemon == "none"
57+
update_chef_status("extension")
5058
end
51-
@exit_code
52-
5359
rescue => e
5460
Chef::Log.error e
5561
report_status_to_azure "#{e} - Check log file for details", "error"
@@ -60,5 +66,13 @@ def disable_chef
6066
end
6167
@exit_code
6268
end
69+
70+
def update_chef_status(option_name)
71+
if @exit_code == 0
72+
report_status_to_azure "chef-#{option_name} disabled", "success"
73+
else
74+
report_status_to_azure "chef-#{option_name} disable failed - #{@error_message}", "error"
75+
end
76+
end
6377
end
6478

0 commit comments

Comments
 (0)