From db4908c3ae154919e12142e3ca69e9d5851e87c2 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Sun, 29 Aug 2021 16:25:31 +0200 Subject: [PATCH] feat: add restore command --- TODO.md | 2 +- cli/app/restore.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 5bef1af5..27fd932f 100644 --- a/TODO.md +++ b/TODO.md @@ -22,7 +22,7 @@ - [x] `cp` - [x] `logs` - [x] `ps` - - [ ] `restore` (WIP: decentral1se) + - [x] `restore` - [x] `rm` - [ ] `run` - [ ] `rollback` diff --git a/cli/app/restore.go b/cli/app/restore.go index d163f553..b98d1680 100644 --- a/cli/app/restore.go +++ b/cli/app/restore.go @@ -1,6 +1,17 @@ package app import ( + "errors" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path" + "strings" + + "coopcloud.tech/abra/cli/internal" + "coopcloud.tech/abra/config" + "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -9,6 +20,7 @@ var restoreAllServicesFlag = &cli.BoolFlag{ Name: "all", Value: false, Destination: &restoreAllServices, + Aliases: []string{"a"}, Usage: "Restore all services", } @@ -16,4 +28,66 @@ var appRestoreCommand = &cli.Command{ Name: "restore", Flags: []cli.Flag{restoreAllServicesFlag}, ArgsUsage: " []", + Action: func(c *cli.Context) error { + appName := c.Args().First() + if appName == "" { + internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided")) + } + + if c.Args().Len() > 1 && restoreAllServices { + internal.ShowSubcommandHelpAndError(c, errors.New("cannot use / and '--all' together")) + } + + appFiles, err := config.LoadAppFiles("") + if err != nil { + logrus.Fatal(err) + } + + appEnv, err := config.GetApp(appFiles, appName) + if err != nil { + logrus.Fatal(err) + } + + abraSh := path.Join(config.ABRA_DIR, "apps", appEnv.Type, "abra.sh") + if _, err := os.Stat(abraSh); err != nil { + if os.IsNotExist(err) { + logrus.Fatalf("'%s' does not exist?", abraSh) + } + logrus.Fatal(err) + } + + sourceCmd := fmt.Sprintf("source %s", abraSh) + execCmd := "abra_restore" + if !restoreAllServices { + serviceName := c.Args().Get(1) + if serviceName == "" { + internal.ShowSubcommandHelpAndError(c, errors.New("no service(s) target provided")) + } + execCmd = fmt.Sprintf("abra_restore_%s", serviceName) + } + + bytes, err := ioutil.ReadFile(abraSh) + if err != nil { + logrus.Fatal(err) + } + if !strings.Contains(string(bytes), execCmd) { + logrus.Fatalf("%s doesn't have a '%s' function", appEnv.Type, execCmd) + } + + backupFile := c.Args().Get(2) + if backupFile != "" { + execCmd = fmt.Sprintf("%s %s", execCmd, backupFile) + } + + sourceAndExec := fmt.Sprintf("%s; %s", sourceCmd, execCmd) + cmd := exec.Command("bash", "-c", sourceAndExec) + output, err := cmd.Output() + if err != nil { + logrus.Fatal(err) + } + + fmt.Print(string(output)) + + return nil + }, }