Add the system command with reboot subcommand

This commit is contained in:
2022-05-13 23:06:51 +02:00
parent 7197fce351
commit c9ba91c585
7 changed files with 356 additions and 59 deletions

View File

@@ -36,7 +36,7 @@ func Execute() {
cobra.OnInitialize(initConfig)
rootCmd.SetUsageTemplate(rootUsage)
rootCmd.AddCommand(runCmd, serviceCmd, fileCmd)
rootCmd.AddCommand(runCmd, serviceCmd, fileCmd, systemCmd)
home, err := homedir.Dir()
if err != nil {

View File

@@ -0,0 +1,90 @@
package cli
import (
"context"
"crypto/tls"
"fmt"
"time"
"github.com/alitto/pond"
"github.com/apex/log"
"github.com/dpogorzelski/speedrun/pkg/speedrun/cloud"
portalpb "github.com/dpogorzelski/speedrun/proto/portal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"storj.io/drpc/drpcconn"
)
var systemCmd = &cobra.Command{
Use: "system",
Short: "Manage the system",
TraverseChildren: true,
}
var rebootCmd = &cobra.Command{
Use: "reboot",
Short: "Reboot the system",
Example: " speedrun system reboot",
Args: cobra.NoArgs,
RunE: reboot,
}
func init() {
systemCmd.SetUsageTemplate(usage)
systemCmd.AddCommand(rebootCmd)
}
func reboot(cmd *cobra.Command, _ []string) error {
usePrivateIP := viper.GetBool("portal.use-private-ip")
tlsConfig, err := cloud.SetupTLS()
if err != nil {
return err
}
target, err := cmd.Flags().GetString("target")
if err != nil {
return err
}
portals, err := cloud.GetInstances(target)
if err != nil {
return err
}
pool := pond.New(1000, 10000)
for _, p := range portals {
portal := p
pool.Submit(func() {
fields := log.Fields{
"host": portal.Name,
"address": portal.GetAddress(usePrivateIP),
}
log := log.WithFields(fields)
addr := fmt.Sprintf("%s:%d", portal.GetAddress(usePrivateIP), 1337)
rawconn, err := tls.Dial("tcp", addr, tlsConfig)
if err != nil {
log.Error(err.Error())
return
}
conn := drpcconn.New(rawconn)
defer conn.Close()
c := portalpb.NewDRPCPortalClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
r, err := c.SystemReboot(ctx, &portalpb.SystemRebootRequest{})
if err != nil {
log.Error(err.Error())
return
}
log.WithField("state", r.GetState()).Infof(r.GetMessage())
})
}
pool.StopAndWait()
return nil
}

View File

@@ -5,7 +5,7 @@ Usage:{{if .Runnable}}
Core Commands:{{range .Commands}}{{if (or (eq .Name "help") (eq .Name "completion"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
Action Commands:{{range .Commands}}{{if (or (eq .Name "run") (eq .Name "exec") (eq .Name "service") (eq .Name "file"))}}
Action Commands:{{range .Commands}}{{if (or (eq .Name "run") (eq .Name "exec") (eq .Name "service") (eq .Name "file") (eq .Name "system") )}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
{{if .HasAvailableLocalFlags}}
Flags: