From f8e60bead5facd43241347e786ddb00fb1f2a632 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Sun, 26 Jul 2020 00:04:18 -0700 Subject: [PATCH] systemctl command --- CloudronManifest.json | 19 ++++++++++++++ cmd/cli.go | 61 +++++++++++++++++++++++++++++++++++++++++-- cmd/commands.go | 18 ++++++++++--- 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 CloudronManifest.json diff --git a/CloudronManifest.json b/CloudronManifest.json new file mode 100644 index 00000000..7ae27752 --- /dev/null +++ b/CloudronManifest.json @@ -0,0 +1,19 @@ +{ + "id": "com.statping", + "title": "Statping", + "author": "Hunter Long ", + "description": "Monitor your web services and remote servers", + "tagline": "Server Monitoring", + "version": "0.90.61", + "healthCheckPath": "/health", + "httpPort": 8080, + "addons": { + "localstorage": {} + }, + "manifestVersion": 2, + "website": "https://github.com/statping/statping", + "contactEmail": "info@statping.com", + "icon": "https://assets.statping.com/icon.png", + "tags": [ "monitoring", "uptime" ], + "mediaLinks": [ "https://assets.statping.com/cloudron.png" ] +}s diff --git a/cmd/cli.go b/cmd/cli.go index 389b0ca4..e3c2d20a 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -34,8 +34,65 @@ func assetsCli() error { return nil } -func systemctlCli() error { - fmt.Println("still in the works...") +func systemctlCli(dir string, uninstall bool, port int64) error { + location := "/etc/systemd/system/statping.service" + + if uninstall { + if _, _, err := utils.Command("systemctl", "stop", "statping"); err != nil { + log.Errorln(err) + } + if _, _, err := utils.Command("systemctl", "disable", "statping"); err != nil { + log.Errorln(err) + } + if err := utils.DeleteFile(location); err != nil { + log.Errorln(err) + } + return nil + } + if ok := utils.FolderExists(dir); !ok { + return errors.New("directory does not exist: " + dir) + } + + binPath, err := os.Executable() + if err != nil { + return err + } + + config := []byte(`[Unit] +Description=Statping Server +After=network.target +After=systemd-user-sessions.service +After=network-online.target + +[Service] +Type=simple +Restart=always +Environment=STATPING_DIR=` + dir + ` +Environment=ALLOW_REPORTS=true +ExecStart=` + binPath + ` --port=` + utils.ToString(port) + ` +WorkingDirectory=` + dir + ` + +[Install] +WantedBy=multi-user.target" +`) + fmt.Println("Saving systemctl service to: ", location) + fmt.Printf("Using directory %s for Statping data\n", dir) + fmt.Printf("Running on port %d\n", port) + if err := utils.SaveFile(location, config); err != nil { + return err + } + if _, _, err := utils.Command("systemctl", "daemon-reload"); err != nil { + return err + } + if _, _, err := utils.Command("systemctl", "enable", "statping.service"); err != nil { + return err + } + if _, _, err := utils.Command("systemctl", "start", "statping"); err != nil { + return err + } + fmt.Println("Statping was will auto start on reboots") + fmt.Println("systemctl service: /etc/systemd/system/statping.service") + return nil } diff --git a/cmd/commands.go b/cmd/commands.go index b9609115..19d2b59c 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/statping/statping/utils" "io" "os" "os/exec" @@ -72,17 +73,26 @@ var versionCmd = &cobra.Command{ var systemctlCmd = &cobra.Command{ Use: "systemctl [install/uninstall]", - Short: "Install or Uninstall systemctl links", + Short: "Install or Uninstall systemctl services", RunE: func(cmd *cobra.Command, args []string) error { - if err := systemctlCli(); err != nil { + if args[1] == "install" { + if len(args) < 3 { + return errors.New("requires 'install '") + } + } + port := utils.ToInt(args[2]) + if port == 0 { + port = 80 + } + if err := systemctlCli(args[1], args[0] == "uninstall", port); err != nil { return err } os.Exit(0) return nil }, Args: func(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return errors.New("requires 'install' or 'uninstall' as arguments") + if len(args) < 2 { + return errors.New("requires 'install ' or 'uninstall' as arguments") } return nil },