server

package
v0.0.0-...-aae20f0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: GPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ServerAddCommand = &cobra.Command{

	Use:     i18n.G("add [[server] | --local] [flags]"),
	Aliases: strings.Split(serverAddAliases, ","),

	Short: i18n.G("Add a new server"),
	Long: i18n.G(`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: i18n.G("  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(i18n.G("cannot use [server] and --local together"))
		}

		if len(args) == 0 && !local {
			log.Fatal(i18n.G("missing argument or --local/-l flag"))
		}

		name := "default"
		if !local {
			name = internal.ValidateDomain(args)
		}

		timeout := client.WithTimeout(5)

		if local {
			created, err := createServerDir(name)
			if err != nil {
				log.Fatal(err)
			}

			log.Debug(i18n.G("attempting to create client for %s", name))

			if _, err := client.New(name, timeout); err != nil {
				cleanUp(name)
				log.Fatal(err)
			}

			if created {
				log.Info(i18n.G("local server successfully added"))
			} else {
				log.Warn(i18n.G("local server already exists"))
			}

			return
		}

		_, err := createServerDir(name)
		if err != nil {
			log.Fatal(err)
		}

		created, err := newContext(name)
		if err != nil {
			cleanUp(name)
			log.Fatal(i18n.G("unable to create local context: %s", err))
		}

		log.Debug(i18n.G("attempting to create client for %s", name))

		if _, err := client.New(name, timeout); err != nil {
			cleanUp(name)
			log.Fatal(i18n.G("ssh %s error: %s", name, sshPkg.Fatal(name, err)))
		}

		if created {
			log.Info(i18n.G("%s successfully added", name))

			if _, err := dns.EnsureIPv4(name); err != nil {
				log.Warn(i18n.G("unable to resolve IPv4 for %s", name))
			}

			return
		}

		log.Warn(i18n.G("%s already exists", name))
	},
}
View Source
var ServerCommand = &cobra.Command{

	Use:     i18n.G("server [cmd] [args] [flags]"),
	Aliases: strings.Split(serverAliases, ","),

	Short: i18n.G("Manage servers"),
}

ServerCommand defines the `abra server` command and its subcommands

View Source
var ServerListCommand = &cobra.Command{

	Use:     i18n.G("list [flags]"),
	Aliases: strings.Split(serverListAliases, ","),

	Short: i18n.G("List managed servers"),
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		dockerContextStore := contextPkg.NewDefaultDockerContextStore()
		contexts, err := dockerContextStore.Store.List()
		if err != nil {
			log.Fatal(err)
		}

		table, err := formatter.CreateTable()
		if err != nil {
			log.Fatal(err)
		}

		headers := []string{i18n.G("NAME"), i18n.G("HOST")}
		table.Headers(headers...)

		serverNames, err := config.ReadServerNames()
		if err != nil {
			log.Fatal(err)
		}

		var rows [][]string
		for _, serverName := range serverNames {
			var row []string
			for _, dockerCtx := range contexts {
				endpoint, err := contextPkg.GetContextEndpoint(dockerCtx)
				if err != nil && strings.Contains(err.Error(), "does not exist") {

					continue
				}

				if dockerCtx.Name == serverName {
					sp, err := ssh.ParseURL(endpoint)
					if err != nil {
						log.Fatal(err)
					}

					if sp.Host == "" {
						sp.Host = i18n.G("unknown")
					}

					row = []string{serverName, sp.Host}
					rows = append(rows, row)
				}
			}

			if len(row) == 0 {
				if serverName == "default" {
					row = []string{serverName, i18n.G("local")}
				} else {
					row = []string{serverName, i18n.G("unknown")}
				}
				rows = append(rows, row)
			}

			table.Row(row...)
		}

		if internal.MachineReadable {
			out, err := formatter.ToJSON(headers, rows)
			if err != nil {
				log.Fatal(i18n.G("unable to render to JSON: %s", err))
			}

			fmt.Println(out)

			return
		}

		if err := formatter.PrintTable(table); err != nil {
			log.Fatal(err)
		}
	},
}
View Source
var ServerPruneCommand = &cobra.Command{

	Use:     i18n.G("prune <server> [flags]"),
	Aliases: strings.Split(serverPruneliases, ","),

	Short: i18n.G("Prune resources on a server"),
	Long: i18n.G(`Prunes unused containers, networks, and dangling images.

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.`),
	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)

		cl, err := client.New(serverName)
		if err != nil {
			log.Fatal(err)
		}

		var filterArgs filters.Args

		cr, err := cl.ContainersPrune(cmd.Context(), filterArgs)
		if err != nil {
			log.Fatal(err)
		}

		cntSpaceReclaimed := formatter.ByteCountSI(cr.SpaceReclaimed)
		log.Info(i18n.G("containers pruned: %d; space reclaimed: %s", len(cr.ContainersDeleted), cntSpaceReclaimed))

		nr, err := cl.NetworksPrune(cmd.Context(), filterArgs)
		if err != nil {
			log.Fatal(err)
		}

		log.Info(i18n.G("networks pruned: %d", len(nr.NetworksDeleted)))

		pruneFilters := filters.NewArgs()
		if allFilter {
			log.Debug(i18n.G("removing all images, not only dangling ones"))
			pruneFilters.Add("dangling", "false")
		}

		ir, err := cl.ImagesPrune(cmd.Context(), pruneFilters)
		if err != nil {
			log.Fatal(err)
		}

		imgSpaceReclaimed := formatter.ByteCountSI(ir.SpaceReclaimed)
		log.Info(i18n.G("images pruned: %d; space reclaimed: %s", len(ir.ImagesDeleted), imgSpaceReclaimed))

		if volumesFilter {
			vr, err := cl.VolumesPrune(cmd.Context(), filterArgs)
			if err != nil {
				log.Fatal(err)
			}

			volSpaceReclaimed := formatter.ByteCountSI(vr.SpaceReclaimed)
			log.Info(i18n.G("volumes pruned: %d; space reclaimed: %s", len(vr.VolumesDeleted), volSpaceReclaimed))
		}

		return
	},
}
View Source
var ServerRemoveCommand = &cobra.Command{

	Use:     i18n.G("remove <server> [flags]"),
	Aliases: strings.Split(serverRemoveAliases, ","),

	Short: i18n.G("Remove a managed server"),
	Long: i18n.G(`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.`),
	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)
		}

		if err := os.RemoveAll(filepath.Join(config.SERVERS_DIR, serverName)); err != nil {
			log.Fatal(err)
		}

		log.Info(i18n.G("%s is now lost in time, like tears in rain", serverName))

		return
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL