forked from coop-cloud/abra
177 lines
4.2 KiB
Go
177 lines
4.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
|
|
"coopcloud.tech/abra/client"
|
|
"coopcloud.tech/abra/config"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var serverListCommand = &cli.Command{
|
|
Name: "list",
|
|
Aliases: []string{"ls"},
|
|
Usage: "List locally-defined servers.",
|
|
ArgsUsage: emptyArgsUsage,
|
|
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 := 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 {
|
|
// This most likely means its a non-docker swarm context and we can ignore
|
|
continue
|
|
}
|
|
if ctx.Name == serverName {
|
|
row = []string{serverName, endpoint}
|
|
}
|
|
}
|
|
if len(row) == 0 {
|
|
row = []string{serverName, "UNKNOWN"}
|
|
}
|
|
table.Append(row)
|
|
|
|
}
|
|
return nil
|
|
|
|
},
|
|
}
|
|
|
|
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
|
|
},
|
|
}
|
|
|
|
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>",
|
|
HideHelp: true,
|
|
}
|
|
|
|
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
|
|
},
|
|
}
|
|
|
|
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
|
|
},
|
|
}
|
|
|
|
// 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,
|
|
},
|
|
}
|