Add 'environment' package

This commit adds a new environment package that manages all calls to the
underlying operating system.
This commit is contained in:
Tom Wiesing 2022-09-18 14:24:22 +02:00
parent 822c70cd69
commit f19619ef9f
No known key found for this signature in database
60 changed files with 539 additions and 308 deletions

View file

@ -1,12 +1,10 @@
package cmd
import (
"io/fs"
"os"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/backup"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"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"
@ -59,7 +57,7 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
}
defer func() {
logging.LogMessage(context.IOStream, "Removing snapshot staging directory")
os.RemoveAll(sPath)
dis.Environment.RemoveAll(sPath)
}()
} else {
// staging mode: use dest as a destination
@ -73,8 +71,8 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
// create the directory (if it doesn't already exist)
logging.LogMessage(context.IOStream, "Creating staging directory")
err = os.Mkdir(sPath, fs.ModePerm)
if !os.IsExist(err) && err != nil {
err = dis.Core.Environment.Mkdir(sPath, environment.DefaultDirPerm)
if !environment.IsExist(err) && err != nil {
return errSnapshotFailed.WithMessageF(err)
}
err = nil
@ -86,7 +84,7 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
Dest: sPath,
Auto: bk.Positionals.Dest == "",
})
backup.WriteReport(context.IOStream)
backup.WriteReport(dis.Core.Environment, context.IOStream)
return nil
}, context.IOStream, "Generating Backup")
@ -108,7 +106,7 @@ func (bk backupC) Run(context wisski_distillery.Context) error {
if err := logging.LogOperation(func() error {
context.IOStream.Println(archivePath)
count, err = targz.Package(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)
})
context.Println("")

View file

@ -3,7 +3,7 @@ package cmd
import (
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/pkg/execx"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/tkw1536/goprogram/exit"
)
@ -47,7 +47,7 @@ func (bu blindUpdate) Run(context wisski_distillery.Context) error {
code, err := instance.Shell(context.IOStream, "/runtime/blind_update.sh")
if err != nil {
return errBlindUpdateFailed.WithMessageF(instance.Slug, execx.ExecCommandError)
return errBlindUpdateFailed.WithMessageF(instance.Slug, environment.ExecCommandError)
}
if code != 0 {
return errBlindUpdateFailed.WithMessageF(instance.Slug, code)

View file

@ -2,12 +2,12 @@ package cmd
import (
"io/fs"
"os"
"path/filepath"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/exit"
@ -67,11 +67,15 @@ var errBootstrapCreateFile = exit.Error{
}
func (bs bootstrap) Run(context wisski_distillery.Context) error {
// installation environment is the native environment!
// TODO: Should this be configurable?
var env environment.Native
root := bs.Directory
// check that we didn't get a different base directory
{
got, err := core.ReadBaseDirectory()
got, err := core.ReadBaseDirectory(env)
if err == nil && got != "" && got != root {
return errBootstrapDifferent.WithMessageF(got)
}
@ -79,10 +83,10 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error {
{
logging.LogMessage(context.IOStream, "Creating root deployment directory")
if err := os.MkdirAll(root, fs.ModeDir); err != nil {
if err := env.MkdirAll(root, environment.DefaultDirPerm); err != nil {
return errBootstrapFailedToCreateDirectory.WithMessageF(root)
}
if err := core.WriteBaseDirectory(root); err != nil {
if err := core.WriteBaseDirectory(env, root); err != nil {
return errBootstrapFailedToSaveDirectory.WithMessageF(root)
}
context.Println(root)
@ -98,18 +102,18 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error {
tpl.DefaultDomain = bs.Hostname
// and use thge defaults
if err := tpl.SetDefaults(); err != nil {
if err := tpl.SetDefaults(env); err != nil {
return errBootstrapWriteConfig.WithMessageF(err)
}
{
logging.LogMessage(context.IOStream, "Copying over wdcli executable")
exe, err := os.Executable()
exe, err := env.Executable()
if err != nil {
return errBoostrapFailedToCopyExe.WithMessageF(err)
}
err = fsx.CopyFile(wdcliPath, exe)
err = fsx.CopyFile(env, wdcliPath, exe)
if err != nil && err != fsx.ErrCopySameFile {
return errBoostrapFailedToCopyExe.WithMessageF(err)
}
@ -117,9 +121,9 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error {
}
{
if !fsx.IsFile(envPath) {
if !fsx.IsFile(env, envPath) {
if err := logging.LogOperation(func() error {
env, err := os.Create(envPath)
env, err := env.Create(envPath, environment.DefaultFilePerm)
if err != nil {
return err
}
@ -133,7 +137,8 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error {
if err := logging.LogOperation(func() error {
context.Println(tpl.SelfOverridesFile)
if err := os.WriteFile(
if err := environment.WriteFile(
env,
tpl.SelfOverridesFile,
core.DefaultOverridesJSON,
fs.ModePerm,
@ -142,7 +147,8 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error {
}
context.Println(tpl.AuthorizedKeys)
if err := os.WriteFile(
if err := environment.WriteFile(
env,
tpl.AuthorizedKeys,
core.DefaultAuthorizedKeys,
fs.ModePerm,
@ -160,14 +166,14 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error {
// re-read the configuration and print it!
logging.LogMessage(context.IOStream, "Configuration is now complete")
f, err := os.Open(envPath)
f, err := env.Open(envPath)
if err != nil {
return errBootstrapOpenConfig.WithMessageF(err)
}
defer f.Close()
var cfg config.Config
if err := cfg.Unmarshal(f); err != nil {
if err := cfg.Unmarshal(env, f); err != nil {
return errBootstrapOpenConfig.WithMessageF(err)
}
context.Println(cfg)

View file

@ -1,10 +1,10 @@
package cmd
import (
"os"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
)
@ -29,13 +29,10 @@ func (monday) Description() wisski_distillery.Description {
}
func (monday monday) AfterParse() error {
_, err := os.Stat(monday.Positionals.GraphdbZip)
if os.IsNotExist(err) {
// TODO: Use a generic environment here!
if !fsx.IsFile(environment.Native{}, monday.Positionals.GraphdbZip) {
return errNoGraphDBZip.WithMessageF(monday.Positionals.GraphdbZip)
}
if err != nil {
return err
}
return nil
}

View file

@ -57,7 +57,7 @@ func (p provision) Run(context wisski_distillery.Context) error {
// check that the base directory does not exist
logging.LogMessage(context.IOStream, "Checking that base directory %s does not exist", instance.FilesystemBase)
if fsx.IsDirectory(instance.FilesystemBase) {
if fsx.IsDirectory(dis.Environment, instance.FilesystemBase) {
return errProvisionAlreadyExists.WithMessageF(slug)
}

View file

@ -1,8 +1,6 @@
package cmd
import (
"os"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/core"
@ -73,7 +71,7 @@ func (p purge) Run(context wisski_distillery.Context) error {
// remove the filesystem
logging.LogMessage(context.IOStream, "Removing from filesystem %s", instance.FilesystemBase)
if err := os.RemoveAll(instance.FilesystemBase); err != nil {
if err := dis.Core.Environment.RemoveAll(instance.FilesystemBase); err != nil {
context.EPrintln(err)
}

View file

@ -33,7 +33,9 @@ var errRebuildFailed = exit.Error{
}
func (rb rebuild) Run(context wisski_distillery.Context) error {
instances, err := context.Environment.Instances().Load(rb.Positionals.Slug...)
dis := context.Environment
instances, err := dis.Instances().Load(rb.Positionals.Slug...)
if err != nil {
return err
}
@ -44,7 +46,7 @@ func (rb rebuild) Run(context wisski_distillery.Context) error {
logging.LogOperation(func() error {
s := instance.Barrel()
if err := logging.LogOperation(func() error {
return s.Install(context.IOStream, component.InstallationContext{})
return s.Install(dis.Core.Environment, context.IOStream, component.InstallationContext{})
}, context.IOStream, "Installing docker stack"); err != nil {
globalErr = err
return err

View file

@ -58,7 +58,7 @@ func (r reserve) Run(context wisski_distillery.Context) error {
// check that the base directory does not exist
logging.LogMessage(context.IOStream, "Checking that base directory %s does not exist", instance.FilesystemBase)
if fsx.IsDirectory(instance.FilesystemBase) {
if fsx.IsDirectory(dis.Environment, instance.FilesystemBase) {
return errProvisionAlreadyExists.WithMessageF(slug)
}
@ -66,7 +66,7 @@ func (r reserve) Run(context wisski_distillery.Context) error {
s := instance.Reserve()
{
if err := logging.LogOperation(func() error {
return s.Install(context.IOStream, component.InstallationContext{})
return s.Install(dis.Core.Environment, context.IOStream, component.InstallationContext{})
}, context.IOStream, "Installing docker stack"); err != nil {
return err
}

View file

@ -2,11 +2,11 @@ package cmd
import (
"io/fs"
"os"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
"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"
@ -60,7 +60,7 @@ func (bi snapshot) Run(context wisski_distillery.Context) error {
}
defer func() {
logging.LogMessage(context.IOStream, "Removing snapshot staging directory")
os.RemoveAll(sPath)
dis.Core.Environment.RemoveAll(sPath)
}()
} else {
// staging mode: use dest as a destination
@ -74,8 +74,8 @@ func (bi snapshot) Run(context wisski_distillery.Context) error {
// create the directory (if it doesn't already exist)
logging.LogMessage(context.IOStream, "Creating staging directory")
err = os.Mkdir(sPath, fs.ModePerm)
if !os.IsExist(err) && err != nil {
err = dis.Core.Environment.Mkdir(sPath, fs.ModePerm)
if !environment.IsExist(err) && err != nil {
return errSnapshotFailed.WithMessageF(err)
}
err = nil
@ -92,7 +92,7 @@ func (bi snapshot) Run(context wisski_distillery.Context) error {
})
// write out the report, ignoring any errors!
sreport.WriteReport(context.IOStream)
sreport.WriteReport(dis.Core.Environment, context.IOStream)
return nil
}, context.IOStream, "Generating Snapshot")
@ -115,7 +115,7 @@ func (bi snapshot) Run(context wisski_distillery.Context) error {
if err := logging.LogOperation(func() error {
context.IOStream.Println(archivePath)
count, err = targz.Package(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)
})
context.Println("")

View file

@ -1,13 +1,12 @@
package cmd
import (
"os"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/component"
"github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/pkg/execx"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
"github.com/tkw1536/goprogram/exit"
@ -43,13 +42,10 @@ var errNoGraphDBZip = exit.Error{
}
func (s systemupdate) AfterParse() error {
_, err := os.Stat(s.Positionals.GraphdbZip)
if os.IsNotExist(err) {
// TODO: Use a generic environment here!
if !fsx.IsFile(environment.Native{}, s.Positionals.GraphdbZip) {
return errNoGraphDBZip.WithMessageF(s.Positionals.GraphdbZip)
}
if err != nil {
return err
}
return nil
}
@ -85,7 +81,7 @@ func (si systemupdate) Run(context wisski_distillery.Context) error {
dis.SnapshotsArchivePath(),
} {
context.Println(d)
if err := os.MkdirAll(d, os.ModeDir); err != nil {
if err := dis.Core.Environment.MkdirAll(d, environment.DefaultDirPerm); err != nil {
return errBoostrapFailedToCreateDirectory.WithMessageF(d, err)
}
}
@ -123,10 +119,10 @@ func (si systemupdate) Run(context wisski_distillery.Context) error {
if err := logging.LogOperation(func() error {
for _, component := range dis.Installables() {
stack := component.Stack()
stack := component.Stack(dis.Core.Environment)
ctx := component.Context(ctx)
if err := logging.LogOperation(func() error {
return stack.Install(context.IOStream, ctx)
return stack.Install(dis.Core.Environment, context.IOStream, ctx)
}, context.IOStream, "Installing docker stack %q", component.Name()); err != nil {
return err
}
@ -143,7 +139,7 @@ func (si systemupdate) Run(context wisski_distillery.Context) error {
}
if err := logging.LogOperation(func() error {
return unpack.InstallDir(dis.Config.RuntimeDir(), "runtime", config.Runtime, func(dst, src string) {
return unpack.InstallDir(dis.Core.Environment, dis.Config.RuntimeDir(), "runtime", config.Runtime, func(dst, src string) {
context.Printf("[copy] %s\n", dst)
})
}, context.IOStream, "Unpacking Runtime Components"); err != nil {
@ -175,10 +171,11 @@ var errMustExecFailed = exit.Error{
// mustExec indicates that the given executable process must complete successfully.
// If it does not, returns errMustExecFailed
func (si systemupdate) mustExec(context wisski_distillery.Context, workdir string, exe string, argv ...string) error {
dis := context.Environment
if workdir == "" {
workdir = context.Environment.Config.DeployRoot
}
code := execx.Exec(context.IOStream, workdir, exe, argv...)
code := dis.Core.Environment.Exec(context.IOStream, workdir, exe, argv...)
if code != 0 {
err := errMustExecFailed.WithMessageF(code)
err.ExitCode = exit.ExitCode(code)

View file

@ -1,11 +1,11 @@
package cmd
import (
"io/fs"
"os"
"io"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/exit"
)
@ -42,7 +42,7 @@ func (upc updateprefixconfig) Run(context wisski_distillery.Context) error {
target := ddis.ResolverConfigPath()
// print the configuration
config, err := os.OpenFile(target, os.O_WRONLY, fs.ModePerm)
config, err := dis.Core.Environment.Create(target, environment.DefaultFilePerm)
if err != nil {
return errPrefixUpdateFailed.WithMessageF(err)
}
@ -58,7 +58,7 @@ func (upc updateprefixconfig) Run(context wisski_distillery.Context) error {
context.IOStream.Printf("%s", data)
// and write it out!
if _, err := config.WriteString(data); err != nil {
if _, err := io.WriteString(config, data); err != nil {
return err
}
@ -70,7 +70,7 @@ func (upc updateprefixconfig) Run(context wisski_distillery.Context) error {
// and restart the resolver to apply the config!
logging.LogMessage(context.IOStream, "restarting resolver stack")
if err := ddis.Stack().Restart(context.IOStream); err != nil {
if err := ddis.Stack(ddis.Environment).Restart(context.IOStream); err != nil {
return errPrefixUpdateFailed.WithMessageF(err)
}