From 85ecf2d20a19cfd3ae837c5787124c5ef9aa5f14 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 8 Nov 2018 13:42:13 +0100 Subject: [PATCH] create generic service manager (systemd only, TODO:init) --- internal/ser/common.go | 9 +++++++++ internal/ser/errors.go | 7 +++++++ internal/ser/loader.go | 45 +++++++++++++++++++++++++++++++++++++++++ internal/ser/systemd.go | 31 ++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 internal/ser/common.go create mode 100644 internal/ser/errors.go create mode 100644 internal/ser/loader.go create mode 100644 internal/ser/systemd.go diff --git a/internal/ser/common.go b/internal/ser/common.go new file mode 100644 index 0000000..ff3465f --- /dev/null +++ b/internal/ser/common.go @@ -0,0 +1,9 @@ +package ser + +// ServiceManager is the common interface for service managers (systemd, init.d) +type ServiceManager interface { + // Name of executable (to check if installed and for debug) + Name() string + // Exec a specific action on a service + Exec(action, service string) error +} diff --git a/internal/ser/errors.go b/internal/ser/errors.go new file mode 100644 index 0000000..ebd6cf8 --- /dev/null +++ b/internal/ser/errors.go @@ -0,0 +1,7 @@ +package ser + +import ( + "errors" +) + +var ErrUnknownAction = errors.New("unknown action") diff --git a/internal/ser/loader.go b/internal/ser/loader.go new file mode 100644 index 0000000..4c35168 --- /dev/null +++ b/internal/ser/loader.go @@ -0,0 +1,45 @@ +package ser + +import ( + "errors" + "os/exec" +) + +var ErrUnknownManager = errors.New("unknown service manager") +var ErrNoCandidateInstalled = errors.New("no package-manager candidate installed") + +// available service managers +// var available = []string{"systemd", "init"} +var available = []string{"systemd"} + +func Load(_manager string) (ServiceManager, error) { + + // 1. fail if unknown manager + known := false + for _, mgr := range available { + if _manager == mgr { + known = true + break + } + } + if !known { + return nil, ErrUnknownManager + } + + // 2. Create manager accordingly + var manager ServiceManager + switch _manager { + case "systemd": + manager = new(Systemd) + default: + return nil, ErrUnknownManager + } + + // 2. fail if not installed + if exec.Command("which", manager.Name()).Run() == nil { + return nil, ErrNoCandidateInstalled + } + + return manager, nil + +} diff --git a/internal/ser/systemd.go b/internal/ser/systemd.go new file mode 100644 index 0000000..0b9e8ed --- /dev/null +++ b/internal/ser/systemd.go @@ -0,0 +1,31 @@ +package ser + +import ( + "os/exec" +) + +type Systemd struct{} + +// available actions +var actions = []string{"enable", "disable", "start", "stop", "reload", "restart"} + +func (d Systemd) Name() string { return "systemctl" } + +func (d Systemd) Exec(action, service string) error { + + // 1. fail if unknown action + known := false + for _, act := range actions { + if action == act { + known = true + break + } + } + if !known { + return ErrUnknownAction + } + + // 2. execute command + return exec.Command("systemctl", action, service).Run() + +}