forked from toolshed/abra
refactor: break up cli pkg into nice small chunks
This commit is contained in:
28
cli/server/add.go
Normal file
28
cli/server/add.go
Normal file
@ -0,0 +1,28 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"coopcloud.tech/abra/client"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var serverAddCommand = &cli.Command{
|
||||
Name: "add",
|
||||
Usage: "Add a new server, reachable on <host>.",
|
||||
ArgsUsage: "<host> [<user>] [<port>]",
|
||||
Description: "[<user>], [<port>] SSH connection details",
|
||||
Action: func(c *cli.Context) error {
|
||||
arg_len := c.Args().Len()
|
||||
args := c.Args().Slice()
|
||||
if arg_len < 3 {
|
||||
args = append(args, make([]string, 3-arg_len)...)
|
||||
}
|
||||
if err := client.CreateContext(args[0], args[1], args[2]); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
fmt.Println(args[0])
|
||||
return nil
|
||||
},
|
||||
}
|
64
cli/server/init.go
Normal file
64
cli/server/init.go
Normal file
@ -0,0 +1,64 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"coopcloud.tech/abra/client"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var serverInitCommand = &cli.Command{
|
||||
Name: "init",
|
||||
Usage: "Initialise server for deploying apps",
|
||||
HideHelp: true,
|
||||
ArgsUsage: "<host>",
|
||||
Description: `
|
||||
Initialise swarm mode on the target <host>.
|
||||
|
||||
This initialisation explicitly chooses the "single host swarm" mode which uses
|
||||
the default IPv4 address as the advertising address. This can be re-configured
|
||||
later for more advanced use cases.
|
||||
`,
|
||||
Action: func(c *cli.Context) error {
|
||||
host := c.Args().First()
|
||||
if host == "" {
|
||||
cli.ShowSubcommandHelp(c)
|
||||
return nil
|
||||
}
|
||||
|
||||
cl, err := client.NewClientWithContext(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var ipv4 net.IP
|
||||
ips, _ := net.LookupIP(host)
|
||||
for _, ip := range ips {
|
||||
ipv4 = ip.To4()
|
||||
}
|
||||
|
||||
if string(ipv4) == "" {
|
||||
return fmt.Errorf("unable to retrieve ipv4 address for %s", host)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
initReq := swarm.InitRequest{
|
||||
ListenAddr: "0.0.0.0:2377",
|
||||
AdvertiseAddr: string(ipv4),
|
||||
}
|
||||
if _, err := cl.SwarmInit(ctx, initReq); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
netOpts := types.NetworkCreate{Driver: "overlay", Scope: "swarm"}
|
||||
if _, err := cl.NetworkCreate(ctx, "proxy", netOpts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
55
cli/server/list.go
Normal file
55
cli/server/list.go
Normal file
@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"coopcloud.tech/abra/cli/formatter"
|
||||
"coopcloud.tech/abra/client"
|
||||
"coopcloud.tech/abra/config"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var serverListCommand = &cli.Command{
|
||||
Name: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Usage: "List locally-defined servers.",
|
||||
ArgsUsage: " ",
|
||||
HideHelp: true,
|
||||
Action: func(c *cli.Context) error {
|
||||
dockerContextStore := client.NewDefaultDockerContextStore()
|
||||
contexts, err := dockerContextStore.Store.List()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
tableColumns := []string{"Name", "Connection"}
|
||||
table := formatter.CreateTable(tableColumns)
|
||||
defer table.Render()
|
||||
|
||||
serverNames, err := config.ReadServerNames()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
for _, serverName := range serverNames {
|
||||
|
||||
var row []string
|
||||
for _, ctx := range contexts {
|
||||
endpoint, err := client.GetContextEndpoint(ctx)
|
||||
if err != nil && strings.Contains(err.Error(), "does not exist") {
|
||||
// No local context found, we can continue safely
|
||||
continue
|
||||
}
|
||||
if ctx.Name == serverName {
|
||||
row = []string{serverName, endpoint}
|
||||
}
|
||||
}
|
||||
if len(row) == 0 {
|
||||
row = []string{serverName, "UNKNOWN"}
|
||||
}
|
||||
table.Append(row)
|
||||
|
||||
}
|
||||
return nil
|
||||
|
||||
},
|
||||
}
|
106
cli/server/new.go
Normal file
106
cli/server/new.go
Normal file
@ -0,0 +1,106 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hetznercloud/hcloud-go/hcloud"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
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: "<name>",
|
||||
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: "<provider>",
|
||||
Subcommands: []*cli.Command{
|
||||
serverNewHetznerCloudCommand,
|
||||
},
|
||||
}
|
25
cli/server/remove.go
Normal file
25
cli/server/remove.go
Normal file
@ -0,0 +1,25 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"coopcloud.tech/abra/client"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var serverRemoveCommand = &cli.Command{
|
||||
Name: "remove",
|
||||
Aliases: []string{"rm", "delete"},
|
||||
Usage: "Remove a locally-defined server",
|
||||
HideHelp: true,
|
||||
Action: func(c *cli.Context) error {
|
||||
server := c.Args().First()
|
||||
if server == "" {
|
||||
cli.ShowSubcommandHelp(c)
|
||||
return nil
|
||||
}
|
||||
if err := client.DeleteContext(server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
26
cli/server/server.go
Normal file
26
cli/server/server.go
Normal file
@ -0,0 +1,26 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Reminder: The list commands are in is the order they appear in the help menu
|
||||
var ServerCommand = &cli.Command{
|
||||
Name: "server",
|
||||
ArgsUsage: "<host>",
|
||||
Usage: "Manage the servers that host your apps",
|
||||
Description: `
|
||||
Manage the lifecycle of a server.
|
||||
|
||||
These commands support creating new servers using 3rd party integrations,
|
||||
initialising existing servers to support Co-op Cloud deployments and managing
|
||||
the connections to those servers.
|
||||
`,
|
||||
Subcommands: []*cli.Command{
|
||||
serverNewCommand,
|
||||
serverInitCommand,
|
||||
serverAddCommand,
|
||||
serverListCommand,
|
||||
serverRemoveCommand,
|
||||
},
|
||||
}
|
Reference in New Issue
Block a user