1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
package server
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)
var EnableRescueCommand = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "enable-rescue [FLAGS] SERVER",
Short: "Enable rescue for a server",
Args: cobra.ExactArgs(1),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Server().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
cmd.Flags().String("type", "linux64", "Rescue type")
cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("linux64", "linux32"))
cmd.Flags().StringSlice("ssh-key", nil, "ID or name of SSH key to inject (can be specified multiple times)")
cmd.RegisterFlagCompletionFunc("ssh-key", cmpl.SuggestCandidatesF(client.SSHKey().Names))
return cmd
},
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error {
idOrName := args[0]
server, _, err := client.Server().Get(ctx, idOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", idOrName)
}
var (
opts hcloud.ServerEnableRescueOpts
)
rescueType, _ := cmd.Flags().GetString("type")
opts.Type = hcloud.ServerRescueType(rescueType)
sshKeys, _ := cmd.Flags().GetStringSlice("ssh-key")
for _, sshKeyIDOrName := range sshKeys {
sshKey, _, err := client.SSHKey().Get(ctx, sshKeyIDOrName)
if err != nil {
return err
}
if sshKey == nil {
return fmt.Errorf("SSH key not found: %s", sshKeyIDOrName)
}
opts.SSHKeys = append(opts.SSHKeys, sshKey)
}
result, _, err := client.Server().EnableRescue(ctx, server, opts)
if err != nil {
return err
}
if err := waiter.ActionProgress(ctx, result.Action); err != nil {
return err
}
fmt.Printf("Rescue enabled for server %d with root password: %s\n", server.ID, result.RootPassword)
return nil
},
}
|