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

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

Instance Attribute Summary collapse

Attributes inherited from Simple

#status_load_success

Attributes inherited from Chef::Provider

#action, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Simple

#shared_resource_requirements

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

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #introduced, #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 Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::ShellOut

apply_default_env, maybe_add_timeout, #shell_out, #shell_out!

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 Mixin::PowershellExec

#powershell_exec

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

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!, #resources, #with_run_context

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.



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

def status_check_success
  @status_check_success
end

Class Method Details

.supports?(resource, action) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.supports?(resource, action)
  service_script_exist?(:systemd, resource.service_name)
end

Instance Method Details

#define_resource_requirementsObject



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

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



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

def disable_service
  if current_resource.masked || current_resource.indirect
    logger.trace("#{new_resource} cannot be disabled: it is masked or indirect")
    return
  end
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} disable #{Shellwords.escape(new_resource.service_name)}", **options)
end

#enable_serviceObject



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

def enable_service
  if current_resource.masked || current_resource.indirect
    logger.trace("#{new_resource} cannot be enabled: it is masked or indirect")
    return
  end
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} enable #{Shellwords.escape(new_resource.service_name)}", **options)
end

#get_systemctl_options_argsObject



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

def get_systemctl_options_args
  if new_resource.user
    raise NotImplementedError, "#{new_resource} does not support the user property on a target_mode host (yet)" if Chef::Config.target_mode?

    uid = Etc.getpwnam(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

  [options, args]
end

#is_active?Boolean

Returns:

  • (Boolean)


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

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

#is_enabled?Boolean

Returns:

  • (Boolean)


180
181
182
183
# File 'lib/chef/provider/service/systemd.rb', line 180

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

#is_indirect?Boolean

Returns:

  • (Boolean)


185
186
187
188
189
# File 'lib/chef/provider/service/systemd.rb', line 185

def is_indirect?
  options, args = get_systemctl_options_args
  s = shell_out("#{systemctl_path} #{args} is-enabled #{Shellwords.escape(new_resource.service_name)}", **options)
  s.stdout.include?("indirect")
end

#is_masked?Boolean

Returns:

  • (Boolean)


191
192
193
194
195
# File 'lib/chef/provider/service/systemd.rb', line 191

def is_masked?
  options, args = get_systemctl_options_args
  s = shell_out("#{systemctl_path} #{args} is-enabled #{Shellwords.escape(new_resource.service_name)}", **options)
  s.exitstatus != 0 && s.stdout.include?("masked")
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
61
62
63
64
# 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
    logger.trace("#{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)
      current_resource.indirect(false)
    end
  else
    current_resource.running(is_active?)
  end

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

#mask_serviceObject



165
166
167
168
# File 'lib/chef/provider/service/systemd.rb', line 165

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

#reload_serviceObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/provider/service/systemd.rb', line 134

def reload_service
  if new_resource.reload_command
    super
  else
    if current_resource.running
      options, args = get_systemctl_options_args
      shell_out!("#{systemctl_path} #{args} reload #{Shellwords.escape(new_resource.service_name)}", default_env: false, **options)
    else
      start_service
    end
  end
end

#restart_serviceObject



125
126
127
128
129
130
131
132
# File 'lib/chef/provider/service/systemd.rb', line 125

def restart_service
  if new_resource.restart_command
    super
  else
    options, args = get_systemctl_options_args
    shell_out!("#{systemctl_path} #{args} restart #{Shellwords.escape(new_resource.service_name)}", default_env: false, **options)
  end
end

#start_serviceObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef/provider/service/systemd.rb', line 99

def start_service
  if current_resource.running
    logger.trace("#{new_resource} already running, not starting")
  else
    if new_resource.start_command
      super
    else
      options, args = get_systemctl_options_args
      shell_out!("#{systemctl_path} #{args} start #{Shellwords.escape(new_resource.service_name)}", default_env: false, **options)
    end
  end
end

#stop_serviceObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/provider/service/systemd.rb', line 112

def stop_service
  unless current_resource.running
    logger.trace("#{new_resource} not running, not stopping")
  else
    if new_resource.stop_command
      super
    else
      options, args = get_systemctl_options_args
      shell_out!("#{systemctl_path} #{args} stop #{Shellwords.escape(new_resource.service_name)}", default_env: false, **options)
    end
  end
end

#unmask_serviceObject



170
171
172
173
# File 'lib/chef/provider/service/systemd.rb', line 170

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

#user_services_requirementsObject

systemd supports user services just fine



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

def user_services_requirements; end