forked from coop-cloud/abra
feat: implement hetzner new command
This commit is contained in:
parent
fa16ce20eb
commit
3e91174ce0
5
.envrc.sample
Normal file
5
.envrc.sample
Normal 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)
|
2
TODO.md
2
TODO.md
@ -10,7 +10,7 @@
|
||||
- [x] `add`
|
||||
- [ ] `new`
|
||||
- [ ] `capsul`
|
||||
- [ ] `hetzner` (XXX: in progress (decentral1se))
|
||||
- [x] `hetzner`
|
||||
- [x] `rm`
|
||||
- [x] `init`
|
||||
- [ ] `abra app`
|
||||
|
@ -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
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user