module SyncWrap::SystemDService
Support module for components which install SystemD services. Provide unit file installation with `systemctl` calls, and also provides standard enable, start, status, restart, stop, and disable commands for use in the CLI.
Host component dependencies: <Distro>
Attributes
An array of ListenStream configuration values for systemd_socket (Default: nil -> unspecified and will raise if accessed)
The name of a systemd service unit file to create/rput. The name should include a “.service” suffix. Will rput the same name (or .erb extended template) under :sync_paths /etc/systemd/system. (Required for use of public interface)
The name of the systemd socket unit file to create/rput for socket activation. If specified, the name should include a “.socket” suffix. Will rput the same name (or .erb extended template) under :sync_paths /etc/systemd/system.
Public Class Methods
# File lib/syncwrap/systemd_service.rb, line 49 def initialize( opts = {} ) @systemd_service = nil @systemd_socket = nil @listen_streams = nil super end
Public Instance Methods
Disable all systemd_units.
# File lib/syncwrap/systemd_service.rb, line 109 def disable require_systemd_service! systemctl( 'disable', *systemd_units ) end
Enable all systemd_units.
# File lib/syncwrap/systemd_service.rb, line 115 def enable require_systemd_service! systemctl( 'enable', *systemd_units ) end
Install the systemd units files by rput_unit_files, detecting changes and performs `systemctl` daemon-reload, (re-)enable, and restart or start as required. If restart_required evaluates to true, for example given other external changes, a restart is mandated.
# File lib/syncwrap/systemd_service.rb, line 63 def install_units( restart_required = false ) require_systemd_service! units_d = rput_unit_files sock_d = systemd_socket && units_d.find { |_,f| File.basename( f ) == systemd_socket } if !units_d.empty? systemctl( 'daemon-reload' ) systemctl( 'reenable', *systemd_units ) end if ( restart_required || !units_d.empty? ) restart( with_socket: !!sock_d ) else start end units_d end
Restart the service. If option :with_socket is passed as true, restart all systemd_units which includes systemd_socket if present.
# File lib/syncwrap/systemd_service.rb, line 93 def restart( opts = {} ) require_systemd_service! if opts[ :with_socket ] systemctl( 'restart', *systemd_units ) else systemctl( 'restart', systemd_service ) end end
Start all systemd_units.
# File lib/syncwrap/systemd_service.rb, line 85 def start require_systemd_service! systemctl( 'start', *systemd_units ) end
Output status of systemd_units (useful via CLI with –verbose). Exit codes 0-3 are accepted from `systemctl`, since these will be returned in normal operational contexts, for example, when services are intentionally and manually stopped. Exit on error is also disabled, which may be relevent for merged commands.
# File lib/syncwrap/systemd_service.rb, line 125 def status require_systemd_service! systemctl_status( *systemd_units, error: false, accept: [0,1,2,3] ) end
Stop all systemd_units.
# File lib/syncwrap/systemd_service.rb, line 103 def stop require_systemd_service! systemctl( 'stop', *systemd_units ) end
Protected Instance Methods
# File lib/syncwrap/systemd_service.rb, line 45 def listen_streams @listen_streams or raise "#{self.class.name}#listen_streams not specified" end
Raise if #systemd_service not specified.
# File lib/syncwrap/systemd_service.rb, line 150 def require_systemd_service! raise "#{self.class.name}#systemd_service not set" unless systemd_service end
Perform rput of systemd unit files and return changes array. This can be overridden, for example to use system provided units (no-op and return `[]`) or for additional “drop-in” config files (e.g. foo.service.d/overrides.conf). Any changes signal that the service (or if included in changes, the socket) should be restarted.
# File lib/syncwrap/systemd_service.rb, line 138 def rput_unit_files srcs = systemd_units.map { |u| "/etc/systemd/system/" + u } rput( *srcs, "/etc/systemd/system/", user: :root ) end
Return Array of the unit names that were specified (not nil) via systemd_service and systemd_socket.
# File lib/syncwrap/systemd_service.rb, line 145 def systemd_units [systemd_socket, systemd_service].compact end