Files
kopia/cli/command_maintenance_set.go
Jarek Kowalski fa7976599c repo: refactored repository interfaces (#780)
- `repo.Repository` is now read-only and only has methods that can be supported over kopia server
- `repo.RepositoryWriter` has read-write methods that can be supported over kopia server
- `repo.DirectRepository` is read-only and contains all methods of `repo.Repository` plus some low-level methods for data inspection
- `repo.DirectRepositoryWriter` contains write methods for `repo.DirectRepository`

- `repo.Reader` removed and merged with `repo.Repository`
- `repo.Writer` became `repo.RepositoryWriter`
- `*repo.DirectRepository` struct became `repo.DirectRepository`
  interface

Getting `{Direct}RepositoryWriter` requires using `NewWriter()` or `NewDirectWriter()` on a read-only repository and multiple simultaneous writers are supported at the same time, each writing to their own indexes and pack blobs.

`repo.Open` returns `repo.Repository` (which is also `repo.RepositoryWriter`).

* content: removed implicit flush on content manager close
* repo: added tests for WriteSession() and implicit flush behavior
* invalidate manifest manager after write session

* cli: disable maintenance in 'kopia server start'
  Server will close the repository before completing.

* repo: unconditionally close RepositoryWriter in {Direct,}WriteSession
* repo: added panic in case somebody tries to create RepositoryWriter after closing repository
  - used atomic to manage SharedManager.closed

* removed stale example
* linter: fixed spurious failures

Co-authored-by: Julio López <julio+gh@kasten.io>
2021-01-20 11:41:47 -08:00

121 lines
3.9 KiB
Go

package cli
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/maintenance"
)
var (
maintenanceSetCommand = maintenanceCommands.Command("set", "Set maintenance parameters")
maintenanceSetOwner = maintenanceSetCommand.Flag("owner", "Set maintenance owner user@hostname").String()
maintenanceSetEnableQuick = maintenanceSetCommand.Flag("enable-quick", "Enable or disable quick maintenance").BoolList()
maintenanceSetEnableFull = maintenanceSetCommand.Flag("enable-full", "Enable or disable full maintenance").BoolList()
maintenanceSetQuickFrequency = maintenanceSetCommand.Flag("quick-interval", "Set quick maintenance interval").DurationList()
maintenanceSetFullFrequency = maintenanceSetCommand.Flag("full-interval", "Set full maintenance interval").DurationList()
maintenanceSetPauseQuick = maintenanceSetCommand.Flag("pause-quick", "Pause quick maintenance for a specified duration").DurationList()
maintenanceSetPauseFull = maintenanceSetCommand.Flag("pause-full", "Pause full maintenance for a specified duration").DurationList()
)
func setMaintenanceOwnerFromFlags(ctx context.Context, p *maintenance.Params, rep repo.DirectRepositoryWriter, changed *bool) {
if v := *maintenanceSetOwner; v != "" {
if v == "me" {
p.Owner = rep.ClientOptions().UsernameAtHost()
} else {
p.Owner = v
}
*changed = true
log(ctx).Infof("Setting maintenance owner to %v", p.Owner)
}
}
func setMaintenanceEnabledAndIntervalFromFlags(ctx context.Context, c *maintenance.CycleParams, cycleName string, enableFlag []bool, intervalFlag []time.Duration, changed *bool) {
// we use lists to distinguish between flag not set
// Zero elements == not set, more than zero - flag set, in which case we pick the last value
if len(enableFlag) > 0 {
lastVal := enableFlag[len(enableFlag)-1]
c.Enabled = lastVal
*changed = true
if lastVal {
log(ctx).Infof("Periodic %v maintenance enabled.", cycleName)
} else {
log(ctx).Infof("Periodic %v maintenance disabled.", cycleName)
}
}
if len(intervalFlag) > 0 {
lastVal := intervalFlag[len(intervalFlag)-1]
c.Interval = lastVal
*changed = true
log(ctx).Infof("Interval for %v maintenance set to %v.", cycleName, lastVal)
}
}
func runMaintenanceSetParams(ctx context.Context, rep repo.DirectRepositoryWriter) error {
p, err := maintenance.GetParams(ctx, rep)
if err != nil {
return errors.Wrap(err, "unable to get current parameters")
}
s, err := maintenance.GetSchedule(ctx, rep)
if err != nil {
return errors.Wrap(err, "unable to get current parameters")
}
var changedParams, changedSchedule bool
setMaintenanceOwnerFromFlags(ctx, p, rep, &changedParams)
setMaintenanceEnabledAndIntervalFromFlags(ctx, &p.QuickCycle, "quick", *maintenanceSetEnableQuick, *maintenanceSetQuickFrequency, &changedParams)
setMaintenanceEnabledAndIntervalFromFlags(ctx, &p.FullCycle, "full", *maintenanceSetEnableFull, *maintenanceSetFullFrequency, &changedParams)
if v := *maintenanceSetPauseQuick; len(v) > 0 {
pauseDuration := v[len(v)-1]
s.NextQuickMaintenanceTime = rep.Time().Add(pauseDuration)
changedSchedule = true
log(ctx).Infof("Quick maintenance paused until %v", formatTimestamp(s.NextQuickMaintenanceTime))
}
if v := *maintenanceSetPauseFull; len(v) > 0 {
pauseDuration := v[len(v)-1]
s.NextFullMaintenanceTime = rep.Time().Add(pauseDuration)
changedSchedule = true
log(ctx).Infof("Full maintenance paused until %v", formatTimestamp(s.NextFullMaintenanceTime))
}
if !changedParams && !changedSchedule {
return errors.Errorf("no changes specified")
}
if changedSchedule {
if err := maintenance.SetSchedule(ctx, rep, s); err != nil {
return errors.Wrap(err, "unable to set schedule")
}
}
if changedParams {
if err := maintenance.SetParams(ctx, rep, p); err != nil {
return errors.Wrap(err, "unable to set params")
}
}
return nil
}
func init() {
maintenanceSetCommand.Action(directRepositoryWriteAction(runMaintenanceSetParams))
}