class Chef::Provider::SystemdUnit

Public Instance Methods

action_create() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 59
def action_create
  if current_resource.content != new_resource.to_ini
    converge_by("creating unit: #{new_resource.unit_name}") do
      manage_unit_file(:create)
      daemon_reload if new_resource.triggers_reload
    end
  end
end
action_delete() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 68
def action_delete
  if ::File.exist?(unit_path)
    converge_by("deleting unit: #{new_resource.unit_name}") do
      manage_unit_file(:delete)
      daemon_reload if new_resource.triggers_reload
    end
  end
end
action_disable() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 101
def action_disable
  if current_resource.static
    logger.trace("#{new_resource.unit_name} is a static unit, disabling is a NOP.")
  end

  if current_resource.enabled && !current_resource.static
    converge_by("disabling unit: #{new_resource.unit_name}") do
      systemctl_execute!(:disable, new_resource.unit_name)
    end
  end
end
action_enable() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 89
def action_enable
  if current_resource.static
    logger.trace("#{new_resource.unit_name} is a static unit, enabling is a NOP.")
  end

  unless current_resource.enabled || current_resource.static
    converge_by("enabling unit: #{new_resource.unit_name}") do
      systemctl_execute!(:enable, new_resource.unit_name)
    end
  end
end
action_mask() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 119
def action_mask
  unless current_resource.masked
    converge_by("masking unit: #{new_resource.unit_name}") do
      systemctl_execute!(:mask, new_resource.unit_name)
    end
  end
end
action_preset() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 77
def action_preset
  converge_by("restoring enable/disable preset configuration for unit: #{new_resource.unit_name}") do
    systemctl_execute!(:preset, new_resource.unit_name)
  end
end
action_reenable() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 113
def action_reenable
  converge_by("reenabling unit: #{new_resource.unit_name}") do
    systemctl_execute!(:reenable, new_resource.unit_name)
  end
end
action_reload() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 157
def action_reload
  if current_resource.active
    converge_by("reloading unit: #{new_resource.unit_name}") do
      systemctl_execute!(:reload, new_resource.unit_name)
    end
  else
    logger.trace("#{new_resource.unit_name} is not active, skipping reload.")
  end
end
action_reload_or_restart() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 173
def action_reload_or_restart
  converge_by("reload-or-restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!("reload-or-restart", new_resource.unit_name)
  end
end
action_reload_or_try_restart() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 179
def action_reload_or_try_restart
  converge_by("reload-or-try-restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!("reload-or-try-restart", new_resource.unit_name)
  end
end
action_restart() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 151
def action_restart
  converge_by("restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!(:restart, new_resource.unit_name)
  end
end
action_revert() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 83
def action_revert
  converge_by("reverting to vendor version of unit: #{new_resource.unit_name}") do
    systemctl_execute!(:revert, new_resource.unit_name)
  end
end
action_start() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 135
def action_start
  unless current_resource.active
    converge_by("starting unit: #{new_resource.unit_name}") do
      systemctl_execute!(:start, new_resource.unit_name)
    end
  end
end
action_stop() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 143
def action_stop
  if current_resource.active
    converge_by("stopping unit: #{new_resource.unit_name}") do
      systemctl_execute!(:stop, new_resource.unit_name)
    end
  end
end
action_try_restart() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 167
def action_try_restart
  converge_by("try-restarting unit: #{new_resource.unit_name}") do
    systemctl_execute!("try-restart", new_resource.unit_name)
  end
end
action_unmask() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 127
def action_unmask
  if current_resource.masked
    converge_by("unmasking unit: #{new_resource.unit_name}") do
      systemctl_execute!(:unmask, new_resource.unit_name)
    end
  end
end
active?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 185
def active?
  systemctl_execute("is-active", new_resource.unit_name).exitstatus == 0
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 189
def enabled?
  systemctl_execute("is-enabled", new_resource.unit_name).exitstatus == 0
end
load_current_resource() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 35
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.triggers_reload(new_resource.triggers_reload)

  current_resource
end
masked?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 193
def masked?
  systemctl_execute(:status, new_resource.unit_name).stdout.include?("masked")
end
static?() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 197
def static?
  systemctl_execute("is-enabled", new_resource.unit_name).stdout.include?("static")
end

Private Instance Methods

daemon_reload() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 221
def daemon_reload
  shell_out_with_systems_locale!("#{systemctl_path} daemon-reload")
end
manage_unit_file(action = :nothing) click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 211
def manage_unit_file(action = :nothing)
  Chef::Resource::File.new(unit_path, run_context).tap do |f|
    f.owner "root"
    f.group "root"
    f.mode "0644"
    f.content new_resource.to_ini
    f.verify :systemd_unit if new_resource.verify
  end.run_action(action)
end
systemctl_args() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 241
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 233
def systemctl_cmd
  @systemctl_cmd ||= "#{systemctl_path} #{systemctl_args}"
end
systemctl_execute(action, unit) click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 229
def systemctl_execute(action, unit)
  shell_out("#{systemctl_cmd} #{action} #{Shellwords.escape(unit)}", systemctl_opts)
end
systemctl_execute!(action, unit) click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 225
def systemctl_execute!(action, unit)
  shell_out_with_systems_locale!("#{systemctl_cmd} #{action} #{Shellwords.escape(unit)}", systemctl_opts)
end
systemctl_opts() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 245
def systemctl_opts
  @systemctl_opts ||=
    if new_resource.user
      uid = Etc.getpwuid(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 237
def systemctl_path
  @systemctl_path ||= which("systemctl")
end
unit_path() click to toggle source
# File lib/chef/provider/systemd_unit.rb, line 203
def unit_path
  if new_resource.user
    "/etc/systemd/user/#{new_resource.unit_name}"
  else
    "/etc/systemd/system/#{new_resource.unit_name}"
  end
end