forked from toolshed/abra
refactor!: migrate to urfave/cli v1
Better flexible flags handling.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -22,7 +23,7 @@ import (
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -47,26 +48,21 @@ source for this script can be seen here:
|
||||
|
||||
var local bool
|
||||
var localFlag = &cli.BoolFlag{
|
||||
Name: "local",
|
||||
Aliases: []string{"l"},
|
||||
Value: false,
|
||||
Name: "local, l",
|
||||
Usage: "Use local server",
|
||||
Destination: &local,
|
||||
}
|
||||
|
||||
var provision bool
|
||||
var provisionFlag = &cli.BoolFlag{
|
||||
Name: "provision",
|
||||
Aliases: []string{"p"},
|
||||
Value: false,
|
||||
Name: "provision, p",
|
||||
Usage: "Provision server so it can deploy apps",
|
||||
Destination: &provision,
|
||||
}
|
||||
|
||||
var sshAuth string
|
||||
var sshAuthFlag = &cli.StringFlag{
|
||||
Name: "ssh-auth",
|
||||
Aliases: []string{"sh"},
|
||||
Name: "ssh-auth, sh",
|
||||
Value: "identity-file",
|
||||
Usage: "Select SSH authentication method (identity-file, password)",
|
||||
Destination: &sshAuth,
|
||||
@ -74,18 +70,14 @@ var sshAuthFlag = &cli.StringFlag{
|
||||
|
||||
var askSudoPass bool
|
||||
var askSudoPassFlag = &cli.BoolFlag{
|
||||
Name: "ask-sudo-pass",
|
||||
Aliases: []string{"as"},
|
||||
Value: false,
|
||||
Name: "ask-sudo-pass, as",
|
||||
Usage: "Ask for sudo password",
|
||||
Destination: &askSudoPass,
|
||||
}
|
||||
|
||||
var traefik bool
|
||||
var traefikFlag = &cli.BoolFlag{
|
||||
Name: "traefik",
|
||||
Aliases: []string{"t"},
|
||||
Value: false,
|
||||
Name: "traefik, t",
|
||||
Usage: "Deploy traefik",
|
||||
Destination: &traefik,
|
||||
}
|
||||
@ -320,7 +312,7 @@ If nothing works, you try running the Docker install script manually on your ser
|
||||
|
||||
func initSwarmLocal(c *cli.Context, cl *dockerClient.Client, domainName string) error {
|
||||
initReq := swarm.InitRequest{ListenAddr: "0.0.0.0:2377"}
|
||||
if _, err := cl.SwarmInit(c.Context, initReq); err != nil {
|
||||
if _, err := cl.SwarmInit(context.Background(), initReq); err != nil {
|
||||
if strings.Contains(err.Error(), "is already part of a swarm") ||
|
||||
strings.Contains(err.Error(), "must specify a listening address") {
|
||||
logrus.Infof("swarm mode already initialised on %s", domainName)
|
||||
@ -332,7 +324,7 @@ func initSwarmLocal(c *cli.Context, cl *dockerClient.Client, domainName string)
|
||||
}
|
||||
|
||||
netOpts := types.NetworkCreate{Driver: "overlay", Scope: "swarm"}
|
||||
if _, err := cl.NetworkCreate(c.Context, "proxy", netOpts); err != nil {
|
||||
if _, err := cl.NetworkCreate(context.Background(), "proxy", netOpts); err != nil {
|
||||
if !strings.Contains(err.Error(), "proxy already exists") {
|
||||
return err
|
||||
}
|
||||
@ -354,7 +346,7 @@ func initSwarm(c *cli.Context, cl *dockerClient.Client, domainName string) error
|
||||
ListenAddr: "0.0.0.0:2377",
|
||||
AdvertiseAddr: ipv4,
|
||||
}
|
||||
if _, err := cl.SwarmInit(c.Context, initReq); err != nil {
|
||||
if _, err := cl.SwarmInit(context.Background(), initReq); err != nil {
|
||||
if strings.Contains(err.Error(), "is already part of a swarm") ||
|
||||
strings.Contains(err.Error(), "must specify a listening address") {
|
||||
logrus.Infof("swarm mode already initialised on %s", domainName)
|
||||
@ -366,7 +358,7 @@ func initSwarm(c *cli.Context, cl *dockerClient.Client, domainName string) error
|
||||
}
|
||||
|
||||
netOpts := types.NetworkCreate{Driver: "overlay", Scope: "swarm"}
|
||||
if _, err := cl.NetworkCreate(c.Context, "proxy", netOpts); err != nil {
|
||||
if _, err := cl.NetworkCreate(context.Background(), "proxy", netOpts); err != nil {
|
||||
if !strings.Contains(err.Error(), "proxy already exists") {
|
||||
return err
|
||||
}
|
||||
@ -414,9 +406,10 @@ func deployTraefik(c *cli.Context, cl *dockerClient.Client, domainName string) e
|
||||
return nil
|
||||
}
|
||||
|
||||
var serverAddCommand = &cli.Command{
|
||||
Name: "add",
|
||||
Usage: "Add a server to your configuration",
|
||||
var serverAddCommand = cli.Command{
|
||||
Name: "add",
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Add a server to your configuration",
|
||||
Description: `
|
||||
This command adds a new server to your configuration so that it can be managed
|
||||
by Abra. This can be useful when you already have a server provisioned and want
|
||||
@ -467,17 +460,21 @@ In this example, Abra will run the following operations:
|
||||
|
||||
You may omit flags to avoid performing this provisioning logic.
|
||||
`,
|
||||
Aliases: []string{"a"},
|
||||
Flags: []cli.Flag{
|
||||
internal.DebugFlag,
|
||||
internal.NoInputFlag,
|
||||
localFlag,
|
||||
provisionFlag,
|
||||
sshAuthFlag,
|
||||
askSudoPassFlag,
|
||||
traefikFlag,
|
||||
internal.DebugFlag,
|
||||
internal.NoInputFlag,
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
ArgsUsage: "<domain> [<user>] [<port>]",
|
||||
Action: func(c *cli.Context) error {
|
||||
if c.Args().Len() > 0 && local || !internal.ValidateSubCmdFlags(c) {
|
||||
if len(c.Args()) > 0 && local || !internal.ValidateSubCmdFlags(c) {
|
||||
err := errors.New("cannot use <domain> and --local together")
|
||||
internal.ShowSubcommandHelpAndError(c, err)
|
||||
}
|
||||
@ -543,7 +540,7 @@ You may omit flags to avoid performing this provisioning logic.
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := cl.Info(c.Context); err != nil {
|
||||
if _, err := cl.Info(context.Background()); err != nil {
|
||||
cleanUp(domainName)
|
||||
logrus.Fatalf("couldn't make a remote docker connection to %s? use --provision/-p to attempt to install", domainName)
|
||||
}
|
||||
|
Reference in New Issue
Block a user