wdcli: Use progress writer instead of IOStream

This commit is contained in:
Tom Wiesing 2022-11-30 11:39:29 +01:00
parent 890022ae64
commit 3b78b06fff
No known key found for this signature in database
49 changed files with 396 additions and 393 deletions

View file

@ -13,7 +13,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream"
"golang.org/x/exp/slices"
)
@ -50,7 +49,7 @@ type BackupDescription struct {
}
// New create a new Backup
func (exporter *Exporter) NewBackup(ctx context.Context, io stream.IOStream, description BackupDescription) (backup Backup) {
func (exporter *Exporter) NewBackup(ctx context.Context, progress io.Writer, description BackupDescription) (backup Backup) {
backup.Description = description
// catch anything critical that happened during the snapshot
@ -61,16 +60,16 @@ func (exporter *Exporter) NewBackup(ctx context.Context, io stream.IOStream, des
// do the create keeping track of time!
logging.LogOperation(func() error {
backup.StartTime = time.Now().UTC()
backup.run(ctx, io, exporter)
backup.run(ctx, progress, exporter)
backup.EndTime = time.Now().UTC()
return nil
}, io, "Writing backup files")
}, progress, "Writing backup files")
return
}
func (backup *Backup) run(ctx context.Context, ios stream.IOStream, exporter *Exporter) {
func (backup *Backup) run(ctx context.Context, progress io.Writer, exporter *Exporter) {
// create a manifest
manifest, done := backup.handleManifest(backup.Description.Dest)
defer done()
@ -81,7 +80,7 @@ func (backup *Backup) run(ctx context.Context, ios stream.IOStream, exporter *Ex
// Component backup tasks
logging.LogOperation(func() error {
st := status.NewWithCompat(ios.Stdout, 0)
st := status.NewWithCompat(progress, 0)
st.Start()
defer st.Stop()
@ -96,7 +95,7 @@ func (backup *Backup) run(ctx context.Context, ios stream.IOStream, exporter *Ex
component.NewStagingContext(
ctx,
exporter.Environment,
stream.NewIOStream(writer, writer, nil, 0),
writer,
filepath.Join(backup.Description.Dest, bc.BackupName()),
manifest,
),
@ -111,11 +110,11 @@ func (backup *Backup) run(ctx context.Context, ios stream.IOStream, exporter *Ex
}
return nil
}, ios, "Backing up core components")
}, progress, "Backing up core components")
// backup instances
logging.LogOperation(func() error {
st := status.NewWithCompat(ios.Stdout, 0)
st := status.NewWithCompat(progress, 0)
st.Start()
defer st.Stop()
@ -149,7 +148,7 @@ func (backup *Backup) run(ctx context.Context, ios stream.IOStream, exporter *Ex
manifest <- dir
return exporter.NewSnapshot(ctx, instance, stream.NewIOStream(writer, writer, nil, 0), SnapshotDescription{
return exporter.NewSnapshot(ctx, instance, writer, SnapshotDescription{
Dest: dir,
})
},
@ -166,6 +165,6 @@ func (backup *Backup) run(ctx context.Context, ios stream.IOStream, exporter *Ex
})
return nil
}, ios, "Creating instance snapshots")
}, progress, "Creating instance snapshots")
}

View file

