Skip to content

Commit 5c173e5

Browse files
Dipta Dastamalsaha
authored andcommitted
Post backup/restore status update (#691)
* Post backup/restore status update * Add status package * Handle backup/restore error * Fail pod for back/restore error
1 parent 50bbb0a commit 5c173e5

File tree

4 files changed

+92
-16
lines changed

4 files changed

+92
-16
lines changed

backup_pvc.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"path/filepath"
55

66
"github.com/appscode/go/flags"
7+
"github.com/appscode/go/log"
78
"github.com/appscode/stash/pkg/restic"
89
"github.com/spf13/cobra"
910
"k8s.io/apimachinery/pkg/util/errors"
@@ -36,25 +37,25 @@ func NewCmdBackupPVC() *cobra.Command {
3637
// init restic wrapper
3738
resticWrapper, err := restic.NewResticWrapper(setupOpt)
3839
if err != nil {
39-
return err
40+
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
4041
}
4142
// Run backup
4243
backupOutput, backupErr := resticWrapper.RunBackup(&backupOpt)
4344
// If metrics are enabled then generate metrics
4445
if metrics.Enabled {
4546
err := backupOutput.HandleMetrics(&metrics, backupErr)
4647
if err != nil {
47-
return errors.NewAggregate([]error{backupErr, err})
48+
return handleResticError(outputDir, restic.DefaultOutputFileName, errors.NewAggregate([]error{backupErr, err}))
4849
}
4950
}
51+
if backupErr != nil {
52+
return handleResticError(outputDir, restic.DefaultOutputFileName, backupErr)
53+
}
5054
// If output directory specified, then write the output in "output.json" file in the specified directory
51-
if backupErr == nil && outputDir != "" {
52-
err := backupOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
53-
if err != nil {
54-
return err
55-
}
55+
if outputDir != "" {
56+
return backupOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
5657
}
57-
return backupErr
58+
return nil
5859
},
5960
}
6061

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

8990
return cmd
9091
}
92+
93+
// works for both backup and restore output
94+
func handleResticError(outputDir, fileName string, backupErr error) error {
95+
if outputDir == "" || fileName == "" {
96+
return backupErr
97+
}
98+
log.Infoln("Writing restic error to output file, error:", backupErr.Error())
99+
backupOut := restic.BackupOutput{Error: backupErr.Error()}
100+
return backupOut.WriteOutput(filepath.Join(outputDir, fileName))
101+
}

restore_pvc.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,25 @@ func NewCmdRestorePVC() *cobra.Command {
3535
// init restic wrapper
3636
resticWrapper, err := restic.NewResticWrapper(setupOpt)
3737
if err != nil {
38-
return err
38+
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
3939
}
4040
// Run restore
4141
restoreOutput, restoreErr := resticWrapper.RunRestore(restoreOpt)
4242
// If metrics are enabled then generate metrics
4343
if metrics.Enabled {
4444
err := restoreOutput.HandleMetrics(&metrics, restoreErr)
4545
if err != nil {
46-
return errors.NewAggregate([]error{restoreErr, err})
46+
return handleResticError(outputDir, restic.DefaultOutputFileName, errors.NewAggregate([]error{restoreErr, err}))
4747
}
4848
}
49+
if restoreErr != nil {
50+
return handleResticError(outputDir, restic.DefaultOutputFileName, restoreErr)
51+
}
4952
// If output directory specified, then write the output in "output.json" file in the specified directory
50-
if restoreErr == nil && outputDir != "" {
51-
err := restoreOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
52-
if err != nil {
53-
return err
54-
}
53+
if outputDir != "" {
54+
return restoreOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
5555
}
56-
return restoreErr
56+
return nil
5757
},
5858
}
5959

root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func NewRootCmd() *cobra.Command {
4545
rootCmd.AddCommand(NewCmdBackup())
4646
rootCmd.AddCommand(NewCmdBackupPVC())
4747
rootCmd.AddCommand(NewCmdRestorePVC())
48+
rootCmd.AddCommand(NewCmdUpdateStatus())
4849
rootCmd.AddCommand(NewCmdRecover())
4950
rootCmd.AddCommand(NewCmdCheck())
5051
rootCmd.AddCommand(NewCmdScaleDown())

update_status.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cmds
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/appscode/go/flags"
7+
cs "github.com/appscode/stash/client/clientset/versioned"
8+
"github.com/appscode/stash/pkg/restic"
9+
"github.com/appscode/stash/pkg/status"
10+
"github.com/spf13/cobra"
11+
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/tools/clientcmd"
13+
)
14+
15+
func NewCmdUpdateStatus() *cobra.Command {
16+
var (
17+
masterURL string
18+
kubeconfigPath string
19+
opt = status.UpdateStatusOptions{
20+
OutputFileName: restic.DefaultOutputFileName,
21+
}
22+
)
23+
24+
cmd := &cobra.Command{
25+
Use: "update-status",
26+
Short: "Update status of Repository, Backup/Restore Session",
27+
DisableAutoGenTag: true,
28+
RunE: func(cmd *cobra.Command, args []string) error {
29+
flags.EnsureRequiredFlags(cmd, "namespace", "output-dir")
30+
31+
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
32+
if err != nil {
33+
return err
34+
}
35+
opt.KubeClient, err = kubernetes.NewForConfig(config)
36+
if err != nil {
37+
return err
38+
}
39+
opt.StashClient, err = cs.NewForConfig(config)
40+
if err != nil {
41+
return err
42+
}
43+
44+
if opt.BackupSession != "" {
45+
return opt.UpdateBackupStatusFromFile()
46+
}
47+
if opt.RestoreSession != "" {
48+
return opt.UpdateRestoreStatusFromFile()
49+
}
50+
return fmt.Errorf("backup-session or, restore-session not specified")
51+
},
52+
}
53+
54+
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
55+
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
56+
57+
cmd.Flags().StringVar(&opt.Namespace, "namespace", "default", "Namespace of Backup/Restore Session")
58+
cmd.Flags().StringVar(&opt.Repository, "repository", opt.Repository, "Name of the Repository")
59+
cmd.Flags().StringVar(&opt.BackupSession, "backup-session", opt.BackupSession, "Name of the Backup Session")
60+
cmd.Flags().StringVar(&opt.RestoreSession, "restore-session", opt.RestoreSession, "Name of the Restore Session")
61+
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)")
62+
63+
return cmd
64+
}

0 commit comments

Comments
 (0)