From 973d99c0fbb0b5b775543655ca334ef210ac47d7 Mon Sep 17 00:00:00 2001 From: Julien Reichardt Date: Mon, 9 Sep 2019 01:25:45 +0200 Subject: [PATCH] Install DPPM service optionally when available --- .drone.yml | 2 +- src/cli.cr | 3 ++- src/host.cr | 7 ++++++- src/prefix/pkg.cr | 2 +- src/service.cr | 8 +++++--- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.drone.yml b/.drone.yml index 1250a19..a34949c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -26,7 +26,7 @@ steps: path: /root/.cache image: jrei/crystal-alpine commands: - - crystal spec + - crystal spec -D allow_root - name: build docs image: jrei/crystal-alpine diff --git a/src/cli.cr b/src/cli.cr index c954a1a..a984087 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -402,7 +402,8 @@ module DPPM::CLI app.add( vars: {"uid" => Process.uid.to_s, "gid" => Process.gid.to_s}, shared: true, - confirmation: !no_confirm + confirmation: !no_confirm, + add_service: !!Host.service_available? ) do no_confirm || CLI.confirm_prompt { raise "DPPM installation canceled." } end diff --git a/src/host.cr b/src/host.cr index 024701a..0641d5a 100644 --- a/src/host.cr +++ b/src/host.cr @@ -62,9 +62,14 @@ struct DPPM::Host Log.error "Unsupported architecture"; exit 1 {% end %} + def self.service_available? + Service.init? || Log.warn "services management unavailable", "DPPM is still usable. Consider OpenRC or systemd init systems" + Service.init? + end + # All system environment variables class_getter vars : Hash(String, String) do - Service.init? || DPPM::Log.warn "services management unavailable", "DPPM is still usable. Consider OpenRC or systemd init systems" + service_available? { "arch" => @@arch, "kernel" => @@kernel, diff --git a/src/prefix/pkg.cr b/src/prefix/pkg.cr index e0b88f7..b9b6104 100644 --- a/src/prefix/pkg.cr +++ b/src/prefix/pkg.cr @@ -88,7 +88,7 @@ class DPPM::Prefix::Pkg def create_global_bin_symlinks(force : Bool = false) each_binary_with_path do |path, binary| global_bin = Path["/usr/local/bin", binary].to_s - File.delete global_bin if File.exists? global_bin + File.delete global_bin if File.symlink?(global_bin) || File.exists?(global_bin) File.symlink (path / binary).to_s, global_bin end end diff --git a/src/service.cr b/src/service.cr index 80d946b..f329830 100644 --- a/src/service.cr +++ b/src/service.cr @@ -2,10 +2,12 @@ require "exec" require "./service/*" module Service + extend self + @@init : Systemd.class | OpenRC.class | Nil @@initialized = false - def self.init? : Systemd.class | OpenRC.class | Nil + def init? : Systemd.class | OpenRC.class | Nil if !@@initialized init_system = File.basename File.real_path "/sbin/init" if init_system == "systemd" @@ -18,11 +20,11 @@ module Service @@init end - def self.init : Systemd.class | OpenRC.class + def init : Systemd.class | OpenRC.class init? || raise "Unsupported init system" end - def self.exec?(command : String, args : Array(String) | Tuple) : Bool + def exec?(command : String, args : Array(String) | Tuple) : Bool success = false Exec.new command, args, output: DPPM::Log.output, error: DPPM::Log.error do |process| success = process.wait.success?