diff --git a/cmd/clusterctl/cmd/config.go b/cmd/clusterctl/cmd/config.go index 5d3c874..af6d9a4 100644 --- a/cmd/clusterctl/cmd/config.go +++ b/cmd/clusterctl/cmd/config.go @@ -1,24 +1,12 @@ -/* -Copyright © 2024 Matthew Stobbs +// +// 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 ( diff --git a/cmd/clusterctl/cmd/daemon.go b/cmd/clusterctl/cmd/daemon.go index 49bf5ac..f065e82 100644 --- a/cmd/clusterctl/cmd/daemon.go +++ b/cmd/clusterctl/cmd/daemon.go @@ -1,29 +1,16 @@ -/* -Copyright © 2024 Matthew Stobbs +// +// 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 ( - "fmt" - + "git.staur.ca/stobbsm/clustvirt/lib/log" "github.com/spf13/cobra" ) @@ -35,7 +22,8 @@ var daemonCmd = &cobra.Command{ The clustvirt daemon is responsible for connecting to libvirtd hosts, and provides the REST API and WebUI to manage the cluster.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("daemon called") + log.Info("clustctl.daemon"). + Msg("must follow with a daemon command") }, } @@ -47,7 +35,7 @@ func init() { // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: // daemonCmd.PersistentFlags().String("foo", "", "A help for foo") - + daemonCmd.PersistentFlags().String("config", "", "configuration file to use") // Cobra supports local flags which will only run when this command // is called directly, e.g.: // daemonCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") diff --git a/cmd/clusterctl/cmd/reload.go b/cmd/clusterctl/cmd/daemon_reload.go similarity index 64% rename from cmd/clusterctl/cmd/reload.go rename to cmd/clusterctl/cmd/daemon_reload.go index 8f48303..983b7d5 100644 --- a/cmd/clusterctl/cmd/reload.go +++ b/cmd/clusterctl/cmd/daemon_reload.go @@ -1,6 +1,12 @@ -/* -Copyright © 2024 NAME HERE -*/ +// Copyright Matthew Stobbs +// +// 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 ( diff --git a/cmd/clusterctl/cmd/restart.go b/cmd/clusterctl/cmd/daemon_restart.go similarity index 65% rename from cmd/clusterctl/cmd/restart.go rename to cmd/clusterctl/cmd/daemon_restart.go index e015733..0a12ba5 100644 --- a/cmd/clusterctl/cmd/restart.go +++ b/cmd/clusterctl/cmd/daemon_restart.go @@ -1,6 +1,12 @@ -/* -Copyright © 2024 NAME HERE -*/ +// Copyright Matthew Stobbs +// +// 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 ( diff --git a/cmd/clusterctl/cmd/start.go b/cmd/clusterctl/cmd/daemon_start.go similarity index 55% rename from cmd/clusterctl/cmd/start.go rename to cmd/clusterctl/cmd/daemon_start.go index c3c0cc5..721d067 100644 --- a/cmd/clusterctl/cmd/start.go +++ b/cmd/clusterctl/cmd/daemon_start.go @@ -1,11 +1,17 @@ -/* -Copyright © 2024 NAME HERE -*/ +// Copyright Matthew Stobbs +// +// 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 ( - "fmt" - + "git.staur.ca/stobbsm/clustvirt/daemon" + "git.staur.ca/stobbsm/clustvirt/lib/log" "github.com/spf13/cobra" ) @@ -16,7 +22,10 @@ var startCmd = &cobra.Command{ Long: `Starts the clustvirt daemon loading configuration from files, ENV variables, and command line flags in that order.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("start called") + log.Info("clustctl.daemon"). + Str("command", "start"). + Msg("daemon start called") + daemon.Start() }, } diff --git a/cmd/clusterctl/cmd/stop.go b/cmd/clusterctl/cmd/daemon_stop.go similarity index 66% rename from cmd/clusterctl/cmd/stop.go rename to cmd/clusterctl/cmd/daemon_stop.go index 21a92ff..85aa0d7 100644 --- a/cmd/clusterctl/cmd/stop.go +++ b/cmd/clusterctl/cmd/daemon_stop.go @@ -1,6 +1,12 @@ -/* -Copyright © 2024 NAME HERE -*/ +// Copyright Matthew Stobbs +// +// 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 ( diff --git a/cmd/clusterctl/cmd/log.go b/cmd/clusterctl/cmd/log.go new file mode 100644 index 0000000..8558814 --- /dev/null +++ b/cmd/clusterctl/cmd/log.go @@ -0,0 +1,40 @@ +// Copyright Matthew Stobbs +// +// 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 ( + "fmt" + + "github.com/spf13/cobra" +) + +// logCmd represents the log command +var logCmd = &cobra.Command{ + Use: "log", + Short: "Get the log of clustvirt", + Long: `Fetch the log for clustvirt`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("log called") + }, +} + +func init() { + rootCmd.AddCommand(logCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // logCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // logCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/clusterctl/cmd/root.go b/cmd/clusterctl/cmd/root.go index 497453d..83a3da3 100644 --- a/cmd/clusterctl/cmd/root.go +++ b/cmd/clusterctl/cmd/root.go @@ -18,13 +18,10 @@ import ( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "clusterctl", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains -examples and usage of using your application. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Short: "Control clustvirt components", + Long: `Provides the commands needed to control the clustvirt daemon, make configuration changes, + check the status of the daemon, and get runtime metrics. + Providing no arguments defaults to status`, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, diff --git a/cmd/clusterctl/cmd/status.go b/cmd/clusterctl/cmd/status.go new file mode 100644 index 0000000..59d6fb3 --- /dev/null +++ b/cmd/clusterctl/cmd/status.go @@ -0,0 +1,43 @@ +// Copyright Matthew Stobbs +// +// 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 ( + "fmt" + + "github.com/spf13/cobra" +) + +// statusCmd represents the status command +var statusCmd = &cobra.Command{ + Use: "status", + Short: "Get the current status of clustvirt", + Long: `Get the current status of clustvirt, including how many hosts are connected, + the number of users connected, the status of the daemon process, + how many guests (VMs, LXC containers, etc.) are running, + and any system issues that have been recorded.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("status called") + }, +} + +func init() { + rootCmd.AddCommand(statusCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // statusCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // statusCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/daemon/main.go b/daemon/main.go new file mode 100644 index 0000000..64ffaff --- /dev/null +++ b/daemon/main.go @@ -0,0 +1,32 @@ +package daemon + +import ( + "fmt" + "os" + + "git.staur.ca/stobbsm/clustvirt/lib/log" +) + +// path to store the PID, configurable +var RunPath string +var PIDFile string + +func init() { + // set the RunPath to store the PID + RunPath = `/run/` + PIDFile = RunPath + `clustvirt.pid` +} + +func Start() { + pid := pid() + if err := os.WriteFile(PIDFile, []byte(fmt.Sprint(pid)), 0666); err != nil { + log.Error("daemon.Start"). + Err(err). + Send() + os.Exit(1) + } +} + +func pid() int { + return os.Getpid() +}