Skip to content

Commit

Permalink
Refactor Restic helpers (#4060)
Browse files Browse the repository at this point in the history
Move command generation into the `restic` pkg. Allows for re-use
in other functions
  • Loading branch information
Vaibhav Kamra authored and Ilya Kislenko committed Oct 9, 2018
1 parent f988a34 commit 3eb8275
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
26 changes: 3 additions & 23 deletions pkg/function/backup_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package function

import (
"context"
"fmt"

"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -41,25 +40,6 @@ func (*backupDataFunc) Name() string {
return "BackupData"
}

func generateSnapshotsCommand(destArtifact string, profile *param.Profile) []string {
// Restic Snapshots command
command := restic.SnapshotsCommand(profile, destArtifact)
return []string{"sh", "-o", "errexit", "-o", "pipefail", "-c", command}
}

func generateInitCommand(destArtifact string, profile *param.Profile) []string {
// Restic Repository Init command
command := restic.InitCommand(profile, destArtifact)
return []string{"sh", "-o", "errexit", "-o", "pipefail", "-c", command}
}

func generateBackupCommand(includePath, destArtifact, id string, profile *param.Profile) []string {
// Restic Backup command
command := restic.BackupCommand(profile, destArtifact)
command = fmt.Sprintf("%s --tag %s %s", command, id, includePath)
return []string{"sh", "-o", "errexit", "-o", "pipefail", "-c", command}
}

func validateProfile(profile *param.Profile) error {
if profile == nil {
return errors.New("Profile must be non-nil")
Expand All @@ -72,13 +52,13 @@ func validateProfile(profile *param.Profile) error {

func getOrCreateRepository(cli kubernetes.Interface, namespace, pod, container, artifactPrefix string, profile *param.Profile) error {
// Use the snapshots command to check if the repository exists
cmd := generateSnapshotsCommand(artifactPrefix, profile)
cmd := restic.SnapshotsCommand(profile, artifactPrefix)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd)
formatAndLog(pod, container, stdout)
formatAndLog(pod, container, stderr)
if err != nil {
// Create a repository
cmd := generateInitCommand(artifactPrefix, profile)
cmd := restic.InitCommand(profile, artifactPrefix)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd)
formatAndLog(pod, container, stdout)
formatAndLog(pod, container, stderr)
Expand Down Expand Up @@ -125,7 +105,7 @@ func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
}

// Create backup and dump it on the object store
cmd := generateBackupCommand(includePath, backupArtifactPrefix, backupIdentifier, tp.Profile)
cmd := restic.BackupCommand(tp.Profile, backupArtifactPrefix, backupIdentifier, includePath)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd)
formatAndLog(pod, container, stdout)
formatAndLog(pod, container, stderr)
Expand Down
13 changes: 1 addition & 12 deletions pkg/function/restore_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package function

import (
"context"
"fmt"

"github.com/pkg/errors"

Expand Down Expand Up @@ -65,13 +64,6 @@ func fetchPodVolumes(pod string, tp param.TemplateParams) (map[string]string, er
}
}

func generateRestoreCommand(backupArtifactPrefix, restorePath, id string, profile *param.Profile) ([]string, error) {
// Restic restore command
command := restic.RestoreCommand(profile, backupArtifactPrefix)
command = fmt.Sprintf("%s --tag %s latest --target %s", command, id, restorePath)
return []string{"bash", "-o", "errexit", "-o", "pipefail", "-c", command}, nil
}

func (*restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {
var namespace, pod, image, backupArtifactPrefix, restorePath, backupIdentifier string
var vols map[string]string
Expand Down Expand Up @@ -105,10 +97,7 @@ func (*restoreDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args
return nil, err
}
// Generate restore command
cmd, err := generateRestoreCommand(backupArtifactPrefix, restorePath, backupIdentifier, tp.Profile)
if err != nil {
return nil, err
}
cmd := restic.RestoreCommand(tp.Profile, backupArtifactPrefix, backupIdentifier, restorePath)
if len(vols) == 0 {
// Fetch Volumes
vols, err = fetchPodVolumes(pod, tp)
Expand Down
30 changes: 18 additions & 12 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,58 @@ import (
"github.com/kanisterio/kanister/pkg/param"
)

func shCommand(command string) []string {
return []string{"sh", "-o", "errexit", "-o", "pipefail", "-c", command}
}

// BackupCommand returns restic backup command
func BackupCommand(profile *param.Profile, repository string) string {
func BackupCommand(profile *param.Profile, repository, id, includePath string) []string {
cmd := resticArgs(profile, repository)
cmd = append(cmd, "backup")
command := strings.Join(cmd, " ")
return command
command = fmt.Sprintf("%s --tag %s %s", command, id, includePath)
return shCommand(command)
}

// RestoreCommand returns restic restore command
func RestoreCommand(profile *param.Profile, repository string) string {
func RestoreCommand(profile *param.Profile, repository, id, restorePath string) []string {
cmd := resticArgs(profile, repository)
cmd = append(cmd, "restore")
command := strings.Join(cmd, " ")
return command
command = fmt.Sprintf("%s --tag %s latest --target %s", command, id, restorePath)
return shCommand(command)
}

// SnapshotsCommand returns restic snapshots command
func SnapshotsCommand(profile *param.Profile, repository string) string {
func SnapshotsCommand(profile *param.Profile, repository string) []string {
cmd := resticArgs(profile, repository)
cmd = append(cmd, "snapshots")
command := strings.Join(cmd, " ")
return command
return shCommand(command)
}

// InitCommand returns restic init command
func InitCommand(profile *param.Profile, repository string) string {
func InitCommand(profile *param.Profile, repository string) []string {
cmd := resticArgs(profile, repository)
cmd = append(cmd, "init")
command := strings.Join(cmd, " ")
return command
return shCommand(command)
}

// ForgetCommand returns restic forget command
func ForgetCommand(profile *param.Profile, repository string) string {
func ForgetCommand(profile *param.Profile, repository string) []string {
cmd := resticArgs(profile, repository)
cmd = append(cmd, "forget")
command := strings.Join(cmd, " ")
return command
return shCommand(command)
}

// PruneCommand returns restic prune command
func PruneCommand(profile *param.Profile, repository string) string {
func PruneCommand(profile *param.Profile, repository string) []string {
cmd := resticArgs(profile, repository)
cmd = append(cmd, "prune")
command := strings.Join(cmd, " ")
return command
return shCommand(command)
}

const (
Expand Down

0 comments on commit 3eb8275

Please sign in to comment.