Skip to content

Commit

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

import (
"context"
"fmt"
"os"

"github.com/mrsimonemms/devpod-provider-hetzner/pkg/hetzner"
"github.com/mrsimonemms/devpod-provider-hetzner/pkg/options"
"github.com/spf13/cobra"
)

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

status, err := hetzner.NewHetzner(options.Token).Status(context.Background(), options.MachineID)
if err != nil {
return err
}

_, err = fmt.Fprint(os.Stdout, status)
return err
},
}

func init() {
Expand Down
25 changes: 24 additions & 1 deletion pkg/hetzner/hetzner.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,30 @@ func (h *Hetzner) Init(ctx context.Context) error {
}

func (h *Hetzner) Status(ctx context.Context, name string) (client.Status, error) {
return client.Status("@todo"), nil
server, _, err := h.client.Server.GetByName(ctx, name)
if err != nil {
return client.StatusNotFound, err
}
if server == nil {
// No server - check the volume
volume, err := h.volumeByName(ctx, name)
if err != nil {
return client.StatusNotFound, err
} else if volume != nil {
return client.StatusStopped, nil
}

return client.StatusNotFound, nil
}

// Is it busy?
if server.Status != hcloud.ServerStatusRunning {
return client.StatusBusy, nil
}

// @todo(sje): do we need to check if the cloud-init script is finished? "ssh user@path cloud-init status"

return client.StatusRunning, nil
}

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

0 comments on commit 5e844d4

Please sign in to comment.