snapshot: Lock instances before making snapshot
This commit is contained in:
parent
f1c5d518c2
commit
5e7c5b2d23
11 changed files with 167 additions and 0 deletions
67
cmd/instance_lock.go
Normal file
67
cmd/instance_lock.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/core"
|
||||
"github.com/tkw1536/goprogram/exit"
|
||||
)
|
||||
|
||||
// InstanceLock is then 'instance_lock' command
|
||||
var InstanceLock wisski_distillery.Command = instanceLock{}
|
||||
|
||||
type instanceLock struct {
|
||||
Lock bool `short:"l" long:"lock" description:"Lock the provided WissKI instance"`
|
||||
Unlock bool `short:"u" long:"unlock" description:"Unlock the provided WissKI instance"`
|
||||
Positionals struct {
|
||||
Slug string `positional-arg-name:"SLUG" required:"1-1" description:"slug of instance to lock or unlock"`
|
||||
} `positional-args:"true"`
|
||||
}
|
||||
|
||||
func (instanceLock) Description() wisski_distillery.Description {
|
||||
return wisski_distillery.Description{
|
||||
Requirements: core.Requirements{
|
||||
NeedsDistillery: true,
|
||||
},
|
||||
Command: "instance_lock",
|
||||
Description: "Locks or unlocks a WissKI instance",
|
||||
}
|
||||
}
|
||||
|
||||
var errLockUnlockExcluded = exit.Error{
|
||||
Message: "Exactly one of `--lock` and `--unlock` must be provied",
|
||||
ExitCode: exit.ExitCommandArguments,
|
||||
}
|
||||
|
||||
func (l instanceLock) AfterParse() error {
|
||||
if l.Lock == l.Unlock {
|
||||
return errLockUnlockExcluded
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var errNotUnlock = exit.Error{
|
||||
Message: "Unable to unlock instance: Not locked",
|
||||
ExitCode: exit.ExitCommandArguments,
|
||||
}
|
||||
|
||||
func (l instanceLock) Run(context wisski_distillery.Context) error {
|
||||
instance, err := context.Environment.Instances().WissKI(l.Positionals.Slug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Unlock {
|
||||
if !instance.Unlock() {
|
||||
return errNotUnlock
|
||||
}
|
||||
context.Println("unlocked")
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := instance.TryLock(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
context.Println("locked")
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue