fix: make more server new command more robust
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
See coop-cloud/organising#163.
This commit is contained in:
parent
3c3d8dc0e7
commit
683ef0c3de
|
@ -41,3 +41,14 @@ func ValidateApp(c *cli.Context) config.App {
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateDomain ensures the domain name arg is valid.
|
||||||
|
func ValidateDomain(c *cli.Context) string {
|
||||||
|
domainName := c.Args().First()
|
||||||
|
|
||||||
|
if domainName == "" {
|
||||||
|
ShowSubcommandHelpAndError(c, errors.New("no domain provided"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return domainName
|
||||||
|
}
|
||||||
|
|
|
@ -1,29 +1,88 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
|
"os/user"
|
||||||
|
|
||||||
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var serverAddCommand = &cli.Command{
|
var serverAddCommand = &cli.Command{
|
||||||
Name: "add",
|
Name: "add",
|
||||||
Usage: "Add a new server, reachable on <server>.",
|
Usage: "Add a new server",
|
||||||
Aliases: []string{"a"},
|
Description: `
|
||||||
ArgsUsage: "<server> [<user>] [<port>]",
|
This command adds a new server that abra will communicate with, to deploy apps.
|
||||||
Description: "[<user>], [<port>] SSH connection details",
|
|
||||||
|
The <domain> argument must be a publicy accessible domain name which points to
|
||||||
|
your server. You should have SSH access to this server, Abra will assume port
|
||||||
|
22 and will use your current system username to make an initial connection. You
|
||||||
|
can use the <user> and <port> arguments to adjust this.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
abra server add varia.zone 12345 glodemodem
|
||||||
|
|
||||||
|
Abra will construct the following SSH connection string then:
|
||||||
|
|
||||||
|
ssh://globemodem@varia.zone:12345
|
||||||
|
|
||||||
|
All communication between Abra and the server will use this SSH connection.
|
||||||
|
|
||||||
|
`,
|
||||||
|
Aliases: []string{"a"},
|
||||||
|
ArgsUsage: "<domain> [<user>] [<port>]",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
argLen := c.Args().Len()
|
domainName := internal.ValidateDomain(c)
|
||||||
args := c.Args().Slice()
|
|
||||||
if argLen < 3 {
|
var username string
|
||||||
args = append(args, make([]string, 3-argLen)...)
|
var port string
|
||||||
|
|
||||||
|
username = c.Args().Get(1)
|
||||||
|
if username == "" {
|
||||||
|
systemUser, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
username = systemUser.Username
|
||||||
}
|
}
|
||||||
if err := client.CreateContext(args[0], args[1], args[2]); err != nil {
|
|
||||||
|
port = c.Args().Get(2)
|
||||||
|
if port == "" {
|
||||||
|
port = "22"
|
||||||
|
}
|
||||||
|
|
||||||
|
store := client.NewDefaultDockerContextStore()
|
||||||
|
contexts, err := store.Store.List()
|
||||||
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println(args[0])
|
|
||||||
|
for _, context := range contexts {
|
||||||
|
if context.Name == domainName {
|
||||||
|
logrus.Fatalf("Server at '%s' already exists?", domainName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.CreateContext(domainName, username, port); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
cl, err := client.New(domainName)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := cl.Info(ctx); err != nil {
|
||||||
|
logrus.Fatalf("Unable to make a connection to '%s'?", domainName)
|
||||||
|
logrus.Debug(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Infof("Server at '%s' has been added", domainName)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue