refactor: NewClientWithContext -> New, and use server only
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
decentral1se 2021-09-05 00:41:31 +02:00
parent dac679db48
commit 07a43cb314
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
14 changed files with 55 additions and 54 deletions

View File

@ -76,8 +76,8 @@ var appCpCommand = &cli.Command{
}
ctx := context.Background()
host := appFiles[appName].Server
cl, err := client.NewClientWithContext(host)
server := appFiles[appName].Server
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -31,8 +31,8 @@ var appDeployCommand = &cli.Command{
logrus.Fatal(err)
}
host := appFiles[appName].Server
cl, err := client.NewClientWithContext(host)
server := appFiles[appName].Server
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -79,8 +79,8 @@ var appLogsCommand = &cli.Command{
}
ctx := context.Background()
host := appFiles[appName].Server
cl, err := client.NewClientWithContext(host)
server := appFiles[appName].Server
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -29,9 +29,9 @@ var appPsCommand = &cli.Command{
logrus.Fatal(err)
}
host := appFiles[appName].Server
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -59,9 +59,9 @@ var appRemoveCommand = &cli.Command{
}
appPath := appFiles[appName].Path
host := appFiles[appName].Server
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -53,9 +53,9 @@ var appRunCommand = &cli.Command{
logrus.Fatal(err)
}
host := appFiles[appName].Server
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -51,7 +51,7 @@ var appSecretGenerateCommand = &cli.Command{
}
secretsToCreate := make(map[string]string)
host := appFiles[appName].Server
server := appFiles[appName].Server
secretEnvVars := secret.ReadSecretEnvVars(appEnv.Env)
if allSecrets {
secretsToCreate = secretEnvVars
@ -66,14 +66,14 @@ var appSecretGenerateCommand = &cli.Command{
}
}
secretVals, err := secret.GenerateSecrets(secretsToCreate, appEnv.StackName(), host)
secretVals, err := secret.GenerateSecrets(secretsToCreate, appEnv.StackName(), server)
if err != nil {
logrus.Fatal(err)
}
if internal.Pass {
for name, data := range secretVals {
if err := secret.PassInsertSecret(data, name, appEnv.StackName(), host); err != nil {
if err := secret.PassInsertSecret(data, name, appEnv.StackName(), server); err != nil {
logrus.Fatal(err)
}
}
@ -112,14 +112,14 @@ var appSecretInsertCommand = &cli.Command{
logrus.Fatal(err)
}
host := appFiles[appName].Server
server := appFiles[appName].Server
secretName := fmt.Sprintf("%s_%s_%s", appEnv.StackName(), name, version)
if err := client.StoreSecret(secretName, data, host); err != nil {
if err := client.StoreSecret(secretName, data, server); err != nil {
logrus.Fatal(err)
}
if internal.Pass {
if err := secret.PassInsertSecret(data, name, appEnv.StackName(), host); err != nil {
if err := secret.PassInsertSecret(data, name, appEnv.StackName(), server); err != nil {
logrus.Fatal(err)
}
}
@ -158,9 +158,9 @@ var appSecretRmCommand = &cli.Command{
logrus.Fatal(err)
}
host := appFiles[appName].Server
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}
@ -181,7 +181,7 @@ var appSecretRmCommand = &cli.Command{
logrus.Fatal(err)
}
if internal.Pass {
if err := secret.PassRmSecret(parsed, appEnv.StackName(), host); err != nil {
if err := secret.PassRmSecret(parsed, appEnv.StackName(), server); err != nil {
logrus.Fatal(err)
}
}
@ -191,7 +191,7 @@ var appSecretRmCommand = &cli.Command{
logrus.Fatal(err)
}
if internal.Pass {
if err := secret.PassRmSecret(parsed, appEnv.StackName(), host); err != nil {
if err := secret.PassRmSecret(parsed, appEnv.StackName(), server); err != nil {
logrus.Fatal(err)
}
}
@ -227,9 +227,9 @@ var appSecretLsCommand = &cli.Command{
tableCol := []string{"Name", "Version", "Generated Name", "Created On Server"}
table := abraFormatter.CreateTable(tableCol)
host := appFiles[appName].Server
server := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
cl, err := client.New(server)
if err != nil {
logrus.Fatal(err)
}

View File

@ -33,7 +33,7 @@ var appUndeployCommand = &cli.Command{
ctx := context.Background()
host := appFiles[appName].Server
cl, err := client.NewClientWithContext(host)
cl, err := client.New(host)
if err != nil {
logrus.Fatal(err)
}

View File

@ -13,19 +13,20 @@ import (
"github.com/urfave/cli/v2"
)
func getAppsHost(appName string) string {
// getAppServer retrieves the server of an app.
func getAppServer(appName string) string {
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
var host string
var server string
if app, ok := appFiles[appName]; ok {
host = app.Server
server = app.Server
} else {
logrus.Fatalf(`app "%s" does not exist`, appName)
}
return host
return server
}
var appVolumeListCommand = &cli.Command{
@ -37,10 +38,10 @@ var appVolumeListCommand = &cli.Command{
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
}
host := getAppsHost(appName)
ctx := context.Background()
volumeList, err := client.GetVolumes(ctx, host, appName)
server := getAppServer(appName)
volumeList, err := client.GetVolumes(ctx, server, appName)
if err != nil {
logrus.Fatal(err)
}
@ -71,9 +72,9 @@ var appVolumeRemoveCommand = &cli.Command{
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided!"))
}
host := getAppsHost(appName)
server := getAppServer(appName)
ctx := context.Background()
volumeList, err := client.GetVolumes(ctx, host, appName)
volumeList, err := client.GetVolumes(ctx, server, appName)
if err != nil {
logrus.Fatal(err)
}
@ -93,7 +94,7 @@ var appVolumeRemoveCommand = &cli.Command{
volumesToRemove = volumeNames
}
err = client.RemoveVolumes(ctx, host, volumesToRemove, internal.Force)
err = client.RemoveVolumes(ctx, server, volumesToRemove, internal.Force)
if err != nil {
logrus.Fatal(err)
}

View File

@ -19,21 +19,21 @@ var serverInitCommand = &cli.Command{
Name: "init",
Usage: "Initialise server for deploying apps",
HideHelp: true,
ArgsUsage: "<host>",
ArgsUsage: "<server>",
Description: `
Initialise swarm mode on the target <host>.
Initialise swarm mode on the target <server>.
This initialisation explicitly chooses the "single host swarm" mode which uses
the default IPv4 address as the advertising address. This can be re-configured
later for more advanced use cases.
`,
Action: func(c *cli.Context) error {
host := c.Args().First()
if host == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no host provided"))
server := c.Args().First()
if server == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no server provided"))
}
cl, err := client.NewClientWithContext(host)
cl, err := client.New(server)
if err != nil {
return err
}
@ -50,13 +50,13 @@ later for more advanced use cases.
}
ctx := context.Background()
ips, err := resolver.LookupIPAddr(ctx, host)
ips, err := resolver.LookupIPAddr(ctx, server)
if err != nil {
logrus.Fatal(err)
}
if len(ips) == 0 {
return fmt.Errorf("unable to retrieve ipv4 address for %s", host)
return fmt.Errorf("unable to retrieve ipv4 address for %s", server)
}
ipv4 := ips[0].IP.To4().String()

View File

@ -1,3 +1,4 @@
// Package client provides Docker client initiatialisation functions.
package client
import (
@ -8,35 +9,34 @@ import (
"github.com/sirupsen/logrus"
)
func NewClientWithContext(contextName string) (*client.Client, error) {
// New initiates a new Docker client.
func New(contextName string) (*client.Client, error) {
context, err := GetContext(contextName)
if err != nil {
return nil, err
}
ctxEndpoint, err := GetContextEndpoint(context)
if err != nil {
return nil, err
}
helper := newConnectionHelper(ctxEndpoint)
httpClient := &http.Client{
// No tls
// No proxy
// No tls, no proxy
Transport: &http.Transport{
DialContext: helper.Dialer,
},
}
var clientOpts []client.Opt
clientOpts = append(clientOpts,
client.WithHTTPClient(httpClient),
client.WithHost(helper.Host),
client.WithDialContext(helper.Dialer),
)
// FIXME: Maybe don't have this variable here and load it beforehand
version := os.Getenv("DOCKER_API_VERSION")
if version != "" {
clientOpts = append(clientOpts, client.WithVersion(version))
} else {
@ -44,9 +44,9 @@ func NewClientWithContext(contextName string) (*client.Client, error) {
}
cl, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
logrus.Fatalf("unable to create Docker client: %s", err)
}
return cl, nil
}

View File

@ -7,7 +7,7 @@ import (
)
func StoreSecret(secretName, secretValue, server string) error {
cl, err := NewClientWithContext(server)
cl, err := New(server)
if err != nil {
return err
}

View File

@ -53,7 +53,7 @@ func getStackServices(ctx context.Context, dockerclient client.APIClient, namesp
// GetDeployedServicesByLabel filters services by label
func GetDeployedServicesByLabel(contextName string, label string) StackStatus {
cl, err := abraClient.NewClientWithContext(contextName)
cl, err := abraClient.New(contextName)
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
// No local context found, bail out gracefully
@ -74,7 +74,7 @@ func GetDeployedServicesByLabel(contextName string, label string) StackStatus {
}
func GetAllDeployedServices(contextName string) StackStatus {
cl, err := abraClient.NewClientWithContext(contextName)
cl, err := abraClient.New(contextName)
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
// No local context found, bail out gracefully

View File

@ -10,7 +10,7 @@ import (
func GetVolumes(ctx context.Context, server string, appName string) ([]*types.Volume, error) {
cl, err := NewClientWithContext(server)
cl, err := New(server)
if err != nil {
return nil, err
}
@ -36,7 +36,7 @@ func GetVolumeNames(volumes []*types.Volume) []string {
}
func RemoveVolumes(ctx context.Context, server string, volumeNames []string, force bool) error {
cl, err := NewClientWithContext(server)
cl, err := New(server)
if err != nil {
return err
}