feat: add restore command
This commit is contained in:
parent
ddbe9ffcb5
commit
db4908c3ae
2
TODO.md
2
TODO.md
@ -22,7 +22,7 @@
|
|||||||
- [x] `cp`
|
- [x] `cp`
|
||||||
- [x] `logs`
|
- [x] `logs`
|
||||||
- [x] `ps`
|
- [x] `ps`
|
||||||
- [ ] `restore` (WIP: decentral1se)
|
- [x] `restore`
|
||||||
- [x] `rm`
|
- [x] `rm`
|
||||||
- [ ] `run`
|
- [ ] `run`
|
||||||
- [ ] `rollback`
|
- [ ] `rollback`
|
||||||
|
@ -1,6 +1,17 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
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"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,6 +20,7 @@ var restoreAllServicesFlag = &cli.BoolFlag{
|
|||||||
Name: "all",
|
Name: "all",
|
||||||
Value: false,
|
Value: false,
|
||||||
Destination: &restoreAllServices,
|
Destination: &restoreAllServices,
|
||||||
|
Aliases: []string{"a"},
|
||||||
Usage: "Restore all services",
|
Usage: "Restore all services",
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,4 +28,66 @@ var appRestoreCommand = &cli.Command{
|
|||||||
Name: "restore",
|
Name: "restore",
|
||||||
Flags: []cli.Flag{restoreAllServicesFlag},
|
Flags: []cli.Flag{restoreAllServicesFlag},
|
||||||
ArgsUsage: "<service> [<backup file>]",
|
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
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user