fix: more robust docker context problem handling

See coop-cloud/organising#325
See coop-cloud/organising#340
This commit is contained in:
decentral1se 2023-01-23 14:56:34 +01:00
parent 27e0708ac7
commit b089109c94
Signed by untrusted user: decentral1se
GPG Key ID: 03789458B3D0C410
6 changed files with 71 additions and 1 deletions

View File

@ -8,6 +8,7 @@ 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"
@ -97,6 +98,10 @@ can take some time.
alreadySeen := make(map[string]bool)
for _, app := range apps {
if _, ok := alreadySeen[app.Server]; !ok {
if err := context.HasDockerContext(app.Server); err != nil {
logrus.Fatal(err)
}
if err := ssh.EnsureHostKey(app.Server); err != nil {
logrus.Fatal(fmt.Sprintf(internal.SSHFailMsg, app.Server))
}

View File

@ -6,6 +6,7 @@ import (
"coopcloud.tech/abra/pkg/app"
"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"
@ -146,6 +147,10 @@ func NewAction(c *cli.Context) error {
var secrets AppSecrets
var secretTable *jsontable.JSONTable
if Secrets {
if err := context.HasDockerContext(NewAppServer); err != nil {
logrus.Fatal(err)
}
if err := ssh.EnsureHostKey(NewAppServer); err != nil {
logrus.Fatal(err)
}

View File

@ -8,6 +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"
@ -138,6 +139,10 @@ func ValidateApp(c *cli.Context) config.App {
logrus.Fatal(err)
}
if err := context.HasDockerContext(app.Server); err != nil {
logrus.Fatal(err)
}
if err := ssh.EnsureHostKey(app.Server); err != nil {
logrus.Fatal(err)
}

View File

@ -12,11 +12,20 @@ import (
"github.com/urfave/cli"
)
var problemsFilter bool
var problemsFilterFlag = &cli.BoolFlag{
Name: "problems, p",
Usage: "Show only servers with potential connection problems",
Destination: &problemsFilter,
}
var serverListCommand = cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "List managed servers",
Flags: []cli.Flag{
problemsFilterFlag,
internal.DebugFlag,
internal.MachineReadableFlag,
},
@ -48,6 +57,7 @@ var serverListCommand = cli.Command{
// No local context found, we can continue safely
continue
}
if ctx.Name == serverName {
sp, err := ssh.ParseURL(endpoint)
if err != nil {
@ -56,6 +66,7 @@ var serverListCommand = cli.Command{
row = []string{serverName, sp.Host, sp.User, sp.Port}
}
}
if len(row) == 0 {
if serverName == "default" {
row = []string{serverName, "local", "n/a", "n/a"}
@ -63,7 +74,14 @@ var serverListCommand = cli.Command{
row = []string{serverName, "unknown", "unknown", "unknown"}
}
}
table.Append(row)
if problemsFilter {
if row[1] == "unknown" {
table.Append(row)
}
} else {
table.Append(row)
}
}
return nil

View File

@ -2,6 +2,7 @@ package context
import (
"errors"
"fmt"
"github.com/docker/cli/cli/command"
dConfig "github.com/docker/cli/cli/config"
@ -42,3 +43,34 @@ func GetContextEndpoint(ctx contextStore.Metadata) (string, error) {
func newContextStore(dir string, config contextStore.Config) contextStore.Store {
return contextStore.New(dir, config)
}
// MissingContextMsg helps end-uers debug missing docker context issues.
var missingContextMsg = `unable to find Docker context for %s?
Please run "abra server ls" to confirm. If you see "unknown" in the table
output then you need to run the following command:
abra server add %s <args>
See "abra server add --help" for more.
`
// HasDockerContext figures out if a local setup has a working docker context
// configuration or not. This usually tells us if they'll be able to make a SSH
// connection to a server or not and can be a useful way to signal to end-users
// that they need to fix something up if missing.
func HasDockerContext(serverName string) error {
dockerContextStore := NewDefaultDockerContextStore()
contexts, err := dockerContextStore.Store.List()
if err != nil {
return err
}
for _, ctx := range contexts {
if ctx.Name == serverName {
return nil
}
}
return fmt.Errorf(missingContextMsg, serverName, serverName)
}

View File

@ -6,6 +6,7 @@ import (
"net"
"net/url"
ctxPkg "coopcloud.tech/abra/pkg/context"
sshPkg "coopcloud.tech/abra/pkg/ssh"
"github.com/docker/cli/cli/connhelper"
"github.com/docker/cli/cli/connhelper/ssh"
@ -36,6 +37,10 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne
return nil, errors.Wrap(err, "ssh host connection is not valid")
}
if err := ctxPkg.HasDockerContext(ctxConnDetails.Host); err != nil {
return nil, err
}
if err := sshPkg.EnsureHostKey(ctxConnDetails.Host); err != nil {
return nil, err
}