@ -2,6 +2,7 @@ package exporter
import (
"context"
"fmt"
"io"
"path/filepath"
@ -11,7 +12,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/targz"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream"
)
// ExportTask describes a task that makes either a [Backup] or a [Snapshot].
@ -43,7 +43,7 @@ type export interface {
// MakeExport performs an export task as described by flags.
// Output is directed to the provided io.
func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, task ExportTask) (err error) {
func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, task ExportTask) (err error) {
// extract parameters
Title := "Backup"
Slug := ""
@ -53,7 +53,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, ta
}
// determine target paths
logging.LogMessage(io, "Determining target paths")
logging.LogMessage(progress, "Determining target paths")
var stagingDir, archivePath string
if task.StagingOnly {
stagingDir = task.Dest
@ -69,11 +69,11 @@ func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, ta
if !task.StagingOnly && archivePath == "" {
archivePath = exporter.NewArchivePath(Slug)
}
io.Printf("Staging Directory: %s\n", stagingDir)
io.Printf("Archive Path: %s\n", archivePath)
fmt.Fprintf(progress, "Staging Directory: %s\n", stagingDir)
fmt.Fprintf(progress, "Archive Path: %s\n", archivePath)
// create the staging directory
logging.LogMessage(io, "Creating staging directory")
logging.LogMessage(progress, "Creating staging directory")
err = exporter.Environment.Mkdir(stagingDir, environment.DefaultDirPerm)
if !environment.IsExist(err) && err != nil {
return err
@ -83,7 +83,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, ta
// we need the staging directory to be deleted at the end
if !task.StagingOnly {
defer func() {
logging.LogMessage(io, "Removing staging directory")
logging.LogMessage(progress, "Removing staging directory")
exporter.Environment.RemoveAll(stagingDir)
}()
}
@ -96,11 +96,11 @@ func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, ta
var sl export
if task.Instance == nil {
task.BackupDescription.Dest = stagingDir
backup := exporter.NewBackup(ctx, io, task.BackupDescription)
backup := exporter.NewBackup(ctx, progress, task.BackupDescription)
sl = &backup
} else {
task.SnapshotDescription.Dest = stagingDir
snapshot := exporter.NewSnapshot(ctx, task.Instance, io, task.SnapshotDescription)
snapshot := exporter.NewSnapshot(ctx, task.Instance, progress, task.SnapshotDescription)
sl = &snapshot
}
@ -109,7 +109,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, ta
// find the report path
reportPath := filepath.Join(stagingDir, "report.txt")
io.Println(reportPath)
fmt.Fprintln(progress, reportPath)
// create the path
report, err := exporter.Environment.Create(reportPath, environment.DefaultFilePerm)
@ -122,28 +122,28 @@ func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, ta
_, err := sl.Report(report)
return err
}
}, io, "Generating %s", Title)
}, progress, "Generating %s", Title)
// if we only requested staging
// all that is left is to write the log entry
if task.StagingOnly {
logging.LogMessage(io, "Writing Log Entry")
logging.LogMessage(progress, "Writing Log Entry")
// write out the log entry
entry.Path = stagingDir
entry.Packed = false
exporter.ExporterLogger.Add(ctx, entry)
io.Printf("Wrote %s\n", stagingDir)
fmt.Fprintf(progress, "Wrote %s\n", stagingDir)
return nil
}
// package everything up as an archive!
if err := logging.LogOperation(func() error {
var count int64
defer func() { io.Printf("Wrote %d byte(s) to %s\n", count, archivePath) }()
defer func() { fmt.Fprintf(progress, "Wrote %d byte(s) to %s\n", count, archivePath) }()
st := status.NewWithCompat(io.Stdout, 1)
st := status.NewWithCompat(progress, 1)
st.Start()
defer st.Stop()
@ -152,12 +152,12 @@ func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, ta
})
return err
}, io, "Writing archive"); err != nil {
}, progress, "Writing archive"); err != nil {
return err
}
// write out the log entry
logging.LogMessage(io, "Writing Log Entry")
logging.LogMessage(progress, "Writing Log Entry")
entry.Path = archivePath
entry.Packed = true
exporter.ExporterLogger.Add(ctx, entry)

View file

