Skip to content

Commit

Permalink
Post backup/restore status update (#691)
Browse files Browse the repository at this point in the history
* Post backup/restore status update
* Add status package
* Handle backup/restore error
* Fail pod for back/restore error
  • Loading branch information
Dipta Das authored and tamalsaha committed Mar 21, 2019
1 parent 50bbb0a commit 5c173e5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
27 changes: 19 additions & 8 deletions backup_pvc.go
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"github.com/appscode/go/flags"
"github.com/appscode/go/log"
"github.com/appscode/stash/pkg/restic"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -36,25 +37,25 @@ func NewCmdBackupPVC() *cobra.Command {
// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(setupOpt)
if err != nil {
return err
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
// Run backup
backupOutput, backupErr := resticWrapper.RunBackup(&backupOpt)
// If metrics are enabled then generate metrics
if metrics.Enabled {
err := backupOutput.HandleMetrics(&metrics, backupErr)
if err != nil {
return errors.NewAggregate([]error{backupErr, err})
return handleResticError(outputDir, restic.DefaultOutputFileName, errors.NewAggregate([]error{backupErr, err}))
}
}
if backupErr != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, backupErr)
}
// If output directory specified, then write the output in "output.json" file in the specified directory
if backupErr == nil && outputDir != "" {
err := backupOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
if err != nil {
return err
}
if outputDir != "" {
return backupOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
}
return backupErr
return nil
},
}

Expand Down Expand Up @@ -88,3 +89,13 @@ func NewCmdBackupPVC() *cobra.Command {

return cmd
}

// works for both backup and restore output
func handleResticError(outputDir, fileName string, backupErr error) error {
if outputDir == "" || fileName == "" {
return backupErr
}
log.Infoln("Writing restic error to output file, error:", backupErr.Error())
backupOut := restic.BackupOutput{Error: backupErr.Error()}
return backupOut.WriteOutput(filepath.Join(outputDir, fileName))
}
16 changes: 8 additions & 8 deletions restore_pvc.go
Expand Up @@ -35,25 +35,25 @@ func NewCmdRestorePVC() *cobra.Command {
// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(setupOpt)
if err != nil {
return err
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
// Run restore
restoreOutput, restoreErr := resticWrapper.RunRestore(restoreOpt)
// If metrics are enabled then generate metrics
if metrics.Enabled {
err := restoreOutput.HandleMetrics(&metrics, restoreErr)
if err != nil {
return errors.NewAggregate([]error{restoreErr, err})
return handleResticError(outputDir, restic.DefaultOutputFileName, errors.NewAggregate([]error{restoreErr, err}))
}
}
if restoreErr != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, restoreErr)
}
// If output directory specified, then write the output in "output.json" file in the specified directory
if restoreErr == nil && outputDir != "" {
err := restoreOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
if err != nil {
return err
}
if outputDir != "" {
return restoreOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
}
return restoreErr
return nil
},
}

Expand Down
1 change: 1 addition & 0 deletions root.go
Expand Up @@ -45,6 +45,7 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(NewCmdBackup())
rootCmd.AddCommand(NewCmdBackupPVC())
rootCmd.AddCommand(NewCmdRestorePVC())
rootCmd.AddCommand(NewCmdUpdateStatus())
rootCmd.AddCommand(NewCmdRecover())
rootCmd.AddCommand(NewCmdCheck())
rootCmd.AddCommand(NewCmdScaleDown())
Expand Down
64 changes: 64 additions & 0 deletions update_status.go
@@ -0,0 +1,64 @@
package cmds

import (
"fmt"

"github.com/appscode/go/flags"
cs "github.com/appscode/stash/client/clientset/versioned"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/status"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

func NewCmdUpdateStatus() *cobra.Command {
var (
masterURL string
kubeconfigPath string
opt = status.UpdateStatusOptions{
OutputFileName: restic.DefaultOutputFileName,
}
)

cmd := &cobra.Command{
Use: "update-status",
Short: "Update status of Repository, Backup/Restore Session",
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "namespace", "output-dir")

config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
return err
}
opt.KubeClient, err = kubernetes.NewForConfig(config)
if err != nil {
return err
}
opt.StashClient, err = cs.NewForConfig(config)
if err != nil {
return err
}

if opt.BackupSession != "" {
return opt.UpdateBackupStatusFromFile()
}
if opt.RestoreSession != "" {
return opt.UpdateRestoreStatusFromFile()
}
return fmt.Errorf("backup-session or, restore-session not specified")
},
}

cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")

cmd.Flags().StringVar(&opt.Namespace, "namespace", "default", "Namespace of Backup/Restore Session")
cmd.Flags().StringVar(&opt.Repository, "repository", opt.Repository, "Name of the Repository")
cmd.Flags().StringVar(&opt.BackupSession, "backup-session", opt.BackupSession, "Name of the Backup Session")
cmd.Flags().StringVar(&opt.RestoreSession, "restore-session", opt.RestoreSession, "Name of the Restore Session")
cmd.Flags().StringVar(&opt.OutputDir, "output-dir", opt.OutputDir, "Directory where output.json file will be written (keep empty if you don't need to write output in file)")

return cmd
}

0 comments on commit 5c173e5

Please sign in to comment.