2017-09-13 23:27:18 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Pruner struct {
|
|
|
|
Now time.Time
|
|
|
|
DryRun bool
|
|
|
|
DatasetFilter zfs.DatasetFilter
|
|
|
|
SnapshotPrefix string
|
|
|
|
PrunePolicy PrunePolicy
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
type PruneResult struct {
|
|
|
|
Filesystem *zfs.DatasetPath
|
|
|
|
All []zfs.FilesystemVersion
|
|
|
|
Keep []zfs.FilesystemVersion
|
|
|
|
Remove []zfs.FilesystemVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pruner) Run(ctx context.Context) (r []PruneResult, err error) {
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
log := ctx.Value(contextKeyLog).(Logger)
|
|
|
|
|
|
|
|
if p.DryRun {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.Info("doing dry run")
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
filesystems, err := zfs.ZFSListMapping(p.DatasetFilter)
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithError(err).Error("error applying filesystem filter")
|
2017-09-16 21:12:26 +02:00
|
|
|
return nil, err
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
if len(filesystems) <= 0 {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.Info("no filesystems matching filter")
|
2017-09-16 21:12:26 +02:00
|
|
|
return nil, err
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
r = make([]PruneResult, 0, len(filesystems))
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
for _, fs := range filesystems {
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
log := log.WithField("filesystem", fs.ToString())
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
fsversions, err := zfs.ZFSListFilesystemVersions(fs, &PrefixSnapshotFilter{p.SnapshotPrefix})
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithError(err).Error("error listing filesytem versions")
|
2017-09-13 23:27:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(fsversions) == 0 {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithField("prefix", p.SnapshotPrefix).Info("no filesystem versions matching prefix")
|
2017-09-13 23:27:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgj, err := json.Marshal(fsversions)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithField("fsversions", string(dbgj)).Debug()
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
keep, remove, err := p.PrunePolicy.Prune(fs, fsversions)
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithError(err).Error("error evaluating prune policy")
|
2017-09-13 23:27:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgj, err = json.Marshal(keep)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithField("keep", string(dbgj)).Debug()
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
dbgj, err = json.Marshal(remove)
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithField("remove", string(dbgj)).Debug()
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
r = append(r, PruneResult{fs, fsversions, keep, remove})
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
makeFields := func(v zfs.FilesystemVersion) (fields map[string]interface{}) {
|
|
|
|
fields = make(map[string]interface{})
|
|
|
|
fields["version"] = v.ToAbsPath(fs)
|
2017-09-13 23:27:18 +02:00
|
|
|
timeSince := v.Creation.Sub(p.Now)
|
2017-09-22 14:13:58 +02:00
|
|
|
fields["age_ns"] = timeSince
|
2017-09-13 23:27:18 +02:00
|
|
|
const day time.Duration = 24 * time.Hour
|
|
|
|
days := timeSince / day
|
|
|
|
remainder := timeSince % day
|
2017-09-22 14:13:58 +02:00
|
|
|
fields["age_str"] = fmt.Sprintf("%dd%s", days, remainder)
|
|
|
|
return
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range remove {
|
2017-09-22 14:13:58 +02:00
|
|
|
fields := makeFields(v)
|
|
|
|
log.WithFields(fields).Info("destroying version")
|
2017-09-13 23:27:18 +02:00
|
|
|
// echo what we'll do and exec zfs destroy if not dry run
|
|
|
|
// TODO special handling for EBUSY (zfs hold)
|
|
|
|
// TODO error handling for clones? just echo to cli, skip over, and exit with non-zero status code (we're idempotent)
|
|
|
|
if !p.DryRun {
|
|
|
|
err := zfs.ZFSDestroyFilesystemVersion(fs, v)
|
|
|
|
if err != nil {
|
|
|
|
// handle
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithFields(fields).WithError(err).Error("error destroying version")
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|