2021-08-02 01:10:41 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2021-08-29 14:25:31 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-08-29 14:25:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-08-02 01:10:41 +00:00
|
|
|
)
|
|
|
|
|
2021-08-02 06:36:35 +00:00
|
|
|
var restoreAllServices bool
|
|
|
|
var restoreAllServicesFlag = &cli.BoolFlag{
|
2022-01-18 13:13:20 +00:00
|
|
|
Name: "all, a",
|
2021-08-02 06:36:35 +00:00
|
|
|
Destination: &restoreAllServices,
|
|
|
|
Usage: "Restore all services",
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
var appRestoreCommand = cli.Command{
|
|
|
|
Name: "restore",
|
|
|
|
Aliases: []string{"rs"},
|
|
|
|
Usage: "Restore an app from a backup",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
internal.DebugFlag,
|
|
|
|
internal.NoInputFlag,
|
|
|
|
restoreAllServicesFlag,
|
|
|
|
},
|
|
|
|
Before: internal.SubCommandBefore,
|
2021-08-02 01:10:41 +00:00
|
|
|
ArgsUsage: "<service> [<backup file>]",
|
2021-08-29 14:25:31 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-09-05 21:17:35 +00:00
|
|
|
app := internal.ValidateApp(c)
|
2021-08-29 14:25:31 +00:00
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
if len(c.Args()) > 1 && restoreAllServices {
|
2021-08-29 14:25:31 +00:00
|
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use <service>/<backup file> and '--all' together"))
|
|
|
|
}
|
|
|
|
|
2021-12-25 13:04:07 +00:00
|
|
|
abraSh := path.Join(config.RECIPES_DIR, app.Type, "abra.sh")
|
2021-08-29 14:25:31 +00:00
|
|
|
if _, err := os.Stat(abraSh); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2021-12-25 01:03:09 +00:00
|
|
|
logrus.Fatalf("%s does not exist?", abraSh)
|
2021-08-29 14:25:31 +00:00
|
|
|
}
|
|
|
|
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) {
|
2021-12-25 01:03:09 +00:00
|
|
|
logrus.Fatalf("%s doesn't have a %s function", app.Type, execCmd)
|
2021-08-29 14:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-09-10 22:54:02 +00:00
|
|
|
if err := internal.RunCmd(cmd); err != nil {
|
2021-08-29 14:25:31 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|