Skip to content

Commit

Permalink
Move service creation to Prefix::App
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Jan 18, 2019
1 parent 480d79a commit d6a0516
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 135 deletions.
4 changes: 2 additions & 2 deletions spec/service_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ describe Service do

{% for sysinit in %w(OpenRC Systemd) %}
describe {{sysinit}} do
Service.init = Service::{{sysinit.id}}
test_prefix = Prefix.new(__DIR__ + "/service_test", create: true)
test_app = test_prefix.new_app(TEST_APP_PACKAGE_NAME)
FileUtils.cp_r __DIR__ + "/samples/" + TEST_APP_PACKAGE_NAME, test_app.path


Service.init = Service::{{sysinit.id}}
service_config = Service::{{sysinit.id}}::Config.new

it "creates a service" do
Service::{{sysinit.id}}.new(TEST_APP_PACKAGE_NAME).create(test_app, user, group)
test_app.service_create user, group
end

it "parses the service" do
Expand Down
8 changes: 4 additions & 4 deletions src/manager/application/add.cr
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ struct Manager::Application::Add

# Create system user and group for the application
if Process.root?
@app.service?.try do |service|
if @app.service?
# Create system services
service.create @app, @user, @group
service.enable @app
Log.info service.type + " system service added", service.name
@app.service_create @user, @group
@app.service_enable
Log.info @app.service.type + " system service added", @app.service.name
end

libcrown = Libcrown.new
Expand Down
56 changes: 51 additions & 5 deletions src/prefix/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ struct Prefix::App
Pkg.new @prefix, File.basename(File.dirname(File.real_path(app_path))), nil, @pkg_file
end

getter service : Service::OpenRC | Service::Systemd do
Service.init.new @name
getter? service : Service::OpenRC | Service::Systemd | Nil

def service : Service::OpenRC | Service::Systemd
if _service = @service
_service
else
raise "service not available"
end
end

getter service_dir : String do
Expand All @@ -33,10 +39,47 @@ struct Prefix::App
service_dir + service.type
end

def service? : Service::OpenRC | Service::Systemd | Nil
if Service.init?
@service ||= service
def service_create(user : String, group : String)
_service = service
(exec = pkg_file.exec) || raise "exec key not present in #{pkg_file.path}"

Dir.mkdir_p service_dir

Log.info "creating system service", @name

# Set service options
{description: pkg_file.description,
directory: path,
command: path + exec["start"],
user: user,
group: group,
restart_delay: "9",
umask: "007",
log_output: log_file_output,
log_error: log_file_error,
}.each do |key, value|
_service.config.set key.to_s, value
end

# add a reload directive if available
if exec_reload = exec["reload"]?
_service.config.set("reload", exec_reload)
end

# Add a PATH environment variable if not empty
if !(path_vars = env_vars).empty?
_service.config.env_set("PATH", path_vars)
end
if pkg_env = pkg_file.env
pkg_env.each { |var, value| _service.config.env_set var, value }
end

# Convert back hashes to service files
File.write service_file, _service.config.build
end

def service_enable
service.link service_file
end

def database? : Database::MySQL | Nil
Expand Down Expand Up @@ -77,6 +120,9 @@ struct Prefix::App
@logs_dir = @path + "log/"
@log_file_output = @logs_dir + "output.log"
@log_file_error = @logs_dir + "error.log"
Service.init?.try do |_service|
@service = _service.new @name
end
end

def set_config(key : String, value)
Expand Down
58 changes: 58 additions & 0 deletions src/service/init_system.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Service::InitSystem
getter name : String,
file : String,
boot_file : String

def type : String
self.class.type
end

def boot? : Bool
File.exists? @boot_file
end

def exists? : Bool
File.symlink?(@file) || File.exists?(@file)
end

def writable? : Bool
File.writable? @file
end

def boot(value : Bool) : Bool
case value
when boot? # nothing to do
when true then File.symlink @file, @boot_file
when false then File.delete boot_file
end
value
end

