class Chef::Provider::SystemdUnit

Public Instance Methods

active?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 224
def active?
  # Note: "activating" is not active (as with type=notify or a oneshot)
  systemd_unit_status["ActiveState"] == "active"
end
define_resource_requirements() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 50
def define_resource_requirements
  super

  requirements.assert(:create) do |a|
    a.assertion { IniParse.parse(new_resource.to_ini) }
    a.failure_message "Unit content is not valid INI text"
  end
end
enabled?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 229
def enabled?
  # 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_unit_status["UnitFileState"])
end
indirect?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 246
def indirect?
  systemd_unit_status["UnitFileState"] == "indirect"
end
load_current_resource() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 34
def load_current_resource
  @current_resource = Chef::Resource::SystemdUnit.new(new_resource.name)

  current_resource.unit_name(new_resource.unit_name)
  current_resource.content(::File.read(unit_path)) if ::File.exist?(unit_path)
  current_resource.user(new_resource.user)
  current_resource.enabled(enabled?)
  current_resource.active(active?)
  current_resource.masked(masked?)
  current_resource.static(static?)
  current_resource.indirect(indirect?)
  current_resource.triggers_reload(new_resource.triggers_reload)

  current_resource
end
masked?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 236
def masked?
  # Note: masked-runtime is excluded, because runtime is volatile, and
  # because masked-runtime is not masked.
  systemd_unit_status["UnitFileState"] == "masked"
end
static?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 242
def static?
  systemd_unit_status["UnitFileState"] == "static"
end
systemd_unit_status() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 59
def systemd_unit_status
  @systemd_unit_status ||= begin
    # Collect all the status information for a unit and return it at once
    # This may fail if we are managing a template unit (e.g. with '@'), in which case
    # we just ignore the error because unit status is irrelevant in that case
    s = shell_out(*systemctl_cmd, "show", "-p", "UnitFileState", "-p", "ActiveState", new_resource.unit_name, **systemctl_opts)
    # e.g. /bin/systemctl --system show -p UnitFileState -p ActiveState syslog.socket
    # Returns something like:
    # ActiveState=inactive
    # UnitFileState=static
    status = {}
    s.stdout.each_line do |line|
      k, v = line.strip.split("=")
      status[k] = v
    end

    status
  end
end

Private Instance Methods

daemon_reload() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 272
def daemon_reload
  shell_out!(systemctl_cmd, "daemon-reload", **systemctl_opts, default_env: false)
end
manage_unit_file(the_action = :nothing) click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 260
def manage_unit_file(the_action = :nothing)
  file unit_path do
    owner "root"
    group "root"
    mode "0644"
    sensitive new_resource.sensitive
    content new_resource.to_ini
    verify :systemd_unit if new_resource.verify
    action the_action
  end
end
systemctl_args() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 292
def systemctl_args
  @systemctl_args ||= new_resource.user ? "--user" : "--system"
end
systemctl_cmd() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 284
def systemctl_cmd
  @systemctl_cmd ||= [ systemctl_path, systemctl_args ]
end
systemctl_execute(action, unit, **options) click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 280
def systemctl_execute(action, unit, **options)
  shell_out(systemctl_cmd, action, unit, **systemctl_opts.merge(options))
end
systemctl_execute!(action, unit, **options) click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 276
def systemctl_execute!(action, unit, **options)
  shell_out!(systemctl_cmd, action, unit, **systemctl_opts.merge(options))
end
systemctl_opts() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 296
def systemctl_opts
  @systemctl_opts ||=
    if new_resource.user
      uid = Etc.getpwnam(new_resource.user).uid
      {
        user: new_resource.user,
        environment: {
          "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/#{uid}/bus",
        },
      }
    else
      {}
    end
end
systemctl_path() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 288
def systemctl_path
  @systemctl_path ||= which("systemctl")
end
unit_path() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 252
def unit_path
  if new_resource.user
    "/etc/systemd/user/#{new_resource.unit_name}"
  else
    "/etc/systemd/system/#{new_resource.unit_name}"
  end
end