87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
// Copyright Matthew Stobbs <matthew@stobbs.ca>
|
|
//
|
|
// Package cmd contains the main application file for ClustVirt.
|
|
// This command is used for everything, implementing the command pattern
|
|
// with the help of spf13/cobra.
|
|
// After building, the executable's first argument must be one of the valid
|
|
// commands, such as `daemon`, `api`, `query`, `host`, etc. This pattern allows
|
|
// for a simple way to access functionality, reducing the need to use the
|
|
// REST api directly.
|
|
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"syscall"
|
|
|
|
"git.staur.ca/stobbsm/clustvirt/lib/log"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// restartCmd represents the restart command
|
|
var restartCmd = &cobra.Command{
|
|
Use: "restart",
|
|
Short: "Restart the clustvirt daemon process",
|
|
Long: `Some configuration requires the daemon to restart itself.
|
|
This command sends the SIGUSR2 signal, telling the daemon to restart`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
log.Info("cmd.daemon.restart").
|
|
Msg("restarting daemon")
|
|
sPid, err := os.ReadFile(`/run/clustvirt.pid`)
|
|
if err != nil {
|
|
log.Error("cmd.daemon.restart").
|
|
Err(err).
|
|
Msg("unable to restart")
|
|
os.Exit(1)
|
|
}
|
|
pid, err := strconv.Atoi(string(sPid))
|
|
if err != nil {
|
|
log.Error("cmd.daemon.restart").
|
|
Err(err).
|
|
Msg("unable to restart")
|
|
os.Exit(1)
|
|
}
|
|
proc, err := os.FindProcess(pid)
|
|
if err != nil {
|
|
log.Error("cmd.daemon.restart").
|
|
Err(err).
|
|
Msg("unable to restart")
|
|
os.Exit(1)
|
|
}
|
|
// make sure the process found is currently alive
|
|
if err = proc.Signal(syscall.Signal(0)); err != nil {
|
|
log.Error("cmd.daemon.restart").
|
|
Err(err).
|
|
Int("PID", pid).
|
|
Msg("expected process doesn't seem to exist")
|
|
}
|
|
if err = proc.Signal(syscall.SIGUSR2); err != nil {
|
|
log.Error("cmd.daemon.restart").
|
|
Err(err).
|
|
Msg("unable to restart")
|
|
os.Exit(1)
|
|
}
|
|
ps, err := proc.Wait()
|
|
if err != nil {
|
|
log.Error("cmd.daemon.restart").
|
|
Err(err).
|
|
Msg("unable to restart")
|
|
os.Exit(1)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
daemonCmd.AddCommand(restartCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// restartCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// restartCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|