From 7915a101a5bf729261cc0725adba95e8865535ab Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Fri, 27 Nov 2020 16:39:04 +0530 Subject: [PATCH 1/9] Update in task creation Signed-off-by: ayushbhatt29 --- lib/chef/azure/task.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chef/azure/task.rb b/lib/chef/azure/task.rb index 59873969..e3fafb7f 100644 --- a/lib/chef/azure/task.rb +++ b/lib/chef/azure/task.rb @@ -69,7 +69,7 @@ def is_installed? def install_service(bootstrap_directory, log_location, chef_daemon_interval) puts "#{Time.now} Installing chef-client scheduled task..." - result = shell_out("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'ruby chef-client -L #{log_location}/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"") + result = shell_out("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'C:/opscode/chef/embedded/bin/ruby.exe C:/opscode/chef/bin/chef-client -L C:/chef/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"") result.error? ? result.error! : (puts "#{Time.now} Installed chef-client scheduled task.") end From 5301bbfb95362d89f80022ed506ae2fe517300d1 Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Mon, 30 Nov 2020 13:09:15 +0530 Subject: [PATCH 2/9] Update in task_spec Signed-off-by: ayushbhatt29 --- spec/unit/task_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/task_spec.rb b/spec/unit/task_spec.rb index f2d8b606..3a4547a8 100644 --- a/spec/unit/task_spec.rb +++ b/spec/unit/task_spec.rb @@ -81,7 +81,7 @@ chef_daemon_interval = 17 log_location = "log" bootstrap_directory = "bootstrap_dir" - expect(instance).to receive(:shell_out).with("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'ruby chef-client -L #{log_location}/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"").and_return(double("result", :error? => false)) + expect(instance).to receive(:shell_out).with("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'C:/opscode/chef/embedded/bin/ruby.exe C:/opscode/chef/bin/chef-client -L C:/chef/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"").and_return(double("result", :error? => false)) instance.send(:install_service, bootstrap_directory, log_location, chef_daemon_interval) end end From 69f0c6a158cf489c1bb1b0fa074ffb139c70a70f Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Wed, 9 Dec 2020 15:16:52 +0530 Subject: [PATCH 3/9] Update for service and task file Signed-off-by: ayushbhatt29 --- lib/chef/azure/service.rb | 7 +++++++ lib/chef/azure/task.rb | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/lib/chef/azure/service.rb b/lib/chef/azure/service.rb index 81b199cb..304ab2cb 100644 --- a/lib/chef/azure/service.rb +++ b/lib/chef/azure/service.rb @@ -10,6 +10,13 @@ class ChefService CLIENT_RB_INTERVAL_ATTRIBUTE_NAME = 'interval' def enable(extension_root, bootstrap_directory, log_location, chef_daemon_interval = DEFAULT_CHEF_DAEMON_INTERVAL) + if windows? + result = shell_out("SCHTASKS.EXE /QUERY /TN \"chef-client\"") + if !result.error? + disable = shell_out("SCHTASKS.EXE /CHANGE /TN \"chef-client\" /DISABLE") + disable.error? ? disable.error! : (puts "#{Time.now} Disabled chef-client scheduled task.") + end + end log_location = log_location || bootstrap_directory exit_code = 0 message = "success" diff --git a/lib/chef/azure/task.rb b/lib/chef/azure/task.rb index e3fafb7f..25ccd876 100644 --- a/lib/chef/azure/task.rb +++ b/lib/chef/azure/task.rb @@ -21,6 +21,11 @@ class ChefTask DEFAULT_CHEF_DAEMON_INTERVAL = 30 def enable(bootstrap_directory, log_location, chef_daemon_interval = DEFAULT_CHEF_DAEMON_INTERVAL) + result = shell_out("sc.exe query chef-client") + if result.exitstatus == 0 and result.stdout.include?("RUNNING") + stop = shell_out("sc.exe stop chef-client") + stop.error? ? stop.error! : (puts "#{Time.now} Stopped chef-client service.") + end log_location = log_location || bootstrap_directory exit_code = 0 message = "success" From 509ecccac5101117440c2e14f83d0c151702f312 Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Mon, 28 Dec 2020 13:44:48 +0530 Subject: [PATCH 4/9] Update for service file for removing daemon interval setting Signed-off-by: ayushbhatt29 --- lib/chef/azure/service.rb | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/lib/chef/azure/service.rb b/lib/chef/azure/service.rb index 304ab2cb..ece0828b 100644 --- a/lib/chef/azure/service.rb +++ b/lib/chef/azure/service.rb @@ -27,11 +27,7 @@ def enable(extension_root, bootstrap_directory, log_location, chef_daemon_interv puts "#{Time.now} chef-client service is already installed." if chef_daemon_interval_changed?(chef_daemon_interval, "#{bootstrap_directory}\\client.rb") puts "#{Time.now} yes..chef-client service interval has been changed by the user..updating the interval value to #{chef_daemon_interval} minutes." - if windows? - set_interval( - "#{bootstrap_directory}\\client.rb", - interval_in_seconds(chef_daemon_interval) - ) + if windows? restart_service else disable_cron @@ -42,11 +38,7 @@ def enable(extension_root, bootstrap_directory, log_location, chef_daemon_interv puts "#{Time.now} no..chef-client service interval has not been changed by the user..exiting." end else - if windows? - set_interval( - "#{bootstrap_directory}\\client.rb", - interval_in_seconds(chef_daemon_interval) - ) + if windows? install_service start_service(bootstrap_directory, log_location) if !is_running? else From 634ece3ae2b44f00dcaa8a00e34dad534eb99357 Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Mon, 28 Dec 2020 19:25:41 +0530 Subject: [PATCH 5/9] Update for service_spec Signed-off-by: ayushbhatt29 --- spec/unit/service_spec.rb | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/spec/unit/service_spec.rb b/spec/unit/service_spec.rb index 4f13a91a..8b68a73b 100644 --- a/spec/unit/service_spec.rb +++ b/spec/unit/service_spec.rb @@ -25,10 +25,7 @@ allow(instance).to receive(:chef_daemon_interval_changed?).and_return(true) end - it 'updates the interval in client.rb file and restarts the service' do - expect(instance).to receive(:interval_in_seconds).and_return(1020) - expect(instance).to receive(:set_interval).with( - "/bootstrap_directory\\client.rb", 1020) + it 'updates the interval in client.rb file and restarts the service' do expect(instance).to receive(:restart_service) response = instance.send(:enable, '/extension_root', '/bootstrap_directory', '/log_location', 17) expect(response).to be == [0, 'success'] @@ -78,10 +75,7 @@ allow(instance).to receive(:is_running?).and_return(false) end - it 'invokes methods to set the interval in client.rb file, enable and start the chef-service' do - expect(instance).to receive(:interval_in_seconds).and_return(300) - expect(instance).to receive(:set_interval).with( - '/bootstrap_directory\\client.rb', 300) + it 'invokes methods to set the interval in client.rb file, enable and start the chef-service' do expect(instance).to receive(:install_service) expect(instance).to receive(:start_service).with( '/bootstrap_directory', '/log_location') @@ -95,10 +89,7 @@ allow(instance).to receive(:is_running?).and_return(true) end - it 'invokes methods to set the interval in client.rb file and enable the chef-service' do - expect(instance).to receive(:interval_in_seconds).and_return(1800) - expect(instance).to receive(:set_interval).with( - '/bootstrap_directory\\client.rb', 1800) + it 'invokes methods to set the interval in client.rb file and enable the chef-service' do expect(instance).to receive(:install_service) expect(instance).to_not receive(:start_service).with( '/bootstrap_directory', '/log_location') From 580a9a0c7e1663b01539f0ba60a550dd6325a648 Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Wed, 6 Jan 2021 16:46:47 +0530 Subject: [PATCH 6/9] changes in travis file Signed-off-by: ayushbhatt29 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 170ab16b..1bf582b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ rvm: - - 2.4.1 + - 2.7.0 script: bundle exec rake spec From 2000ea61029051d51ed04d42c74c4170ccfe5374 Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Tue, 12 Jan 2021 19:32:58 +0530 Subject: [PATCH 7/9] update in task file Signed-off-by: ayushbhatt29 --- lib/chef/azure/task.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/chef/azure/task.rb b/lib/chef/azure/task.rb index 25ccd876..6af89f83 100644 --- a/lib/chef/azure/task.rb +++ b/lib/chef/azure/task.rb @@ -15,16 +15,20 @@ # See the License for the specific language governing permissions and # limitations under the License. # +require 'chef/azure/helpers/shared' -class ChefTask +class ChefTask include Chef::Mixin::ShellOut + include ChefAzure::Shared DEFAULT_CHEF_DAEMON_INTERVAL = 30 def enable(bootstrap_directory, log_location, chef_daemon_interval = DEFAULT_CHEF_DAEMON_INTERVAL) - result = shell_out("sc.exe query chef-client") - if result.exitstatus == 0 and result.stdout.include?("RUNNING") - stop = shell_out("sc.exe stop chef-client") - stop.error? ? stop.error! : (puts "#{Time.now} Stopped chef-client service.") + if windows? + result = shell_out("sc.exe query chef-client") + if result.exitstatus == 0 and result.stdout.include?("RUNNING") + stop = shell_out("sc.exe stop chef-client") + stop.error? ? stop.error! : (puts "#{Time.now} Stopped chef-client service.") + end end log_location = log_location || bootstrap_directory exit_code = 0 From 064e25139a92e814e773fbe24c6d46970c8a9bca Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Mon, 18 Jan 2021 16:10:33 +0530 Subject: [PATCH 8/9] update in task and service file Signed-off-by: ayushbhatt29 --- lib/chef/azure/service.rb | 14 ++++++++++++-- lib/chef/azure/task.rb | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/chef/azure/service.rb b/lib/chef/azure/service.rb index ece0828b..8eb978bb 100644 --- a/lib/chef/azure/service.rb +++ b/lib/chef/azure/service.rb @@ -10,11 +10,11 @@ class ChefService CLIENT_RB_INTERVAL_ATTRIBUTE_NAME = 'interval' def enable(extension_root, bootstrap_directory, log_location, chef_daemon_interval = DEFAULT_CHEF_DAEMON_INTERVAL) + # Checking if chef-client task is enabled and if enabled then disabling it. if windows? result = shell_out("SCHTASKS.EXE /QUERY /TN \"chef-client\"") if !result.error? - disable = shell_out("SCHTASKS.EXE /CHANGE /TN \"chef-client\" /DISABLE") - disable.error? ? disable.error! : (puts "#{Time.now} Disabled chef-client scheduled task.") + disable_task end end log_location = log_location || bootstrap_directory @@ -252,4 +252,14 @@ def set_startup_type startup_type = shell_out("powershell.exe -Command Set-Service -Name 'chef-client' -StartupType automatic") startup_type.error! end + + def disable_task + disable = shell_out("SCHTASKS.EXE /CHANGE /TN \"chef-client\" /DISABLE") + if !disable.error? + puts "#{Time.now} Disabled chef-client scheduled task" + else + puts "Unable to disable chef-client task as chef-client cannot run as task and service simultaneously.. exiting" + disable.error! + end + end end diff --git a/lib/chef/azure/task.rb b/lib/chef/azure/task.rb index 6af89f83..7708edb1 100644 --- a/lib/chef/azure/task.rb +++ b/lib/chef/azure/task.rb @@ -23,11 +23,11 @@ class ChefTask DEFAULT_CHEF_DAEMON_INTERVAL = 30 def enable(bootstrap_directory, log_location, chef_daemon_interval = DEFAULT_CHEF_DAEMON_INTERVAL) + # Checking if chef-client service is running or not and if running then stop that service. if windows? result = shell_out("sc.exe query chef-client") if result.exitstatus == 0 and result.stdout.include?("RUNNING") - stop = shell_out("sc.exe stop chef-client") - stop.error? ? stop.error! : (puts "#{Time.now} Stopped chef-client service.") + stop_service end end log_location = log_location || bootstrap_directory @@ -91,4 +91,15 @@ def update_chef_sch_task(chef_daemon_interval) def total_minutes(hours, minutes) (hours * 60) + minutes end + + + def stop_service + stop = shell_out("sc.exe stop chef-client") + if stop.error? + puts "Could not Stop chef-client service as chef-client cannot run as service and task simultaneously.. exiting" + stop.error! + else + puts "#{Time.now} Stopped chef-client service." + end + end end From 5f5039b3ef2857e896c5592c741d45f6b8b6dc36 Mon Sep 17 00:00:00 2001 From: ayushbhatt29 Date: Thu, 21 Jan 2021 16:16:37 +0530 Subject: [PATCH 9/9] update in task and task_spec Signed-off-by: ayushbhatt29 --- lib/chef/azure/task.rb | 2 +- spec/unit/task_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chef/azure/task.rb b/lib/chef/azure/task.rb index 7708edb1..02e6750f 100644 --- a/lib/chef/azure/task.rb +++ b/lib/chef/azure/task.rb @@ -78,7 +78,7 @@ def is_installed? def install_service(bootstrap_directory, log_location, chef_daemon_interval) puts "#{Time.now} Installing chef-client scheduled task..." - result = shell_out("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'C:/opscode/chef/embedded/bin/ruby.exe C:/opscode/chef/bin/chef-client -L C:/chef/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"") + result = shell_out("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'C:/opscode/chef/embedded/bin/ruby.exe C:/opscode/chef/bin/chef-client -L #{bootstrap_directory}/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"") result.error? ? result.error! : (puts "#{Time.now} Installed chef-client scheduled task.") end diff --git a/spec/unit/task_spec.rb b/spec/unit/task_spec.rb index 3a4547a8..435d39be 100644 --- a/spec/unit/task_spec.rb +++ b/spec/unit/task_spec.rb @@ -81,7 +81,7 @@ chef_daemon_interval = 17 log_location = "log" bootstrap_directory = "bootstrap_dir" - expect(instance).to receive(:shell_out).with("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'C:/opscode/chef/embedded/bin/ruby.exe C:/opscode/chef/bin/chef-client -L C:/chef/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"").and_return(double("result", :error? => false)) + expect(instance).to receive(:shell_out).with("SCHTASKS.EXE /CREATE /TN \"chef-client\" /F /SC \"MINUTE\" /MO \"#{chef_daemon_interval}\" /TR \"cmd /c 'C:/opscode/chef/embedded/bin/ruby.exe C:/opscode/chef/bin/chef-client -L #{bootstrap_directory}/chef-client.log -c #{bootstrap_directory}/client.rb'\" /RU \"NT Authority\\System\" /RP /RL \"HIGHEST\"").and_return(double("result", :error? => false)) instance.send(:install_service, bootstrap_directory, log_location, chef_daemon_interval) end end