Class: Koch::SystemdService

Inherits:
Resource show all
Defined in:
lib/koch/systemd_service.rb

Overview

A Systemd service

Instance Attribute Summary

Attributes inherited from Resource

#changed, #name

Instance Method Summary collapse

Methods inherited from Resource

dsl_writer, #initialize

Constructor Details

This class inherits a constructor from Koch::Resource

Instance Method Details

#apply!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/koch/systemd_service.rb', line 10

def apply!
  fatal "Systemd services require contents." if contents.nil?

  full_name = "/etc/systemd/system/#{name}.service"
  old_contents = begin
    File.read(full_name)
  rescue Errno::ENOENT
    nil
  end
  if old_contents == contents
    debug "Systemd service #{name} unchanged"
    return
  end

  @changed = true

  info "Diff for Systemd service #{name}:"
  info diff(old_contents, contents)
  maybe("Changed Systemd service #{name}") do
    File.write(full_name, contents)
  end
  if system("systemctl is-enabled --quiet #{name}.service")
    maybe "systemctl daemon-reload"
  else
    maybe "systemctl enable --now #{name}"
  end
end