refactor: better SSH connection details handling
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -8,21 +8,43 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/server"
|
||||
"coopcloud.tech/abra/pkg/ssh"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/sfreiberg/simplessh"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
dockerInstallMsg = `
|
||||
A docker installation cannot be found on %s. This is a required system dependency
|
||||
for running Co-op Cloud on your server. If you would like, Abra can attempt to install
|
||||
Docker for you using the upstream non-interactive installation script.
|
||||
|
||||
See the following documentation for more:
|
||||
|
||||
https://docs.docker.com/engine/install/debian/#install-using-the-convenience-script
|
||||
|
||||
N.B Docker doesn't recommend it for production environments but many use it for
|
||||
such purposes. Docker stable is now installed by default by this script. The
|
||||
source for this script can be seen here:
|
||||
|
||||
https://github.com/docker/docker-install
|
||||
|
||||
`
|
||||
)
|
||||
|
||||
var local bool
|
||||
var localFlag = &cli.BoolFlag{
|
||||
Name: "local",
|
||||
@ -68,6 +90,40 @@ var traefikFlag = &cli.BoolFlag{
|
||||
Destination: &traefik,
|
||||
}
|
||||
|
||||
func cleanUp(domainName string) {
|
||||
logrus.Warnf("cleaning up context for %s", domainName)
|
||||
if err := client.DeleteContext(domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Warnf("cleaning up server directory for %s", domainName)
|
||||
if err := os.RemoveAll(filepath.Join(config.ABRA_SERVER_FOLDER, domainName)); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func installDockerLocal(c *cli.Context) error {
|
||||
fmt.Println(fmt.Sprintf(dockerInstallMsg, "this local server"))
|
||||
|
||||
response := false
|
||||
prompt := &survey.Confirm{
|
||||
Message: fmt.Sprintf("attempt install docker on local server?"),
|
||||
}
|
||||
if err := survey.AskOne(prompt, &response); err != nil {
|
||||
return err
|
||||
}
|
||||
if !response {
|
||||
logrus.Fatal("exiting as requested")
|
||||
}
|
||||
|
||||
cmd := exec.Command("bash", "-c", "curl -s https://get.docker.com | bash")
|
||||
if err := internal.RunCmd(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newLocalServer(c *cli.Context, domainName string) error {
|
||||
if err := createServerDir(domainName); err != nil {
|
||||
return err
|
||||
@ -80,7 +136,7 @@ func newLocalServer(c *cli.Context, domainName string) error {
|
||||
|
||||
if _, err := exec.LookPath("docker"); err != nil {
|
||||
if provision {
|
||||
if err := installDocker(c, cl, domainName); err != nil {
|
||||
if err := installDockerLocal(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := initSwarm(c, cl, domainName); err != nil {
|
||||
@ -104,21 +160,7 @@ func newLocalServer(c *cli.Context, domainName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func newContext(c *cli.Context, domainName string) error {
|
||||
username := c.Args().Get(1)
|
||||
if username == "" {
|
||||
systemUser, err := user.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
username = systemUser.Username
|
||||
}
|
||||
|
||||
port := c.Args().Get(2)
|
||||
if port == "" {
|
||||
port = "22"
|
||||
}
|
||||
|
||||
func newContext(c *cli.Context, domainName, username, port string) error {
|
||||
store := client.NewDefaultDockerContextStore()
|
||||
contexts, err := store.Store.List()
|
||||
if err != nil {
|
||||
@ -144,48 +186,19 @@ func newContext(c *cli.Context, domainName string) error {
|
||||
func newClient(c *cli.Context, domainName string) (*dockerClient.Client, error) {
|
||||
cl, err := client.New(domainName)
|
||||
if err != nil {
|
||||
logrus.Warn("cleaning up context due to connection failure")
|
||||
if err := client.DeleteContext(domainName); err != nil {
|
||||
return &dockerClient.Client{}, err
|
||||
}
|
||||
return &dockerClient.Client{}, err
|
||||
}
|
||||
return cl, nil
|
||||
}
|
||||
|
||||
func installDocker(c *cli.Context, cl *dockerClient.Client, domainName string) error {
|
||||
logrus.Debugf("attempting to construct SSH client for %s", domainName)
|
||||
|
||||
client, err := ssh.New(domainName, sshAuth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
logrus.Debugf("successfully created SSH client for %s", domainName)
|
||||
|
||||
result, err := client.Exec("which docker")
|
||||
func installDocker(c *cli.Context, cl *dockerClient.Client, sshCl *simplessh.Client, domainName string) error {
|
||||
result, err := sshCl.Exec("which docker")
|
||||
if err != nil && string(result) != "" {
|
||||
return err
|
||||
}
|
||||
|
||||
if string(result) == "" {
|
||||
fmt.Println(fmt.Sprintf(`
|
||||
A docker installation cannot be found on %s. This is a required system dependency
|
||||
for running Co-op Cloud on your server. If you would like, Abra can attempt to install
|
||||
Docker for you using the upstream non-interactive installation script.
|
||||
|
||||
See the following documentation for more:
|
||||
|
||||
https://docs.docker.com/engine/install/debian/#install-using-the-convenience-script
|
||||
|
||||
N.B Docker doesn't recommend it for production environments but many use it for
|
||||
such purposes. Docker stable is now installed by default by this script. The
|
||||
source for this script can be seen here:
|
||||
|
||||
https://github.com/docker/docker-install
|
||||
|
||||
`, domainName))
|
||||
fmt.Println(fmt.Sprintf(dockerInstallMsg, domainName))
|
||||
|
||||
response := false
|
||||
prompt := &survey.Confirm{
|
||||
@ -209,13 +222,13 @@ source for this script can be seen here:
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("running '%s' on %s now with sudo password", cmd, domainName)
|
||||
_, err := client.ExecSudo(cmd, sudoPass)
|
||||
_, err := sshCl.ExecSudo(cmd, sudoPass)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
logrus.Debugf("running '%s' on %s now without sudo password", cmd, domainName)
|
||||
_, err := client.Exec(cmd)
|
||||
_, err := sshCl.Exec(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -378,11 +391,25 @@ You may omit flags to avoid performing this provisioning logic.
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
username := c.Args().Get(1)
|
||||
if username == "" {
|
||||
systemUser, err := user.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
username = systemUser.Username
|
||||
}
|
||||
|
||||
port := c.Args().Get(2)
|
||||
if port == "" {
|
||||
port = "22"
|
||||
}
|
||||
|
||||
if err := createServerDir(domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := newContext(c, domainName); err != nil {
|
||||
if err := newContext(c, domainName, username, port); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
@ -392,7 +419,15 @@ You may omit flags to avoid performing this provisioning logic.
|
||||
}
|
||||
|
||||
if provision {
|
||||
if err := installDocker(c, cl, domainName); err != nil {
|
||||
logrus.Debugf("attempting to construct SSH client for %s", domainName)
|
||||
sshCl, err := ssh.New(domainName, sshAuth, username, port)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
defer sshCl.Close()
|
||||
logrus.Debugf("successfully created SSH client for %s", domainName)
|
||||
|
||||
if err := installDocker(c, cl, sshCl, domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
if err := initSwarm(c, cl, domainName); err != nil {
|
||||
|
Reference in New Issue
Block a user