forked from coop-cloud/abra
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"strings"
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
"coopcloud.tech/abra/pkg/config"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var restoreAllServices bool
|
|
var restoreAllServicesFlag = &cli.BoolFlag{
|
|
Name: "all",
|
|
Value: false,
|
|
Destination: &restoreAllServices,
|
|
Aliases: []string{"a"},
|
|
Usage: "Restore all services",
|
|
}
|
|
|
|
var appRestoreCommand = &cli.Command{
|
|
Name: "restore",
|
|
Usage: "Restore an app from a backup",
|
|
Aliases: []string{"r"},
|
|
Flags: []cli.Flag{restoreAllServicesFlag},
|
|
ArgsUsage: "<service> [<backup file>]",
|
|
Action: func(c *cli.Context) error {
|
|
app := internal.ValidateApp(c)
|
|
|
|
if c.Args().Len() > 1 && restoreAllServices {
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use <service>/<backup file> and '--all' together"))
|
|
}
|
|
|
|
abraSh := path.Join(config.ABRA_DIR, "apps", app.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", app.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)
|
|
if err := internal.RunCmd(cmd); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|