adding functionality to commands

This commit is contained in:
Matthew Stobbs 2024-03-29 20:05:51 -06:00
parent ece374d1a9
commit ebbb4ebb49
10 changed files with 183 additions and 68 deletions

View File

@ -1,24 +1,12 @@
/*
Copyright © 2024 Matthew Stobbs <matthew@stobbs.ca
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// 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 (

View File

@ -1,29 +1,16 @@
/*
Copyright © 2024 Matthew Stobbs <matthew@stobbs.ca
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// 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 (
"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")

View File

@ -1,6 +1,12 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
// 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 (

View File

@ -1,6 +1,12 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
// 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 (

View File

@ -1,11 +1,17 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
// 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 (
"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()
},
}

View File

@ -1,6 +1,12 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
// 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 (

40
cmd/clusterctl/cmd/log.go Normal file
View File

@ -0,0 +1,40 @@
// 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 (
"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")
}

View File

@ -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) { },

View File

@ -0,0 +1,43 @@
// 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 (
"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")
}

32
daemon/main.go Normal file
View File

@ -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()
}