diff --git a/install/install.go b/install/install.go index 0077d04..209ea69 100644 --- a/install/install.go +++ b/install/install.go @@ -42,5 +42,5 @@ func Install(configPath string, authRoot string, authToken string) { logging.Info("Config saved") logging.Info("Attempting to install service") - InstallService(configPath) + InstallService() } diff --git a/install/service_linux.go b/install/service_linux.go index 93a99bd..19280c1 100644 --- a/install/service_linux.go +++ b/install/service_linux.go @@ -21,6 +21,7 @@ import ( "io/ioutil" "os/exec" "syscall" + "os" ) const SYSTEMD = ` @@ -41,7 +42,7 @@ SendSIGKILL=no WantedBy=multi-user.target ` -func InstallService(configPath string) { +func InstallService() { cmd := exec.Command("useradd", "--system", "--home", "/var/lib/pufferd", "--user-group", "pufferd") err := cmd.Run() @@ -62,6 +63,9 @@ func InstallService(configPath string) { } } + info, err := os.Stat("/etc/systemd/system/pufferd.service") + exists := info != nil + err = ioutil.WriteFile("/etc/systemd/system/pufferd.service", []byte(SYSTEMD), 0664) if err != nil { logging.Error("Cannot write systemd file, will not install service", err) @@ -77,5 +81,15 @@ func InstallService(configPath string) { } logging.Info(string(output)) + if exists { + cmd = exec.Command("systemctl", "daemon-reload") + output, err = cmd.CombinedOutput() + if err != nil { + logging.Error("Error reloading systemctl", err) + logging.Error(string(output)) + return + } + logging.Info(string(output)) + } logging.Info("Service may be started with: systemctl start pufferd") } diff --git a/install/service_windows.go b/install/service_windows.go index a4b1a17..51e9d2c 100644 --- a/install/service_windows.go +++ b/install/service_windows.go @@ -18,6 +18,6 @@ package install import "github.com/pufferpanel/pufferd/logging" -func InstallService(configPath string) { +func InstallService() { logging.Error("No configured service installer for this OS") } diff --git a/pufferd.go b/pufferd.go index d388bd1..2b63c1c 100644 --- a/pufferd.go +++ b/pufferd.go @@ -62,6 +62,7 @@ func main() { var uninstall bool var configPath string var pid int + var installService bool flag.StringVar(&loggingLevel, "logging", "INFO", "Lowest logging level to display") flag.StringVar(&authRoot, "auth", "", "Base URL to the authorization server") flag.StringVar(&authToken, "token", "", "Authorization token") @@ -73,6 +74,7 @@ func main() { flag.BoolVar(&uninstall, "uninstall", false, "Uninstall pufferd") flag.StringVar(&configPath, "config", "config.json", "Path to pufferd config.json") flag.IntVar(&pid, "shutdown", 0, "PID to shut down") + flag.BoolVar(&installService, "installService", false, "Installs the pufferd service file") flag.Parse() versionString := fmt.Sprintf("pufferd %s (%s %s)", VERSION, BUILDDATE, GITHASH) @@ -80,7 +82,10 @@ func main() { if pid != 0 { logging.Info("Shutting down") shutdown.Command(pid) - return + } + + if installService { + install.InstallService() } if _, err := os.Stat(configPath); os.IsNotExist(err) { @@ -141,7 +146,7 @@ func main() { migration.MigrateFromScales() } - if license || version || regenerate || migrate { + if license || version || regenerate || migrate || installService || pid != 0 { return } @@ -253,3 +258,4 @@ func main() { } shutdown.Shutdown() } +