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_mask, #action_reload, #action_restart, #action_start, #action_stop, #action_unmask, #initialize, #load_new_resource_state, #shared_resource_requirements, #supports, #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, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #events, include_resource_dsl, include_resource_dsl_module, #initialize, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

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 DeprecatedLWRPClass

#const_missing, #deprecated_constants, #register_deprecated_lwrp_class

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context

Methods included from Mixin::ShellOut

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

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

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.



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

def status_check_success
  @status_check_success
end

Class Method Details

.supports?(resource, action) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#define_resource_requirementsObject



67
68
69
70
71
72
73
74
75
# File 'lib/chef/provider/service/systemd.rb', line 67

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



148
149
150
151
# File 'lib/chef/provider/service/systemd.rb', line 148

def disable_service
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} disable #{new_resource.service_name}", options)
end

#enable_serviceObject



143
144
145
146
# File 'lib/chef/provider/service/systemd.rb', line 143

def enable_service
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} enable #{new_resource.service_name}", options)
end

#get_systemctl_options_argsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/provider/service/systemd.rb', line 77

def get_systemctl_options_args
  if new_resource.user
    uid = node["etc"]["passwd"][new_resource.user]["uid"]
    options = {
      "environment" => {
        "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/#{uid}/bus",
      },
      "user" => new_resource.user,
    }
    args = "--user"
  else
    options = {}
    args = "--system"
  end

  return options, args
end

#is_active?Boolean

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/chef/provider/service/systemd.rb', line 163

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

#is_enabled?Boolean

Returns:

  • (Boolean)


168
169
170
171
# File 'lib/chef/provider/service/systemd.rb', line 168

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

#is_masked?Boolean

Returns:

  • (Boolean)


173
174
175
176
177
# File 'lib/chef/provider/service/systemd.rb', line 173

def is_masked?
  options, args = get_systemctl_options_args
  s = shell_out("#{systemctl_path} #{args} is-enabled #{new_resource.service_name}", options)
  s.exitstatus != 0 && s.stdout.include?("masked")
end

#load_current_resourceObject



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

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)
      current_resource.masked(false)
    end
  else
    current_resource.running(is_active?)
  end

  current_resource.enabled(is_enabled?)
  current_resource.masked(is_masked?)
  current_resource
end

#mask_serviceObject



153
154
155
156
# File 'lib/chef/provider/service/systemd.rb', line 153

def mask_service
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} mask #{new_resource.service_name}", options)
end

#reload_serviceObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/provider/service/systemd.rb', line 130

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

#restart_serviceObject



121
122
123
124
125
126
127
128
# File 'lib/chef/provider/service/systemd.rb', line 121

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

#start_serviceObject



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

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

#stop_serviceObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef/provider/service/systemd.rb', line 108

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

#unmask_serviceObject



158
159
160
161
# File 'lib/chef/provider/service/systemd.rb', line 158

def unmask_service
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} unmask #{new_resource.service_name}", options)
end

#user_services_requirementsObject

systemd supports user services just fine



64
65
# File 'lib/chef/provider/service/systemd.rb', line 64

def user_services_requirements
end