@ -2,10 +2,10 @@ package exporter
import (
"context"
"fmt"
"io"
"path/filepath"
"time"
"github.com/tkw1536/goprogram/stream"
)
// ShouldPrune determines if a file with the provided modification time should be
@ -15,7 +15,7 @@ func (exporter *Exporter) ShouldPrune(modtime time.Time) bool {
}
// Prune prunes all old exports
func (exporter *Exporter) PruneExports(ctx context.Context, io stream.IOStream) error {
func (exporter *Exporter) PruneExports(ctx context.Context, progress io.Writer) error {
sPath := exporter.ArchivePath()
// list all the files
@ -43,7 +43,7 @@ func (exporter *Exporter) PruneExports(ctx context.Context, io stream.IOStream)
// assemble path, and then remove the file!
path := filepath.Join(sPath, entry.Name())
io.Printf("Removing %s cause it is older than %d days\n", path, exporter.Config.MaxBackupAge)
fmt.Fprintf(progress, "Removing %s cause it is older than %d days\n", path, exporter.Config.MaxBackupAge)
if err := exporter.Still.Environment.Remove(path); err != nil {
return err

View file

@ -14,7 +14,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/lib/collection"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream"
"golang.org/x/exp/slices"
)
@ -45,20 +44,20 @@ type Snapshot struct {
}
// Snapshot creates a new snapshot of this instance into dest
func (snapshots *Exporter) NewSnapshot(ctx context.Context, instance *wisski.WissKI, io stream.IOStream, desc SnapshotDescription) (snapshot Snapshot) {
func (snapshots *Exporter) NewSnapshot(ctx context.Context, instance *wisski.WissKI, progress io.Writer, desc SnapshotDescription) (snapshot Snapshot) {
logging.LogMessage(io, "Locking instance")
logging.LogMessage(progress, "Locking instance")
if !instance.Locker().TryLock(ctx) {
err := locker.Locked
io.EPrintln(err)
logging.LogMessage(io, "Aborting snapshot creation")
fmt.Fprintln(progress, err)
logging.LogMessage(progress, "Aborting snapshot creation")
return Snapshot{
ErrPanic: err,
}
}
defer func() {
logging.LogMessage(io, "Unlocking instance")
logging.LogMessage(progress, "Unlocking instance")
instance.Locker().Unlock(ctx)
}()
@ -75,27 +74,27 @@ func (snapshots *Exporter) NewSnapshot(ctx context.Context, instance *wisski.Wis
logging.LogOperation(func() error {
snapshot.StartTime = time.Now().UTC()
snapshot.ErrWhitebox = snapshot.makeParts(ctx, io, snapshots, instance, false)
snapshot.ErrBlackbox = snapshot.makeParts(ctx, io, snapshots, instance, true)
snapshot.ErrWhitebox = snapshot.makeParts(ctx, progress, snapshots, instance, false)
snapshot.ErrBlackbox = snapshot.makeParts(ctx, progress, snapshots, instance, true)
snapshot.EndTime = time.Now().UTC()
return nil
}, io, "Writing snapshot files")
}, progress, "Writing snapshot files")
slices.Sort(snapshot.Manifest)
return
}
func (snapshot *Snapshot) makeParts(ctx context.Context, ios stream.IOStream, snapshots *Exporter, instance *wisski.WissKI, needsRunning bool) map[string]error {
func (snapshot *Snapshot) makeParts(ctx context.Context, progress io.Writer, snapshots *Exporter, instance *wisski.WissKI, needsRunning bool) map[string]error {
if !needsRunning && !snapshot.Description.Keepalive {
stack := instance.Barrel().Stack()
logging.LogMessage(ios, "Stopping instance")
snapshot.ErrStop = stack.Down(ctx, ios)
logging.LogMessage(progress, "Stopping instance")
snapshot.ErrStop = stack.Down(ctx, progress)
defer func() {
logging.LogMessage(ios, "Starting instance")
snapshot.ErrStart = stack.Up(ctx, ios)
logging.LogMessage(progress, "Starting instance")
snapshot.ErrStart = stack.Up(ctx, progress)
}()
}
// handle writing the manifest!
@ -103,7 +102,7 @@ func (snapshot *Snapshot) makeParts(ctx context.Context, ios stream.IOStream, sn
defer done()
// create a new status
st := status.NewWithCompat(ios.Stdout, 0)
st := status.NewWithCompat(progress, 0)
st.Start()
defer st.Stop()
@ -126,7 +125,7 @@ func (snapshot *Snapshot) makeParts(ctx context.Context, ios stream.IOStream, sn
component.NewStagingContext(
ctx,
snapshots.Environment,
stream.NewIOStream(writer, writer, nil, 0),
writer,
filepath.Join(snapshot.Description.Dest, sc.SnapshotName()),
manifest,
),