Skip to content

Commit

Permalink
Use IO for parsing/build service files
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Sep 26, 2019
1 parent 6dfa10a commit 229738d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 66 deletions.
4 changes: 3 additions & 1 deletion src/prefix/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ struct DPPM::Prefix::App
if pkg_env = pkg_file.env
service.config.env_vars.merge! pkg_env
end
File.write service_file, service.config_build
File.open service_file, "w" do |io|
service.config.build io
end
service
end

Expand Down
10 changes: 10 additions & 0 deletions src/service/init_system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ module Service::InitSystem
file : Path,
boot_file : Path

getter config : Config do
if @file && File.exists? @file.to_s
File.open @file.to_s do |io|
Config.parse io
end
else
Config.new
end
end

def type : String
self.class.type
end
Expand Down
12 changes: 0 additions & 12 deletions src/service/openrc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ class Service::OpenRC
@boot_file = Path["/etc/runlevels/default", @name]
end

getter config : Config do
if @file && File.exists? @file.to_s
Config.from_openrc File.read(@file.to_s)
else
Config.new
end
end

def config_build : String
config.to_openrc
end

def self.each(&block : String -> _)
Dir.each_child "/etc/init.d" do |service|
yield service
Expand Down
80 changes: 42 additions & 38 deletions src/service/openrc_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Service::Config
private OPENRC_NETWORK_SERVICE = "net"

# ameba:disable Metrics/CyclomaticComplexity
def self.from_openrc(data : String)
def self.parse(data : String | IO)
service = new
line_number = 1
function_name = ""
Expand Down Expand Up @@ -71,50 +71,54 @@ class Service::Config
service
end

# ameba:disable Metrics/CyclomaticComplexity
def to_openrc : String
def build : String
String.build do |str|
str << OPENRC_SHEBANG << "\n\n"
str << OPENRC_SUPERVISOR << '\n'
str << OPENRC_PIDFILE
str << "\nextra_started_commands=reload" if @reload_signal
str << "\ncommand_user='#{@user}:#{@group}'" if @user || @group
str << "\ndirectory='" << @directory << '\'' if @directory
if command = @command
command_elements = command.partition ' '
str << "\ncommand='" << command_elements[0] << '\''
if !command_elements[2].empty?
str << "\ncommand_args='" << command_elements[2] << '\''
end
end
str << "\noutput_log='" << @log_output << '\'' if @log_output
str << "\nerror_log='" << @log_error << '\'' if @log_error
str << "\ndescription='" << @description << '\'' if @description
str << "\nrespawn_delay=" << @restart_delay if @restart_delay
str << "\numask=" << @umask if @umask
build str
end
end

if !@env_vars.empty?
str << '\n' << OPENRC_ENV_VARS_PREFIX
build_env_vars str
str << "'\""
# ameba:disable Metrics/CyclomaticComplexity
def build(io : IO) : Nil
io << OPENRC_SHEBANG << "\n\n"
io << OPENRC_SUPERVISOR << '\n'
io << OPENRC_PIDFILE
io << "\nextra_started_commands=reload" if @reload_signal
io << "\ncommand_user='#{@user}:#{@group}'" if @user || @group
io << "\ndirectory='" << @directory << '\'' if @directory
if command = @command
command_elements = command.partition ' '
io << "\ncommand='" << command_elements[0] << '\''
if !command_elements[2].empty?
io << "\ncommand_args='" << command_elements[2] << '\''
end
end
io << "\noutput_log='" << @log_output << '\'' if @log_output
io << "\nerror_log='" << @log_error << '\'' if @log_error
io << "\ndescription='" << @description << '\'' if @description
io << "\nrespawn_delay=" << @restart_delay if @restart_delay
io << "\numask=" << @umask if @umask

str << "\n\ndepend() {\n\tafter "
@after << OPENRC_NETWORK_SERVICE
@after.join ' ', str
str << "}\n"
if !@env_vars.empty?
io << '\n' << OPENRC_ENV_VARS_PREFIX
build_env_vars io
io << "'\""
end

if @reload_signal
str << <<-E
io << "\n\ndepend() {\n\tafter "
@after << OPENRC_NETWORK_SERVICE
@after.join ' ', io
io << "}\n"

reload() {
\tebegin "Reloading $RC_SVCNAME"
\t#{OPENRC_RELOAD_COMMAND}#{@reload_signal}
\teend $? "Failed to reload $RC_SVCNAME"
}
if @reload_signal
io << <<-E
E
end
reload() {
\tebegin "Reloading $RC_SVCNAME"
\t#{OPENRC_RELOAD_COMMAND}#{@reload_signal}
\teend $? "Failed to reload $RC_SVCNAME"
}
E
end
end
end
12 changes: 0 additions & 12 deletions src/service/systemd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ class Service::Systemd
@boot_file = Path["/etc/systemd/system/multi-user.target.wants/#{@name}.service"]
end

getter config : Config do
if @file && File.exists? @file.to_s
Config.from_systemd File.read(@file.to_s)
else
Config.new
end
end

def config_build : String
config.to_systemd
end

def self.each(&block : String -> _)
{"/lib/systemd/system", "/etc/systemd/system"}.each do |service_dir|
Dir.each_child service_dir do |service|
Expand Down
12 changes: 9 additions & 3 deletions src/service/systemd_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Service::Config
private SYSTEMD_SHELL_LOG_REDIRECT = "/bin/sh -c '2>>"
private SYSTEMD_NETWORK_SERVICE = "network.target"

def self.from_systemd(data : String)
def self.parse(data : String | IO)
service = new
ini = INI.parse data
if reload = ini["Service"]["ExecReload"]?
Expand Down Expand Up @@ -49,8 +49,14 @@ class Service::Config
service
end

def build : String
String.build do |str|
build str
end
end

# ameba:disable Metrics/CyclomaticComplexity
def to_systemd : String
def build(io : IO) : Nil
# Transform the hash to a systemd service
systemd = {"Unit" => Hash(String, String).new,
"Service" => {
Expand Down Expand Up @@ -111,6 +117,6 @@ class Service::Config
if !@env_vars.empty?
systemd["Service"]["Environment"] = build_env_vars
end
INI.build systemd
INI.build io, systemd
end
end

0 comments on commit 229738d

Please sign in to comment.