forked from toolshed/abra
refactor!: cobra migrate
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/pkg/autocomplete"
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
contextPkg "coopcloud.tech/abra/pkg/context"
|
||||
@ -14,15 +13,109 @@ import (
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"coopcloud.tech/abra/pkg/server"
|
||||
sshPkg "coopcloud.tech/abra/pkg/ssh"
|
||||
"github.com/urfave/cli/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var local bool
|
||||
var localFlag = &cli.BoolFlag{
|
||||
Name: "local",
|
||||
Aliases: []string{"l"},
|
||||
Usage: "Use local server",
|
||||
Destination: &local,
|
||||
var ServerAddCommand = &cobra.Command{
|
||||
Use: "add [[server] | --local] [flags]",
|
||||
Aliases: []string{"a"},
|
||||
Short: "Add a new server",
|
||||
Long: `Add a new server to your configuration so that it can be managed by Abra.
|
||||
|
||||
Abra relies on the standard SSH command-line and ~/.ssh/config for client
|
||||
connection details. You must configure an entry per-host in your ~/.ssh/config
|
||||
for each server:
|
||||
|
||||
Host 1312.net 1312
|
||||
Hostname 1312.net
|
||||
User antifa
|
||||
Port 12345
|
||||
IdentityFile ~/.ssh/antifa@somewhere
|
||||
|
||||
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
|
||||
Co-op Cloud config located on the server itself, and not on your local
|
||||
developer machine. The domain is then set to "default".`,
|
||||
Example: " abra server add 1312.net",
|
||||
Args: cobra.RangeArgs(0, 1),
|
||||
ValidArgsFunction: func(
|
||||
cmd *cobra.Command,
|
||||
args []string,
|
||||
toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
if !local {
|
||||
return autocomplete.ServerNameComplete()
|
||||
}
|
||||
return nil, cobra.ShellCompDirectiveDefault
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) > 0 && local {
|
||||
log.Fatal("cannot use [server] and --local together")
|
||||
}
|
||||
|
||||
if len(args) == 0 && !local {
|
||||
log.Fatal("missing argument or --local/-l flag")
|
||||
}
|
||||
|
||||
name := "default"
|
||||
if !local {
|
||||
name = internal.ValidateDomain(args)
|
||||
}
|
||||
|
||||
// NOTE(d1): reasonable 5 second timeout for connections which can't
|
||||
// succeed. The connection is attempted twice, so this results in 10
|
||||
// seconds.
|
||||
timeout := client.WithTimeout(5)
|
||||
|
||||
if local {
|
||||
created, err := createServerDir(name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Debugf("attempting to create client for %s", name)
|
||||
|
||||
if _, err := client.New(name, timeout); err != nil {
|
||||
cleanUp(name)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if created {
|
||||
log.Info("local server successfully added")
|
||||
} else {
|
||||
log.Warn("local server already exists")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := dns.EnsureIPv4(name); err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
|
||||
_, err := createServerDir(name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
created, err := newContext(name)
|
||||
if err != nil {
|
||||
cleanUp(name)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Debugf("attempting to create client for %s", name)
|
||||
|
||||
if _, err := client.New(name, timeout); err != nil {
|
||||
cleanUp(name)
|
||||
log.Fatal(sshPkg.Fatal(name, err))
|
||||
}
|
||||
|
||||
if created {
|
||||
log.Infof("%s successfully added", name)
|
||||
} else {
|
||||
log.Warnf("%s already exists", name)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// cleanUp cleans up the partially created context/client details for a failed
|
||||
@ -93,101 +186,16 @@ func createServerDir(name string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
var serverAddCommand = cli.Command{
|
||||
Name: "add",
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Add a new server",
|
||||
UsageText: "abra server add <domain> [options]",
|
||||
Description: `Add a new server to your configuration so that it can be managed by Abra.
|
||||
var (
|
||||
local bool
|
||||
)
|
||||
|
||||
Abra relies on the standard SSH command-line and ~/.ssh/config for client
|
||||
connection details. You must configure an entry per-host in your ~/.ssh/config
|
||||
for each server:
|
||||
|
||||
Host example.com example
|
||||
Hostname example.com
|
||||
User exampleUser
|
||||
Port 12345
|
||||
IdentityFile ~/.ssh/example@somewhere
|
||||
|
||||
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
|
||||
Co-op Cloud config located on the server itself, and not on your local
|
||||
developer machine. The domain is then set to "default".`,
|
||||
Flags: []cli.Flag{
|
||||
internal.DebugFlag,
|
||||
internal.NoInputFlag,
|
||||
localFlag,
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
if cmd.Args().Len() > 0 && local || !internal.ValidateSubCmdFlags(cmd) {
|
||||
err := errors.New("cannot use <name> and --local together")
|
||||
internal.ShowSubcommandHelpAndError(cmd, err)
|
||||
}
|
||||
|
||||
var name string
|
||||
if local {
|
||||
name = "default"
|
||||
} else {
|
||||
name = internal.ValidateDomain(cmd)
|
||||
}
|
||||
|
||||
// NOTE(d1): reasonable 5 second timeout for connections which can't
|
||||
// succeed. The connection is attempted twice, so this results in 10
|
||||
// seconds.
|
||||
timeout := client.WithTimeout(5)
|
||||
|
||||
if local {
|
||||
created, err := createServerDir(name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Debugf("attempting to create client for %s", name)
|
||||
|
||||
if _, err := client.New(name, timeout); err != nil {
|
||||
cleanUp(name)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if created {
|
||||
log.Info("local server successfully added")
|
||||
} else {
|
||||
log.Warn("local server already exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := dns.EnsureIPv4(name); err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
|
||||
_, err := createServerDir(name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
created, err := newContext(name)
|
||||
if err != nil {
|
||||
cleanUp(name)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Debugf("attempting to create client for %s", name)
|
||||
if _, err := client.New(name, timeout); err != nil {
|
||||
cleanUp(name)
|
||||
log.Fatal(sshPkg.Fatal(name, err))
|
||||
}
|
||||
|
||||
if created {
|
||||
log.Infof("%s successfully added", name)
|
||||
} else {
|
||||
log.Warnf("%s already exists", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
func init() {
|
||||
ServerAddCommand.Flags().BoolVarP(
|
||||
&local,
|
||||
"local",
|
||||
"l",
|
||||
false,
|
||||
"use local server",
|
||||
)
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@ -11,20 +10,15 @@ import (
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"github.com/docker/cli/cli/connhelper/ssh"
|
||||
"github.com/urfave/cli/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var serverListCommand = cli.Command{
|
||||
Name: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Usage: "List managed servers",
|
||||
UsageText: "abra server list [options]",
|
||||
Flags: []cli.Flag{
|
||||
internal.MachineReadableFlag,
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
var ServerListCommand = &cobra.Command{
|
||||
Use: "list [flags]",
|
||||
Aliases: []string{"ls"},
|
||||
Short: "List managed servers",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
dockerContextStore := contextPkg.NewDefaultDockerContextStore()
|
||||
contexts, err := dockerContextStore.Store.List()
|
||||
if err != nil {
|
||||
@ -86,12 +80,22 @@ var serverListCommand = cli.Command{
|
||||
if err != nil {
|
||||
log.Fatal("unable to render to JSON: %s", err)
|
||||
}
|
||||
|
||||
fmt.Println(out)
|
||||
return nil
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(table)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerListCommand.Flags().BoolVarP(
|
||||
&internal.MachineReadable,
|
||||
"machine",
|
||||
"m",
|
||||
false,
|
||||
"print machine-readable output",
|
||||
)
|
||||
}
|
||||
|
@ -1,62 +1,41 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"coopcloud.tech/abra/cli/internal"
|
||||
"coopcloud.tech/abra/pkg/autocomplete"
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/urfave/cli/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var allFilter bool
|
||||
var ServerPruneCommand = &cobra.Command{
|
||||
Use: "prune <server> [flags]",
|
||||
Aliases: []string{"p"},
|
||||
Short: "Prune resources on a server",
|
||||
Long: `Prunes unused containers, networks, and dangling images.
|
||||
|
||||
var allFilterFlag = &cli.BoolFlag{
|
||||
Name: "all",
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Remove all unused images not just dangling ones",
|
||||
Destination: &allFilter,
|
||||
}
|
||||
|
||||
var volumesFilter bool
|
||||
|
||||
var volumesFilterFlag = &cli.BoolFlag{
|
||||
Name: "volumes",
|
||||
Aliases: []string{"v"},
|
||||
Usage: "Prune volumes. This will remove app data, Be Careful!",
|
||||
Destination: &volumesFilter,
|
||||
}
|
||||
|
||||
var serverPruneCommand = cli.Command{
|
||||
Name: "prune",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "Prune resources on a server",
|
||||
UsageText: "abra server prune <server> [options]",
|
||||
Description: `Prunes unused containers, networks, and dangling images.
|
||||
|
||||
Use "-v/--volumes" to remove volumes that are not associated with a deployed
|
||||
Use "--volumes/-v" to remove volumes that are not associated with a deployed
|
||||
app. This can result in unwanted data loss if not used carefully.`,
|
||||
Flags: []cli.Flag{
|
||||
allFilterFlag,
|
||||
volumesFilterFlag,
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: func(
|
||||
cmd *cobra.Command,
|
||||
args []string,
|
||||
toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
return autocomplete.ServerNameComplete()
|
||||
},
|
||||
Before: internal.SubCommandBefore,
|
||||
ShellComplete: autocomplete.ServerNameComplete,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
serverName := internal.ValidateServer(cmd)
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
serverName := internal.ValidateServer(args)
|
||||
|
||||
cl, err := client.New(serverName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var args filters.Args
|
||||
var filterArgs filters.Args
|
||||
|
||||
cr, err := cl.ContainersPrune(ctx, args)
|
||||
cr, err := cl.ContainersPrune(cmd.Context(), filterArgs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -64,7 +43,7 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
cntSpaceReclaimed := formatter.ByteCountSI(cr.SpaceReclaimed)
|
||||
log.Infof("containers pruned: %d; space reclaimed: %s", len(cr.ContainersDeleted), cntSpaceReclaimed)
|
||||
|
||||
nr, err := cl.NetworksPrune(ctx, args)
|
||||
nr, err := cl.NetworksPrune(cmd.Context(), filterArgs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -77,7 +56,7 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
pruneFilters.Add("dangling", "false")
|
||||
}
|
||||
|
||||
ir, err := cl.ImagesPrune(ctx, pruneFilters)
|
||||
ir, err := cl.ImagesPrune(cmd.Context(), pruneFilters)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -86,7 +65,7 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
log.Infof("images pruned: %d; space reclaimed: %s", len(ir.ImagesDeleted), imgSpaceReclaimed)
|
||||
|
||||
if volumesFilter {
|
||||
vr, err := cl.VolumesPrune(ctx, args)
|
||||
vr, err := cl.VolumesPrune(cmd.Context(), filterArgs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -95,6 +74,29 @@ app. This can result in unwanted data loss if not used carefully.`,
|
||||
log.Infof("volumes pruned: %d; space reclaimed: %s", len(vr.VolumesDeleted), volSpaceReclaimed)
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
allFilter bool
|
||||
volumesFilter bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
ServerPruneCommand.Flags().BoolVarP(
|
||||
&allFilter,
|
||||
"all",
|
||||
"a",
|
||||
false,
|
||||
"remove all unused images",
|
||||
)
|
||||
|
||||
ServerPruneCommand.Flags().BoolVarP(
|
||||
&volumesFilter,
|
||||
"volumes",
|
||||
"v",
|
||||
false,
|
||||
"remove volumes",
|
||||
)
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@ -10,24 +9,27 @@ import (
|
||||
"coopcloud.tech/abra/pkg/client"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"github.com/urfave/cli/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var serverRemoveCommand = cli.Command{
|
||||
Name: "remove",
|
||||
Aliases: []string{"rm"},
|
||||
UsageText: "abra server remove <domain> [options]",
|
||||
Usage: "Remove a managed server",
|
||||
Description: `Remove a managed server.
|
||||
var ServerRemoveCommand = &cobra.Command{
|
||||
Use: "remove <server> [flags]",
|
||||
Aliases: []string{"rm"},
|
||||
Short: "Remove a managed server",
|
||||
Long: `Remove a managed server.
|
||||
|
||||
Abra will remove the internal bookkeeping ($ABRA_DIR/servers/...) and
|
||||
underlying client connection context. This server will then be lost in time,
|
||||
like tears in rain.`,
|
||||
Before: internal.SubCommandBefore,
|
||||
ShellComplete: autocomplete.ServerNameComplete,
|
||||
HideHelp: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
serverName := internal.ValidateServer(cmd)
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: func(
|
||||
cmd *cobra.Command,
|
||||
args []string,
|
||||
toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
return autocomplete.ServerNameComplete()
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
serverName := internal.ValidateServer(args)
|
||||
|
||||
if err := client.DeleteContext(serverName); err != nil {
|
||||
log.Fatal(err)
|
||||
@ -39,6 +41,6 @@ like tears in rain.`,
|
||||
|
||||
log.Infof("%s is now lost in time, like tears in rain", serverName)
|
||||
|
||||
return nil
|
||||
return
|
||||
},
|
||||
}
|
||||
|
@ -1,19 +1,10 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
// ServerCommand defines the `abra server` command and its subcommands
|
||||
var ServerCommand = cli.Command{
|
||||
Name: "server",
|
||||
Aliases: []string{"s"},
|
||||
Usage: "Manage servers",
|
||||
UsageText: "abra server [command] [arguments] [options]",
|
||||
Commands: []*cli.Command{
|
||||
&serverAddCommand,
|
||||
&serverListCommand,
|
||||
&serverRemoveCommand,
|
||||
&serverPruneCommand,
|
||||
},
|
||||
var ServerCommand = &cobra.Command{
|
||||
Use: "server [cmd] [args] [flags]",
|
||||
Aliases: []string{"s"},
|
||||
Short: "Manage servers",
|
||||
}
|
||||
|
Reference in New Issue
Block a user