Skip to content

Commit

Permalink
Merge pull request #332 from chef-partners/ayush/task-creation-update
Browse files Browse the repository at this point in the history
Fix for inconsistent behavior with System Path and error while running dameon as service
  • Loading branch information
Vasundhara Jagdale authored Jan 22, 2021
2 parents b68a1db + 5f5039b commit 967bcbc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rvm:
- 2.4.1
- 2.7.0

script: bundle exec rake spec
29 changes: 19 additions & 10 deletions lib/chef/azure/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# 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_task
end
end
log_location = log_location || bootstrap_directory
exit_code = 0
message = "success"
Expand All @@ -20,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
Expand All @@ -35,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
Expand Down Expand Up @@ -253,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
24 changes: 22 additions & 2 deletions lib/chef/azure/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@
# 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)
# 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_service
end
end
log_location = log_location || bootstrap_directory
exit_code = 0
message = "success"
Expand Down Expand Up @@ -69,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 '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 #{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

Expand All @@ -82,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
15 changes: 3 additions & 12 deletions spec/unit/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 #{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
Expand Down

0 comments on commit 967bcbc

Please sign in to comment.