forked from toolshed/abra
refactor: break up cli pkg into nice small chunks
This commit is contained in:
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
|
||||
},
|
||||
}
|
Reference in New Issue
Block a user