class Chef::Provider::Service::Systemd

Attributes

status_check_success[RW]

Public Class Methods

supports?(resource, action) click to toggle source
# 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

Public Instance Methods

define_resource_requirements() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 68
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_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 150
def disable_service
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} disable #{Shellwords.escape(new_resource.service_name)}", **options)
end
enable_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 145
def enable_service
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} enable #{Shellwords.escape(new_resource.service_name)}", **options)
end
get_systemctl_options_args() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 78
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?() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 165
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?() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 170
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_masked?() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 175
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_resource() click to toggle source
# 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)
    end
  else
    current_resource.running(is_active?)
  end

  current_resource.enabled(is_enabled?)
  current_resource.masked(is_masked?)
  current_resource
end
mask_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 155
def mask_service
  options, args = get_systemctl_options_args
  shell_out!("#{systemctl_path} #{args} mask #{Shellwords.escape(new_resource.service_name)}", **options)
end
reload_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 132
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_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 123
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_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 97
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_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 110
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_service() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 160
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_requirements() click to toggle source

systemd supports user services just fine

# File lib/chef/provider/service/systemd.rb, line 65
def user_services_requirements
end

Private Instance Methods

systemctl_path() click to toggle source
# File lib/chef/provider/service/systemd.rb, line 183
def systemctl_path
  if @systemctl_path.nil?
    @systemctl_path = which("systemctl")
  end
  @systemctl_path
end