diff --git a/.gitignore b/.gitignore index 243101a..d96d1c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +installer watchdog.service /cmd/install/static /watchdog diff --git a/cmd/install/dist/etc/systemd/system/watchdog.service.tmpl b/cmd/install/dist/etc/systemd/system/watchdog.service.tmpl index 3754c01..438c903 100644 --- a/cmd/install/dist/etc/systemd/system/watchdog.service.tmpl +++ b/cmd/install/dist/etc/systemd/system/watchdog.service.tmpl @@ -3,6 +3,11 @@ # sudo mkdir -p /opt/{{ .Exec }}/ /var/log/{{ .Exec }} # sudo chown -R {{ .Exec }}:{{ .Exec }} /opt/{{ .Exec }}/ /var/log/{{ .Exec }} +# Post-install +# sudo systemctl daemon-reload +# sudo systemctl restart {{ .Exec }}.service +# sudo journalctl -xefu {{ .Exec }} + [Unit] Description={{ .Name }} - {{ .Desc }} Documentation={{ .URL }} @@ -24,7 +29,7 @@ Group={{ .Group }} {{ end -}} WorkingDirectory=/opt/{{ .Exec }} -ExecStart=/opt/{{ .Exec }} {{ .Args }} +ExecStart=/opt/{{ .Exec }}/{{ .Exec }} {{ .Args }} ExecReload=/bin/kill -USR1 $MAINPID {{if .Production -}} diff --git a/cmd/install/install.go b/cmd/install/install.go index 4a40854..6ed3143 100644 --- a/cmd/install/install.go +++ b/cmd/install/install.go @@ -4,17 +4,6 @@ // hence there are a few unnecessary things for the sake of the trying it out package main -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "log" - "text/template" - - "git.rootprojects.org/root/watchdog.go/cmd/install/static" -) - type Config struct { Name string `json:"name"` Desc string `json:"desc"` @@ -30,48 +19,5 @@ type Config struct { func main() { - b, err := static.ReadFile("dist/etc/systemd/system/watchdog.service.tmpl") - if err != nil { - log.Fatal(err) - return - } - s := string(b) - - j, err := static.ReadFile("dist/etc/systemd/system/watchdog.service.json") - if err != nil { - log.Fatal(err) - return - } - - //conf := map[string]string{} - conf := &Config{} - err = json.Unmarshal(j, &conf) - if nil != err { - log.Fatal(err) - return - } - if "" == conf.Group { - conf.Group = conf.User - } - - serviceFile := conf.Exec + ".service" - - rw := &bytes.Buffer{} - // not sure what the template name does, but whatever - tmpl, err := template.New("service").Parse(s) - if err != nil { - log.Fatal(err) - return - } - - err = tmpl.Execute(rw, conf) - if nil != err { - log.Fatal(err) - return - } - - if err := ioutil.WriteFile(serviceFile, rw.Bytes(), 0644); err != nil { - log.Fatalf("ioutil.WriteFile error: %v", err) - } - fmt.Printf("Wrote %q\n", serviceFile) + install() } diff --git a/cmd/install/install_darwin.go b/cmd/install/install_darwin.go new file mode 100644 index 0000000..bb61051 --- /dev/null +++ b/cmd/install/install_darwin.go @@ -0,0 +1,7 @@ +package main + +import "log" + +func install() { + log.Fatal("not yet implemented") +} diff --git a/cmd/install/install_linux.go b/cmd/install/install_linux.go new file mode 100644 index 0000000..f5dbd6e --- /dev/null +++ b/cmd/install/install_linux.go @@ -0,0 +1,60 @@ +// +build !windows !darwin +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "text/template" + + "git.rootprojects.org/root/watchdog.go/cmd/install/static" +) + +func install() { + b, err := static.ReadFile("dist/etc/systemd/system/watchdog.service.tmpl") + if err != nil { + log.Fatal(err) + return + } + s := string(b) + + j, err := static.ReadFile("dist/etc/systemd/system/watchdog.service.json") + if err != nil { + log.Fatal(err) + return + } + + //conf := map[string]string{} + conf := &Config{} + err = json.Unmarshal(j, &conf) + if nil != err { + log.Fatal(err) + return + } + if "" == conf.Group { + conf.Group = conf.User + } + + serviceFile := conf.Exec + ".service" + + rw := &bytes.Buffer{} + // not sure what the template name does, but whatever + tmpl, err := template.New("service").Parse(s) + if err != nil { + log.Fatal(err) + return + } + + err = tmpl.Execute(rw, conf) + if nil != err { + log.Fatal(err) + return + } + + if err := ioutil.WriteFile(serviceFile, rw.Bytes(), 0644); err != nil { + log.Fatalf("ioutil.WriteFile error: %v", err) + } + fmt.Printf("Wrote %q\n", serviceFile) +} diff --git a/cmd/install/install_windows.go b/cmd/install/install_windows.go new file mode 100644 index 0000000..c461b81 --- /dev/null +++ b/cmd/install/install_windows.go @@ -0,0 +1,17 @@ +package main + +import ( + "log" + //"golang.org/x/sys/windows" +) + +// See +// https://github.com/golang/go/issues/28804 +// https://stackoverflow.com/questions/31558066/how-to-ask-for-administer-privileges-on-windows-with-go/31561120 +// https://stackoverflow.com/questions/27366298/check-if-application-is-running-as-administrator-in-golang +// https://www.reddit.com/r/golang/comments/53dthc/way_to_detect_if_the_programs_running_with/ +// https://play.golang.org/p/bBtRZrk4_p +func install() { + //token := windows.Token(0) + log.Fatal("not yet implemented") +}