Skip to content

Commit

Permalink
adding stop/start for onprem && reboot (#1548)
Browse files Browse the repository at this point in the history
* adding stop/start for onprem && reboot

* .

* .

* .

* .

---------

Co-authored-by: Ian Eyberg <[email protected]>
  • Loading branch information
eyberg and Ian Eyberg authored Nov 21, 2023
1 parent da621e6 commit 046e73a
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 35 deletions.
30 changes: 29 additions & 1 deletion cmd/cmd_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func InstanceCommands() *cobra.Command {
var cmdInstance = &cobra.Command{
Use: "instance",
Short: "manage nanos instances",
ValidArgs: []string{"create", "list", "delete", "stop", "start", "logs"},
ValidArgs: []string{"create", "list", "delete", "stop", "start", "reboot", "logs"},
Args: cobra.OnlyValidArgs,
}

Expand All @@ -32,6 +32,7 @@ func InstanceCommands() *cobra.Command {
cmdInstance.AddCommand(instanceDeleteCommand())
cmdInstance.AddCommand(instanceStopCommand())
cmdInstance.AddCommand(instanceStartCommand())
cmdInstance.AddCommand(instanceRebootCommand())
cmdInstance.AddCommand(instanceLogsCommand())

return cmdInstance
Expand Down Expand Up @@ -200,6 +201,16 @@ func instanceStartCommand() *cobra.Command {
return cmdInstanceStart
}

func instanceRebootCommand() *cobra.Command {
var cmdInstanceReboot = &cobra.Command{
Use: "reboot <instance_name>",
Short: "reboot instance on provider",
Run: instanceRebootCommandHandler,
Args: cobra.MinimumNArgs(1),
}
return cmdInstanceReboot
}

func instanceStartCommandHandler(cmd *cobra.Command, args []string) {
c, err := getInstanceCommandDefaultConfig(cmd)
if err != nil {
Expand All @@ -217,6 +228,23 @@ func instanceStartCommandHandler(cmd *cobra.Command, args []string) {
}
}

func instanceRebootCommandHandler(cmd *cobra.Command, args []string) {
c, err := getInstanceCommandDefaultConfig(cmd)
if err != nil {
exitWithError(err.Error())
}

p, ctx, err := getProviderAndContext(c, c.CloudConfig.Platform)
if err != nil {
exitForCmd(cmd, err.Error())
}

err = p.RebootInstance(ctx, args[0])
if err != nil {
exitWithError(err.Error())
}
}

func instanceStopCommand() *cobra.Command {
var cmdInstanceStop = &cobra.Command{
Use: "stop <instance_name>",
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ require (
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31
github.com/vmware/govmomi v0.22.2
github.com/vultr/govultr/v3 v3.2.0
golang.org/x/crypto v0.14.0
golang.org/x/crypto v0.15.0
golang.org/x/oauth2 v0.3.0
golang.org/x/sys v0.13.0
golang.org/x/sys v0.14.0
google.golang.org/api v0.103.0
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37
google.golang.org/grpc v1.51.0
Expand Down Expand Up @@ -105,9 +105,9 @@ require (
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -591,8 +591,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211209124913-491a49abca63/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -676,14 +676,14 @@ golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -693,8 +693,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
1 change: 1 addition & 0 deletions lepton/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Provider interface {
DeleteInstance(ctx *Context, instancename string) error
StopInstance(ctx *Context, instancename string) error
StartInstance(ctx *Context, instancename string) error
RebootInstance(ctx *Context, instancename string) error
GetInstanceLogs(ctx *Context, instancename string) (string, error)
PrintInstanceLogs(ctx *Context, instancename string, watch bool) error

Expand Down
5 changes: 5 additions & 0 deletions provider/aws/aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func getAWSInstances(region string, filter []*ec2.Filter) []lepton.CloudInstance
return cinstances
}

// RebootInstance reboots the instance.
func (p *AWS) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance stops instance from AWS by ami name
func (p *AWS) StartInstance(ctx *lepton.Context, instanceName string) error {

Expand Down
6 changes: 6 additions & 0 deletions provider/azure/azure_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (a *Azure) GetVM(ctx context.Context, vmName string) (vm compute.VirtualMac
return
}

// RebootInstance reboots the instance.
// prob use the below?
func (a *Azure) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// RestartVM restarts the selected VM
func (a *Azure) RestartVM(ctx context.Context, vmName string) (osr autorest.Response, err error) {
vmClient := a.getVMClient()
Expand Down
5 changes: 5 additions & 0 deletions provider/digitalocean/digital_ocean_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ func (do *DigitalOcean) DeleteInstance(ctx *lepton.Context, instancename string)
return nil
}

// RebootInstance reboots the instance.
func (do *DigitalOcean) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance starts an instance in DO
func (do *DigitalOcean) StartInstance(ctx *lepton.Context, instancename string) error {
instance, err := do.GetInstanceByName(ctx, instancename)
Expand Down
5 changes: 5 additions & 0 deletions provider/disabled/provider_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func (p *Provider) StartInstance(ctx *lepton.Context, instancename string) error
return nil
}

// RebootInstance ...
func (p *Provider) RebootInstance(ctx *lepton.Context, instancename string) error {
return nil
}

// GetInstanceLogs ...
func (p *Provider) GetInstanceLogs(ctx *lepton.Context, instancename string) (string, error) {
return "", nil
Expand Down
5 changes: 5 additions & 0 deletions provider/gcp/gcp_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ func (p *GCloud) DeleteInstance(ctx *lepton.Context, instancename string) error
return nil
}

// RebootInstance reboots the instance.
func (p *GCloud) RebootInstance(ctx *lepton.Context, instancename string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance starts an instance in GCloud
func (p *GCloud) StartInstance(ctx *lepton.Context, instancename string) error {

Expand Down
5 changes: 5 additions & 0 deletions provider/hyperv/hyperv_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ func (p *Provider) StopInstance(ctx *lepton.Context, instancename string) error
return StopVirtualMachine(instancename)
}

// RebootInstance reboots the instance.
func (p *Provider) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance initiates virtual machine in hyper-v
func (p *Provider) StartInstance(ctx *lepton.Context, instancename string) error {
return StartVirtualMachine(instancename)
Expand Down
5 changes: 5 additions & 0 deletions provider/ibm/ibm_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ func (v *IBM) DeleteInstance(ctx *lepton.Context, instanceID string) error {
return nil
}

// RebootInstance reboots the instance.
func (v *IBM) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance starts an instance in IBM
func (v *IBM) StartInstance(ctx *lepton.Context, instanceID string) error {
return nil
Expand Down
5 changes: 5 additions & 0 deletions provider/linode/linode_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ func (v *Linode) DeleteInstance(ctx *lepton.Context, instanceID string) error {
return nil
}

// RebootInstance reboots the instance.
func (v *Linode) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance starts an instance in linode
func (v *Linode) StartInstance(ctx *lepton.Context, instanceID string) error {
client := &http.Client{}
Expand Down
6 changes: 6 additions & 0 deletions provider/oci/oci_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package oci
import (
"context"
"errors"
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -248,6 +249,11 @@ func (p *Provider) StopInstance(ctx *lepton.Context, instancename string) error
return nil
}

// RebootInstance reboots the instance.
func (p *Provider) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance starts an instance
func (p *Provider) StartInstance(ctx *lepton.Context, instancename string) error {
instance, err := p.GetInstanceByName(ctx, instancename)
Expand Down
73 changes: 71 additions & 2 deletions provider/onprem/onprem_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"math/rand"
"net"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -526,13 +527,81 @@ func (p *OnPrem) ListInstances(ctx *lepton.Context) error {
}

// StartInstance from on premise
// right now this assumes it was paused; not a boot; there's another
// call we can use here to get the status first
func (p *OnPrem) StartInstance(ctx *lepton.Context, instancename string) error {
return fmt.Errorf("operation not supported")
instance, err := p.GetMetaInstanceByName(ctx, instancename)
if err != nil {
return err
}

last := instance.Mgmt

commands := []string{
`{ "execute": "qmp_capabilities" }`,
`{ "execute": "cont" }`,
}

executeQMP(commands, last)
return nil
}

func executeQMP(commands []string, last string) {
c, err := net.Dial("tcp", "localhost:"+last)
if err != nil {
fmt.Println(err)
}
defer c.Close()

for i := 0; i < len(commands); i++ {
_, err := c.Write([]byte(commands[i] + "\n"))
if err != nil {
fmt.Println(err)
}
received := make([]byte, 1024)
_, err = c.Read(received)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}

// RebootInstance from on premise
func (p *OnPrem) RebootInstance(ctx *lepton.Context, instancename string) error {
instance, err := p.GetMetaInstanceByName(ctx, instancename)
if err != nil {
return err
}

last := instance.Mgmt

commands := []string{
`{ "execute": "qmp_capabilities" }`,
`{ "execute": "system_reset" }`,
}

executeQMP(commands, last)

return nil
}

// StopInstance from on premise
func (p *OnPrem) StopInstance(ctx *lepton.Context, instancename string) error {
return fmt.Errorf("operation not supported")
instance, err := p.GetMetaInstanceByName(ctx, instancename)
if err != nil {
return err
}

last := instance.Mgmt

commands := []string{
`{ "execute": "qmp_capabilities" }`,
`{ "execute": "stop" }`,
}

executeQMP(commands, last)
return nil
}

// DeleteInstance from on premise
Expand Down
18 changes: 1 addition & 17 deletions provider/onprem/onprem_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,8 @@ func (op *OnPrem) AttachVolume(ctx *lepton.Context, instanceName string, volumeN
deviceAddCmd,
}

c, err := net.Dial("tcp", "localhost:"+last)
if err != nil {
fmt.Println(err)
}
defer c.Close()
executeQMP(commands, last)

for i := 0; i < len(commands); i++ {
_, err := c.Write([]byte(commands[i] + "\n"))
if err != nil {
fmt.Println(err)
}
received := make([]byte, 1024)
_, err = c.Read(received)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions provider/openshift/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (oc *OpenShift) StopInstance(ctx *lepton.Context, instancename string) erro
return nil
}

// RebootInstance reboots a nanos instance.
func (oc *OpenShift) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}

// StartInstance starts a nanos instance
func (oc *OpenShift) StartInstance(ctx *lepton.Context, instancename string) error {
return nil
Expand Down
Loading

0 comments on commit 046e73a

Please sign in to comment.