forked from toolshed/abra
		
	feat: implement hetzner new command
This commit is contained in:
		
							
								
								
									
										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` |     - [x] `add` | ||||||
|     - [ ] `new` |     - [ ] `new` | ||||||
|       - [ ] `capsul` |       - [ ] `capsul` | ||||||
|       - [ ] `hetzner` (XXX: in progress (decentral1se)) |       - [x] `hetzner` | ||||||
|     - [x] `rm` |     - [x] `rm` | ||||||
|     - [x] `init` |     - [x] `init` | ||||||
|   - [ ] `abra app` |   - [ ] `abra app` | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ package server | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  |  | ||||||
|  | 	"coopcloud.tech/abra/cli/formatter" | ||||||
| 	"github.com/hetznercloud/hcloud-go/hcloud" | 	"github.com/hetznercloud/hcloud-go/hcloud" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"github.com/urfave/cli/v2" | 	"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 | creation request. You must already have a Hetzner Cloud account and an account | ||||||
| API token before using this command. | 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. | environment variable or otherwise passing the "--env/-e" flag. | ||||||
| `, | `, | ||||||
| 	Flags: []cli.Flag{ | 	Flags: []cli.Flag{ | ||||||
| @ -59,7 +60,7 @@ environment variable or otherwise passing the "--env/-e" flag. | |||||||
| 			Name:        "token", | 			Name:        "token", | ||||||
| 			Aliases:     []string{"T"}, | 			Aliases:     []string{"T"}, | ||||||
| 			Usage:       "Hetzner Cloud API token", | 			Usage:       "Hetzner Cloud API token", | ||||||
| 			EnvVars:     []string{"HCLOUD_API_TOKEN"}, | 			EnvVars:     []string{"HCLOUD_TOKEN"}, | ||||||
| 			Destination: &hetznerCloudAPIToken, | 			Destination: &hetznerCloudAPIToken, | ||||||
| 		}, | 		}, | ||||||
| 	}, | 	}, | ||||||
| @ -69,27 +70,43 @@ environment variable or otherwise passing the "--env/-e" flag. | |||||||
| 			return cli.ShowSubcommandHelp(c) | 			return cli.ShowSubcommandHelp(c) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		if hetznerCloudAPIToken == "" { | ||||||
|  | 			logrus.Fatal("API token is missing, cannot continue") | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		ctx := context.Background() | 		ctx := context.Background() | ||||||
| 		client := hcloud.NewClient(hcloud.WithToken(hetznerCloudAPIToken)) | 		client := hcloud.NewClient(hcloud.WithToken(hetznerCloudAPIToken)) | ||||||
|  |  | ||||||
| 		// var sshkeys []hcloud.SSHKey | 		var sshKeys []*hcloud.SSHKey | ||||||
| 		// for _, sshkey := range HetznerCloudSSHKeys { | 		for _, sshKey := range c.StringSlice("ssh-keys") { | ||||||
| 		// 	sshkeys = append(sshkeys, hcloud.SSHKey{Name: sshkey}) | 			sshKey, _, err := client.SSHKey.GetByName(ctx, sshKey) | ||||||
| 		// } | 			if err != nil { | ||||||
|  | 				logrus.Fatal(err) | ||||||
|  | 			} | ||||||
|  | 			sshKeys = append(sshKeys, sshKey) | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		// TODO: finish passing arguments |  | ||||||
| 		serverOpts := hcloud.ServerCreateOpts{ | 		serverOpts := hcloud.ServerCreateOpts{ | ||||||
| 			Name:       name, | 			Name:       name, | ||||||
| 			ServerType: &hcloud.ServerType{Name: hetznerCloudType}, | 			ServerType: &hcloud.ServerType{Name: hetznerCloudType}, | ||||||
| 			Image:      &hcloud.Image{Name: hetznerCloudImage}, | 			Image:      &hcloud.Image{Name: hetznerCloudImage}, | ||||||
| 			// SSHKeys:    HetznerCloudSSHKeys, | 			SSHKeys:    sshKeys, | ||||||
| 			// Location:   HetznerCloudLocation, | 			Location:   &hcloud.Location{Name: hetznerCloudLocation}, | ||||||
| 		} | 		} | ||||||
| 		_, _, err := client.Server.Create(ctx, serverOpts) | 		res, _, err := client.Server.Create(ctx, serverOpts) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			logrus.Fatal(err) | 			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 | 		return nil | ||||||
| 	}, | 	}, | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user