2017-09-02 12:24:17 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2017-09-02 12:52:12 +02:00
|
|
|
"github.com/kr/pretty"
|
2017-09-02 12:24:17 +02:00
|
|
|
"github.com/spf13/cobra"
|
2017-09-23 18:20:22 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2017-09-02 12:24:17 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
2017-09-23 18:20:22 +02:00
|
|
|
"time"
|
2017-09-02 12:24:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var testCmd = &cobra.Command{
|
2017-09-17 23:40:40 +02:00
|
|
|
Use: "test",
|
|
|
|
Short: "test configuration",
|
|
|
|
PersistentPreRun: testCmdGlobalInit,
|
2017-09-02 12:24:17 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
var testCmdGlobal struct {
|
|
|
|
log Logger
|
|
|
|
conf *Config
|
|
|
|
}
|
|
|
|
|
2017-09-02 12:24:17 +02:00
|
|
|
var testConfigSyntaxCmd = &cobra.Command{
|
|
|
|
Use: "config",
|
2017-09-02 12:52:12 +02:00
|
|
|
Short: "parse config file and dump parsed datastructure",
|
2017-09-02 12:24:17 +02:00
|
|
|
Run: doTestConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
var testDatasetMapFilter = &cobra.Command{
|
2017-09-16 21:12:26 +02:00
|
|
|
Use: "pattern jobname test/zfs/dataset/path",
|
2017-09-02 12:24:17 +02:00
|
|
|
Short: "test dataset mapping / filter specified in config",
|
|
|
|
Example: ` zrepl test pattern prune.clean_backups tank/backups/legacyscript/foo`,
|
|
|
|
Run: doTestDatasetMapFilter,
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
var testPrunePolicyArgs struct {
|
|
|
|
side PrunePolicySide
|
|
|
|
showKept bool
|
|
|
|
showRemoved bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var testPrunePolicyCmd = &cobra.Command{
|
|
|
|
Use: "prune jobname",
|
|
|
|
Short: "do a dry-run of the pruning part of a job",
|
|
|
|
Run: doTestPrunePolicy,
|
|
|
|
}
|
|
|
|
|
2017-09-02 12:24:17 +02:00
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(testCmd)
|
|
|
|
testCmd.AddCommand(testConfigSyntaxCmd)
|
|
|
|
testCmd.AddCommand(testDatasetMapFilter)
|
2017-09-16 21:12:26 +02:00
|
|
|
|
|
|
|
testPrunePolicyCmd.Flags().VarP(&testPrunePolicyArgs.side, "side", "s", "prune_lhs (left) or prune_rhs (right)")
|
|
|
|
testPrunePolicyCmd.Flags().BoolVar(&testPrunePolicyArgs.showKept, "kept", false, "show kept snapshots")
|
|
|
|
testPrunePolicyCmd.Flags().BoolVar(&testPrunePolicyArgs.showRemoved, "removed", true, "show removed snapshots")
|
|
|
|
testCmd.AddCommand(testPrunePolicyCmd)
|
2017-09-02 12:24:17 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 23:40:40 +02:00
|
|
|
func testCmdGlobalInit(cmd *cobra.Command, args []string) {
|
2017-09-17 18:20:05 +02:00
|
|
|
|
2017-09-23 18:20:22 +02:00
|
|
|
out := logger.NewOutlets()
|
|
|
|
out.Add(WriterOutlet{NoFormatter{}, os.Stdout}, logger.Info)
|
|
|
|
log := logger.NewLogger(out, 1*time.Second)
|
2017-09-22 14:13:58 +02:00
|
|
|
testCmdGlobal.log = log
|
2017-09-17 18:20:05 +02:00
|
|
|
|
|
|
|
var err error
|
2017-09-22 14:02:07 +02:00
|
|
|
if testCmdGlobal.conf, err = ParseConfig(rootArgs.configFile); err != nil {
|
2017-09-17 18:20:05 +02:00
|
|
|
testCmdGlobal.log.Printf("error parsing config file: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-02 12:24:17 +02:00
|
|
|
func doTestConfig(cmd *cobra.Command, args []string) {
|
2017-09-02 12:52:12 +02:00
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
log, conf := testCmdGlobal.log, testCmdGlobal.conf
|
2017-09-02 12:52:12 +02:00
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
log.Printf("config ok")
|
|
|
|
log.Printf("%# v", pretty.Formatter(conf))
|
2017-09-02 12:24:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestDatasetMapFilter(cmd *cobra.Command, args []string) {
|
2017-09-17 18:20:05 +02:00
|
|
|
|
|
|
|
log, conf := testCmdGlobal.log, testCmdGlobal.conf
|
|
|
|
|
2017-09-02 12:24:17 +02:00
|
|
|
if len(args) != 2 {
|
|
|
|
log.Printf("specify job name as first postitional argument, test input as second")
|
|
|
|
log.Printf(cmd.UsageString())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
n, i := args[0], args[1]
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
jobi, err := conf.LookupJob(n)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s", err)
|
2017-09-02 12:24:17 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
var mf *DatasetMapFilter
|
2017-09-02 12:24:17 +02:00
|
|
|
switch j := jobi.(type) {
|
2017-09-10 16:13:05 +02:00
|
|
|
case *PullJob:
|
|
|
|
mf = j.Mapping
|
|
|
|
case *SourceJob:
|
|
|
|
mf = j.Datasets
|
|
|
|
case *LocalJob:
|
2017-09-02 12:24:17 +02:00
|
|
|
mf = j.Mapping
|
|
|
|
default:
|
|
|
|
panic("incomplete implementation")
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := zfs.NewDatasetPath(i)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("cannot parse test input as ZFS dataset path: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
if mf.filterMode {
|
2017-09-02 12:24:17 +02:00
|
|
|
pass, err := mf.Filter(ip)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error evaluating filter: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
log.Printf("filter result: %v", pass)
|
|
|
|
} else {
|
|
|
|
res, err := mf.Map(ip)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error evaluating mapping: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-09-02 12:40:22 +02:00
|
|
|
toStr := "NO MAPPING"
|
|
|
|
if res != nil {
|
|
|
|
toStr = res.ToString()
|
|
|
|
}
|
|
|
|
log.Printf("%s => %s", ip.ToString(), toStr)
|
2017-09-02 12:24:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-09-16 21:12:26 +02:00
|
|
|
|
|
|
|
func doTestPrunePolicy(cmd *cobra.Command, args []string) {
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
log, conf := testCmdGlobal.log, testCmdGlobal.conf
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
if cmd.Flags().NArg() != 1 {
|
|
|
|
log.Printf("specify job name as first positional argument")
|
|
|
|
log.Printf(cmd.UsageString())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
jobname := cmd.Flags().Arg(0)
|
|
|
|
jobi, err := conf.LookupJob(jobname)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
jobp, ok := jobi.(PruningJob)
|
|
|
|
if !ok {
|
|
|
|
log.Printf("job doesn't do any prunes")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("job dump:\n%s", pretty.Sprint(jobp))
|
|
|
|
|
|
|
|
pruner, err := jobp.Pruner(testPrunePolicyArgs.side, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("cannot create test pruner: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("start pruning")
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), contextKeyLog, log)
|
|
|
|
result, err := pruner.Run(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error running pruner: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(result, func(i, j int) bool {
|
|
|
|
return strings.Compare(result[i].Filesystem.ToString(), result[j].Filesystem.ToString()) == -1
|
|
|
|
})
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
for _, r := range result {
|
|
|
|
fmt.Fprintf(&b, "%s\n", r.Filesystem.ToString())
|
|
|
|
|
|
|
|
if testPrunePolicyArgs.showKept {
|
|
|
|
fmt.Fprintf(&b, "\tkept:\n")
|
|
|
|
for _, v := range r.Keep {
|
|
|
|
fmt.Fprintf(&b, "\t- %s\n", v.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if testPrunePolicyArgs.showRemoved {
|
|
|
|
fmt.Fprintf(&b, "\tremoved:\n")
|
|
|
|
for _, v := range r.Remove {
|
|
|
|
fmt.Fprintf(&b, "\t- %s\n", v.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("pruning result:\n%s", b.String())
|
|
|
|
|
|
|
|
}
|