Add physical backend migrator command (#5143)

This commit is contained in:
Jim Kalafut
2018-09-25 16:18:22 -07:00
committed by GitHub
parent 1bcc20d254
commit 3ea652b0b4
5 changed files with 706 additions and 3 deletions

View File

@@ -37,6 +37,7 @@ import (
"github.com/hashicorp/vault/command/server"
serverseal "github.com/hashicorp/vault/command/server/seal"
"github.com/hashicorp/vault/helper/gated-writer"
"github.com/hashicorp/vault/helper/jsonutil"
"github.com/hashicorp/vault/helper/logging"
"github.com/hashicorp/vault/helper/mlock"
"github.com/hashicorp/vault/helper/namespace"
@@ -52,6 +53,8 @@ import (
var _ cli.Command = (*ServerCommand)(nil)
var _ cli.CommandAutocomplete = (*ServerCommand)(nil)
const migrationLock = "core/migration"
type ServerCommand struct {
*BaseCommand
@@ -460,6 +463,19 @@ func (c *ServerCommand) Run(args []string) int {
return 1
}
migrationStatus, err := CheckMigration(backend)
if err != nil {
c.UI.Error("Error checking migration status")
return 1
}
if migrationStatus != nil {
startTime := migrationStatus.Start.Format(time.RFC3339)
c.UI.Error(wrapAtLength(fmt.Sprintf("Storage migration in progress (started: %s). "+
"Use 'vault operator migrate -reset' to force clear the migration lock.", startTime)))
return 1
}
infoKeys := make([]string, 0, 10)
info := make(map[string]string)
info["log level"] = c.flagLogLevel
@@ -1773,6 +1789,51 @@ func (c *ServerCommand) removePidFile(pidPath string) error {
return os.Remove(pidPath)
}
type MigrationStatus struct {
Start time.Time `json:"start"`
}
func CheckMigration(b physical.Backend) (*MigrationStatus, error) {
entry, err := b.Get(context.Background(), migrationLock)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var status MigrationStatus
if err := jsonutil.DecodeJSON(entry.Value, &status); err != nil {
return nil, err
}
return &status, nil
}
func SetMigration(b physical.Backend, active bool) error {
if !active {
return b.Delete(context.Background(), migrationLock)
}
status := MigrationStatus{
Start: time.Now(),
}
enc, err := jsonutil.EncodeJSON(status)
if err != nil {
return err
}
entry := &physical.Entry{
Key: migrationLock,
Value: enc,
}
return b.Put(context.Background(), entry)
}
type grpclogFaker struct {
logger log.Logger
log bool