150 lines
4.1 KiB
Go
150 lines
4.1 KiB
Go
package space_worker
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"playbookctl/internal/utils"
|
|
"time"
|
|
)
|
|
|
|
func (app *SpaceWorker) Restore(targetHost string, timestamp string, inventory string, roles ...string) error {
|
|
app.log.Debug(fmt.Sprintf("workDir: %s", app.workDir))
|
|
|
|
var host string
|
|
if targetHost != "" {
|
|
host = targetHost
|
|
} else {
|
|
var err error
|
|
host, err = app.GetDefaultHost()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
preRestoreFile := fmt.Sprintf("/tmp/%s", time.Now().Format("20060102_150405"))
|
|
|
|
ansibleArgs, err := app.setupPreRestoreAnsibleArgs(app.AnsibleVerbose, host, roles, preRestoreFile, timestamp, inventory)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
command := exec.Command(app.AnsibleBin, ansibleArgs...)
|
|
command.Dir = app.workDir
|
|
command.Stdin = os.Stdin
|
|
command.Stdout = os.Stdout
|
|
command.Stderr = os.Stderr
|
|
|
|
app.log.Info(fmt.Sprintf("Target Host: %s", host))
|
|
|
|
app.log.Info("Pre restore Ansible")
|
|
app.log.Debug(fmt.Sprintf("ansible command line: %s", command.Args))
|
|
err = command.Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
app.log.Info("Send files")
|
|
command = exec.Command("/bin/bash", "-c", preRestoreFile)
|
|
command.Dir = app.workDir
|
|
command.Stdin = os.Stdin
|
|
command.Stdout = os.Stdout
|
|
command.Stderr = os.Stderr
|
|
_ = command.Run()
|
|
|
|
app.log.Info("Restore Ansible")
|
|
ansibleArgs, err = app.setupRestoreAnsibleArgs(app.AnsibleVerbose, host, roles, preRestoreFile, timestamp, inventory)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
command = exec.Command(app.AnsibleBin, ansibleArgs...)
|
|
command.Dir = app.workDir
|
|
command.Stdin = os.Stdin
|
|
command.Stdout = os.Stdout
|
|
command.Stderr = os.Stderr
|
|
err = command.Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
app.log.Info("Clean temp files")
|
|
command = exec.Command("/bin/bash", "-c", preRestoreFile+"_clean")
|
|
command.Dir = app.workDir
|
|
command.Stdin = os.Stdin
|
|
command.Stdout = os.Stdout
|
|
command.Stderr = os.Stderr
|
|
err = command.Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = utils.RemoveFile(preRestoreFile); err != nil {
|
|
return err
|
|
}
|
|
if err = utils.RemoveFile(preRestoreFile + "_clean"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (app *SpaceWorker) setupPreRestoreAnsibleArgs(verbose uint8, targetHost string, roles []string, preRestoreFile string, timestamp string, inventory string) ([]string, error) {
|
|
var ansibleArgs []string
|
|
|
|
switch verbose {
|
|
case 1:
|
|
ansibleArgs = append(ansibleArgs, "-v")
|
|
case 2:
|
|
ansibleArgs = append(ansibleArgs, "-vv")
|
|
case 3:
|
|
ansibleArgs = append(ansibleArgs, "-vvv")
|
|
}
|
|
|
|
ansibleArgs = append(ansibleArgs, "-i", "hosts.yml")
|
|
ansibleArgs = append(ansibleArgs, "-l", targetHost)
|
|
|
|
if len(roles) > 0 {
|
|
for _, role := range roles {
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", fmt.Sprintf("dd_prerestore_%s=true", role))
|
|
}
|
|
} else {
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", "dd_prerestore=true")
|
|
}
|
|
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", fmt.Sprintf("dd_restore_datetime=%s", timestamp))
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", fmt.Sprintf("dd_restore_inventory=%s", inventory))
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", fmt.Sprintf("dd_prerestore_file=%s", preRestoreFile))
|
|
|
|
return append(ansibleArgs, "playbook.yml"), nil
|
|
}
|
|
|
|
func (app *SpaceWorker) setupRestoreAnsibleArgs(verbose uint8, targetHost string, roles []string, preRestoreFile string, timestamp string, inventory string) ([]string, error) {
|
|
var ansibleArgs []string
|
|
|
|
switch verbose {
|
|
case 1:
|
|
ansibleArgs = append(ansibleArgs, "-v")
|
|
case 2:
|
|
ansibleArgs = append(ansibleArgs, "-vv")
|
|
case 3:
|
|
ansibleArgs = append(ansibleArgs, "-vvv")
|
|
}
|
|
|
|
ansibleArgs = append(ansibleArgs, "-i", "hosts.yml")
|
|
ansibleArgs = append(ansibleArgs, "-l", targetHost)
|
|
|
|
if len(roles) > 0 {
|
|
for _, role := range roles {
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", fmt.Sprintf("dd_restore_%s=true", role))
|
|
}
|
|
} else {
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", "dd_restore=true")
|
|
}
|
|
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", fmt.Sprintf("dd_restore_datetime=%s", timestamp))
|
|
ansibleArgs = append(ansibleArgs, "--extra-vars", fmt.Sprintf("dd_restore_inventory=%s", inventory))
|
|
|
|
return append(ansibleArgs, "playbook.yml"), nil
|
|
}
|