From d74b7636a122ff94407f9f705c16d572f60f5b86 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 2 Aug 2021 01:54:03 +0200 Subject: [PATCH] WIP make a start on the hetzner command --- TODO.md | 2 +- cli/server.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++- go.mod | 1 + go.sum | 2 ++ 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 11d61eaa..d4a3f0a2 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,7 @@ - [x] `add` - [ ] `new` - [ ] `capsul` - - [ ] `hetzner` + - [ ] `hetzner` (XXX: in progress (decentral1se)) - [x] `rm` - [x] `init` - [ ] `abra app` diff --git a/cli/server.go b/cli/server.go index 245c16c7..446bfcd8 100644 --- a/cli/server.go +++ b/cli/server.go @@ -10,6 +10,7 @@ import ( "coopcloud.tech/abra/config" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" + "github.com/hetznercloud/hcloud-go/hcloud" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -77,12 +78,101 @@ var serverAddCommand = &cli.Command{ }, } +var HetznerCloudType string +var HetznerCloudImage string +var HetznerCloudSSHKeys cli.StringSlice +var HetznerCloudLocation string +var HetznerCloudAPIToken string +var serverNewHetznerCloudCommand = &cli.Command{ + Name: "hetzner", + Usage: "Create a new Hetzner virtual server", + ArgsUsage: "", + Description: ` +Create a new Hetzner virtual server. + +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 +environment variable or otherwise passing the "--env/-e" flag. +`, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "type", + Aliases: []string{"t"}, + Usage: "Server type", + Destination: &HetznerCloudType, + Value: "cx11", + }, + &cli.StringFlag{ + Name: "image", + Aliases: []string{"i"}, + Usage: "Image type", + Value: "debian-10", + Destination: &HetznerCloudImage, + }, + &cli.StringSliceFlag{ + Name: "ssh-keys", + Aliases: []string{"s"}, + Usage: "SSH keys", + Destination: &HetznerCloudSSHKeys, + }, + &cli.StringFlag{ + Name: "location", + Aliases: []string{"l"}, + Usage: "Server location", + Value: "hel1", + Destination: &HetznerCloudLocation, + }, + &cli.StringFlag{ + Name: "token", + Aliases: []string{"T"}, + Usage: "Hetzner Cloud API token", + EnvVars: []string{"HCLOUD_API_TOKEN"}, + Destination: &HetznerCloudAPIToken, + }, + }, + Action: func(c *cli.Context) error { + name := c.Args().First() + if name == "" { + cli.ShowSubcommandHelp(c) + return nil + } + + ctx := context.Background() + client := hcloud.NewClient(hcloud.WithToken(HetznerCloudAPIToken)) + + // var sshkeys []hcloud.SSHKey + // for _, sshkey := range HetznerCloudSSHKeys { + // sshkeys = append(sshkeys, hcloud.SSHKey{Name: sshkey}) + // } + + // TODO: finish passing arguments + serverOpts := hcloud.ServerCreateOpts{ + Name: name, + ServerType: &hcloud.ServerType{Name: HetznerCloudType}, + Image: &hcloud.Image{Name: HetznerCloudImage}, + // SSHKeys: HetznerCloudSSHKeys, + // Location: HetznerCloudLocation, + } + _, _, err := client.Server.Create(ctx, serverOpts) + if err != nil { + logrus.Fatal(err) + } + + return nil + }, +} + var serverNewCommand = &cli.Command{ Name: "new", Usage: "Create a new server using a 3rd party provider", Description: "Use a provider plugin to create a new server which can then be used to house a new Co-op Cloud installation.", ArgsUsage: "", - HideHelp: true, + Subcommands: []*cli.Command{ + serverNewHetznerCloudCommand, + }, } var serverRemoveCommand = &cli.Command{ diff --git a/go.mod b/go.mod index af35abcd..35488de8 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/docker/go-units v0.4.0 github.com/fvbommel/sortorder v1.0.2 // indirect github.com/go-git/go-git/v5 v5.4.2 + github.com/hetznercloud/hcloud-go v1.28.0 // indirect github.com/moby/sys/mount v0.2.0 // indirect github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 github.com/morikuni/aec v1.0.0 // indirect diff --git a/go.sum b/go.sum index caf978d3..c0a8adea 100644 --- a/go.sum +++ b/go.sum @@ -431,6 +431,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hetznercloud/hcloud-go v1.28.0 h1:T2a0CVGETf7BoWIdZ/TACqmTZAa/ROutcfdUHYiPAQ4= +github.com/hetznercloud/hcloud-go v1.28.0/go.mod h1:2C5uMtBiMoFr3m7lBFPf7wXTdh33CevmZpQIIDPGYJI= github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ= github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=