handler_pipe.go 3.33 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7 8
package tabacco

import (
	"context"
	"errors"
	"io"
	"os"
	"path/filepath"
ale's avatar
ale committed
9 10

	"git.autistici.org/ale/tabacco/jobs"
ale's avatar
ale committed
11 12 13 14 15 16 17 18
)

// The pipeHandler must work on a 1:1 dataset/atom mapping, because it
// generates a single file on the repository, and thus it can't
// distinguish multiple atoms inside it.
type pipeHandler struct {
	backupCmd, restoreCmd string
	shell                 *Shell
19
	spec                  HandlerSpec
ale's avatar
ale committed
20 21
}

22
func newPipeHandler(spec HandlerSpec, shell *Shell) (Handler, error) {
ale's avatar
ale committed
23
	backupCmd, ok := spec.Params["backup_command"].(string)
ale's avatar
ale committed
24
	if !ok || backupCmd == "" {
ale's avatar
ale committed
25
		return nil, errors.New("missing backup_command")
ale's avatar
ale committed
26
	}
ale's avatar
ale committed
27
	restoreCmd, ok := spec.Params["restore_command"].(string)
ale's avatar
ale committed
28
	if !ok || restoreCmd == "" {
ale's avatar
ale committed
29
		return nil, errors.New("missing restore_command")
ale's avatar
ale committed
30 31
	}
	return &pipeHandler{
32
		spec:       spec,
ale's avatar
ale committed
33 34 35 36 37 38
		backupCmd:  backupCmd,
		restoreCmd: restoreCmd,
		shell:      shell,
	}, nil
}

39 40 41 42
func (h *pipeHandler) Spec() HandlerSpec {
	return h.spec
}

ale's avatar
ale committed
43 44
func (h *pipeHandler) DatasetsForBackup(ds Dataset) []Dataset {
	var dsl []Dataset
ale's avatar
ale committed
45 46
	for _, atom := range ds.Atoms {
		atom.SourcePath = filepath.Join(ds.Name, atom.Name)
ale's avatar
ale committed
47 48 49 50 51 52 53 54
		dsl = append(dsl, makeSingleAtomDataset(ds, atom))
	}
	return dsl
}

func (h *pipeHandler) BackupJob(repo Repository, backup Backup, ds Dataset) jobs.Job {
	if len(ds.Atoms) > 1 {
		panic("more than 1 atom in pipe source")
ale's avatar
ale committed
55
	}
ale's avatar
ale committed
56 57 58 59

	return jobs.JobFunc(func(ctx context.Context) error {
		return h.backupAtom(ctx, repo, backup, ds, ds.Atoms[0])
	})
ale's avatar
ale committed
60 61 62 63 64 65 66 67 68 69 70 71
}

func (h *pipeHandler) backupAtom(ctx context.Context, repo Repository, backup Backup, ds Dataset, atom Atom) error {
	return h.shell.RunStdoutPipe(
		ctx,
		expandVars(h.backupCmd, backup, ds, atom),
		func(stdout io.Reader) error {
			return repo.BackupStream(ctx, backup, singleAtomDataset(ds, atom), stdout)
		},
	)
}

ale's avatar
ale committed
72 73
func (h *pipeHandler) DatasetsForRestore(ds Dataset) []Dataset {
	var dsl []Dataset
ale's avatar
ale committed
74
	for _, atom := range ds.Atoms {
ale's avatar
ale committed
75 76 77 78 79 80 81 82 83 84 85 86 87
		dsl = append(dsl, makeSingleAtomDataset(ds, atom))
	}
	return dsl
}

func (h *pipeHandler) RestoreJob(repo Repository, backup Backup, ds Dataset, target string) jobs.Job {
	var restoreJobs []jobs.Job
	for _, atom := range ds.Atoms {
		func(atom Atom) {
			restoreJobs = append(restoreJobs, jobs.JobFunc(func(ctx context.Context) error {
				return h.restoreAtom(ctx, repo, backup, ds, atom, target)
			}))
		}(atom)
ale's avatar
ale committed
88
	}
ale's avatar
ale committed
89
	return jobs.AsyncGroup(restoreJobs)
ale's avatar
ale committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

func (h *pipeHandler) restoreAtom(ctx context.Context, repo Repository, backup Backup, ds Dataset, atom Atom, target string) error {
	return h.shell.RunStdinPipe(
		ctx,
		expandVars(h.restoreCmd, backup, ds, atom),
		func(stdin io.Writer) error {
			return repo.RestoreStream(ctx, backup, singleAtomDataset(ds, atom), target, stdin)
		},
	)
}

func singleAtomDataset(ds Dataset, atom Atom) Dataset {
	return Dataset{
		Name:    ds.Name,
		Handler: ds.Handler,
		Atoms:   []Atom{atom},
	}
}

func expandVars(s string, backup Backup, ds Dataset, atom Atom) string {
	return os.Expand(s, func(key string) string {
		switch key {
		case "backup.id":
			return backup.ID
		case "ds.name":
			return ds.Name
		case "atom.name":
			return atom.Name
		case "atom.path":
			return filepath.Join(ds.Name, atom.Name)
		default:
			return os.Getenv(key)
		}
	})
}
ale's avatar
ale committed
126 127 128 129 130 131 132 133

func makeSingleAtomDataset(ds Dataset, atom Atom) Dataset {
	return Dataset{
		Name:    filepath.Join(ds.Name, atom.Name),
		Handler: ds.Handler,
		Atoms:   []Atom{atom},
	}
}