def real_file : String
File.real_path file
end

private def delete_internal
stop if run?
boot false if boot?
File.delete @file if exists?
end

def check_availability
if exists?
raise "system service already exist: " + name
elsif !File.writable? File.dirname(@file)
Log.warn "service creation unavailable, root permissions required", name
else
Log.info "service available for creation", name
end
end

def check_delete
if !writable?
raise "root execution needed for system service deletion: " + name
elsif !exists?
raise "service doesn't exist: " + name
end
end
end
25 changes: 12 additions & 13 deletions src/service/openrc.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
require "./system"
require "./init_system"

struct Service::OpenRC
include System
include InitSystem
class_getter type : String = "openrc"

class_getter version : String do
output, error = Exec.new "/sbin/openrc", {"-V"}, &.wait
output.to_s =~ /([0-9]+\.[0-9]+\.[0-9]+)/
$1.not_nil!
rescue
raise "can't retrieve the OpenRC version: #{output}#{error}"
end

getter config : Config do
Config.new @file
end
Expand All @@ -27,8 +35,8 @@ struct Service::OpenRC
delete_internal
end

def enable(app : Prefix::App)
File.symlink app.service_file, @file
def link(service_file : String)
File.symlink service_file, @file
File.chmod @file, 0o750
end

Expand All @@ -37,15 +45,6 @@ struct Service::OpenRC
Service.exec? "/sbin/rc-service", {@name, {{action}}}
end
{% end %}

def self.version : String
output, error = Exec.new "/sbin/openrc", {"-V"}, &.wait
if output.to_s =~ /([0-9]+\.[0-9]+\.[0-9]+)/
$1
else
raise "can't retrieve the OpenRC version: #{output}#{error}"
end
end
end

require "./openrc/*"
2 changes: 1 addition & 1 deletion src/service/openrc/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct Service::OpenRC::Config
def initialize(@section : Hash(String, String | Array(String) | Hash(String, Array(String))))
end

def self.new(file : String? = nil)
def self.new(file : String? = nil) : Config
if file && File.exists? file
parse File.read(file)
else
Expand Down
96 changes: 0 additions & 96 deletions src/service/system.cr

This file was deleted.

24 changes: 11 additions & 13 deletions src/service/systemd.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
require "./system"
require "./init_system"

struct Service::Systemd
include System
include InitSystem
class_getter type : String = "systemd"

class_getter version : Int32 do
output, error = Exec.new "/bin/systemctl", {"--version"}, &.wait
output.to_s.lines[0].lstrip("systemd ").to_i
rescue
raise "can't retrieve the systemd version: #{output}#{error}"
end

getter config : Config do
Config.new @file
end
Expand Down Expand Up @@ -32,8 +39,8 @@ struct Service::Systemd
Service.exec? "/bin/systemctl", {"--no-ask-password", "daemon-reload"}
end

def enable(app : Prefix::App) : Bool
File.symlink app.service_file, @file
def link(service_file : String)
File.symlink service_file, @file
Service.exec? "/bin/systemctl", {"--no-ask-password", "daemon-reload"}
end

Expand All @@ -42,15 +49,6 @@ struct Service::Systemd
Service.exec? "/bin/systemctl", {"-q", "--no-ask-password", {{action}}, @name}
end
{% end %}

def self.version : Int32
output, error = Exec.new "/bin/systemctl", {"--version"}, &.wait
if output.to_s =~ / ([0-9]+)\n/
$1.to_i
else
raise "can't retrieve the systemd version: #{output}#{error}"
end
end
end

require "./systemd/*"
2 changes: 1 addition & 1 deletion src/service/systemd/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct Service::Systemd::Config
def initialize(@section : Hash(String, Hash(String, String)))
end

def self.new(file : String? = nil)
def self.new(file : String? = nil) : Config
if file && File.exists? file
parse File.read(file)
else
Expand Down

0 comments on commit d6a0516

Please sign in to comment.