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, #after_resource, #current_resource, #logger, #new_resource, #run_context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Simple

#shared_resource_requirements

Methods inherited from Chef::Provider::Service

#initialize, #load_new_resource_state, #shared_resource_requirements, #supports

Methods included from Chef::Platform::ServiceHelpers

#config_for_service, #service_resource_providers

Methods inherited from Chef::Provider

action, action_description, action_descriptions, #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, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #recipe_name, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, use, use_inline_resources, #validate_required_properties!, #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::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::Secret

#default_secret_config, #default_secret_service, #secret, #with_secret_config, #with_secret_service

Methods included from DSL::RenderHelpers

#render_json, #render_toml, #render_yaml

Methods included from DSL::ReaderHelpers

#parse_file, #parse_json, #parse_toml, #parse_yaml

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::ChefVault

#chef_vault, #chef_vault_item, #chef_vault_item_for_environment

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 DSL::Recipe

#exec, #have_resource_class_for?, #resource_class_for

Methods included from DSL::Definitions

add_definition, #evaluate_resource_definition, #has_resource_definition?

Methods included from DSL::Resources

add_resource_dsl, remove_resource_dsl

Methods included from DSL::Cheffish

load_cheffish

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe

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

Methods included from DSL::Compliance

#include_input, #include_profile, #include_waiver

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



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

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



195
196
197
198
# File 'lib/chef/provider/service/systemd.rb', line 195

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

#enable_serviceObject



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

def enable_service
  # This function can safely assume that enableable? is true
  options, args = get_systemctl_options_args
  shell_out!(systemctl_path, args, "enable", new_resource.service_name, **options)
end

#enableable?(action) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chef/provider/service/systemd.rb', line 173

def enableable?(action)
  if current_resource.masked
    logger.debug("#{new_resource} cannot be #{action}d: it is masked")
    return false
  end
  if current_resource.static
    logger.debug("#{new_resource} cannot be #{action}d: it is static")
    return false
  end
  if current_resource.indirect
    logger.debug("#{new_resource} cannot be #{action}d: it is indirect")
    return false
  end
  true
end

#get_systemctl_options_argsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/provider/service/systemd.rb', line 105

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 = TargetIO::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)


210
211
212
213
# File 'lib/chef/provider/service/systemd.rb', line 210

def is_active?
  # Note: "activating" is not active (as with type=notify or a oneshot)
  systemd_service_status["ActiveState"] == "active"
end

#is_enabled?Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
223
224
225
# File 'lib/chef/provider/service/systemd.rb', line 215

def is_enabled?
  # if the service is in sysv compat mode, shellout to determine if enabled
  if systemd_service_status["UnitFileState"] == "bad"
    options, args = get_systemctl_options_args
    return shell_out(systemctl_path, args, "is-enabled", new_resource.service_name, "--quiet", **options).exitstatus == 0
  end
  # See https://github.com/systemd/systemd/blob/master/src/systemctl/systemctl-is-enabled.c
  # Note: enabled-runtime is excluded because this is volatile, and the state of enabled-runtime
  # specifically means that the service is not enabled
  %w{enabled static generated alias indirect}.include?(systemd_service_status["UnitFileState"])
end

#is_indirect?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/chef/provider/service/systemd.rb', line 227

def is_indirect?
  systemd_service_status["UnitFileState"] == "indirect"
end

#is_masked?Boolean

Returns:

  • (Boolean)


235
236
237
238
239
# File 'lib/chef/provider/service/systemd.rb', line 235

def is_masked?
  # Note: masked-runtime is excluded, because runtime is volatile, and
  # because masked-runtime is not masked.
  systemd_service_status["UnitFileState"] == "masked"
end

#is_static?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/chef/provider/service/systemd.rb', line 231

def is_static?
  systemd_service_status["UnitFileState"] == "static"
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
65
66
# 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.static(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.static(is_static?)
  current_resource.indirect(is_indirect?)
  current_resource
end

#mask_serviceObject



200
201
202
203
# File 'lib/chef/provider/service/systemd.rb', line 200

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

#reload_serviceObject



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/chef/provider/service/systemd.rb', line 160

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", new_resource.service_name, default_env: false, **options)
    else
      start_service
    end
  end
end

#restart_serviceObject



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

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

#start_serviceObject



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

def start_service
  if current_resource.running
    logger.debug("#{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", new_resource.service_name, default_env: false, **options)
    end
  end
end

#stop_serviceObject



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/chef/provider/service/systemd.rb', line 138

def stop_service
  unless current_resource.running
    logger.debug("#{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", new_resource.service_name, default_env: false, **options)
    end
  end
end

#systemd_service_statusObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/provider/service/systemd.rb', line 81

def systemd_service_status
  @systemd_service_status ||= begin
    # Collect all the status information for a service and returns it at once
    options, args = get_systemctl_options_args
    s = shell_out!(systemctl_path, args, "show", "-p", "UnitFileState", "-p", "ActiveState", new_resource.service_name, **options)
    # e.g. /bin/systemctl --system show  -p UnitFileState -p ActiveState sshd.service
    # Returns something like:
    # ActiveState=active
    # UnitFileState=enabled
    status = {}
    s.stdout.each_line do |line|
      k, v = line.strip.split("=")
      status[k] = v
    end

    # Assert requisite keys exist
    unless status.key?("UnitFileState") && status.key?("ActiveState")
      raise Chef::Exceptions::Service, "'#{systemctl_path} show' not reporting status for #{new_resource.service_name}!"
    end

    status
  end
end

#unmask_serviceObject



205
206
207
208
# File 'lib/chef/provider/service/systemd.rb', line 205

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



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

def user_services_requirements; end