feat: provision docker installation

This commit is contained in:
2021-10-24 23:15:38 +02:00
parent 1396f15c78
commit 9a0e12258a
4 changed files with 164 additions and 8 deletions

View File

@ -14,10 +14,12 @@ import (
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/client"
"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"
)
@ -40,6 +42,24 @@ var provisionFlag = &cli.BoolFlag{
Destination: &provision,
}
var sshAuth string
var sshAuthFlag = &cli.StringFlag{
Name: "ssh-auth",
Aliases: []string{"sh"},
Value: "identity-file",
Usage: "Select SSH authentication method (identity-file, password)",
Destination: &sshAuth,
}
var askSudoPass bool
var askSudoPassFlag = &cli.BoolFlag{
Name: "ask-sudo-pass",
Aliases: []string{"as"},
Value: false,
Usage: "Ask for sudo password",
Destination: &askSudoPass,
}
var traefik bool
var traefikFlag = &cli.BoolFlag{
Name: "traefik",
@ -165,9 +185,67 @@ source for this script can be seen here:
logrus.Fatal("exiting as requested")
}
// TODO: implement this remote installer run
// https://stackoverflow.com/questions/37679939/how-do-i-execute-a-command-on-a-remote-machine-in-a-golang-cli
logrus.Warn("NOT IMPLEMENTED - COMING SOON")
hasIdentityFile := true
if sshAuth == "password" {
hasIdentityFile = false
}
hostConfig, err := ssh.GetHostConfig(domainName, hasIdentityFile)
if err != nil {
return err
}
var client *simplessh.Client
if sshAuth == "identity-file" {
var err error
client, err = simplessh.ConnectWithAgent(hostConfig.Host, hostConfig.User)
if err != nil {
return err
}
} else {
password := ""
prompt := &survey.Password{
Message: "SSH password?",
}
if err := survey.AskOne(prompt, &password); err != nil {
return err
}
var err error
client, err = simplessh.ConnectWithPassword(hostConfig.Host, hostConfig.User, password)
if err != nil {
return err
}
}
defer client.Close()
logrus.Debugf("successfully created SSH client for %s", domainName)
cmd := "curl -s https://get.docker.com | bash"
var sudoPass string
if askSudoPass {
prompt := &survey.Password{
Message: "sudo password?",
}
if err := survey.AskOne(prompt, &sudoPass); err != nil {
return err
}
logrus.Debugf("running '%s' on %s now with sudo password", cmd, domainName)
_, err := client.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)
if err != nil {
return err
}
}
logrus.Infof("docker has successfully been installed on %s", domainName)
return nil
}
}
@ -245,12 +323,24 @@ var serverAddCommand = &cli.Command{
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.
by Abra. This can be useful when you already have a server provisioned and want
to start running Abra commands against it.
This can be useful when you already have a server provisioned and want to start
running Abra commands against it. This command can also provision your server
("--provision/-p") so that it is capable of hosting Co-op Cloud apps. See below
for more on that.
This command can also provision your server ("--provision/-p") so that it is
capable of hosting Co-op Cloud apps. Abra will default to expecting that you
have a working SSH config for the host in your ~/.ssh/config file. E.g. for
"example.com", you'll want to have something like:
Host example.com
Hostname 192.168.178.31 # domain name also works
User myuserontheserver
Port 12345
IdentityFile ~/.ssh/mysecretkey.local
If you have no SSH key configured for this host and are instead using password
authentication, you may pass "--ssh-auth password" to have Abra ask you for the
password. "--ask-sudo-pass" may be passed if you run your provisioning commands
via sudo privilege escalation.
If "--local" is passed, then Abra assumes that the current local server is
intended as the target server. This is useful when you want to have your entire
@ -285,6 +375,8 @@ You may omit flags to avoid performing this provisioning logic.
Flags: []cli.Flag{
localFlag,
provisionFlag,
sshAuthFlag,
askSudoPassFlag,
traefikFlag,
},
ArgsUsage: "<domain> [<user>] [<port>]",
@ -299,6 +391,11 @@ You may omit flags to avoid performing this provisioning logic.
internal.ShowSubcommandHelpAndError(c, err)
}
if sshAuth != "password" || sshAuth != "identity-file" {
err := errors.New("--ssh-auth only accepts 'identity-file' or 'password'")
internal.ShowSubcommandHelpAndError(c, err)
}
if local {
if err := newLocalServer(c, "default"); err != nil {
logrus.Fatal(err)