forked from toolshed/abra
Compare commits
1 Commits
diffing-re
...
backup-rev
Author | SHA1 | Date | |
---|---|---|---|
63387a011a
|
@ -1,414 +1,422 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/autocomplete"
|
"coopcloud.tech/abra/pkg/autocomplete"
|
||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
containerPkg "coopcloud.tech/abra/pkg/container"
|
containerPkg "coopcloud.tech/abra/pkg/container"
|
||||||
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
|
"coopcloud.tech/abra/pkg/service"
|
||||||
"coopcloud.tech/abra/pkg/upstream/container"
|
"coopcloud.tech/abra/pkg/upstream/container"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
dockerClient "github.com/docker/docker/client"
|
|
||||||
"github.com/docker/docker/pkg/archive"
|
|
||||||
"github.com/klauspost/pgzip"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
type backupConfig struct {
|
var snapshot string
|
||||||
preHookCmd string
|
var snapshotFlag = &cli.StringFlag{
|
||||||
postHookCmd string
|
Name: "snapshot, s",
|
||||||
backupPaths []string
|
Usage: "Lists specific snapshot",
|
||||||
|
Destination: &snapshot,
|
||||||
}
|
}
|
||||||
|
|
||||||
var appBackupCommand = cli.Command{
|
var includePath string
|
||||||
Name: "backup",
|
var includePathFlag = &cli.StringFlag{
|
||||||
Aliases: []string{"bk"},
|
Name: "path, p",
|
||||||
Usage: "Run app backup",
|
Usage: "Include path",
|
||||||
ArgsUsage: "<domain> [<service>]",
|
Destination: &includePath,
|
||||||
|
}
|
||||||
|
|
||||||
|
var resticRepo string
|
||||||
|
var resticRepoFlag = &cli.StringFlag{
|
||||||
|
Name: "repo, r",
|
||||||
|
Usage: "Restic repository",
|
||||||
|
Destination: &resticRepo,
|
||||||
|
}
|
||||||
|
|
||||||
|
var appBackupListCommand = cli.Command{
|
||||||
|
Name: "list",
|
||||||
|
Aliases: []string{"ls"},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
internal.DebugFlag,
|
internal.DebugFlag,
|
||||||
internal.OfflineFlag,
|
internal.OfflineFlag,
|
||||||
internal.ChaosFlag,
|
snapshotFlag,
|
||||||
|
includePathFlag,
|
||||||
},
|
},
|
||||||
Before: internal.SubCommandBefore,
|
Before: internal.SubCommandBefore,
|
||||||
|
Usage: "List all backups",
|
||||||
BashComplete: autocomplete.AppNameComplete,
|
BashComplete: autocomplete.AppNameComplete,
|
||||||
Description: `
|
|
||||||
Run an app backup.
|
|
||||||
|
|
||||||
A backup command and pre/post hook commands are defined in the recipe
|
|
||||||
configuration. Abra reads this configuration and run the comands in the context
|
|
||||||
of the deployed services. Pass <service> if you only want to back up a single
|
|
||||||
service. All backups are placed in the ~/.abra/backups directory.
|
|
||||||
|
|
||||||
A single backup file is produced for all backup paths specified for a service.
|
|
||||||
If we have the following backup configuration:
|
|
||||||
|
|
||||||
- "backupbot.backup.path=/var/lib/foo,/var/lib/bar"
|
|
||||||
|
|
||||||
And we run "abra app backup example.com app", Abra will produce a file that
|
|
||||||
looks like:
|
|
||||||
|
|
||||||
~/.abra/backups/example_com_app_609341138.tar.gz
|
|
||||||
|
|
||||||
This file is a compressed archive which contains all backup paths. To see paths, run:
|
|
||||||
|
|
||||||
tar -tf ~/.abra/backups/example_com_app_609341138.tar.gz
|
|
||||||
|
|
||||||
(Make sure to change the name of the backup file)
|
|
||||||
|
|
||||||
This single file can be used to restore your app. See "abra app restore" for more.
|
|
||||||
`,
|
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
app := internal.ValidateApp(c)
|
app := internal.ValidateApp(c)
|
||||||
|
|
||||||
recipe, err := recipePkg.Get(app.Recipe, internal.Offline)
|
if err := recipe.EnsureExists(app.Recipe); err != nil {
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !internal.Chaos {
|
if !internal.Chaos {
|
||||||
if err := recipePkg.EnsureIsClean(app.Recipe); err != nil {
|
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !internal.Offline {
|
if !internal.Offline {
|
||||||
if err := recipePkg.EnsureUpToDate(app.Recipe); err != nil {
|
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := recipePkg.EnsureLatest(app.Recipe); err != nil {
|
if err := recipe.EnsureLatest(app.Recipe); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backupConfigs := make(map[string]backupConfig)
|
|
||||||
for _, service := range recipe.Config.Services {
|
|
||||||
if backupsEnabled, ok := service.Deploy.Labels["backupbot.backup"]; ok {
|
|
||||||
if backupsEnabled == "true" {
|
|
||||||
fullServiceName := fmt.Sprintf("%s_%s", app.StackName(), service.Name)
|
|
||||||
bkConfig := backupConfig{}
|
|
||||||
|
|
||||||
logrus.Debugf("backup config detected for %s", fullServiceName)
|
|
||||||
|
|
||||||
if paths, ok := service.Deploy.Labels["backupbot.backup.path"]; ok {
|
|
||||||
logrus.Debugf("detected backup paths for %s: %s", fullServiceName, paths)
|
|
||||||
bkConfig.backupPaths = strings.Split(paths, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
if preHookCmd, ok := service.Deploy.Labels["backupbot.backup.pre-hook"]; ok {
|
|
||||||
logrus.Debugf("detected pre-hook command for %s: %s", fullServiceName, preHookCmd)
|
|
||||||
bkConfig.preHookCmd = preHookCmd
|
|
||||||
}
|
|
||||||
|
|
||||||
if postHookCmd, ok := service.Deploy.Labels["backupbot.backup.post-hook"]; ok {
|
|
||||||
logrus.Debugf("detected post-hook command for %s: %s", fullServiceName, postHookCmd)
|
|
||||||
bkConfig.postHookCmd = postHookCmd
|
|
||||||
}
|
|
||||||
|
|
||||||
backupConfigs[service.Name] = bkConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cl, err := client.New(app.Server)
|
cl, err := client.New(app.Server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceName := c.Args().Get(1)
|
chosenService, err := service.GetServiceByLabel(context.Background(), cl, config.BackupbotLabel, internal.NoInput)
|
||||||
if serviceName != "" {
|
if err != nil {
|
||||||
backupConfig, ok := backupConfigs[serviceName]
|
|
||||||
if !ok {
|
|
||||||
logrus.Fatalf("no backup config for %s? does %s exist?", serviceName, serviceName)
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("running backup for the %s service", serviceName)
|
|
||||||
|
|
||||||
if err := runBackup(cl, app, serviceName, backupConfig); err != nil {
|
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if len(backupConfigs) == 0 {
|
|
||||||
logrus.Fatalf("no backup configs discovered for %s?", app.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
for serviceName, backupConfig := range backupConfigs {
|
logrus.Debugf("retrieved %s as backup enabled service", chosenService.Spec.Name)
|
||||||
logrus.Infof("running backup for the %s service", serviceName)
|
|
||||||
|
|
||||||
if err := runBackup(cl, app, serviceName, backupConfig); err != nil {
|
filters := filters.NewArgs()
|
||||||
|
filters.Add("name", chosenService.Spec.Name)
|
||||||
|
targetContainer, err := containerPkg.GetContainer(
|
||||||
|
context.Background(),
|
||||||
|
cl,
|
||||||
|
filters,
|
||||||
|
internal.NoInput,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
execEnv := []string{fmt.Sprintf("SERVICE=%s", app.Domain)}
|
||||||
|
if snapshot != "" {
|
||||||
|
logrus.Debugf("including SNAPSHOT=%s in backupbot exec invocation", snapshot)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("SNAPSHOT=%s", snapshot))
|
||||||
}
|
}
|
||||||
|
if includePath != "" {
|
||||||
|
logrus.Debugf("including INCLUDE_PATH=%s in backupbot exec invocation", includePath)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("INCLUDE_PATH=%s", includePath))
|
||||||
|
}
|
||||||
|
|
||||||
|
execBackupListOpts := types.ExecConfig{
|
||||||
|
AttachStderr: true,
|
||||||
|
AttachStdin: true,
|
||||||
|
AttachStdout: true,
|
||||||
|
Cmd: []string{"/usr/bin/backup", "--", "ls"},
|
||||||
|
Detach: false,
|
||||||
|
Env: execEnv,
|
||||||
|
Tty: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("running backup list on %s with exec config %v", targetContainer.ID, execBackupListOpts)
|
||||||
|
|
||||||
|
// FIXME: avoid instantiating a new CLI
|
||||||
|
dcli, err := command.NewDockerCli()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := container.RunExec(dcli, cl, targetContainer.ID, &execBackupListOpts); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeStamp generates a file name friendly timestamp.
|
var appBackupDownloadCommand = cli.Command{
|
||||||
func TimeStamp() string {
|
Name: "download",
|
||||||
ts := time.Now().UTC().Format(time.RFC3339)
|
Aliases: []string{"d"},
|
||||||
return strings.Replace(ts, ":", "-", -1)
|
Flags: []cli.Flag{
|
||||||
|
internal.DebugFlag,
|
||||||
|
internal.OfflineFlag,
|
||||||
|
snapshotFlag,
|
||||||
|
includePathFlag,
|
||||||
|
},
|
||||||
|
Before: internal.SubCommandBefore,
|
||||||
|
Usage: "Download a backup",
|
||||||
|
BashComplete: autocomplete.AppNameComplete,
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
app := internal.ValidateApp(c)
|
||||||
|
|
||||||
|
if err := recipe.EnsureExists(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runBackup does the actual backup logic.
|
if !internal.Chaos {
|
||||||
func runBackup(cl *dockerClient.Client, app config.App, serviceName string, bkConfig backupConfig) error {
|
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
|
||||||
if len(bkConfig.backupPaths) == 0 {
|
logrus.Fatal(err)
|
||||||
return fmt.Errorf("backup paths are empty for %s?", serviceName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !internal.Offline {
|
||||||
|
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := recipe.EnsureLatest(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cl, err := client.New(app.Server)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
chosenService, err := service.GetServiceByLabel(context.Background(), cl, config.BackupbotLabel, internal.NoInput)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("retrieved %s as backup enabled service", chosenService.Spec.Name)
|
||||||
|
|
||||||
|
filters := filters.NewArgs()
|
||||||
|
filters.Add("name", chosenService.Spec.Name)
|
||||||
|
targetContainer, err := containerPkg.GetContainer(
|
||||||
|
context.Background(),
|
||||||
|
cl,
|
||||||
|
filters,
|
||||||
|
internal.NoInput,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
execEnv := []string{fmt.Sprintf("SERVICE=%s", app.Domain)}
|
||||||
|
if snapshot != "" {
|
||||||
|
logrus.Debugf("including SNAPSHOT=%s in backupbot exec invocation", snapshot)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("SNAPSHOT=%s", snapshot))
|
||||||
|
}
|
||||||
|
if includePath != "" {
|
||||||
|
logrus.Debugf("including INCLUDE_PATH=%s in backupbot exec invocation", includePath)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("INCLUDE_PATH=%s", includePath))
|
||||||
|
}
|
||||||
|
|
||||||
|
execBackupListOpts := types.ExecConfig{
|
||||||
|
AttachStderr: true,
|
||||||
|
AttachStdin: true,
|
||||||
|
AttachStdout: true,
|
||||||
|
Cmd: []string{"/usr/bin/backup", "--", "download"},
|
||||||
|
Detach: false,
|
||||||
|
Env: execEnv,
|
||||||
|
Tty: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("running backup list on %s with exec config %v", targetContainer.ID, execBackupListOpts)
|
||||||
|
|
||||||
// FIXME: avoid instantiating a new CLI
|
// FIXME: avoid instantiating a new CLI
|
||||||
dcli, err := command.NewDockerCli()
|
dcli, err := command.NewDockerCli()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := container.RunExec(dcli, cl, targetContainer.ID, &execBackupListOpts); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var appBackupCreateCommand = cli.Command{
|
||||||
|
Name: "create",
|
||||||
|
Aliases: []string{"c"},
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
internal.DebugFlag,
|
||||||
|
internal.OfflineFlag,
|
||||||
|
resticRepoFlag,
|
||||||
|
},
|
||||||
|
Before: internal.SubCommandBefore,
|
||||||
|
Usage: "Create a new backup",
|
||||||
|
BashComplete: autocomplete.AppNameComplete,
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
app := internal.ValidateApp(c)
|
||||||
|
|
||||||
|
if err := recipe.EnsureExists(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !internal.Chaos {
|
||||||
|
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !internal.Offline {
|
||||||
|
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := recipe.EnsureLatest(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cl, err := client.New(app.Server)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
chosenService, err := service.GetServiceByLabel(context.Background(), cl, config.BackupbotLabel, internal.NoInput)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("retrieved %s as backup enabled service", chosenService.Spec.Name)
|
||||||
|
|
||||||
filters := filters.NewArgs()
|
filters := filters.NewArgs()
|
||||||
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), serviceName))
|
filters.Add("name", chosenService.Spec.Name)
|
||||||
|
targetContainer, err := containerPkg.GetContainer(
|
||||||
targetContainer, err := containerPkg.GetContainer(context.Background(), cl, filters, true)
|
context.Background(),
|
||||||
|
cl,
|
||||||
|
filters,
|
||||||
|
internal.NoInput,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fullServiceName := fmt.Sprintf("%s_%s", app.StackName(), serviceName)
|
execEnv := []string{fmt.Sprintf("SERVICE=%s", app.Domain)}
|
||||||
if bkConfig.preHookCmd != "" {
|
if resticRepo != "" {
|
||||||
splitCmd := internal.SafeSplit(bkConfig.preHookCmd)
|
logrus.Debugf("including RESTIC_REPO=%s in backupbot exec invocation", resticRepo)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("RESTIC_REPO=%s", resticRepo))
|
||||||
logrus.Debugf("split pre-hook command for %s into %s", fullServiceName, splitCmd)
|
}
|
||||||
|
execBackupListOpts := types.ExecConfig{
|
||||||
preHookExecOpts := types.ExecConfig{
|
|
||||||
AttachStderr: true,
|
AttachStderr: true,
|
||||||
AttachStdin: true,
|
AttachStdin: true,
|
||||||
AttachStdout: true,
|
AttachStdout: true,
|
||||||
Cmd: splitCmd,
|
Cmd: []string{"/usr/bin/backup", "--", "create"},
|
||||||
Detach: false,
|
Detach: false,
|
||||||
|
Env: execEnv,
|
||||||
Tty: true,
|
Tty: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.RunExec(dcli, cl, targetContainer.ID, &preHookExecOpts); err != nil {
|
logrus.Debugf("running backup list on %s with exec config %v", targetContainer.ID, execBackupListOpts)
|
||||||
return fmt.Errorf("failed to run %s on %s: %s", bkConfig.preHookCmd, targetContainer.ID, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("succesfully ran %s pre-hook command: %s", fullServiceName, bkConfig.preHookCmd)
|
// FIXME: avoid instantiating a new CLI
|
||||||
}
|
dcli, err := command.NewDockerCli()
|
||||||
|
|
||||||
var tempBackupPaths []string
|
|
||||||
for _, remoteBackupPath := range bkConfig.backupPaths {
|
|
||||||
sanitisedPath := strings.ReplaceAll(remoteBackupPath, "/", "_")
|
|
||||||
localBackupPath := filepath.Join(config.BACKUP_DIR, fmt.Sprintf("%s%s_%s.tar.gz", fullServiceName, sanitisedPath, TimeStamp()))
|
|
||||||
logrus.Debugf("temporarily backing up %s:%s to %s", fullServiceName, remoteBackupPath, localBackupPath)
|
|
||||||
|
|
||||||
logrus.Infof("backing up %s:%s", fullServiceName, remoteBackupPath)
|
|
||||||
|
|
||||||
content, _, err := cl.CopyFromContainer(context.Background(), targetContainer.ID, remoteBackupPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugf("failed to copy %s from container: %s", remoteBackupPath, err.Error())
|
logrus.Fatal(err)
|
||||||
if err := cleanupTempArchives(tempBackupPaths); err != nil {
|
|
||||||
return fmt.Errorf("failed to clean up temporary archives: %s", err.Error())
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to copy %s from container: %s", remoteBackupPath, err.Error())
|
|
||||||
}
|
|
||||||
defer content.Close()
|
|
||||||
|
|
||||||
_, srcBase := archive.SplitPathDirEntry(remoteBackupPath)
|
|
||||||
preArchive := archive.RebaseArchiveEntries(content, srcBase, remoteBackupPath)
|
|
||||||
if err := copyToFile(localBackupPath, preArchive); err != nil {
|
|
||||||
logrus.Debugf("failed to create tar archive (%s): %s", localBackupPath, err.Error())
|
|
||||||
if err := cleanupTempArchives(tempBackupPaths); err != nil {
|
|
||||||
return fmt.Errorf("failed to clean up temporary archives: %s", err.Error())
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to create tar archive (%s): %s", localBackupPath, err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tempBackupPaths = append(tempBackupPaths, localBackupPath)
|
if err := container.RunExec(dcli, cl, targetContainer.ID, &execBackupListOpts); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Infof("compressing and merging archives...")
|
return nil
|
||||||
|
},
|
||||||
if err := mergeArchives(tempBackupPaths, fullServiceName); err != nil {
|
|
||||||
logrus.Debugf("failed to merge archive files: %s", err.Error())
|
|
||||||
if err := cleanupTempArchives(tempBackupPaths); err != nil {
|
|
||||||
return fmt.Errorf("failed to clean up temporary archives: %s", err.Error())
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to merge archive files: %s", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cleanupTempArchives(tempBackupPaths); err != nil {
|
var appBackupSnapshotsCommand = cli.Command{
|
||||||
return fmt.Errorf("failed to clean up temporary archives: %s", err.Error())
|
Name: "snapshots",
|
||||||
|
Aliases: []string{"s"},
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
internal.DebugFlag,
|
||||||
|
internal.OfflineFlag,
|
||||||
|
snapshotFlag,
|
||||||
|
},
|
||||||
|
Before: internal.SubCommandBefore,
|
||||||
|
Usage: "List backup snapshots",
|
||||||
|
BashComplete: autocomplete.AppNameComplete,
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
app := internal.ValidateApp(c)
|
||||||
|
|
||||||
|
if err := recipe.EnsureExists(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if bkConfig.postHookCmd != "" {
|
if !internal.Chaos {
|
||||||
splitCmd := internal.SafeSplit(bkConfig.postHookCmd)
|
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
logrus.Debugf("split post-hook command for %s into %s", fullServiceName, splitCmd)
|
if !internal.Offline {
|
||||||
|
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
postHookExecOpts := types.ExecConfig{
|
if err := recipe.EnsureLatest(app.Recipe); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cl, err := client.New(app.Server)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
chosenService, err := service.GetServiceByLabel(context.Background(), cl, config.BackupbotLabel, internal.NoInput)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("retrieved %s as backup enabled service", chosenService.Spec.Name)
|
||||||
|
|
||||||
|
filters := filters.NewArgs()
|
||||||
|
filters.Add("name", chosenService.Spec.Name)
|
||||||
|
targetContainer, err := containerPkg.GetContainer(
|
||||||
|
context.Background(),
|
||||||
|
cl,
|
||||||
|
filters,
|
||||||
|
internal.NoInput,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
execEnv := []string{fmt.Sprintf("SERVICE=%s", app.Domain)}
|
||||||
|
if snapshot != "" {
|
||||||
|
logrus.Debugf("including SNAPSHOT=%s in backupbot exec invocation", snapshot)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("SNAPSHOT=%s", snapshot))
|
||||||
|
}
|
||||||
|
execBackupListOpts := types.ExecConfig{
|
||||||
AttachStderr: true,
|
AttachStderr: true,
|
||||||
AttachStdin: true,
|
AttachStdin: true,
|
||||||
AttachStdout: true,
|
AttachStdout: true,
|
||||||
Cmd: splitCmd,
|
Cmd: []string{"/usr/bin/backup", "--", "snapshots"},
|
||||||
Detach: false,
|
Detach: false,
|
||||||
|
Env: execEnv,
|
||||||
Tty: true,
|
Tty: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.RunExec(dcli, cl, targetContainer.ID, &postHookExecOpts); err != nil {
|
logrus.Debugf("running backup list on %s with exec config %v", targetContainer.ID, execBackupListOpts)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("succesfully ran %s post-hook command: %s", fullServiceName, bkConfig.postHookCmd)
|
// FIXME: avoid instantiating a new CLI
|
||||||
}
|
dcli, err := command.NewDockerCli()
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func copyToFile(outfile string, r io.Reader) error {
|
|
||||||
tmpFile, err := os.CreateTemp(filepath.Dir(outfile), ".tar_temp")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpPath := tmpFile.Name()
|
if err := container.RunExec(dcli, cl, targetContainer.ID, &execBackupListOpts); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
_, err = io.Copy(tmpFile, r)
|
|
||||||
tmpFile.Close()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
os.Remove(tmpPath)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = os.Rename(tmpPath, outfile); err != nil {
|
|
||||||
os.Remove(tmpPath)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanupTempArchives(tarPaths []string) error {
|
var appBackupCommand = cli.Command{
|
||||||
for _, tarPath := range tarPaths {
|
Name: "backup",
|
||||||
if err := os.RemoveAll(tarPath); err != nil {
|
Aliases: []string{"b"},
|
||||||
return err
|
Usage: "Manage app backups",
|
||||||
}
|
ArgsUsage: "<domain>",
|
||||||
|
Subcommands: []cli.Command{
|
||||||
logrus.Debugf("remove temporary archive file %s", tarPath)
|
appBackupListCommand,
|
||||||
}
|
appBackupSnapshotsCommand,
|
||||||
|
appBackupDownloadCommand,
|
||||||
return nil
|
appBackupCreateCommand,
|
||||||
}
|
},
|
||||||
|
|
||||||
func mergeArchives(tarPaths []string, serviceName string) error {
|
|
||||||
var out io.Writer
|
|
||||||
var cout *pgzip.Writer
|
|
||||||
|
|
||||||
localBackupPath := filepath.Join(config.BACKUP_DIR, fmt.Sprintf("%s_%s.tar.gz", serviceName, TimeStamp()))
|
|
||||||
|
|
||||||
fout, err := os.Create(localBackupPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to open %s: %s", localBackupPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer fout.Close()
|
|
||||||
out = fout
|
|
||||||
|
|
||||||
cout = pgzip.NewWriter(out)
|
|
||||||
out = cout
|
|
||||||
|
|
||||||
tw := tar.NewWriter(out)
|
|
||||||
|
|
||||||
for _, tarPath := range tarPaths {
|
|
||||||
if err := addTar(tw, tarPath); err != nil {
|
|
||||||
return fmt.Errorf("failed to merge %s: %v", tarPath, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := tw.Close(); err != nil {
|
|
||||||
return fmt.Errorf("failed to close tar writer %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cout != nil {
|
|
||||||
if err := cout.Flush(); err != nil {
|
|
||||||
return fmt.Errorf("failed to flush: %s", err)
|
|
||||||
} else if err = cout.Close(); err != nil {
|
|
||||||
return fmt.Errorf("failed to close compressed writer: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("backed up %s to %s", serviceName, localBackupPath)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func addTar(tw *tar.Writer, pth string) (err error) {
|
|
||||||
var tr *tar.Reader
|
|
||||||
var rc io.ReadCloser
|
|
||||||
var hdr *tar.Header
|
|
||||||
|
|
||||||
if tr, rc, err = openTarFile(pth); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
if hdr, err = tr.Next(); err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err = tw.WriteHeader(hdr); err != nil {
|
|
||||||
break
|
|
||||||
} else if _, err = io.Copy(tw, tr); err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
err = rc.Close()
|
|
||||||
} else {
|
|
||||||
rc.Close()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func openTarFile(pth string) (tr *tar.Reader, rc io.ReadCloser, err error) {
|
|
||||||
var fin *os.File
|
|
||||||
var n int
|
|
||||||
buff := make([]byte, 1024)
|
|
||||||
|
|
||||||
if fin, err = os.Open(pth); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if n, err = fin.Read(buff); err != nil {
|
|
||||||
fin.Close()
|
|
||||||
return
|
|
||||||
} else if n == 0 {
|
|
||||||
fin.Close()
|
|
||||||
err = fmt.Errorf("%s is empty", pth)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = fin.Seek(0, 0); err != nil {
|
|
||||||
fin.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = fin
|
|
||||||
tr = tar.NewReader(rc)
|
|
||||||
|
|
||||||
return tr, rc, nil
|
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,7 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/autocomplete"
|
"coopcloud.tech/abra/pkg/autocomplete"
|
||||||
@ -12,212 +10,112 @@ import (
|
|||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
containerPkg "coopcloud.tech/abra/pkg/container"
|
containerPkg "coopcloud.tech/abra/pkg/container"
|
||||||
"coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/service"
|
||||||
"coopcloud.tech/abra/pkg/upstream/container"
|
"coopcloud.tech/abra/pkg/upstream/container"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
dockerClient "github.com/docker/docker/client"
|
|
||||||
"github.com/docker/docker/pkg/archive"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
type restoreConfig struct {
|
var targetPath string
|
||||||
preHookCmd string
|
var targetPathFlag = &cli.StringFlag{
|
||||||
postHookCmd string
|
Name: "target, t",
|
||||||
|
Usage: "Target path",
|
||||||
|
Destination: &targetPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
var appRestoreCommand = cli.Command{
|
var appRestoreCommand = cli.Command{
|
||||||
Name: "restore",
|
Name: "restore",
|
||||||
Aliases: []string{"rs"},
|
Aliases: []string{"rs"},
|
||||||
Usage: "Run app restore",
|
Usage: "Restore an app backup",
|
||||||
ArgsUsage: "<domain> <service> <file>",
|
ArgsUsage: "<domain> <service>",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
internal.DebugFlag,
|
internal.DebugFlag,
|
||||||
internal.OfflineFlag,
|
internal.OfflineFlag,
|
||||||
internal.ChaosFlag,
|
targetPathFlag,
|
||||||
},
|
},
|
||||||
Before: internal.SubCommandBefore,
|
Before: internal.SubCommandBefore,
|
||||||
BashComplete: autocomplete.AppNameComplete,
|
BashComplete: autocomplete.AppNameComplete,
|
||||||
Description: `
|
|
||||||
Run an app restore.
|
|
||||||
|
|
||||||
Pre/post hook commands are defined in the recipe configuration. Abra reads this
|
|
||||||
configuration and run the comands in the context of the service before
|
|
||||||
restoring the backup.
|
|
||||||
|
|
||||||
Unlike "abra app backup", restore must be run on a per-service basis. You can
|
|
||||||
not restore all services in one go. Backup files produced by Abra are
|
|
||||||
compressed archives which use absolute paths. This allows Abra to restore
|
|
||||||
according to standard tar command logic, i.e. the backup will be restored to
|
|
||||||
the path it was originally backed up from.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
abra app restore example.com app ~/.abra/backups/example_com_app_609341138.tar.gz
|
|
||||||
`,
|
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
app := internal.ValidateApp(c)
|
app := internal.ValidateApp(c)
|
||||||
|
|
||||||
recipe, err := recipe.Get(app.Recipe, internal.Offline)
|
if err := recipe.EnsureExists(app.Recipe); err != nil {
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !internal.Chaos {
|
if !internal.Chaos {
|
||||||
if err := recipePkg.EnsureIsClean(app.Recipe); err != nil {
|
if err := recipe.EnsureIsClean(app.Recipe); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !internal.Offline {
|
if !internal.Offline {
|
||||||
if err := recipePkg.EnsureUpToDate(app.Recipe); err != nil {
|
if err := recipe.EnsureUpToDate(app.Recipe); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := recipePkg.EnsureLatest(app.Recipe); err != nil {
|
if err := recipe.EnsureLatest(app.Recipe); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceName := c.Args().Get(1)
|
|
||||||
if serviceName == "" {
|
|
||||||
internal.ShowSubcommandHelpAndError(c, errors.New("missing <service>?"))
|
|
||||||
}
|
|
||||||
|
|
||||||
backupPath := c.Args().Get(2)
|
|
||||||
if backupPath == "" {
|
|
||||||
internal.ShowSubcommandHelpAndError(c, errors.New("missing <file>?"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := os.Stat(backupPath); err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
logrus.Fatalf("%s doesn't exist?", backupPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
restoreConfigs := make(map[string]restoreConfig)
|
|
||||||
for _, service := range recipe.Config.Services {
|
|
||||||
if restoreEnabled, ok := service.Deploy.Labels["backupbot.restore"]; ok {
|
|
||||||
if restoreEnabled == "true" {
|
|
||||||
fullServiceName := fmt.Sprintf("%s_%s", app.StackName(), service.Name)
|
|
||||||
rsConfig := restoreConfig{}
|
|
||||||
|
|
||||||
logrus.Debugf("restore config detected for %s", fullServiceName)
|
|
||||||
|
|
||||||
if preHookCmd, ok := service.Deploy.Labels["backupbot.restore.pre-hook"]; ok {
|
|
||||||
logrus.Debugf("detected pre-hook command for %s: %s", fullServiceName, preHookCmd)
|
|
||||||
rsConfig.preHookCmd = preHookCmd
|
|
||||||
}
|
|
||||||
|
|
||||||
if postHookCmd, ok := service.Deploy.Labels["backupbot.restore.post-hook"]; ok {
|
|
||||||
logrus.Debugf("detected post-hook command for %s: %s", fullServiceName, postHookCmd)
|
|
||||||
rsConfig.postHookCmd = postHookCmd
|
|
||||||
}
|
|
||||||
|
|
||||||
restoreConfigs[service.Name] = rsConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rsConfig, ok := restoreConfigs[serviceName]
|
|
||||||
if !ok {
|
|
||||||
rsConfig = restoreConfig{}
|
|
||||||
}
|
|
||||||
|
|
||||||
cl, err := client.New(app.Server)
|
cl, err := client.New(app.Server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := runRestore(cl, app, backupPath, serviceName, rsConfig); err != nil {
|
chosenService, err := service.GetServiceByLabel(context.Background(), cl, config.BackupbotLabel, internal.NoInput)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("retrieved %s as backup enabled service", chosenService.Spec.Name)
|
||||||
|
|
||||||
|
filters := filters.NewArgs()
|
||||||
|
filters.Add("name", chosenService.Spec.Name)
|
||||||
|
targetContainer, err := containerPkg.GetContainer(
|
||||||
|
context.Background(),
|
||||||
|
cl,
|
||||||
|
filters,
|
||||||
|
internal.NoInput,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
execEnv := []string{fmt.Sprintf("SERVICE=%s", app.Domain)}
|
||||||
|
if snapshot != "" {
|
||||||
|
logrus.Debugf("including SNAPSHOT=%s in backupbot exec invocation", snapshot)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("SNAPSHOT=%s", snapshot))
|
||||||
|
}
|
||||||
|
if targetPath != "" {
|
||||||
|
logrus.Debugf("including TARGET=%s in backupbot exec invocation", targetPath)
|
||||||
|
execEnv = append(execEnv, fmt.Sprintf("TARGET=%s", targetPath))
|
||||||
|
}
|
||||||
|
execBackupListOpts := types.ExecConfig{
|
||||||
|
AttachStderr: true,
|
||||||
|
AttachStdin: true,
|
||||||
|
AttachStdout: true,
|
||||||
|
Cmd: []string{"/usr/bin/backup", "--", "restore"},
|
||||||
|
Detach: false,
|
||||||
|
Env: execEnv,
|
||||||
|
Tty: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("running backup list on %s with exec config %v", targetContainer.ID, execBackupListOpts)
|
||||||
|
|
||||||
|
// FIXME: avoid instantiating a new CLI
|
||||||
|
dcli, err := command.NewDockerCli()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := container.RunExec(dcli, cl, targetContainer.ID, &execBackupListOpts); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// runRestore does the actual restore logic.
|
|
||||||
func runRestore(cl *dockerClient.Client, app config.App, backupPath, serviceName string, rsConfig restoreConfig) error {
|
|
||||||
// FIXME: avoid instantiating a new CLI
|
|
||||||
dcli, err := command.NewDockerCli()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
filters := filters.NewArgs()
|
|
||||||
filters.Add("name", fmt.Sprintf("^%s_%s", app.StackName(), serviceName))
|
|
||||||
|
|
||||||
targetContainer, err := containerPkg.GetContainer(context.Background(), cl, filters, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fullServiceName := fmt.Sprintf("%s_%s", app.StackName(), serviceName)
|
|
||||||
if rsConfig.preHookCmd != "" {
|
|
||||||
splitCmd := internal.SafeSplit(rsConfig.preHookCmd)
|
|
||||||
|
|
||||||
logrus.Debugf("split pre-hook command for %s into %s", fullServiceName, splitCmd)
|
|
||||||
|
|
||||||
preHookExecOpts := types.ExecConfig{
|
|
||||||
AttachStderr: true,
|
|
||||||
AttachStdin: true,
|
|
||||||
AttachStdout: true,
|
|
||||||
Cmd: splitCmd,
|
|
||||||
Detach: false,
|
|
||||||
Tty: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := container.RunExec(dcli, cl, targetContainer.ID, &preHookExecOpts); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("succesfully ran %s pre-hook command: %s", fullServiceName, rsConfig.preHookCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
backupReader, err := os.Open(backupPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
content, err := archive.DecompressStream(backupReader)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(d1): we use absolute paths so tar knows what to do. it will restore
|
|
||||||
// files according to the paths set in the compressed archive
|
|
||||||
restorePath := "/"
|
|
||||||
|
|
||||||
copyOpts := types.CopyToContainerOptions{AllowOverwriteDirWithFile: false, CopyUIDGID: false}
|
|
||||||
if err := cl.CopyToContainer(context.Background(), targetContainer.ID, restorePath, content, copyOpts); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("restored %s to %s", backupPath, fullServiceName)
|
|
||||||
|
|
||||||
if rsConfig.postHookCmd != "" {
|
|
||||||
splitCmd := internal.SafeSplit(rsConfig.postHookCmd)
|
|
||||||
|
|
||||||
logrus.Debugf("split post-hook command for %s into %s", fullServiceName, splitCmd)
|
|
||||||
|
|
||||||
postHookExecOpts := types.ExecConfig{
|
|
||||||
AttachStderr: true,
|
|
||||||
AttachStdin: true,
|
|
||||||
AttachStdout: true,
|
|
||||||
Cmd: splitCmd,
|
|
||||||
Detach: false,
|
|
||||||
Tty: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := container.RunExec(dcli, cl, targetContainer.ID, &postHookExecOpts); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Infof("succesfully ran %s post-hook command: %s", fullServiceName, rsConfig.postHookCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -173,7 +173,7 @@ keys configured on your account.
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg := "chore: publish new catalogue release changes"
|
msg := "chore: publish new catalogue release changes"
|
||||||
if err := gitPkg.Commit(cataloguePath, msg, internal.Dry); err != nil {
|
if err := gitPkg.Commit(cataloguePath, "**.json", msg, internal.Dry); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
package recipe
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"coopcloud.tech/abra/cli/internal"
|
|
||||||
"coopcloud.tech/abra/pkg/autocomplete"
|
|
||||||
"coopcloud.tech/abra/pkg/config"
|
|
||||||
gitPkg "coopcloud.tech/abra/pkg/git"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
var recipeDiffCommand = cli.Command{
|
|
||||||
Name: "diff",
|
|
||||||
Usage: "Show unstaged changes in recipe config",
|
|
||||||
Description: "Due to limitations in our underlying Git dependency, this command requires /usr/bin/git.",
|
|
||||||
Aliases: []string{"d"},
|
|
||||||
ArgsUsage: "<recipe>",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
internal.DebugFlag,
|
|
||||||
internal.NoInputFlag,
|
|
||||||
},
|
|
||||||
Before: internal.SubCommandBefore,
|
|
||||||
BashComplete: autocomplete.RecipeNameComplete,
|
|
||||||
Action: func(c *cli.Context) error {
|
|
||||||
recipeName := c.Args().First()
|
|
||||||
|
|
||||||
if recipeName != "" {
|
|
||||||
internal.ValidateRecipe(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
recipeDir := path.Join(config.RECIPES_DIR, recipeName)
|
|
||||||
if err := gitPkg.DiffUnstaged(recipeDir); err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
@ -30,7 +30,5 @@ manner. Abra supports convenient automation for recipe maintainenace, see the
|
|||||||
recipeSyncCommand,
|
recipeSyncCommand,
|
||||||
recipeUpgradeCommand,
|
recipeUpgradeCommand,
|
||||||
recipeVersionCommand,
|
recipeVersionCommand,
|
||||||
recipeResetCommand,
|
|
||||||
recipeDiffCommand,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -106,18 +106,6 @@ your SSH keys configured on your account.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isClean, err := gitPkg.IsClean(recipe.Dir())
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isClean {
|
|
||||||
logrus.Infof("%s currently has these unstaged changes 👇", recipe.Name)
|
|
||||||
if err := gitPkg.DiffUnstaged(recipe.Dir()); err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(tags) > 0 {
|
if len(tags) > 0 {
|
||||||
logrus.Warnf("previous git tags detected, assuming this is a new semver release")
|
logrus.Warnf("previous git tags detected, assuming this is a new semver release")
|
||||||
if err := createReleaseFromPreviousTag(tagString, mainAppVersion, recipe, tags); err != nil {
|
if err := createReleaseFromPreviousTag(tagString, mainAppVersion, recipe, tags); err != nil {
|
||||||
@ -256,7 +244,7 @@ func commitRelease(recipe recipe.Recipe, tag string) error {
|
|||||||
|
|
||||||
msg := fmt.Sprintf("chore: publish %s release", tag)
|
msg := fmt.Sprintf("chore: publish %s release", tag)
|
||||||
repoPath := path.Join(config.RECIPES_DIR, recipe.Name)
|
repoPath := path.Join(config.RECIPES_DIR, recipe.Name)
|
||||||
if err := gitPkg.Commit(repoPath, msg, internal.Dry); err != nil {
|
if err := gitPkg.Commit(repoPath, ".", msg, internal.Dry); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
package recipe
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"coopcloud.tech/abra/cli/internal"
|
|
||||||
"coopcloud.tech/abra/pkg/autocomplete"
|
|
||||||
"coopcloud.tech/abra/pkg/config"
|
|
||||||
"github.com/go-git/go-git/v5"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
var recipeResetCommand = cli.Command{
|
|
||||||
Name: "reset",
|
|
||||||
Usage: "Remove all unstaged changes from recipe config",
|
|
||||||
Description: "WARNING, this will delete your changes. Be Careful.",
|
|
||||||
Aliases: []string{"rs"},
|
|
||||||
ArgsUsage: "<recipe>",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
internal.DebugFlag,
|
|
||||||
internal.NoInputFlag,
|
|
||||||
},
|
|
||||||
Before: internal.SubCommandBefore,
|
|
||||||
BashComplete: autocomplete.RecipeNameComplete,
|
|
||||||
Action: func(c *cli.Context) error {
|
|
||||||
recipeName := c.Args().First()
|
|
||||||
|
|
||||||
if recipeName != "" {
|
|
||||||
internal.ValidateRecipe(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
repoPath := path.Join(config.RECIPES_DIR, recipeName)
|
|
||||||
repo, err := git.PlainOpen(repoPath)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ref, err := repo.Head()
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
worktree, err := repo.Worktree()
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := &git.ResetOptions{Commit: ref.Hash(), Mode: git.HardReset}
|
|
||||||
if err := worktree.Reset(opts); err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
@ -8,7 +8,6 @@ import (
|
|||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/autocomplete"
|
"coopcloud.tech/abra/pkg/autocomplete"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
gitPkg "coopcloud.tech/abra/pkg/git"
|
|
||||||
"coopcloud.tech/tagcmp"
|
"coopcloud.tech/tagcmp"
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
@ -199,17 +198,6 @@ likely to change.
|
|||||||
logrus.Infof("dry run: not syncing label %s for recipe %s", nextTag, recipe.Name)
|
logrus.Infof("dry run: not syncing label %s for recipe %s", nextTag, recipe.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
isClean, err := gitPkg.IsClean(recipe.Dir())
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
if !isClean {
|
|
||||||
logrus.Infof("%s currently has these unstaged changes 👇", recipe.Name)
|
|
||||||
if err := gitPkg.DiffUnstaged(recipe.Dir()); err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
"coopcloud.tech/abra/pkg/formatter"
|
"coopcloud.tech/abra/pkg/formatter"
|
||||||
gitPkg "coopcloud.tech/abra/pkg/git"
|
|
||||||
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
||||||
"coopcloud.tech/tagcmp"
|
"coopcloud.tech/tagcmp"
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
@ -327,7 +326,6 @@ You may invoke this command in "wizard" mode and be prompted for input:
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(jsonstring))
|
fmt.Println(string(jsonstring))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -338,18 +336,6 @@ You may invoke this command in "wizard" mode and be prompted for input:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isClean, err := gitPkg.IsClean(recipeDir)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
if !isClean {
|
|
||||||
logrus.Infof("%s currently has these unstaged changes 👇", recipe.Name)
|
|
||||||
if err := gitPkg.DiffUnstaged(recipeDir); err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,8 @@ var REPOS_BASE_URL = "https://git.coopcloud.tech/coop-cloud"
|
|||||||
var CATALOGUE_JSON_REPO_NAME = "recipes-catalogue-json"
|
var CATALOGUE_JSON_REPO_NAME = "recipes-catalogue-json"
|
||||||
var SSH_URL_TEMPLATE = "ssh://git@git.coopcloud.tech:2222/coop-cloud/%s.git"
|
var SSH_URL_TEMPLATE = "ssh://git@git.coopcloud.tech:2222/coop-cloud/%s.git"
|
||||||
|
|
||||||
|
var BackupbotLabel = "coop-cloud.backupbot.enabled"
|
||||||
|
|
||||||
// envVarModifiers is a list of env var modifier strings. These are added to
|
// envVarModifiers is a list of env var modifier strings. These are added to
|
||||||
// env vars as comments and modify their processing by Abra, e.g. determining
|
// env vars as comments and modify their processing by Abra, e.g. determining
|
||||||
// how long secrets should be.
|
// how long secrets should be.
|
||||||
|
@ -28,7 +28,7 @@ func GetContainer(c context.Context, cl *client.Client, filters filters.Args, no
|
|||||||
return types.Container{}, fmt.Errorf("no containers matching the %v filter found?", filter)
|
return types.Container{}, fmt.Errorf("no containers matching the %v filter found?", filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(containers) != 1 {
|
if len(containers) > 1 {
|
||||||
var containersRaw []string
|
var containersRaw []string
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
containerName := strings.Join(container.Names, " ")
|
containerName := strings.Join(container.Names, " ")
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Commit runs a git commit
|
// Commit runs a git commit
|
||||||
func Commit(repoPath, commitMessage string, dryRun bool) error {
|
func Commit(repoPath, glob, commitMessage string, dryRun bool) error {
|
||||||
if commitMessage == "" {
|
if commitMessage == "" {
|
||||||
return fmt.Errorf("no commit message specified?")
|
return fmt.Errorf("no commit message specified?")
|
||||||
}
|
}
|
||||||
@ -33,8 +33,17 @@ func Commit(repoPath, commitMessage string, dryRun bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !dryRun {
|
if !dryRun {
|
||||||
// NOTE(d1): `All: true` does not include untracked files
|
err = commitWorktree.AddGlob(glob)
|
||||||
_, err = commitWorktree.Commit(commitMessage, &git.CommitOptions{All: true})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logrus.Debugf("staged %s for commit", glob)
|
||||||
|
} else {
|
||||||
|
logrus.Debugf("dry run: did not stage %s for commit", glob)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dryRun {
|
||||||
|
_, err = commitWorktree.Commit(commitMessage, &git.CommitOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os/exec"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// getGitDiffArgs builds the `git diff` invocation args. It removes the usage
|
|
||||||
// of a pager and ensures that colours are specified even when Git might detect
|
|
||||||
// otherwise.
|
|
||||||
func getGitDiffArgs(repoPath string) []string {
|
|
||||||
return []string{
|
|
||||||
"-C",
|
|
||||||
repoPath,
|
|
||||||
"--no-pager",
|
|
||||||
"-c",
|
|
||||||
"color.diff=always",
|
|
||||||
"diff",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffUnstaged shows a `git diff`. Due to limitations in the underlying go-git
|
|
||||||
// library, this implementation requires the /usr/bin/git binary. It gracefully
|
|
||||||
// skips if it cannot find the command on the system.
|
|
||||||
func DiffUnstaged(path string) error {
|
|
||||||
if _, err := exec.LookPath("git"); err != nil {
|
|
||||||
logrus.Warnf("unable to locate git command, cannot output diff")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
gitDiffArgs := getGitDiffArgs(path)
|
|
||||||
diff, err := exec.Command("git", gitDiffArgs...).Output()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(string(diff))
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -14,6 +14,69 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetService retrieves a service container based on a label. If prompt is true
|
||||||
|
// and the retrievd count of service containers does not match 1, then a prompt
|
||||||
|
// is presented to let the user choose. A count of 0 is handled gracefully.
|
||||||
|
func GetServiceByLabel(c context.Context, cl *client.Client, label string, prompt bool) (swarm.Service, error) {
|
||||||
|
services, err := cl.ServiceList(c, types.ServiceListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return swarm.Service{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(services) == 0 {
|
||||||
|
return swarm.Service{}, fmt.Errorf("no services deployed?")
|
||||||
|
}
|
||||||
|
|
||||||
|
var backupServices []swarm.Service
|
||||||
|
for _, service := range services {
|
||||||
|
if enabled, exists := service.Spec.Labels[label]; exists && enabled == "true" {
|
||||||
|
backupServices = append(backupServices, service)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(backupServices) == 0 {
|
||||||
|
return swarm.Service{}, fmt.Errorf("no backup services deployed?")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(backupServices) > 1 {
|
||||||
|
var servicesRaw []string
|
||||||
|
for _, service := range backupServices {
|
||||||
|
serviceName := service.Spec.Name
|
||||||
|
created := formatter.HumanDuration(service.CreatedAt.Unix())
|
||||||
|
servicesRaw = append(servicesRaw, fmt.Sprintf("%s (created %v)", serviceName, created))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !prompt {
|
||||||
|
err := fmt.Errorf("expected 1 service but found %v: %s", len(backupServices), strings.Join(servicesRaw, " "))
|
||||||
|
return swarm.Service{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Warnf("ambiguous service list received, prompting for input")
|
||||||
|
|
||||||
|
var response string
|
||||||
|
prompt := &survey.Select{
|
||||||
|
Message: "which service are you looking for?",
|
||||||
|
Options: servicesRaw,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := survey.AskOne(prompt, &response); err != nil {
|
||||||
|
return swarm.Service{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chosenService := strings.TrimSpace(strings.Split(response, " ")[0])
|
||||||
|
for _, service := range backupServices {
|
||||||
|
serviceName := strings.ToLower(service.Spec.Name)
|
||||||
|
if serviceName == chosenService {
|
||||||
|
return service, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Panic("failed to match chosen service")
|
||||||
|
}
|
||||||
|
|
||||||
|
return backupServices[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetService retrieves a service container. If prompt is true and the retrievd
|
// GetService retrieves a service container. If prompt is true and the retrievd
|
||||||
// count of service containers does not match 1, then a prompt is presented to
|
// count of service containers does not match 1, then a prompt is presented to
|
||||||
// let the user choose. A count of 0 is handled gracefully.
|
// let the user choose. A count of 0 is handled gracefully.
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
setup() {
|
|
||||||
load "$PWD/tests/integration/helpers/common"
|
|
||||||
_common_setup
|
|
||||||
}
|
|
||||||
|
|
||||||
@test "show unstaged changes" {
|
|
||||||
run $ABRA recipe diff "$TEST_RECIPE"
|
|
||||||
assert_success
|
|
||||||
refute_output --partial 'traefik.enable'
|
|
||||||
|
|
||||||
run sed -i '/traefik.enable=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"
|
|
||||||
assert_success
|
|
||||||
|
|
||||||
run $ABRA recipe diff "$TEST_RECIPE"
|
|
||||||
assert_success
|
|
||||||
assert_output --partial 'traefik.enable'
|
|
||||||
|
|
||||||
_reset_recipe
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
setup() {
|
|
||||||
load "$PWD/tests/integration/helpers/common"
|
|
||||||
_common_setup
|
|
||||||
}
|
|
||||||
|
|
||||||
@test "reset unstaged changes" {
|
|
||||||
run $ABRA recipe fetch "$TEST_RECIPE"
|
|
||||||
assert_success
|
|
||||||
|
|
||||||
run sed -i '/traefik.enable=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"
|
|
||||||
assert_success
|
|
||||||
|
|
||||||
run $ABRA recipe diff "$TEST_RECIPE"
|
|
||||||
assert_success
|
|
||||||
assert_output --partial 'traefik.enable'
|
|
||||||
|
|
||||||
run $ABRA recipe reset "$TEST_RECIPE"
|
|
||||||
assert_success
|
|
||||||
|
|
||||||
run $ABRA recipe diff "$TEST_RECIPE"
|
|
||||||
assert_success
|
|
||||||
refute_output --partial 'traefik.enable'
|
|
||||||
}
|
|
Reference in New Issue
Block a user