Allow server to make backups

This commit is contained in:
Tom Wiesing 2022-10-07 16:30:07 +02:00
parent aeceae11d5
commit b3a827e042
No known key found for this signature in database
27 changed files with 891 additions and 418 deletions

View file

@ -1,18 +1,11 @@
package cmd
import (
"io"
"path/filepath"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/component/snapshots"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/targz"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/status"
)
// Backup is the 'backup' command
@ -53,13 +46,13 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
}
// do the handling
err := handleSnapshotLike(context, SnapshotFlags{
err := dis.SnapshotManager().HandleSnapshotLike(context.IOStream, snapshots.SnapshotFlags{
Dest: bk.Positionals.Dest,
Slug: "",
Title: "Backup",
StagingOnly: bk.StagingOnly,
Do: func(dest string) SnapshotLike {
Do: func(dest string) snapshots.SnapshotLike {
backup := dis.SnapshotManager().NewBackup(context.IOStream, snapshots.BackupDescription{
Dest: dest,
ConcurrentSnapshots: bk.ConcurrentSnapshots,
@ -73,126 +66,3 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
}
return nil
}
type SnapshotFlags struct {
Dest string
Slug string
Title string // "Backup" or "Snapshot"
StagingOnly bool
Do func(dest string) SnapshotLike
}
type SnapshotLike interface {
LogEntry() models.Snapshot
Report(w io.Writer) (int, error)
}
func handleSnapshotLike(context wisski_distillery.Context, flags SnapshotFlags) (err error) {
dis := context.Environment
// determine target paths
logging.LogMessage(context.IOStream, "Determining target paths")
var stagingDir, archivePath string
if flags.StagingOnly {
stagingDir = flags.Dest
} else {
archivePath = flags.Dest
}
if stagingDir == "" {
stagingDir, err = dis.SnapshotManager().NewStagingDir(flags.Slug)
if err != nil {
return err
}
}
if !flags.StagingOnly && archivePath == "" {
archivePath = dis.SnapshotManager().NewArchivePath(flags.Slug)
}
context.Printf("Staging Directory: %s\n", stagingDir)
context.Printf("Archive Path: %s\n", archivePath)
// create the staging directory
logging.LogMessage(context.IOStream, "Creating staging directory")
err = dis.Core.Environment.Mkdir(stagingDir, environment.DefaultDirPerm)
if !environment.IsExist(err) && err != nil {
return err
}
// if it was requested to not do staging only
// we need the staging directory to be deleted at the end
if !flags.StagingOnly {
defer func() {
logging.LogMessage(context.IOStream, "Removing staging directory")
dis.Environment.RemoveAll(stagingDir)
}()
}
// create the actual snapshot or backup
// write out the report
// and retain a log entry
var entry models.Snapshot
logging.LogOperation(func() error {
// do the snapshot!
sl := flags.Do(stagingDir)
// create a log entry
entry = sl.LogEntry()
// find the report path
reportPath := filepath.Join(stagingDir, "report.txt")
context.Println(reportPath)
// create the path
report, err := dis.Environment.Create(reportPath, environment.DefaultFilePerm)
if err != nil {
return err
}
// and write out the report
{
_, err := sl.Report(report)
return err
}
}, context.IOStream, "Generating %s", flags.Title)
// if we only requested staging
// all that is left is to write the log entry
if flags.StagingOnly {
logging.LogMessage(context.IOStream, "Writing Log Entry")
// write out the log entry
entry.Path = stagingDir
entry.Packed = false
dis.Instances().AddSnapshotLog(entry)
context.Printf("Wrote %s\n", stagingDir)
return nil
}
// package everything up as an archive!
if err := logging.LogOperation(func() error {
var count int64
defer func() { context.Printf("Wrote %d byte(s) to %s\n", count, archivePath) }()
st := status.NewWithCompat(context.Stdout, 1)
st.Start()
defer st.Stop()
count, err = targz.Package(dis.Core.Environment, archivePath, stagingDir, func(dst, src string) {
st.Set(0, dst)
})
return err
}, context.IOStream, "Writing archive"); err != nil {
return err
}
// write out the log entry
logging.LogMessage(context.IOStream, "Writing Log Entry")
entry.Path = archivePath
entry.Packed = true
dis.Instances().AddSnapshotLog(entry)
// and we're done!
return nil
}