Skip to content

Commit

Permalink
Fix service config build
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Sep 27, 2019
1 parent a71ac35 commit cb66471
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 21 deletions.
1 change: 1 addition & 0 deletions spec/cli_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe DPPM::CLI do
it "adds an application" do
spec_with_prefix do |prefix|
add_application prefix_path: prefix.path.to_s
delete_application prefix.path.to_s
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/prefix/app_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def spec_with_app(&block)
spec_with_prefix do |prefix|
pkg = prefix.new_pkg TEST_APP_PACKAGE_NAME
app = pkg.new_app TEST_APP_PACKAGE_NAME
app.add confirmation: false { }
app.add confirmation: false, add_service: false { }
begin
yield app
ensure
Expand Down
6 changes: 5 additions & 1 deletion spec/service_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def spec_with_service_app(service, &block)

test_app.service.file = test_app.service_file
test_app.service_create.config

begin
yield test_app
ensure
Expand Down Expand Up @@ -67,7 +68,10 @@ def assert_service(service, file = __FILE__, line = __LINE__)

it "creates a service file building", file, line do
spec_with_service_app service do |app|
File.read(app.service_file).should eq app.service.config.build
service_config = String.build do |str|
app.service.config_build str
end
File.read(app.service_file).should eq service_config
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/prefix/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct DPPM::Prefix::App
service.config.env_vars.merge! pkg_env
end
File.open service_file, "w" do |io|
service.config.build io
service.config_build io
end
service
end
Expand Down
2 changes: 1 addition & 1 deletion src/service/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Service::Config

def parse_env_vars(env_vars : String)
env_vars.rchop.split("\" ").each do |env|
var, val = env.split("=\"")
var, _, val = env.partition "=\""
@env_vars[var] = val
end
rescue
Expand Down
5 changes: 4 additions & 1 deletion src/service/init_system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module Service::InitSystem
file : Path,
boot_file : Path

private abstract def config_parse(io : IO)
private abstract def config_build(io : IO)

getter config : Config do
if @file && File.exists? @file.to_s
File.open @file.to_s do |io|
Config.parse io
config_parse io
end
else
Config.new
Expand Down
8 changes: 8 additions & 0 deletions src/service/openrc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class Service::OpenRC
Service.exec? "/sbin/rc-service", {@name, {{action}}}
end
{% end %}

private def config_parse(io : IO)
Config.from_openrc io
end

def config_build(io : IO)
config.to_openrc io
end
end

require "./openrc_config"
10 changes: 2 additions & 8 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.parse(data : String | IO)
def self.from_openrc(data : String | IO)
service = new
line_number = 1
function_name = ""
Expand Down Expand Up @@ -71,14 +71,8 @@ class Service::Config
service
end

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

# ameba:disable Metrics/CyclomaticComplexity
def build(io : IO) : Nil
def to_openrc(io : IO) : Nil
io << OPENRC_SHEBANG << "\n\n"
io << OPENRC_SUPERVISOR << '\n'
io << OPENRC_PIDFILE
Expand Down
8 changes: 8 additions & 0 deletions src/service/systemd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class Service::Systemd
Service.exec? "/bin/systemctl", {"-q", "--no-ask-password", {{action}}, @name}
end
{% end %}

private def config_parse(io : IO)
Config.from_systemd io
end

def config_build(io : IO)
config.to_systemd io
end
end

require "./systemd_config"
10 changes: 2 additions & 8 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.parse(data : String | IO)
def self.from_systemd(data : String | IO)
service = new
ini = INI.parse data
if reload = ini["Service"]["ExecReload"]?
Expand Down Expand Up @@ -49,14 +49,8 @@ class Service::Config
service
end

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

# ameba:disable Metrics/CyclomaticComplexity
def build(io : IO) : Nil
def to_systemd(io : IO) : Nil
# Transform the hash to a systemd service
systemd = {"Unit" => Hash(String, String).new,
"Service" => {
Expand Down

0 comments on commit cb66471

Please sign in to comment.