forked from toolshed/abra
refactor!: consolidate SSH handling
Closes coop-cloud/organising#389. Closes coop-cloud/organising#341. Closes coop-cloud/organising#326. Closes coop-cloud/organising#380. Closes coop-cloud/organising#360.
This commit is contained in:
@ -20,6 +20,7 @@ import (
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/archive"
|
||||
"github.com/docker/docker/pkg/system"
|
||||
"github.com/klauspost/pgzip"
|
||||
@ -72,6 +73,11 @@ This single file can be used to restore your app. See "abra app restore" for mor
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
recipe, err := recipe.Get(app.Recipe)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
@ -115,7 +121,7 @@ This single file can be used to restore your app. See "abra app restore" for mor
|
||||
|
||||
logrus.Infof("running backup for the %s service", serviceName)
|
||||
|
||||
if err := runBackup(app, serviceName, backupConfig); err != nil {
|
||||
if err := runBackup(cl, app, serviceName, backupConfig); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
@ -126,7 +132,7 @@ This single file can be used to restore your app. See "abra app restore" for mor
|
||||
for serviceName, backupConfig := range backupConfigs {
|
||||
logrus.Infof("running backup for the %s service", serviceName)
|
||||
|
||||
if err := runBackup(app, serviceName, backupConfig); err != nil {
|
||||
if err := runBackup(cl, app, serviceName, backupConfig); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -143,16 +149,11 @@ func TimeStamp() string {
|
||||
}
|
||||
|
||||
// runBackup does the actual backup logic.
|
||||
func runBackup(app config.App, serviceName string, bkConfig backupConfig) error {
|
||||
func runBackup(cl *dockerClient.Client, app config.App, serviceName string, bkConfig backupConfig) error {
|
||||
if len(bkConfig.backupPaths) == 0 {
|
||||
return fmt.Errorf("backup paths are empty for %s?", serviceName)
|
||||
}
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// FIXME: avoid instantiating a new CLI
|
||||
dcli, err := command.NewDockerCli()
|
||||
if err != nil {
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/archive"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
@ -52,6 +53,11 @@ Example:
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if internal.LocalCmd && internal.RemoteUser != "" {
|
||||
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use --local & --user together"))
|
||||
}
|
||||
@ -129,7 +135,7 @@ Example:
|
||||
logrus.Debug("did not detect any command arguments")
|
||||
}
|
||||
|
||||
if err := runCmdRemote(app, abraSh, targetServiceName, cmdName, parsedCmdArgs); err != nil {
|
||||
if err := runCmdRemote(cl, app, abraSh, targetServiceName, cmdName, parsedCmdArgs); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
@ -170,12 +176,7 @@ func ensureCommand(abraSh, recipeName, execCmd string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runCmdRemote(app config.App, abraSh, serviceName, cmdName, cmdArgs string) error {
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func runCmdRemote(cl *dockerClient.Client, app config.App, abraSh, serviceName, cmdName, cmdArgs string) error {
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), serviceName))
|
||||
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/archive"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
@ -43,6 +44,11 @@ And if you want to copy that file back to your current working directory locally
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
src := c.Args().Get(1)
|
||||
dst := c.Args().Get(2)
|
||||
if src == "" {
|
||||
@ -88,28 +94,24 @@ And if you want to copy that file back to your current working directory locally
|
||||
logrus.Fatalf("%s does not exist locally?", dstPath)
|
||||
}
|
||||
}
|
||||
err := configureAndCp(c, app, srcPath, dstPath, service, isToContainer)
|
||||
if err != nil {
|
||||
|
||||
if err := configureAndCp(c, cl, app, srcPath, dstPath, service, isToContainer); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
return nil
|
||||
|
||||
return nil
|
||||
},
|
||||
BashComplete: autocomplete.AppNameComplete,
|
||||
}
|
||||
|
||||
func configureAndCp(
|
||||
c *cli.Context,
|
||||
cl *dockerClient.Client,
|
||||
app config.App,
|
||||
srcPath string,
|
||||
dstPath string,
|
||||
service string,
|
||||
isToContainer bool) error {
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), service))
|
||||
|
||||
|
@ -8,10 +8,8 @@ import (
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/context"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"coopcloud.tech/abra/pkg/recipe"
|
||||
"coopcloud.tech/abra/pkg/ssh"
|
||||
"coopcloud.tech/tagcmp"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
@ -98,13 +96,6 @@ can take some time.
|
||||
alreadySeen := make(map[string]bool)
|
||||
for _, app := range apps {
|
||||
if _, ok := alreadySeen[app.Server]; !ok {
|
||||
if err := context.HasDockerContext(app.Name, app.Server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ssh.EnsureHostKey(app.Server); err != nil {
|
||||
logrus.Fatal(fmt.Sprintf(internal.SSHFailMsg, app.Server))
|
||||
}
|
||||
alreadySeen[app.Server] = true
|
||||
}
|
||||
}
|
||||
@ -114,7 +105,6 @@ can take some time.
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
var err error
|
||||
catl, err = recipe.ReadRecipeCatalogue()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
@ -212,6 +202,7 @@ can take some time.
|
||||
}
|
||||
allStats[app.Server] = stats
|
||||
}
|
||||
|
||||
if internal.MachineReadable {
|
||||
jsonstring, err := json.Marshal(allStats)
|
||||
if err != nil {
|
||||
@ -221,6 +212,7 @@ can take some time.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
alreadySeen := make(map[string]bool)
|
||||
for _, app := range apps {
|
||||
if _, ok := alreadySeen[app.Server]; ok {
|
||||
|
@ -16,6 +16,7 @@ import (
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/archive"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
@ -55,6 +56,11 @@ Example:
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
serviceName := c.Args().Get(1)
|
||||
if serviceName == "" {
|
||||
internal.ShowSubcommandHelpAndError(c, errors.New("missing <service>?"))
|
||||
@ -104,7 +110,8 @@ Example:
|
||||
if !ok {
|
||||
rsConfig = restoreConfig{}
|
||||
}
|
||||
if err := runRestore(app, backupPath, serviceName, rsConfig); err != nil {
|
||||
|
||||
if err := runRestore(cl, app, backupPath, serviceName, rsConfig); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
@ -113,12 +120,7 @@ Example:
|
||||
}
|
||||
|
||||
// runRestore does the actual restore logic.
|
||||
func runRestore(app config.App, backupPath, serviceName string, rsConfig restoreConfig) error {
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func runRestore(cl *dockerClient.Client, app config.App, backupPath, serviceName string, rsConfig restoreConfig) error {
|
||||
// FIXME: avoid instantiating a new CLI
|
||||
dcli, err := command.NewDockerCli()
|
||||
if err != nil {
|
||||
|
@ -48,6 +48,11 @@ var appSecretGenerateCommand = cli.Command{
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if len(c.Args()) == 1 && !allSecrets {
|
||||
err := errors.New("missing arguments <secret>/<version> or '--all'")
|
||||
internal.ShowSubcommandHelpAndError(c, err)
|
||||
@ -79,7 +84,7 @@ var appSecretGenerateCommand = cli.Command{
|
||||
}
|
||||
}
|
||||
|
||||
secretVals, err := secret.GenerateSecrets(secretsToCreate, app.StackName(), app.Server)
|
||||
secretVals, err := secret.GenerateSecrets(cl, secretsToCreate, app.StackName(), app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
@ -135,6 +140,11 @@ Example:
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if len(c.Args()) != 4 {
|
||||
internal.ShowSubcommandHelpAndError(c, errors.New("missing arguments?"))
|
||||
}
|
||||
@ -144,7 +154,7 @@ Example:
|
||||
data := c.Args().Get(3)
|
||||
|
||||
secretName := fmt.Sprintf("%s_%s_%s", app.StackName(), name, version)
|
||||
if err := client.StoreSecret(secretName, data, app.Server); err != nil {
|
||||
if err := client.StoreSecret(cl, secretName, data, app.Server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -53,6 +53,11 @@ recipes.
|
||||
app := internal.ValidateApp(c)
|
||||
stackName := app.StackName()
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if !internal.Chaos {
|
||||
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
|
||||
logrus.Fatal(err)
|
||||
@ -68,11 +73,6 @@ recipes.
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Debugf("checking whether %s is already deployed", stackName)
|
||||
|
||||
isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, stackName)
|
||||
|
@ -26,12 +26,17 @@ var appVolumeListCommand = cli.Command{
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
filters, err := app.Filters(false, true)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
volumeList, err := client.GetVolumes(context.Background(), app.Server, filters)
|
||||
volumeList, err := client.GetVolumes(cl, context.Background(), app.Server, filters)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
@ -80,12 +85,17 @@ Passing "--force/-f" will select all volumes for removal. Be careful.
|
||||
Action: func(c *cli.Context) error {
|
||||
app := internal.ValidateApp(c)
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
filters, err := app.Filters(false, true)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
volumeList, err := client.GetVolumes(context.Background(), app.Server, filters)
|
||||
volumeList, err := client.GetVolumes(cl, context.Background(), app.Server, filters)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
@ -109,7 +119,7 @@ Passing "--force/-f" will select all volumes for removal. Be careful.
|
||||
volumesToRemove = volumeNames
|
||||
}
|
||||
|
||||
err = client.RemoveVolumes(context.Background(), app.Server, volumesToRemove, internal.Force)
|
||||
err = client.RemoveVolumes(cl, context.Background(), app.Server, volumesToRemove, internal.Force)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
@ -378,73 +378,6 @@ var RemoteUserFlag = &cli.StringFlag{
|
||||
Destination: &RemoteUser,
|
||||
}
|
||||
|
||||
// SSHFailMsg is a hopefully helpful SSH failure message
|
||||
var SSHFailMsg = `
|
||||
Woops, Abra is unable to connect to connect to %s.
|
||||
|
||||
Here are a few tips for debugging your local SSH config. Abra uses plain 'ol
|
||||
SSH to make connections to servers, so if your SSH config is working, Abra is
|
||||
working.
|
||||
|
||||
In the first place, Abra will always try to read your Docker context connection
|
||||
string for SSH connection details. You can view your server context configs
|
||||
with the following command. Are they correct?
|
||||
|
||||
abra server ls
|
||||
|
||||
Is your ssh-agent running? You can start it by running the following command:
|
||||
|
||||
eval "$(ssh-agent)"
|
||||
|
||||
If your SSH private key loaded? You can check by running the following command:
|
||||
|
||||
ssh-add -L
|
||||
|
||||
If, you can add it with:
|
||||
|
||||
ssh-add ~/.ssh/<private-key-part>
|
||||
|
||||
If you are using a non-default public/private key, you can configure this in
|
||||
your ~/.ssh/config file which Abra will read in order to figure out connection
|
||||
details:
|
||||
|
||||
Host foo.coopcloud.tech
|
||||
Hostname foo.coopcloud.tech
|
||||
User bar
|
||||
Port 12345
|
||||
IdentityFile ~/.ssh/bar@foo.coopcloud.tech
|
||||
|
||||
If you're only using password authentication, you can use the following config:
|
||||
|
||||
Host foo.coopcloud.tech
|
||||
Hostname foo.coopcloud.tech
|
||||
User bar
|
||||
Port 12345
|
||||
PreferredAuthentications=password
|
||||
PubkeyAuthentication=no
|
||||
|
||||
Good luck!
|
||||
|
||||
`
|
||||
|
||||
var ServerAddFailMsg = `
|
||||
Failed to add server %s.
|
||||
|
||||
This could be caused by two things.
|
||||
|
||||
Abra isn't picking up your SSH configuration or you need to specify it on the
|
||||
command-line (e.g you use a non-standard port or username to connect). Run
|
||||
"server add" with "-d/--debug" to learn more about what Abra is doing under the
|
||||
hood.
|
||||
|
||||
Docker is not installed on your server. You can pass "-p/--provision" to
|
||||
install Docker and initialise Docker Swarm mode. See help output for "server
|
||||
add"
|
||||
|
||||
See "abra server add -h" for more.
|
||||
|
||||
`
|
||||
|
||||
// SubCommandBefore wires up pre-action machinery (e.g. --debug handling).
|
||||
func SubCommandBefore(c *cli.Context) error {
|
||||
if Debug {
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/dns"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
@ -17,12 +16,13 @@ import (
|
||||
"coopcloud.tech/abra/pkg/recipe"
|
||||
"coopcloud.tech/abra/pkg/upstream/stack"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// DeployAction is the main command-line action for this package
|
||||
func DeployAction(c *cli.Context) error {
|
||||
func DeployAction(c *cli.Context, cl *dockerClient.Client) error {
|
||||
app := ValidateApp(c)
|
||||
|
||||
if !Chaos {
|
||||
@ -40,11 +40,6 @@ func DeployAction(c *cli.Context) error {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
cl, err := client.New(app.Server)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Debugf("checking whether %s is already deployed", app.StackName())
|
||||
|
||||
isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, app.StackName())
|
||||
|
@ -5,15 +5,15 @@ import (
|
||||
"path"
|
||||
|
||||
"coopcloud.tech/abra/pkg/app"
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/context"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"coopcloud.tech/abra/pkg/jsontable"
|
||||
"coopcloud.tech/abra/pkg/recipe"
|
||||
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
||||
"coopcloud.tech/abra/pkg/secret"
|
||||
"coopcloud.tech/abra/pkg/ssh"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
dockerClient "github.com/docker/docker/client"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
@ -25,7 +25,7 @@ type AppSecrets map[string]string
|
||||
var RecipeName string
|
||||
|
||||
// createSecrets creates all secrets for a new app.
|
||||
func createSecrets(sanitisedAppName string) (AppSecrets, error) {
|
||||
func createSecrets(cl *dockerClient.Client, sanitisedAppName string) (AppSecrets, error) {
|
||||
appEnvPath := path.Join(config.ABRA_DIR, "servers", NewAppServer, fmt.Sprintf("%s.env", Domain))
|
||||
appEnv, err := config.ReadEnv(appEnvPath)
|
||||
if err != nil {
|
||||
@ -33,7 +33,7 @@ func createSecrets(sanitisedAppName string) (AppSecrets, error) {
|
||||
}
|
||||
|
||||
secretEnvVars := secret.ReadSecretEnvVars(appEnv)
|
||||
secrets, err := secret.GenerateSecrets(secretEnvVars, sanitisedAppName, NewAppServer)
|
||||
secrets, err := secret.GenerateSecrets(cl, secretEnvVars, sanitisedAppName, NewAppServer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -144,19 +144,15 @@ func NewAction(c *cli.Context) error {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
cl, err := client.New(NewAppServer)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
var secrets AppSecrets
|
||||
var secretTable *jsontable.JSONTable
|
||||
if Secrets {
|
||||
if err := context.HasDockerContext(sanitisedAppName, NewAppServer); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ssh.EnsureHostKey(NewAppServer); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
var err error
|
||||
secrets, err = createSecrets(sanitisedAppName)
|
||||
secrets, err := createSecrets(cl, sanitisedAppName)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ import (
|
||||
|
||||
"coopcloud.tech/abra/pkg/app"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/context"
|
||||
"coopcloud.tech/abra/pkg/recipe"
|
||||
"coopcloud.tech/abra/pkg/ssh"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
@ -142,14 +140,6 @@ func ValidateApp(c *cli.Context) config.App {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := context.HasDockerContext(app.Name, app.Server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ssh.EnsureHostKey(app.Server); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Debugf("validated %s as app argument", appName)
|
||||
|
||||
return app
|
||||
|
@ -1,14 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
@ -16,34 +11,11 @@ import (
|
||||
contextPkg "coopcloud.tech/abra/pkg/context"
|
||||
"coopcloud.tech/abra/pkg/dns"
|
||||
"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"
|
||||
sshPkg "coopcloud.tech/abra/pkg/ssh"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
dockerInstallMsg = `
|
||||
A docker installation cannot be found on %s. This is a required system
|
||||
dependency for running Co-op Cloud apps 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, l",
|
||||
@ -51,35 +23,15 @@ var localFlag = &cli.BoolFlag{
|
||||
Destination: &local,
|
||||
}
|
||||
|
||||
var provision bool
|
||||
var provisionFlag = &cli.BoolFlag{
|
||||
Name: "provision, p",
|
||||
Usage: "Provision server so it can deploy apps",
|
||||
Destination: &provision,
|
||||
}
|
||||
|
||||
var sshAuth string
|
||||
var sshAuthFlag = &cli.StringFlag{
|
||||
Name: "ssh-auth, s",
|
||||
Value: "identity-file",
|
||||
Usage: "Select SSH authentication method (identity-file, password)",
|
||||
Destination: &sshAuth,
|
||||
}
|
||||
|
||||
var askSudoPass bool
|
||||
var askSudoPassFlag = &cli.BoolFlag{
|
||||
Name: "ask-sudo-pass, a",
|
||||
Usage: "Ask for sudo password",
|
||||
Destination: &askSudoPass,
|
||||
}
|
||||
|
||||
func cleanUp(domainName string) {
|
||||
logrus.Warnf("cleaning up context for %s", domainName)
|
||||
if err := client.DeleteContext(domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
if domainName != "default" {
|
||||
logrus.Infof("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)
|
||||
logrus.Infof("attempting to clean up server directory for %s", domainName)
|
||||
|
||||
serverDir := filepath.Join(config.SERVERS_DIR, domainName)
|
||||
files, err := config.GetAllFilesInDirectory(serverDir)
|
||||
@ -97,72 +49,10 @@ func cleanUp(domainName string) {
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
for _, exe := range []string{"wget", "bash"} {
|
||||
exists, err := ensureLocalExecutable(exe)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("%s missing, please install it", exe)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command("bash", "-c", "wget -O- 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
|
||||
}
|
||||
|
||||
cl, err := newClient(c, domainName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if provision {
|
||||
exists, err := ensureLocalExecutable("docker")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
if err := installDockerLocal(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := initSwarmLocal(c, cl, domainName); err != nil {
|
||||
if !strings.Contains(err.Error(), "proxy already exists") {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Info("local server has been added")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// newContext creates a new internal Docker context for a server. This is how
|
||||
// Docker manages SSH connection details. These are stored to disk in
|
||||
// ~/.docker. Abra can manage this completely for the user, so it's an
|
||||
// implementation detail.
|
||||
func newContext(c *cli.Context, domainName, username, port string) error {
|
||||
store := contextPkg.NewDefaultDockerContextStore()
|
||||
contexts, err := store.Store.List()
|
||||
@ -186,187 +76,7 @@ func newContext(c *cli.Context, domainName, username, port string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func newClient(c *cli.Context, domainName string) (*dockerClient.Client, error) {
|
||||
cl, err := client.New(domainName)
|
||||
if err != nil {
|
||||
return &dockerClient.Client{}, err
|
||||
}
|
||||
return cl, nil
|
||||
}
|
||||
|
||||
func installDocker(c *cli.Context, cl *dockerClient.Client, sshCl *ssh.Client, domainName string) error {
|
||||
exists, err := ensureRemoteExecutable("docker", sshCl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
fmt.Println(fmt.Sprintf(dockerInstallMsg, domainName))
|
||||
|
||||
response := false
|
||||
prompt := &survey.Confirm{
|
||||
Message: fmt.Sprintf("attempt install docker on %s?", domainName),
|
||||
}
|
||||
|
||||
if err := survey.AskOne(prompt, &response); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !response {
|
||||
logrus.Fatal("exiting as requested")
|
||||
}
|
||||
|
||||
exes := []string{"wget", "bash"}
|
||||
if askSudoPass {
|
||||
exes = append(exes, "ssh-askpass")
|
||||
}
|
||||
|
||||
for _, exe := range exes {
|
||||
exists, err := ensureRemoteExecutable(exe, sshCl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("%s missing on remote, please install it", exe)
|
||||
}
|
||||
}
|
||||
|
||||
var sudoPass string
|
||||
if askSudoPass {
|
||||
cmd := "wget -O- https://get.docker.com | bash"
|
||||
|
||||
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)
|
||||
|
||||
if sudoPass == "" {
|
||||
return fmt.Errorf("missing sudo password but requested --ask-sudo-pass?")
|
||||
}
|
||||
|
||||
logrus.Warn("installing docker, this could take some time...")
|
||||
|
||||
if err := ssh.RunSudoCmd(cmd, sudoPass, sshCl); err != nil {
|
||||
fmt.Print(fmt.Sprintf(`
|
||||
Abra was unable to bootstrap Docker, see below for logs:
|
||||
|
||||
|
||||
%s
|
||||
|
||||
If nothing works, you can try running the Docker install script manually on your server:
|
||||
|
||||
wget -O- https://get.docker.com | bash
|
||||
|
||||
`, string(err.Error())))
|
||||
logrus.Fatal("Process exited with status 1")
|
||||
}
|
||||
|
||||
logrus.Infof("docker is installed on %s", domainName)
|
||||
|
||||
remoteUser := sshCl.SSHClient.Conn.User()
|
||||
logrus.Infof("adding %s to docker group", remoteUser)
|
||||
permsCmd := fmt.Sprintf("sudo usermod -aG docker %s", remoteUser)
|
||||
if err := ssh.RunSudoCmd(permsCmd, sudoPass, sshCl); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
cmd := "wget -O- https://get.docker.com | bash"
|
||||
|
||||
logrus.Debugf("running %s on %s now without sudo password", cmd, domainName)
|
||||
|
||||
logrus.Warn("installing docker, this could take some time...")
|
||||
|
||||
if out, err := sshCl.Exec(cmd); err != nil {
|
||||
fmt.Print(fmt.Sprintf(`
|
||||
Abra was unable to bootstrap Docker, see below for logs:
|
||||
|
||||
|
||||
%s
|
||||
|
||||
This could be due to several reasons. One of the most common is that your
|
||||
server user account does not have sudo access, and if it does, you need to pass
|
||||
"--ask-sudo-pass" in order to supply Abra with your password.
|
||||
|
||||
If nothing works, you try running the Docker install script manually on your server:
|
||||
|
||||
wget -O- https://get.docker.com | bash
|
||||
|
||||
`, string(out)))
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Infof("docker is installed on %s", domainName)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initSwarmLocal(c *cli.Context, cl *dockerClient.Client, domainName string) error {
|
||||
initReq := swarm.InitRequest{ListenAddr: "0.0.0.0:2377"}
|
||||
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)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
logrus.Infof("initialised swarm mode on local server")
|
||||
}
|
||||
|
||||
netOpts := types.NetworkCreate{Driver: "overlay", Scope: "swarm"}
|
||||
if _, err := cl.NetworkCreate(context.Background(), "proxy", netOpts); err != nil {
|
||||
if !strings.Contains(err.Error(), "proxy already exists") {
|
||||
return err
|
||||
}
|
||||
logrus.Info("swarm overlay network already created on local server")
|
||||
} else {
|
||||
logrus.Infof("swarm overlay network created on local server")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initSwarm(c *cli.Context, cl *dockerClient.Client, domainName string) error {
|
||||
ipv4, err := dns.EnsureIPv4(domainName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
initReq := swarm.InitRequest{
|
||||
ListenAddr: "0.0.0.0:2377",
|
||||
AdvertiseAddr: ipv4,
|
||||
}
|
||||
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)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
logrus.Infof("initialised swarm mode on %s", domainName)
|
||||
}
|
||||
|
||||
netOpts := types.NetworkCreate{Driver: "overlay", Scope: "swarm"}
|
||||
if _, err := cl.NetworkCreate(context.Background(), "proxy", netOpts); err != nil {
|
||||
if !strings.Contains(err.Error(), "proxy already exists") {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("swarm overlay network already created on %s", domainName)
|
||||
} else {
|
||||
logrus.Infof("swarm overlay network created on %s", domainName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// createServerDir creates the ~/.abra/servers/... directory for a new server.
|
||||
func createServerDir(domainName string) error {
|
||||
if err := server.CreateServerDir(domainName); err != nil {
|
||||
if !os.IsExist(err) {
|
||||
@ -374,6 +84,7 @@ func createServerDir(domainName string) error {
|
||||
}
|
||||
logrus.Debugf("server dir for %s already created", domainName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -382,34 +93,21 @@ var serverAddCommand = cli.Command{
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Add a server to your configuration",
|
||||
Description: `
|
||||
Add a new server to your configuration so that it can be managed by Abra. This
|
||||
command can also provision your server ("--provision/-p") with a Docker
|
||||
installation so that it is capable of hosting Co-op Cloud apps.
|
||||
Add a new server to your configuration so that it can be managed by Abra.
|
||||
|
||||
Abra will default to expecting that you have a running ssh-agent and are using
|
||||
SSH keys to connect to your new server. Abra will also read your SSH config
|
||||
(matching "Host" as <domain>). SSH connection details precedence follows as
|
||||
such: command-line > SSH config > guessed defaults.
|
||||
Abra uses the SSH command-line to discover connection details for your server.
|
||||
It is advised to configure an entry per-host in your ~/.ssh/config for each
|
||||
server. For example:
|
||||
|
||||
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.
|
||||
Host example.com
|
||||
Hostname example.com
|
||||
User exampleUser
|
||||
Port 12345
|
||||
IdentityFile ~/.ssh/example@somewhere
|
||||
|
||||
The <domain> argument must be a publicy accessible domain name which points to
|
||||
your server. You should have working SSH access to this server already, 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.
|
||||
Abra can then load SSH connection details from this configuratiion with:
|
||||
|
||||
Example:
|
||||
|
||||
abra server add varia.zone glodemodem 12345 -p
|
||||
|
||||
Abra will construct the following SSH connection and Docker context:
|
||||
|
||||
ssh://globemodem@varia.zone:12345
|
||||
|
||||
All communication between Abra and the server will use this SSH connection.
|
||||
abra server add example.com
|
||||
|
||||
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
|
||||
@ -420,104 +118,64 @@ developer machine.
|
||||
internal.DebugFlag,
|
||||
internal.NoInputFlag,
|
||||
localFlag,
|
||||
provisionFlag,
|
||||
sshAuthFlag,
|
||||
askSudoPassFlag,
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
ArgsUsage: "<domain> [<user>] [<port>]",
|
||||
ArgsUsage: "<domain>",
|
||||
Action: func(c *cli.Context) error {
|
||||
if len(c.Args()) > 0 && local || !internal.ValidateSubCmdFlags(c) {
|
||||
err := errors.New("cannot use <domain> and --local together")
|
||||
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)
|
||||
var domainName string
|
||||
if local {
|
||||
domainName = "default"
|
||||
} else {
|
||||
domainName = internal.ValidateDomain(c)
|
||||
}
|
||||
|
||||
domainName := internal.ValidateDomain(c)
|
||||
|
||||
if local {
|
||||
if err := newLocalServer(c, "default"); err != nil {
|
||||
if err := createServerDir(domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Infof("attempting to create client for %s", domainName)
|
||||
if _, err := client.New(domainName); err != nil {
|
||||
cleanUp(domainName)
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Info("local server added")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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 := dns.EnsureIPv4(domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := createServerDir(domainName); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := newContext(c, domainName, username, port); err != nil {
|
||||
hostConfig, err := sshPkg.GetHostConfig(domainName)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
cl, err := newClient(c, domainName)
|
||||
if err != nil {
|
||||
if err := newContext(c, domainName, hostConfig.User, hostConfig.Port); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
logrus.Infof("attempting to create client for %s", domainName)
|
||||
if _, err := client.New(domainName); err != nil {
|
||||
cleanUp(domainName)
|
||||
logrus.Debugf("failed to construct client for %s, saw %s", domainName, err.Error())
|
||||
logrus.Fatalf(fmt.Sprintf(internal.ServerAddFailMsg, domainName))
|
||||
logrus.Fatal(sshPkg.Fatal(domainName, err))
|
||||
}
|
||||
|
||||
if provision {
|
||||
logrus.Debugf("attempting to construct SSH client for %s", domainName)
|
||||
sshCl, err := ssh.New(domainName, sshAuth, username, port)
|
||||
if err != nil {
|
||||
cleanUp(domainName)
|
||||
logrus.Fatalf(fmt.Sprintf(internal.ServerAddFailMsg, domainName))
|
||||
}
|
||||
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 {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := cl.Info(context.Background()); err != nil {
|
||||
cleanUp(domainName)
|
||||
logrus.Fatalf(fmt.Sprintf(internal.ServerAddFailMsg, domainName))
|
||||
}
|
||||
logrus.Infof("%s added", domainName)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// ensureLocalExecutable ensures that an executable is present on the local machine
|
||||
func ensureLocalExecutable(exe string) (bool, error) {
|
||||
out, err := exec.Command("which", exe).Output()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return string(out) != "", nil
|
||||
}
|
||||
|
||||
// ensureRemoteExecutable ensures that an executable is present on a remote machine
|
||||
func ensureRemoteExecutable(exe string, sshCl *ssh.Client) (bool, error) {
|
||||
out, err := sshCl.Exec(fmt.Sprintf("which %s", exe))
|
||||
if err != nil && string(out) != "" {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return string(out) != "", nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user