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

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

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Simple

#status_load_success

Attributes inherited from Chef::Provider

#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Which

#which

Methods inherited from Simple

#shared_resource_requirements, #whyrun_supported?

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_and_return_stdout_stderr, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #cleanup_after_converge, #converge_by, #events, #initialize, #node, node_map, #process_resource_requirements, provides, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Constructor Details

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

Instance Attribute Details

#status_check_successObject

Returns the value of attribute status_check_success.



29
30
31
# File 'lib/chef/provider/service/systemd.rb', line 29

def status_check_success
  @status_check_success
end

Class Method Details

.provides?(node, resource) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/chef/provider/service/systemd.rb', line 31

def self.provides?(node, resource)
  super && Chef::Platform::ServiceHelpers.service_resource_providers.include?(:systemd)
end

.supports?(resource, action) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/chef/provider/service/systemd.rb', line 35

def self.supports?(resource, action)
  Chef::Platform::ServiceHelpers.config_for_service(resource.service_name).include?(:systemd)
end

Instance Method Details

#define_resource_requirementsObject



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

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



120
121
122
# File 'lib/chef/provider/service/systemd.rb', line 120

def disable_service
  shell_out!("#{systemctl_path} disable #{new_resource.service_name}")
end

#enable_serviceObject



116
117
118
# File 'lib/chef/provider/service/systemd.rb', line 116

def enable_service
  shell_out!("#{systemctl_path} enable #{new_resource.service_name}")
end

#is_active?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/chef/provider/service/systemd.rb', line 124

def is_active?
  shell_out("#{systemctl_path} is-active #{new_resource.service_name} --quiet").exitstatus == 0
end

#is_enabled?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/chef/provider/service/systemd.rb', line 128

def is_enabled?
  shell_out("#{systemctl_path} is-enabled #{new_resource.service_name} --quiet").exitstatus == 0
end

#load_current_resourceObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/provider/service/systemd.rb', line 39

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..")

    unless shell_out(new_resource.status_command).error?
      current_resource.running(true)
    else
      @status_check_success = false
      current_resource.running(false)
      current_resource.enabled(false)
    end
  else
    current_resource.running(is_active?)
  end

  current_resource.enabled(is_enabled?)
  current_resource
end

#reload_serviceObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef/provider/service/systemd.rb', line 104

def reload_service
  if new_resource.reload_command
    super
  else
    if current_resource.running
      shell_out_with_systems_locale!("#{systemctl_path} reload #{new_resource.service_name}")
    else
      start_service
    end
  end
end

#restart_serviceObject



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

def restart_service
  if new_resource.restart_command
    super
  else
    shell_out_with_systems_locale!("#{systemctl_path} restart #{new_resource.service_name}")
  end
end

#start_serviceObject



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

def start_service
  if current_resource.running
    Chef::Log.debug("#{new_resource} already running, not starting")
  else
    if new_resource.start_command
      super
    else
      shell_out_with_systems_locale!("#{systemctl_path} start #{new_resource.service_name}")
    end
  end
end

#stop_serviceObject



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

def stop_service
  unless current_resource.running
    Chef::Log.debug("#{new_resource} not running, not stopping")
  else
    if new_resource.stop_command
      super
    else
      shell_out_with_systems_locale!("#{systemctl_path} stop #{new_resource.service_name}")
    end
  end
end