Skip to content

Commit

Permalink
feat(pkg): add delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Jun 16, 2023
1 parent 716b134 commit 357849b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
36 changes: 36 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,49 @@ 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"
)

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete 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.Delete(ctx, options.MachineID)
if err != nil {
return err
}

// Wait until delete
for {
status, err := hetznerClient.Status(context.Background(), options.MachineID)
if err != nil {
log.Default.Errorf("Error retrieving server status: %v", err)
break
} else if status == client.StatusNotFound {
break
}

time.Sleep(time.Second)
}

return nil
},
}

func init() {
Expand Down
45 changes: 44 additions & 1 deletion pkg/hetzner/hetzner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"encoding/base64"
"strconv"
"text/template"
"time"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/loft-sh/devpod/pkg/client"
"github.com/loft-sh/devpod/pkg/ssh"
"github.com/mrsimonemms/devpod-provider-hetzner/pkg/options"
"github.com/pkg/errors"
)

//go:embed cloud-config.yaml
Expand Down Expand Up @@ -121,7 +123,48 @@ func (h *Hetzner) Create(ctx context.Context, req *hcloud.ServerCreateOpts, disk
}

func (h *Hetzner) Delete(ctx context.Context, name string) error {
return nil
// Delete volume
volume, err := h.volumeByName(ctx, name)
if err != nil {
return err
} else if volume != nil {
// Detatch volume
_, _, err := h.client.Volume.Detach(ctx, volume)
if err != nil {
return errors.Wrap(err, "detach volume")
}
}

// Wait until the volume is detached
for {
time.Sleep(time.Second)

// re-get volume
volume, err = h.volumeByName(ctx, name)
if err != nil {
return err
} else if volume.Server == nil {
break
}
}

// delete volume
if volume != nil {
_, err = h.client.Volume.Delete(ctx, volume)
if err != nil {
return errors.Wrap(err, "delete volume")
}
}

server, err := h.GetByName(ctx, name)
if err != nil {
return err
} else if server == nil {
return nil
}

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

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

0 comments on commit 357849b

Please sign in to comment.