Some checks failed
continuous-integration/drone/push Build is failing
See #483
309 lines
7.3 KiB
Go
309 lines
7.3 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
"coopcloud.tech/abra/pkg/autocomplete"
|
|
"coopcloud.tech/abra/pkg/client"
|
|
"coopcloud.tech/abra/pkg/log"
|
|
"github.com/leonelquinteros/gotext"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var AppBackupListCommand = &cobra.Command{
|
|
Use: gotext.Get("list <domain> [flags]"),
|
|
Aliases: []string{gotext.Get("ls")},
|
|
Short: gotext.Get("List the contents of a snapshot"),
|
|
Args: cobra.ExactArgs(1),
|
|
ValidArgsFunction: func(
|
|
cmd *cobra.Command,
|
|
args []string,
|
|
toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return autocomplete.AppNameComplete()
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
app := internal.ValidateApp(args)
|
|
|
|
cl, err := client.New(app.Server)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
targetContainer, err := internal.RetrieveBackupBotContainer(cl)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
execEnv := []string{
|
|
fmt.Sprintf("SERVICE=%s", app.Domain),
|
|
"MACHINE_LOGS=true",
|
|
}
|
|
|
|
if snapshot != "" {
|
|
log.Debug(gotext.Get("including SNAPSHOT=%s in backupbot exec invocation", snapshot))
|
|
execEnv = append(execEnv, fmt.Sprintf("SNAPSHOT=%s", snapshot))
|
|
}
|
|
|
|
if showAllPaths {
|
|
log.Debug(gotext.Get("including SHOW_ALL=%v in backupbot exec invocation", showAllPaths))
|
|
execEnv = append(execEnv, fmt.Sprintf("SHOW_ALL=%v", showAllPaths))
|
|
}
|
|
|
|
if timestamps {
|
|
log.Debug(gotext.Get("including TIMESTAMPS=%v in backupbot exec invocation", timestamps))
|
|
execEnv = append(execEnv, fmt.Sprintf("TIMESTAMPS=%v", timestamps))
|
|
}
|
|
|
|
if _, err = internal.RunBackupCmdRemote(cl, "ls", targetContainer.ID, execEnv); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
var AppBackupDownloadCommand = &cobra.Command{
|
|
Use: gotext.Get("download <domain> [flags]"),
|
|
Aliases: []string{gotext.Get("d")},
|
|
Short: gotext.Get("Download a snapshot"),
|
|
Long: gotext.Get(`Downloads a backup.tar.gz to the current working directory.
|
|
|
|
"--volumes/-v" includes data contained in volumes alongide paths specified in
|
|
"backupbot.backup.path" labels.`),
|
|
Args: cobra.ExactArgs(1),
|
|
ValidArgsFunction: func(
|
|
cmd *cobra.Command,
|
|
args []string,
|
|
toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return autocomplete.AppNameComplete()
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
app := internal.ValidateApp(args)
|
|
|
|
if err := app.Recipe.Ensure(internal.GetEnsureContext()); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
cl, err := client.New(app.Server)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
targetContainer, err := internal.RetrieveBackupBotContainer(cl)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
execEnv := []string{
|
|
fmt.Sprintf("SERVICE=%s", app.Domain),
|
|
"MACHINE_LOGS=true",
|
|
}
|
|
|
|
if snapshot != "" {
|
|
log.Debug(gotext.Get("including SNAPSHOT=%s in backupbot exec invocation", snapshot))
|
|
execEnv = append(execEnv, fmt.Sprintf("SNAPSHOT=%s", snapshot))
|
|
}
|
|
|
|
if includePath != "" {
|
|
log.Debug(gotext.Get("including INCLUDE_PATH=%s in backupbot exec invocation", includePath))
|
|
execEnv = append(execEnv, fmt.Sprintf("INCLUDE_PATH=%s", includePath))
|
|
}
|
|
|
|
if includeSecrets {
|
|
log.Debug(gotext.Get("including SECRETS=%v in backupbot exec invocation", includeSecrets))
|
|
execEnv = append(execEnv, fmt.Sprintf("SECRETS=%v", includeSecrets))
|
|
}
|
|
|
|
if includeVolumes {
|
|
log.Debug(gotext.Get("including VOLUMES=%v in backupbot exec invocation", includeVolumes))
|
|
execEnv = append(execEnv, fmt.Sprintf("VOLUMES=%v", includeVolumes))
|
|
}
|
|
|
|
if _, err := internal.RunBackupCmdRemote(cl, "download", targetContainer.ID, execEnv); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
remoteBackupDir := "/tmp/backup.tar.gz"
|
|
currentWorkingDir := "."
|
|
if err = CopyFromContainer(cl, targetContainer.ID, remoteBackupDir, currentWorkingDir); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
var AppBackupCreateCommand = &cobra.Command{
|
|
Use: gotext.Get("create <domain> [flags]"),
|
|
Aliases: []string{gotext.Get("c")},
|
|
Short: gotext.Get("Create a new snapshot"),
|
|
Args: cobra.ExactArgs(1),
|
|
ValidArgsFunction: func(
|
|
cmd *cobra.Command,
|
|
args []string,
|
|
toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return autocomplete.AppNameComplete()
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
app := internal.ValidateApp(args)
|
|
|
|
if err := app.Recipe.Ensure(internal.GetEnsureContext()); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
cl, err := client.New(app.Server)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
targetContainer, err := internal.RetrieveBackupBotContainer(cl)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
execEnv := []string{
|
|
fmt.Sprintf("SERVICE=%s", app.Domain),
|
|
"MACHINE_LOGS=true",
|
|
}
|
|
|
|
if retries != "" {
|
|
log.Debug(gotext.Get("including RETRIES=%s in backupbot exec invocation", retries))
|
|
execEnv = append(execEnv, fmt.Sprintf("RETRIES=%s", retries))
|
|
}
|
|
|
|
if _, err := internal.RunBackupCmdRemote(cl, "create", targetContainer.ID, execEnv); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
var AppBackupSnapshotsCommand = &cobra.Command{
|
|
Use: gotext.Get("snapshots <domain> [flags]"),
|
|
Aliases: []string{"s"},
|
|
Short: gotext.Get("List all snapshots"),
|
|
Args: cobra.ExactArgs(1),
|
|
ValidArgsFunction: func(
|
|
cmd *cobra.Command,
|
|
args []string,
|
|
toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return autocomplete.AppNameComplete()
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
app := internal.ValidateApp(args)
|
|
|
|
cl, err := client.New(app.Server)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
targetContainer, err := internal.RetrieveBackupBotContainer(cl)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
execEnv := []string{
|
|
fmt.Sprintf("SERVICE=%s", app.Domain),
|
|
"MACHINE_LOGS=true",
|
|
}
|
|
|
|
if _, err = internal.RunBackupCmdRemote(cl, "snapshots", targetContainer.ID, execEnv); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
var AppBackupCommand = &cobra.Command{
|
|
Use: gotext.Get("backup [cmd] [args] [flags]"),
|
|
Aliases: []string{gotext.Get("b")},
|
|
Short: gotext.Get("Manage app backups"),
|
|
}
|
|
|
|
var (
|
|
snapshot string
|
|
retries string
|
|
includePath string
|
|
showAllPaths bool
|
|
timestamps bool
|
|
includeSecrets bool
|
|
includeVolumes bool
|
|
)
|
|
|
|
func init() {
|
|
AppBackupListCommand.Flags().StringVarP(
|
|
&snapshot,
|
|
"snapshot",
|
|
"s",
|
|
"",
|
|
gotext.Get("list specific snapshot"),
|
|
)
|
|
|
|
AppBackupListCommand.Flags().BoolVarP(
|
|
&showAllPaths,
|
|
"all",
|
|
"a",
|
|
false,
|
|
gotext.Get("show all paths"),
|
|
)
|
|
|
|
AppBackupListCommand.Flags().BoolVarP(
|
|
×tamps,
|
|
"timestamps",
|
|
"t",
|
|
false,
|
|
gotext.Get("include timestamps"),
|
|
)
|
|
|
|
AppBackupDownloadCommand.Flags().StringVarP(
|
|
&snapshot,
|
|
"snapshot",
|
|
"s",
|
|
"",
|
|
gotext.Get("list specific snapshot"),
|
|
)
|
|
|
|
AppBackupDownloadCommand.Flags().StringVarP(
|
|
&includePath,
|
|
"path",
|
|
"p",
|
|
"",
|
|
gotext.Get("volumes path"),
|
|
)
|
|
|
|
AppBackupDownloadCommand.Flags().BoolVarP(
|
|
&includeSecrets,
|
|
"secrets",
|
|
"S",
|
|
false,
|
|
gotext.Get("include secrets"),
|
|
)
|
|
|
|
AppBackupDownloadCommand.Flags().BoolVarP(
|
|
&includeVolumes,
|
|
"volumes",
|
|
"v",
|
|
false,
|
|
gotext.Get("include volumes"),
|
|
)
|
|
|
|
AppBackupDownloadCommand.Flags().BoolVarP(
|
|
&internal.Chaos,
|
|
"chaos",
|
|
"C",
|
|
false,
|
|
gotext.Get("ignore uncommitted recipes changes"),
|
|
)
|
|
|
|
AppBackupCreateCommand.Flags().StringVarP(
|
|
&retries,
|
|
"retries",
|
|
"r",
|
|
"1",
|
|
gotext.Get("number of retry attempts"),
|
|
)
|
|
|
|
AppBackupCreateCommand.Flags().BoolVarP(
|
|
&internal.Chaos,
|
|
"chaos",
|
|
"C",
|
|
false,
|
|
gotext.Get("ignore uncommitted recipes changes"),
|
|
)
|
|
}
|