Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Add -installService command
Browse files Browse the repository at this point in the history
Will install or re-generate and reload systemctl for linux
  • Loading branch information
LordRalex committed May 20, 2017
1 parent 61a81de commit 0fa05e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
16 changes: 15 additions & 1 deletion install/service_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"os/exec"
"syscall"
"os"
)

const SYSTEMD = `
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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")
}
2 changes: 1 addition & 1 deletion install/service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
10 changes: 8 additions & 2 deletions pufferd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -73,14 +74,18 @@ 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)

if pid != 0 {
logging.Info("Shutting down")
shutdown.Command(pid)
return
}

if installService {
install.InstallService()
}

if _, err := os.Stat(configPath); os.IsNotExist(err) {
Expand Down Expand Up @@ -141,7 +146,7 @@ func main() {
migration.MigrateFromScales()
}

if license || version || regenerate || migrate {
if license || version || regenerate || migrate || installService || pid != 0 {
return
}

Expand Down Expand Up @@ -253,3 +258,4 @@ func main() {
}
shutdown.Shutdown()
}

0 comments on commit 0fa05e8

Please sign in to comment.