forked from kadalu/moana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use systemd as service manager when
--service-mgr
argument is passed
Updates: kadalu#310 Signed-off-by: Aravinda Vishwanathapura <[email protected]>
- Loading branch information
1 parent
74851b0
commit b758ce6
Showing
8 changed files
with
111 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class ServiceManagerException < Exception | ||
end | ||
|
||
abstract class ServiceManager | ||
abstract def start(svc_id : String, cmd : Array(String)) | ||
abstract def stop(svc_id : String) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require "../../plugins/helpers" | ||
require "./helpers" | ||
|
||
SYSTEMCTL = "systemctl" | ||
UNIT_FILE_DIR = "/lib/systemd/system/" | ||
|
||
def unit_file_content(svc_id, cmd) | ||
%Q[[Unit] | ||
Description=#{svc_id} | ||
After=network.target | ||
[Service] | ||
PIDFile=/run/kadalu/#{svc_id}.pid | ||
ExecStart=#{cmd} | ||
[Install] | ||
WantedBy=multi-user.target | ||
] | ||
end | ||
|
||
class SystemdServiceManager < ServiceManager | ||
def initialize | ||
end | ||
|
||
def create(svc_id, cmd) | ||
svc_file = "#{UNIT_FILE_DIR}/kadalu-#{svc_id}.service" | ||
return if File.exists?(svc_file) | ||
|
||
File.write( | ||
svc_file, | ||
unit_file_content(svc_id, cmd.join(" ")) | ||
) | ||
rc, _out, err = execute(SYSTEMCTL, ["daemon-reload"]) | ||
if rc != 0 | ||
Log.warning &.emit("Systemd daemon-reload failed", svc_id: svc_id, err: err) | ||
end | ||
end | ||
|
||
def delete(svc_id) | ||
svc_file = "#{UNIT_FILE_DIR}/kadalu-#{svc_id}.service" | ||
File.delete(svc_file) if File.exists?(svc_file) | ||
end | ||
|
||
def start(svc_id, cmd) | ||
create(svc_id, cmd) | ||
content = File.read("#{UNIT_FILE_DIR}/kadalu-#{svc_id}.service") | ||
Log.info { "SVC_ID: #{svc_id}, CMD: #{cmd}, Content: #{content}" } | ||
rc, _out, err = execute(SYSTEMCTL, ["start", "kadalu-#{svc_id}.service"]) | ||
raise ServiceManagerException.new(err) unless rc == 0 | ||
end | ||
|
||
def stop(svc_id) | ||
rc, _out, err = execute(SYSTEMCTL, ["stop", "kadalu-#{svc_id}.service"]) | ||
raise ServiceManagerException.new(err) unless rc == 0 | ||
delete(svc_id) | ||
end | ||
|
||
def is_running?(svc_id) | ||
rc, _out, _err = execute(SYSTEMCTL, ["is-active", "--quiet", "kadalu-#{svc_id}.service"]) | ||
rc == 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters