From 3e91174ce0443fed8e97e0a00b99f51342d2589c Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 2 Aug 2021 14:05:39 +0200 Subject: [PATCH] feat: implement hetzner new command --- .envrc.sample | 5 +++++ TODO.md | 2 +- cli/server/new.go | 37 +++++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 .envrc.sample diff --git a/.envrc.sample b/.envrc.sample new file mode 100644 index 00000000..fb4427e4 --- /dev/null +++ b/.envrc.sample @@ -0,0 +1,5 @@ +# The path to our pass credentials store +export PASSWORD_STORE_DIR=$(pwd)/../../autonomic/passwords/passwords/ + +# The Hetzner Cloud API token for managing our instances +export HCLOUD_TOKEN=$(pass show logins/hetzner/cicd/api_key) diff --git a/TODO.md b/TODO.md index d4a3f0a2..ca2a92b5 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,7 @@ - [x] `add` - [ ] `new` - [ ] `capsul` - - [ ] `hetzner` (XXX: in progress (decentral1se)) + - [x] `hetzner` - [x] `rm` - [x] `init` - [ ] `abra app` diff --git a/cli/server/new.go b/cli/server/new.go index 8ba39b3e..c7de60e0 100644 --- a/cli/server/new.go +++ b/cli/server/new.go @@ -3,6 +3,7 @@ package server import ( "context" + "coopcloud.tech/abra/cli/formatter" "github.com/hetznercloud/hcloud-go/hcloud" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" @@ -24,7 +25,7 @@ This command uses the uses the Hetzner Cloud API bindings to send a server creation request. You must already have a Hetzner Cloud account and an account API token before using this command. -Your token can be loaded from the environment using the HCLOUD_API_TOKEN +Your token can be loaded from the environment using the HCLOUD_TOKEN environment variable or otherwise passing the "--env/-e" flag. `, Flags: []cli.Flag{ @@ -59,7 +60,7 @@ environment variable or otherwise passing the "--env/-e" flag. Name: "token", Aliases: []string{"T"}, Usage: "Hetzner Cloud API token", - EnvVars: []string{"HCLOUD_API_TOKEN"}, + EnvVars: []string{"HCLOUD_TOKEN"}, Destination: &hetznerCloudAPIToken, }, }, @@ -69,27 +70,43 @@ environment variable or otherwise passing the "--env/-e" flag. return cli.ShowSubcommandHelp(c) } + if hetznerCloudAPIToken == "" { + logrus.Fatal("API token is missing, cannot continue") + } + ctx := context.Background() client := hcloud.NewClient(hcloud.WithToken(hetznerCloudAPIToken)) - // var sshkeys []hcloud.SSHKey - // for _, sshkey := range HetznerCloudSSHKeys { - // sshkeys = append(sshkeys, hcloud.SSHKey{Name: sshkey}) - // } + var sshKeys []*hcloud.SSHKey + for _, sshKey := range c.StringSlice("ssh-keys") { + sshKey, _, err := client.SSHKey.GetByName(ctx, sshKey) + if err != nil { + logrus.Fatal(err) + } + sshKeys = append(sshKeys, sshKey) + } - // TODO: finish passing arguments serverOpts := hcloud.ServerCreateOpts{ Name: name, ServerType: &hcloud.ServerType{Name: hetznerCloudType}, Image: &hcloud.Image{Name: hetznerCloudImage}, - // SSHKeys: HetznerCloudSSHKeys, - // Location: HetznerCloudLocation, + SSHKeys: sshKeys, + Location: &hcloud.Location{Name: hetznerCloudLocation}, } - _, _, err := client.Server.Create(ctx, serverOpts) + res, _, err := client.Server.Create(ctx, serverOpts) if err != nil { logrus.Fatal(err) } + tableColumns := []string{"Name", "IPv4", "Root Password"} + table := formatter.CreateTable(tableColumns) + if len(sshKeys) > 0 { + table.Append([]string{name, res.Server.PublicNet.IPv4.IP.String(), "N/A (using SSH keys)"}) + } else { + table.Append([]string{name, res.Server.PublicNet.IPv4.IP.String(), res.RootPassword}) + } + table.Render() + return nil }, }