feat: implement hetzner new command

This commit is contained in:
decentral1se 2021-08-02 14:05:39 +02:00
parent fa16ce20eb
commit 3e91174ce0
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 33 additions and 11 deletions

5
.envrc.sample Normal file
View File

@ -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)

View File

@ -10,7 +10,7 @@
- [x] `add`
- [ ] `new`
- [ ] `capsul`
- [ ] `hetzner` (XXX: in progress (decentral1se))
- [x] `hetzner`
- [x] `rm`
- [x] `init`
- [ ] `abra app`

View File

@ -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
},
}