feat: add restore command

This commit is contained in:
decentral1se 2021-08-29 16:25:31 +02:00
parent ddbe9ffcb5
commit db4908c3ae
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 75 additions and 1 deletions

View File

@ -22,7 +22,7 @@
- [x] `cp`
- [x] `logs`
- [x] `ps`
- [ ] `restore` (WIP: decentral1se)
- [x] `restore`
- [x] `rm`
- [ ] `run`
- [ ] `rollback`

View File

@ -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: "<service> [<backup file>]",
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 <service>/<backup file> 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
},
}