2021-08-02 01:10:41 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-09-10 12:49:25 +00:00
|
|
|
"context"
|
2021-10-01 10:56:04 +00:00
|
|
|
"errors"
|
2021-10-17 21:50:28 +00:00
|
|
|
"os/exec"
|
2021-09-10 12:49:25 +00:00
|
|
|
"os/user"
|
2021-09-22 06:19:28 +00:00
|
|
|
"strings"
|
2021-08-02 01:10:41 +00:00
|
|
|
|
2021-09-10 12:49:25 +00:00
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client"
|
2021-10-02 20:30:08 +00:00
|
|
|
"coopcloud.tech/abra/pkg/server"
|
2021-08-02 01:10:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-10-01 09:59:17 +00:00
|
|
|
var local bool
|
|
|
|
var localFlag = &cli.BoolFlag{
|
|
|
|
Name: "local",
|
|
|
|
Aliases: []string{"L"},
|
|
|
|
Value: false,
|
|
|
|
Usage: "Set up the local server",
|
|
|
|
Destination: &local,
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
var serverAddCommand = &cli.Command{
|
2021-09-10 12:49:25 +00:00
|
|
|
Name: "add",
|
|
|
|
Usage: "Add a new server",
|
|
|
|
Description: `
|
|
|
|
This command adds a new server that abra will communicate with, to deploy apps.
|
|
|
|
|
2021-10-02 20:14:01 +00:00
|
|
|
If "--local" is passed, then Abra assumes that the current local server is
|
|
|
|
intended as the target server.
|
|
|
|
|
|
|
|
Otherwise, you may specify a remote server. 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.
|
2021-09-10 12:49:25 +00:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2021-09-22 06:19:14 +00:00
|
|
|
abra server add varia.zone glodemodem 12345
|
2021-09-10 12:49:25 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
`,
|
2021-10-01 10:56:04 +00:00
|
|
|
Aliases: []string{"a"},
|
2021-10-01 09:59:17 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
localFlag,
|
|
|
|
},
|
2021-09-10 12:49:25 +00:00
|
|
|
ArgsUsage: "<domain> [<user>] [<port>]",
|
2021-08-02 01:10:41 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-10-18 07:43:32 +00:00
|
|
|
|
|
|
|
if c.Args().Len() == 2 && !local {
|
2021-10-01 09:59:17 +00:00
|
|
|
err := errors.New("missing arguments <domain> or '--local'")
|
|
|
|
internal.ShowSubcommandHelpAndError(c, err)
|
|
|
|
}
|
|
|
|
|
2021-10-18 07:43:32 +00:00
|
|
|
if c.Args().Get(2) != "" && local {
|
2021-10-01 09:59:17 +00:00
|
|
|
err := errors.New("cannot use '<domain>' and '--local' together")
|
|
|
|
internal.ShowSubcommandHelpAndError(c, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
domainName := "default"
|
|
|
|
|
|
|
|
if local {
|
2021-10-02 20:30:08 +00:00
|
|
|
if err := server.CreateServerDir(domainName); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-10-17 21:50:28 +00:00
|
|
|
if _, err := exec.LookPath("docker"); err != nil {
|
|
|
|
return errors.New("docker command not found on $PATH, is it installed?")
|
|
|
|
}
|
2021-10-02 20:30:08 +00:00
|
|
|
logrus.Info("local server has been added")
|
2021-10-01 09:59:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
domainName = internal.ValidateDomain(c)
|
2021-09-10 12:49:25 +00:00
|
|
|
|
|
|
|
var username string
|
|
|
|
var port string
|
|
|
|
|
|
|
|
username = c.Args().Get(1)
|
|
|
|
if username == "" {
|
|
|
|
systemUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
username = systemUser.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
port = c.Args().Get(2)
|
|
|
|
if port == "" {
|
|
|
|
port = "22"
|
|
|
|
}
|
|
|
|
|
|
|
|
store := client.NewDefaultDockerContextStore()
|
|
|
|
contexts, err := store.Store.List()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, context := range contexts {
|
|
|
|
if context.Name == domainName {
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Fatalf("server at '%s' already exists?", domainName)
|
2021-09-10 12:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("creating context with domain '%s', username '%s' and port '%s'", domainName, username, port)
|
|
|
|
|
2021-09-10 12:49:25 +00:00
|
|
|
if err := client.CreateContext(domainName, username, port); err != nil {
|
|
|
|
logrus.Fatal(err)
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|
2021-09-10 12:49:25 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
cl, err := client.New(domainName)
|
|
|
|
if err != nil {
|
2021-08-02 01:10:41 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-09-10 12:49:25 +00:00
|
|
|
|
|
|
|
if _, err := cl.Info(ctx); err != nil {
|
2021-09-22 06:19:28 +00:00
|
|
|
if strings.Contains(err.Error(), "command not found") {
|
|
|
|
logrus.Fatalf("docker is not installed on '%s'?", domainName)
|
|
|
|
} else {
|
|
|
|
logrus.Fatalf("unable to make a connection to '%s'?", domainName)
|
|
|
|
}
|
2021-09-10 12:49:25 +00:00
|
|
|
logrus.Debug(err)
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("remote connection to '%s' is definitely up", domainName)
|
2021-09-10 12:49:25 +00:00
|
|
|
|
2021-10-02 20:30:08 +00:00
|
|
|
if err := server.CreateServerDir(domainName); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("server at '%s' has been added", domainName)
|
2021-10-01 09:59:17 +00:00
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|