Browse Source

systemctl command

pull/773/head
hunterlong 2 years ago
parent
commit
f8e60bead5
  1. 19
      CloudronManifest.json
  2. 61
      cmd/cli.go
  3. 18
      cmd/commands.go

19
CloudronManifest.json

@ -0,0 +1,19 @@
{
"id": "com.statping",
"title": "Statping",
"author": "Hunter Long <[email protected]>",
"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": "[email protected]",
"icon": "https://assets.statping.com/icon.png",
"tags": [ "monitoring", "uptime" ],
"mediaLinks": [ "https://assets.statping.com/cloudron.png" ]
}s

61
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
}

18
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 <working_path> <port>'")
}
}
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 <working_path>' or 'uninstall' as arguments")
}
return nil
},

Loading…
Cancel
Save