{backup,snapshot,system_update}: Use better UI

This commit is contained in:
Tom Wiesing 2022-09-30 18:33:42 +02:00
parent 196555e897
commit ea56ff4aac
No known key found for this signature in database
11 changed files with 168 additions and 115 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/logging" "github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/targz" "github.com/FAU-CDI/wisski-distillery/pkg/targz"
"github.com/tkw1536/goprogram/exit" "github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/status"
) )
// Backup is the 'backup' command // Backup is the 'backup' command
@ -16,6 +17,7 @@ var Backup wisski_distillery.Command = backupC{}
type backupC struct { type backupC struct {
NoPrune bool `short:"n" long:"no-prune" description:"Do not prune older backup archives"` NoPrune bool `short:"n" long:"no-prune" description:"Do not prune older backup archives"`
StagingOnly bool `short:"s" long:"staging-only" description:"Do not package into a backup archive, but only create a staging directory"` StagingOnly bool `short:"s" long:"staging-only" description:"Do not package into a backup archive, but only create a staging directory"`
ConcurrentSnapshots int `short:"c" long:"concurrent-snapshots" description:"Maximum number of concurrent snapshots" default:"2"`
Positionals struct { Positionals struct {
Dest string `positional-arg-name:"DEST" description:"Destination path to write backup archive to. Defaults to the snapshots/archives/ directory"` Dest string `positional-arg-name:"DEST" description:"Destination path to write backup archive to. Defaults to the snapshots/archives/ directory"`
} `positional-args:"true"` } `positional-args:"true"`
@ -83,6 +85,7 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
backup := backup.New(context.IOStream, dis, backup.Description{ backup := backup.New(context.IOStream, dis, backup.Description{
Dest: sPath, Dest: sPath,
Auto: bk.Positionals.Dest == "", Auto: bk.Positionals.Dest == "",
ConcurrentSnapshots: bk.ConcurrentSnapshots,
}) })
backup.WriteReport(dis.Core.Environment, context.IOStream) backup.WriteReport(dis.Core.Environment, context.IOStream)
return nil return nil
@ -105,10 +108,13 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
if err := logging.LogOperation(func() error { if err := logging.LogOperation(func() error {
context.IOStream.Println(archivePath) context.IOStream.Println(archivePath)
st := status.NewWithCompat(context.Stdout, 1)
st.Start()
defer st.Stop()
count, err = targz.Package(dis.Core.Environment, archivePath, sPath, func(dst, src string) { count, err = targz.Package(dis.Core.Environment, archivePath, sPath, func(dst, src string) {
context.Printf("\033[2K\r%s", dst) st.Set(0, dst)
}) })
context.Println("")
return err return err
}, context.IOStream, "Writing backup archive"); err != nil { }, context.IOStream, "Writing backup archive"); err != nil {
return errSnapshotFailed.Wrap(err) return errSnapshotFailed.Wrap(err)

View file

@ -10,6 +10,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/logging" "github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/targz" "github.com/FAU-CDI/wisski-distillery/pkg/targz"
"github.com/tkw1536/goprogram/exit" "github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/status"
) )
// Snapshot creates a snapshot of an instance // Snapshot creates a snapshot of an instance
@ -115,10 +116,13 @@ func (bi snapshot) Run(context wisski_distillery.Context) error {
if err := logging.LogOperation(func() error { if err := logging.LogOperation(func() error {
context.IOStream.Println(archivePath) context.IOStream.Println(archivePath)
st := status.NewWithCompat(context.Stdout, 1)
st.Start()
defer st.Stop()
count, err = targz.Package(dis.Core.Environment, archivePath, sPath, func(dst, src string) { count, err = targz.Package(dis.Core.Environment, archivePath, sPath, func(dst, src string) {
context.Printf("\033[2K\r%s", dst) st.Set(0, dst)
}) })
context.Println("")
return err return err
}, context.IOStream, "Writing snapshot archive"); err != nil { }, context.IOStream, "Writing snapshot archive"); err != nil {
return errSnapshotFailed.Wrap(err) return errSnapshotFailed.Wrap(err)

View file

@ -11,8 +11,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/fsx" "github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging" "github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/exit" "github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/lib/status"
"github.com/tkw1536/goprogram/parser" "github.com/tkw1536/goprogram/parser"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream" "github.com/tkw1536/goprogram/stream"
) )
@ -111,18 +111,12 @@ func (si systemupdate) Run(context wisski_distillery.Context) error {
} }
if err := logging.LogOperation(func() error { if err := logging.LogOperation(func() error {
group := &status.Group[component.Installable]{ return status.RunErrorGroup(context.Stdout, status.Group[component.Installable, error]{
Writer: context.Stdout,
PrefixString: func(item component.Installable, index int) string { PrefixString: func(item component.Installable, index int) string {
return fmt.Sprintf("[install %q]: ", item.Name()) return fmt.Sprintf("[install %q]: ", item.Name())
}, },
PrefixAlign: true, PrefixAlign: true,
ErrString: func(item component.Installable, index int, err error) string {
if err == nil {
return "ok"
}
return "failed (" + err.Error() + ")"
},
Handler: func(item component.Installable, index int, writer io.Writer) error { Handler: func(item component.Installable, index int, writer io.Writer) error {
io := stream.NewIOStream(writer, writer, stream.Null, 0) io := stream.NewIOStream(writer, writer, stream.Null, 0)
stack := item.Stack(context.Environment.Environment) stack := item.Stack(context.Environment.Environment)
@ -137,9 +131,7 @@ func (si systemupdate) Run(context wisski_distillery.Context) error {
return nil return nil
}, },
} }, dis.Installables())
return group.Run(dis.Installables())
}, context.IOStream, "Performing Stack Updates"); err != nil { }, context.IOStream, "Performing Stack Updates"); err != nil {
return err return err
} }

14
go.mod
View file

@ -9,17 +9,19 @@ require (
github.com/feiin/sqlstring v0.3.0 github.com/feiin/sqlstring v0.3.0
github.com/go-sql-driver/mysql v1.6.0 github.com/go-sql-driver/mysql v1.6.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/tkw1536/goprogram v0.0.13 github.com/tkw1536/goprogram v0.0.17
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 golang.org/x/exp v0.0.0-20220929160808-de9c53c655b9
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0
gorm.io/driver/mysql v1.3.6 gorm.io/driver/mysql v1.3.6
gorm.io/gorm v1.23.8 gorm.io/gorm v1.23.10
) )
require ( require (
github.com/gosuri/uilive v0.0.4 // indirect
github.com/jessevdk/go-flags v1.5.0 // indirect github.com/jessevdk/go-flags v1.5.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 // indirect github.com/mattn/go-isatty v0.0.16 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/term v0.0.0-20220919170432-7a66f970e087 // indirect
) )

25
go.sum
View file

@ -8,6 +8,8 @@ github.com/feiin/sqlstring v0.3.0 h1:iyPEFijI2BxpY2M+AuhIvdNManzXa2OwGzuPaEMLUgo
github.com/feiin/sqlstring v0.3.0/go.mod h1:xpZTjVUw1nD3hMgF9SMRdPiooKSikLf4PS5j2NTn3RI= github.com/feiin/sqlstring v0.3.0/go.mod h1:xpZTjVUw1nD3hMgF9SMRdPiooKSikLf4PS5j2NTn3RI=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gosuri/uilive v0.0.4 h1:hUEBpQDj8D8jXgtCdBu7sWsy5sbW/5GhuO8KBwJ2jyY=
github.com/gosuri/uilive v0.0.4/go.mod h1:V/epo5LjjlDE5RJUcqx8dbw+zc93y5Ya3yg8tfZ74VI=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
@ -15,23 +17,26 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/tkw1536/goprogram v0.0.12 h1:CJI79IieP750q9j27OmoB1J/PC4povjk8kdckPVJ1YQ= github.com/tkw1536/goprogram v0.0.17 h1:SAD/rHtxm7CTEdUV1a37LUyE6G0MdcKhLzO8fZ8cFKI=
github.com/tkw1536/goprogram v0.0.12/go.mod h1:rX9MKOpJ9qAu4jHV2+n64SKmm3c2D3Hh1V8zC1H3jB4= github.com/tkw1536/goprogram v0.0.17/go.mod h1:Jqs0sTMzhrAGCX3JQrlEwQ0WRWQACCvuQQkaBDp65pE=
github.com/tkw1536/goprogram v0.0.13 h1:tq36MGZ24T+Xjv8y+bWbMGv2zx5prJ7tmi/bvs3wemA=
github.com/tkw1536/goprogram v0.0.13/go.mod h1:rX9MKOpJ9qAu4jHV2+n64SKmm3c2D3Hh1V8zC1H3jB4=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp v0.0.0-20220921164117-439092de6870 h1:j8b6j9gzSigH28O5SjSpQSSh9lFd6f5D/q0aHjNTulc= golang.org/x/exp v0.0.0-20220929160808-de9c53c655b9 h1:lNtcVz/3bOstm7Vebox+5m3nLh/BYWnhmc3AhXOW6oI=
golang.org/x/exp v0.0.0-20220921164117-439092de6870/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= golang.org/x/exp v0.0.0-20220929160808-de9c53c655b9/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 h1:0SH2R3f1b1VmIMG7BXbEZCBUu2dKmHschSmjqGUrW8A= golang.org/x/sync v0.0.0-20220907140024-f12130a52804 h1:0SH2R3f1b1VmIMG7BXbEZCBUu2dKmHschSmjqGUrW8A=
golang.org/x/sync v0.0.0-20220907140024-f12130a52804/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220907140024-f12130a52804/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 h1:wM1k/lXfpc5HdkJJyW9GELpd8ERGdnh8sMGL6Gzq3Ho= golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 h1:wM1k/lXfpc5HdkJJyW9GELpd8ERGdnh8sMGL6Gzq3Ho=
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc= golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc= golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220919170432-7a66f970e087 h1:tPwmk4vmvVCMdr98VgL4JH+qZxPL8fqlUOHnyOM8N3w= golang.org/x/term v0.0.0-20220919170432-7a66f970e087 h1:tPwmk4vmvVCMdr98VgL4JH+qZxPL8fqlUOHnyOM8N3w=
@ -40,5 +45,5 @@ gorm.io/driver/mysql v1.3.6 h1:BhX1Y/RyALb+T9bZ3t07wLnPZBukt+IRkMn8UZSNbGM=
gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c=
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE= gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.9 h1:NSHG021i+MCznokeXR3udGaNyFyBQJW8MbjrJMVCfGw= gorm.io/gorm v1.23.10 h1:4Ne9ZbzID9GUxRkllxN4WjJKpsHx8YbKvekVdgyWh24=
gorm.io/gorm v1.23.9/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= gorm.io/gorm v1.23.10/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=

View file

@ -2,14 +2,17 @@
package backup package backup
import ( import (
"fmt"
"io"
"path/filepath" "path/filepath"
"sync"
"time" "time"
"github.com/FAU-CDI/wisski-distillery/internal/component" "github.com/FAU-CDI/wisski-distillery/internal/component"
"github.com/FAU-CDI/wisski-distillery/internal/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/wisski" "github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/logging" "github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream" "github.com/tkw1536/goprogram/stream"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -35,59 +38,95 @@ func New(io stream.IOStream, dis *wisski.Distillery, description Description) (b
return return
} }
type backupResult struct { func (backup *Backup) run(ios stream.IOStream, dis *wisski.Distillery) {
name string //
err error // MANIFEST
//
manifest := make(chan string) // receive all the entries in the manifest
manifestDone := make(chan struct{}) // to signal that everything is finished
go func() {
defer close(manifestDone)
for file := range manifest {
// get the relative path to the root of the manifest
// or fallback to the absolute path!
path, err := filepath.Rel(backup.Description.Dest, file)
if err != nil {
path = file
} }
func (backup *Backup) run(io stream.IOStream, dis *wisski.Distillery) { // add the file to the manifest array
backup.Manifest = append(backup.Manifest, path)
}
// sort the manifest
slices.Sort(backup.Manifest)
}()
//
// BACKUP COMPONENTS
//
// create a new status display
backups := dis.Backupable() backups := dis.Backupable()
files := make(chan string, len(backups)) // channel for files being added into the backups
results := make(chan backupResult, len(backups)) // channel for results to be stored into
backup.ComponentErrors = make(map[string]error, len(backups)) backup.ComponentErrors = make(map[string]error, len(backups))
wg := &sync.WaitGroup{} // to wait for the results // Component backup tasks
wg.Add(len(backups)) // tell the group about all the operations logging.LogOperation(func() error {
for _, bc := range backups { st := status.NewWithCompat(ios.Stdout, 0)
go func(bc component.Backupable, context component.BackupContext) { st.Start()
defer wg.Done() defer st.Stop()
// make the backup and store the result return status.UseErrorGroup(st, status.Group[component.Backupable, error]{
results <- backupResult{ PrefixString: func(item component.Backupable, index int) string {
name: bc.Name(), return fmt.Sprintf("[backup %q]: ", item.Name())
err: bc.Backup(context), },
} PrefixAlign: true,
}(bc, &context{
Handler: func(bc component.Backupable, index int, writer io.Writer) error {
// create a new context for the backup!
context := &context{
env: dis.Core.Environment, env: dis.Core.Environment,
io: io, io: stream.NewIOStream(writer, writer, nil, 0),
dst: filepath.Join(backup.Description.Dest, bc.BackupName()), dst: filepath.Join(backup.Description.Dest, bc.BackupName()),
files: files, files: manifest,
})
} }
backup.ComponentErrors[bc.Name()] = bc.Backup(context)
return nil
},
}, backups)
}, ios, "Backing up core components")
// backup instances // backup instances
wg.Add(1) logging.LogOperation(func() error {
go func() { st := status.NewWithCompat(ios.Stdout, 0)
defer wg.Done() st.Start()
defer st.Stop()
instancesBackupDir := filepath.Join(backup.Description.Dest, "instances") instancesBackupDir := filepath.Join(backup.Description.Dest, "instances")
if err := dis.Core.Environment.Mkdir(instancesBackupDir, environment.DefaultDirPerm); err != nil { if err := dis.Core.Environment.Mkdir(instancesBackupDir, environment.DefaultDirPerm); err != nil {
backup.InstanceListErr = err backup.InstanceListErr = err
return return nil
} }
// list all instances // list all instances
instances, err := dis.Instances().All() wissKIs, err := dis.Instances().All()
if err != nil { if err != nil {
backup.InstanceListErr = err backup.InstanceListErr = err
return return nil
} }
backup.InstanceSnapshots = make([]wisski.Snapshot, len(instances)) // re-use the backup of the snapshots
for i, instance := range instances { backup.InstanceSnapshots = status.Group[instances.WissKI, wisski.Snapshot]{
backup.InstanceSnapshots[i] = func() wisski.Snapshot { PrefixString: func(item instances.WissKI, index int) string {
return fmt.Sprintf("[snapshot %s]: ", item.Slug)
},
PrefixAlign: true,
Handler: func(instance instances.WissKI, index int, writer io.Writer) wisski.Snapshot {
dir := filepath.Join(instancesBackupDir, instance.Slug) dir := filepath.Join(instancesBackupDir, instance.Slug)
if err := dis.Core.Environment.Mkdir(dir, environment.DefaultDirPerm); err != nil { if err := dis.Core.Environment.Mkdir(dir, environment.DefaultDirPerm); err != nil {
return wisski.Snapshot{ return wisski.Snapshot{
@ -95,44 +134,24 @@ func (backup *Backup) run(io stream.IOStream, dis *wisski.Distillery) {
} }
} }
files <- dir manifest <- dir
return dis.Snapshot(instance, io.NonInteractive(), wisski.SnapshotDescription{
return dis.Snapshot(instance, stream.NewIOStream(writer, writer, nil, 0), wisski.SnapshotDescription{
Dest: dir, Dest: dir,
}) })
}() },
} ResultString: func(res wisski.Snapshot, item instances.WissKI, index int) string {
return "done"
},
WaitString: status.DefaultWaitString[instances.WissKI],
HandlerLimit: backup.Description.ConcurrentSnapshots,
}.Use(st, wissKIs)
return nil
}, ios, "creating instance snapshots")
}() // close the manifest
close(manifest)
// finish processing all the results as soon as the group is done. <-manifestDone
go func() {
defer close(results)
wg.Wait()
}()
// finish the message processing once results are finished.
go func() {
defer close(files) // no more file processing!
for result := range results {
backup.ComponentErrors[result.name] = result.err
}
}()
for file := range files {
// get the relative path to the root of the manifest.
// nothing *should* go wrong, but in case it does, use the original path.
path, err := filepath.Rel(backup.Description.Dest, file)
if err != nil {
path = file
}
// write it to the command line
// and also add it to the manifest
io.Printf("\033[2K\r%s", path)
backup.Manifest = append(backup.Manifest, path)
}
slices.Sort(backup.Manifest) // backup the manifest
io.Println("")
// sort the instances manifest // sort the instances manifest
slices.SortFunc(backup.InstanceSnapshots, func(a, b wisski.Snapshot) bool { slices.SortFunc(backup.InstanceSnapshots, func(a, b wisski.Snapshot) bool {

View file

@ -27,6 +27,7 @@ func (bc *context) sendPath(path string) {
return return
} }
bc.io.Println(dst)
bc.files <- dst bc.files <- dst
} }
@ -93,6 +94,5 @@ func (bc *context) AddFile(path string, op func(file io.Writer) error) error {
bc.sendPath(path) bc.sendPath(path)
// and do whatever they wanted to do // and do whatever they wanted to do
// TODO: Add to the manifest of some sort
return op(file) return op(file)
} }

View file

@ -19,6 +19,8 @@ import (
type Description struct { type Description struct {
Dest string // Destination path Dest string // Destination path
Auto bool // Was the path created automatically? Auto bool // Was the path created automatically?
ConcurrentSnapshots int // maximum number of concurrent snapshots
} }
// Backup describes a backup // Backup describes a backup

View file

@ -1 +0,0 @@
package wisski

View file

@ -16,6 +16,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/logging" "github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/opgroup" "github.com/FAU-CDI/wisski-distillery/pkg/opgroup"
"github.com/FAU-CDI/wisski-distillery/pkg/password" "github.com/FAU-CDI/wisski-distillery/pkg/password"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream" "github.com/tkw1536/goprogram/stream"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -194,6 +195,10 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, dis *Distillery, inst
og := opgroup.NewOpGroup[string](4) og := opgroup.NewOpGroup[string](4)
st := status.NewWithCompat(io.Stdout, 0)
st.Start()
defer st.Stop()
// stop the instance (unless it was explicitly asked to not do so!) // stop the instance (unless it was explicitly asked to not do so!)
if !snapshot.Description.Keepalive { if !snapshot.Description.Keepalive {
logging.LogMessage(io, "Stopping instance") logging.LogMessage(io, "Stopping instance")
@ -207,7 +212,12 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, dis *Distillery, inst
// write bookkeeping information // write bookkeeping information
og.GoErr(func(files chan<- string) error { og.GoErr(func(files chan<- string) error {
line := st.OpenLine("[snapshot bookkeeping]: ", "")
defer line.Close()
defer fmt.Fprintln(line, "done")
bkPath := filepath.Join(snapshot.Description.Dest, "bookkeeping.txt") bkPath := filepath.Join(snapshot.Description.Dest, "bookkeeping.txt")
fmt.Fprintln(line, bkPath)
files <- bkPath files <- bkPath
info, err := dis.Core.Environment.Create(bkPath, environment.DefaultFilePerm) info, err := dis.Core.Environment.Create(bkPath, environment.DefaultFilePerm)
@ -224,17 +234,29 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, dis *Distillery, inst
// backup the filesystem // backup the filesystem
og.GoErr(func(files chan<- string) error { og.GoErr(func(files chan<- string) error {
line := st.OpenLine("[snapshot filesystem]: ", "")
defer line.Close()
defer fmt.Fprintln(line, "done")
fsPath := filepath.Join(snapshot.Description.Dest, filepath.Base(instance.FilesystemBase)) fsPath := filepath.Join(snapshot.Description.Dest, filepath.Base(instance.FilesystemBase))
// copy over whatever is in the base directory // copy over whatever is in the base directory
defer fmt.Fprintln(line, "done")
return fsx.CopyDirectory(dis.Core.Environment, fsPath, instance.FilesystemBase, func(dst, src string) { return fsx.CopyDirectory(dis.Core.Environment, fsPath, instance.FilesystemBase, func(dst, src string) {
fmt.Fprintln(line, dst)
files <- dst files <- dst
}) })
}, &snapshot.ErrFilesystem) }, &snapshot.ErrFilesystem)
// backup the graph db repository // backup the graph db repository
og.GoErr(func(files chan<- string) error { og.GoErr(func(files chan<- string) error {
line := st.OpenLine("[snapshot triplestore]: ", "")
defer line.Close()
defer fmt.Fprintln(line, "done")
tsPath := filepath.Join(snapshot.Description.Dest, instance.GraphDBRepository+".nq") tsPath := filepath.Join(snapshot.Description.Dest, instance.GraphDBRepository+".nq")
fmt.Fprintln(line, tsPath)
files <- tsPath files <- tsPath
nquads, err := dis.Core.Environment.Create(tsPath, environment.DefaultFilePerm) nquads, err := dis.Core.Environment.Create(tsPath, environment.DefaultFilePerm)
@ -250,7 +272,12 @@ func (snapshot *Snapshot) makeBlackbox(io stream.IOStream, dis *Distillery, inst
// backup the sql database // backup the sql database
og.GoErr(func(files chan<- string) error { og.GoErr(func(files chan<- string) error {
line := st.OpenLine("[snapshot sql]: ", "")
defer line.Close()
defer fmt.Fprintln(line, "done")
sqlPath := filepath.Join(snapshot.Description.Dest, snapshot.Instance.SqlDatabase+".sql") sqlPath := filepath.Join(snapshot.Description.Dest, snapshot.Instance.SqlDatabase+".sql")
fmt.Fprintln(line, sqlPath)
files <- sqlPath files <- sqlPath
sql, err := dis.Core.Environment.Create(sqlPath, environment.DefaultFilePerm) sql, err := dis.Core.Environment.Create(sqlPath, environment.DefaultFilePerm)
@ -302,12 +329,9 @@ func (snapshot *Snapshot) waitGroup(io stream.IOStream, og *opgroup.OpGroup[stri
path = file path = file
} }
// write it to the command line // add the manifest
// and also add it to the manifest
io.Printf("\033[2K\r%s", path)
snapshot.Manifest = append(snapshot.Manifest, path) snapshot.Manifest = append(snapshot.Manifest, path)
} }
io.Println("")
} }
// WriteReport writes out the report belonging to this snapshot. // WriteReport writes out the report belonging to this snapshot.

View file

@ -37,7 +37,7 @@ func (op *OpGroup[M]) GoErr(worker func(c chan<- M) error, dest *error) {
}) })
} }
// Wait returns a receive-only reference to the message channel.// // Wait returns a receive-only reference to the message channel.
// The message channel will be closed once all operations on this group have completed. // The message channel will be closed once all operations on this group have completed.
// //
// The Wait function may only be called once. // The Wait function may only be called once.