Skip to content

Commit

Permalink
feat(pkg): add stop command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Jun 16, 2023
1 parent 5e844d4 commit e0f1c4b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
37 changes: 37 additions & 0 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,50 @@ limitations under the License.
package cmd

import (
"context"
"time"

"github.com/loft-sh/devpod/pkg/client"
"github.com/loft-sh/devpod/pkg/log"
"github.com/mrsimonemms/devpod-provider-hetzner/pkg/hetzner"
"github.com/mrsimonemms/devpod-provider-hetzner/pkg/options"
"github.com/spf13/cobra"
)

// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop an instance",
RunE: func(_ *cobra.Command, args []string) error {
options, err := options.FromEnv(false)
if err != nil {
return err
}

ctx := context.Background()

hetznerClient := hetzner.NewHetzner(options.Token)

err = hetznerClient.Stop(ctx, options.MachineID)
if err != nil {
return err
}

// Wait until it's stopped
for {
status, err := hetznerClient.Status(ctx, options.MachineID)
if err != nil {
log.Default.Errorf("Error retrieving server status: %v", err)
break
} else if status == client.StatusStopped {
break
}

time.Sleep(time.Second)
}

return nil
},
}

func init() {
Expand Down
3 changes: 3 additions & 0 deletions pkg/hetzner/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
)

var (
ErrMultipleServersFound = func(name string) error {
return fmt.Errorf("multiple server with name %s found", name)
}
ErrMultipleVolumesFound = func(name string) error {
return fmt.Errorf("multiple volumes with name %s found", name)
}
Expand Down
29 changes: 26 additions & 3 deletions pkg/hetzner/hetzner.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,21 @@ func (h *Hetzner) Delete(ctx context.Context, name string) error {
return nil
}

func (h *Hetzner) GetByName(ctx context.Context, name string) (interface{}, error) {
return nil, nil
func (h *Hetzner) GetByName(ctx context.Context, name string) (*hcloud.Server, error) {
servers, _, err := h.client.Server.List(ctx, hcloud.ServerListOpts{Name: name})
if err != nil {
return nil, err
}

serverLength := len(servers)
if serverLength > 1 {
return nil, ErrMultipleServersFound(name)
}
if serverLength == 0 {
return nil, nil
}

return servers[0], nil
}

func (h *Hetzner) Init(ctx context.Context) error {
Expand Down Expand Up @@ -164,7 +177,17 @@ func (h *Hetzner) Status(ctx context.Context, name string) (client.Status, error
}

func (h *Hetzner) Stop(ctx context.Context, name string) error {
return nil
server, err := h.GetByName(ctx, name)
if err != nil {
return err
}
if server == nil {
return nil
}

_, _, err = h.client.Server.DeleteWithResult(ctx, server)

return err
}

func (h *Hetzner) volumeByName(ctx context.Context, name string) (*hcloud.Volume, error) {
Expand Down

0 comments on commit e0f1c4b

Please sign in to comment.