Class: Chef::Provider::Service::Systemd

Inherits:
Simple show all
Defined in:
lib/chef/provider/service/systemd.rb

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods inherited from Simple

#shared_resource_requirements, #whyrun_supported?

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop, #initialize, #load_new_resource_state, #shared_resource_requirements, #whyrun_supported?

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Unix

#popen4

Methods included from Mixin::Command::Windows

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cleanup_after_converge, #converge, #cookbook_name, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::EnforceOwnershipAndPermissions

#access_controls, #enforce_ownership_and_permissions

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #platform_family?, #search, #value_for_platform, #value_for_platform_family

Constructor Details

This class inherits a constructor from Chef::Provider::Service

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#define_resource_requirementsObject


50
51
52
53
54
55
56
57
58
# File 'lib/chef/provider/service/systemd.rb', line 50

def define_resource_requirements
  shared_resource_requirements
  requirements.assert(:all_actions) do |a|
    a.assertion { @status_check_success } 
    # We won't stop in any case, but in whyrun warn and tell what we're doing.
    a.whyrun ["Failed to determine status of #{@new_resource}, using command #{@new_resource.status_command}.",
      "Assuming service would have been installed and is disabled"]
  end
end

#disable_serviceObject


104
105
106
# File 'lib/chef/provider/service/systemd.rb', line 104

def disable_service
  run_command_with_systems_locale(:command => "/bin/systemctl disable #{@new_resource.service_name}")
end

#enable_serviceObject


100
101
102
# File 'lib/chef/provider/service/systemd.rb', line 100

def enable_service
  run_command_with_systems_locale(:command => "/bin/systemctl enable #{@new_resource.service_name}")
end

#is_active?Boolean

Returns:

  • (Boolean)

108
109
110
# File 'lib/chef/provider/service/systemd.rb', line 108

def is_active?
  run_command_with_systems_locale({:command => "/bin/systemctl is-active #{@new_resource.service_name}", :ignore_failure => true}) == 0
end

#is_enabled?Boolean

Returns:

  • (Boolean)

112
113
114
# File 'lib/chef/provider/service/systemd.rb', line 112

def is_enabled?
  run_command_with_systems_locale({:command => "/bin/systemctl is-enabled #{@new_resource.service_name}", :ignore_failure => true}) == 0
end

#load_current_resourceObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/provider/service/systemd.rb', line 24

def load_current_resource
  @current_resource = Chef::Resource::Service.new(@new_resource.name)
  @current_resource.service_name(@new_resource.service_name)
  @status_check_success = true

  if @new_resource.status_command
    Chef::Log.debug("#{@new_resource} you have specified a status command, running..")

    begin
      if run_command_with_systems_locale(:command => @new_resource.status_command) == 0
        @current_resource.running(true)
      end
    rescue Chef::Exceptions::Exec
      @status_check_success = false
      @current_resource.running(false)
      @current_resource.enabled(false)
      nil
    end
  else
    @current_resource.running(is_active?)
  end

  @current_resource.enabled(is_enabled?)
  @current_resource
end

#reload_serviceObject


92
93
94
95
96
97
98
# File 'lib/chef/provider/service/systemd.rb', line 92

def reload_service
  if @new_resource.reload_command
    super
  else
    run_command_with_systems_locale(:command => "/bin/systemctl reload #{@new_resource.service_name}")
  end
end

#restart_serviceObject


84
85
86
87
88
89
90
# File 'lib/chef/provider/service/systemd.rb', line 84

def restart_service
  if @new_resource.restart_command
    super
  else
    run_command_with_systems_locale(:command => "/bin/systemctl restart #{@new_resource.service_name}")
  end
end

#start_serviceObject


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/provider/service/systemd.rb', line 60

def start_service
  if @current_resource.running
    Chef::Log.debug("#{@new_resource} already running, not starting")
  else
    if @new_resource.start_command
      super
    else
      run_command_with_systems_locale(:command => "/bin/systemctl start #{@new_resource.service_name}")
    end
  end
end

#stop_serviceObject


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/provider/service/systemd.rb', line 72

def stop_service
  unless @current_resource.running
    Chef::Log.debug("#{@new_resource} not running, not stopping")
  else
    if @new_resource.stop_command
      super
    else
      run_command_with_systems_locale(:command => "/bin/systemctl stop #{@new_resource.service_name}")
    end
  end
end