snapshot: Lock instances before making snapshot

This commit is contained in:
Tom Wiesing 2022-10-12 16:18:14 +02:00
parent f1c5d518c2
commit 5e7c5b2d23
No known key found for this signature in database
11 changed files with 167 additions and 0 deletions

View file

@ -58,6 +58,7 @@ func (i info) Run(context wisski_distillery.Context) error {
context.Printf("GraphDB Password: %v\n", instance.GraphDBPassword) context.Printf("GraphDB Password: %v\n", instance.GraphDBPassword)
context.Printf("Running: %v\n", info.Running) context.Printf("Running: %v\n", info.Running)
context.Printf("Locked: %v\n", info.Locked)
context.Printf("Last Rebuild: %v\n", info.LastRebuild.String()) context.Printf("Last Rebuild: %v\n", info.LastRebuild.String())
context.Printf("Skip Prefixes: %v\n", info.NoPrefixes) context.Printf("Skip Prefixes: %v\n", info.NoPrefixes)

67
cmd/instance_lock.go Normal file
View 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
}

View file

@ -102,6 +102,12 @@ func (p purge) Run(context wisski_distillery.Context) error {
context.EPrintln(err) context.EPrintln(err)
} }
// remove the filesystem
logging.LogMessage(context.IOStream, "Remove lock data", instance.FilesystemBase)
if !instance.Unlock() {
context.EPrintln("instance was not locked")
}
logging.LogMessage(context.IOStream, "Instance %s has been purged", slug) logging.LogMessage(context.IOStream, "Instance %s has been purged", slug)
return nil return nil
} }

View file

@ -37,6 +37,7 @@ func init() {
// instance management // instance management
wdcli.Register(cmd.Ls) wdcli.Register(cmd.Ls)
wdcli.Register(cmd.Info) wdcli.Register(cmd.Info)
wdcli.Register(cmd.InstanceLock)
// instance tasks // instance tasks
wdcli.Register(cmd.Shell) wdcli.Register(cmd.Shell)

View file

@ -20,6 +20,7 @@
<b>Excluded from Resolver:</b> <code>{{ .Info.NoPrefixes }}</code><br /> <b>Excluded from Resolver:</b> <code>{{ .Info.NoPrefixes }}</code><br />
<hr /> <hr />
<b>Running:</b> <code>{{ .Info.Running }}</code> <br /> <b>Running:</b> <code>{{ .Info.Running }}</code> <br />
<b>Locked:</b> <code>{{ .Info.Locked }}</code> <br />
<!-- <b>OwnerEmail:</b> <code>{{ .Instance.OwnerEmail }}</code> <br /> --> <!-- <b>OwnerEmail:</b> <code>{{ .Instance.OwnerEmail }}</code> <br /> -->
<hr /> <hr />
<b>Created:</b> <code class="date">{{ .Instance.Created.Format "2006-01-02T15:04:05Z07:00" }}</code> <br /> <b>Created:</b> <code class="date">{{ .Instance.Created.Format "2006-01-02T15:04:05Z07:00" }}</code> <br />

View file

@ -0,0 +1,47 @@
package instances
import (
"errors"
"github.com/FAU-CDI/wisski-distillery/internal/models"
)
var ErrLocked = errors.New("instance is locked")
// TryLock attemps to lock this WissKI
// If this is not possible, returns ErrLocked
func (wisski WissKI) TryLock() error {
table, err := wisski.instances.SQL.QueryTable(true, models.LockTable)
if err != nil {
return ErrLocked
}
result := table.FirstOrCreate(&models.Lock{}, models.Lock{Slug: wisski.Slug})
locked := result.Error == nil && result.RowsAffected == 1
if !locked {
return ErrLocked
}
return nil
}
func (wisski WissKI) IsLocked() (locked bool) {
table, err := wisski.instances.SQL.QueryTable(true, models.LockTable)
if err != nil {
return false
}
// check if this instance is locked
table.Select("count(*) > 0").Where("slug = ?", wisski.Slug).Find(&locked)
return
}
// Unlock unlocks this WissKI instance and returns if it succeeded
func (wisski WissKI) Unlock() bool {
table, err := wisski.instances.SQL.QueryTable(true, models.LockTable)
if err != nil {
return false
}
result := table.Where("slug = ?", wisski.Slug).Delete(&models.Lock{})
return result.Error == nil && result.RowsAffected == 1
}

View file

@ -70,6 +70,11 @@ func (wisski *WissKI) setLastRebuild() error {
// //
// It also logs the current time into the metadata belonging to this instance. // It also logs the current time into the metadata belonging to this instance.
func (wisski *WissKI) Build(stream stream.IOStream, start bool) error { func (wisski *WissKI) Build(stream stream.IOStream, start bool) error {
if err := wisski.TryLock(); err != nil {
return err
}
defer wisski.Unlock()
barrel := wisski.Barrel() barrel := wisski.Barrel()
var context component.InstallationContext var context component.InstallationContext

View file

@ -16,6 +16,8 @@ type WissKIInfo struct {
Slug string // slug Slug string // slug
URL string // complete URL, including http(s) URL string // complete URL, including http(s)
Locked bool // Is this instance currently locked?
// Information about the running instance // Information about the running instance
Running bool Running bool
LastRebuild time.Time LastRebuild time.Time
@ -48,6 +50,12 @@ func (wisski *WissKI) Info(quick bool) (info WissKIInfo, err error) {
return return
}) })
// quick check if this instance is locked
group.Go(func() (err error) {
info.Locked = wisski.IsLocked()
return nil
})
// slower checks for extra properties. // slower checks for extra properties.
// these might execute php code or require additional database queries. // these might execute php code or require additional database queries.
if !quick { if !quick {

View file

@ -44,6 +44,21 @@ type Snapshot struct {
// Snapshot creates a new snapshot of this instance into dest // Snapshot creates a new snapshot of this instance into dest
func (snapshots *Manager) NewSnapshot(instance instances.WissKI, io stream.IOStream, desc SnapshotDescription) (snapshot Snapshot) { func (snapshots *Manager) NewSnapshot(instance instances.WissKI, io stream.IOStream, desc SnapshotDescription) (snapshot Snapshot) {
logging.LogMessage(io, "Locking instance")
if err := instance.TryLock(); err != nil {
io.EPrintln(err)
logging.LogMessage(io, "Aborting snapshot creation")
return Snapshot{
ErrPanic: err,
}
}
defer func() {
logging.LogMessage(io, "Unlocking instance")
instance.Unlock()
}()
// setup the snapshot // setup the snapshot
snapshot.Description = desc snapshot.Description = desc
snapshot.Instance = instance.Instance snapshot.Instance = instance.Instance

View file

@ -96,6 +96,11 @@ func (sql *SQL) Update(io stream.IOStream) error {
&models.Export{}, &models.Export{},
models.ExportTable, models.ExportTable,
}, },
{
"lock",
&models.Lock{},
models.LockTable,
},
} }
// migrate all of the tables! // migrate all of the tables!

11
internal/models/lock.go Normal file
View file

@ -0,0 +1,11 @@
package models
// LockTable is the name of the table the 'Metadatum' model is stored in.
const LockTable = "locks"
// Lock represents a log on WissKI Instances
type Lock struct {
Pk uint `gorm:"column:pk;primaryKey"`
Slug string `gorm:"column:slug;not null"` // slug of instance
}