From f9eb4f0f856e9fb3f36e4f3446e2cb8a5225fe97 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Mon, 6 Dec 2021 16:20:10 +0100 Subject: [PATCH 1/2] Update gomod Signed-off-by: Thomas Hipp --- go.mod | 3 +++ go.sum | 3 +++ 2 files changed, 6 insertions(+) diff --git a/go.mod b/go.mod index a9ec72e0..aa659d31 100644 --- a/go.mod +++ b/go.mod @@ -27,12 +27,15 @@ require ( github.com/mudler/docker-companion v0.4.6-0.20211015133729-bd4704fad372 github.com/opencontainers/image-spec v1.0.2 // indirect github.com/opencontainers/umoci v0.4.8-0.20211009121349-9c76304c034d // indirect + github.com/remyoudompheng/go-misc v0.0.0-20190427085024-2d6ac652a50e // indirect + github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/cobra v1.2.1 github.com/stretchr/testify v1.7.0 github.com/urfave/cli v1.22.5 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.7.0 // indirect go.uber.org/zap v1.19.1 + golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 gopkg.in/antchfx/htmlquery.v1 v1.2.2 gopkg.in/flosch/pongo2.v3 v3.0.0-20141028000813-5e81b817a0c4 diff --git a/go.sum b/go.sum index a820354c..0d2bcaaa 100644 --- a/go.sum +++ b/go.sum @@ -1050,6 +1050,8 @@ github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:5STLWrekHfjyYwxBRVRXNOSewLJ3PWfDJd1VyTS21fI= +github.com/remyoudompheng/go-misc v0.0.0-20190427085024-2d6ac652a50e h1:eTWZyPUnHcuGRDiryS/l2I7FfKjbU3IBx3IjqHPxuKU= +github.com/remyoudompheng/go-misc v0.0.0-20190427085024-2d6ac652a50e/go.mod h1:80FQABjoFzZ2M5uEa6FUaJYEmqU2UOKojlFVak1UAwI= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= @@ -1568,6 +1570,7 @@ golang.org/x/tools v0.0.0-20190110163146-51295c7ec13a/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190228203856-589c23e65e65/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190311215038-5c2858a9cfe5/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= From 3e12d0450a13258954ccec5c5b75f675ea975ee5 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Mon, 6 Dec 2021 16:20:33 +0100 Subject: [PATCH 2/2] *: Switch from zap to logrus Signed-off-by: Thomas Hipp --- distrobuilder/chroot.go | 14 +++++---- distrobuilder/main.go | 14 ++++----- distrobuilder/main_build-dir.go | 2 +- distrobuilder/main_lxc.go | 13 +++++---- distrobuilder/main_lxd.go | 9 +++--- distrobuilder/main_repack-windows.go | 21 +++++++------- generators/common.go | 9 +++--- generators/generators.go | 6 ++-- managers/common.go | 6 ++-- managers/manager.go | 7 ++--- shared/logger.go | 43 ++++++---------------------- sources/almalinux-http.go | 6 ++-- sources/alpine-http.go | 2 +- sources/alt-http.go | 2 +- sources/apertis-http.go | 2 +- sources/archlinux-http.go | 2 +- sources/busybox.go | 2 +- sources/centos-http.go | 2 +- sources/common.go | 6 ++-- sources/fedora-http.go | 4 +-- sources/funtoo-http.go | 2 +- sources/gentoo.go | 2 +- sources/opensuse-http.go | 2 +- sources/openwrt-http.go | 2 +- sources/oraclelinux-http.go | 4 +-- sources/rhel-common.go | 2 +- sources/rocky-http.go | 2 +- sources/rootfs-http.go | 2 +- sources/source.go | 7 ++--- sources/springdalelinux-http.go | 2 +- sources/ubuntu-http.go | 2 +- sources/voidlinux-http.go | 2 +- 32 files changed, 88 insertions(+), 115 deletions(-) diff --git a/distrobuilder/chroot.go b/distrobuilder/chroot.go index ccad5437..c3d7cd9c 100644 --- a/distrobuilder/chroot.go +++ b/distrobuilder/chroot.go @@ -6,11 +6,11 @@ import ( "os" "path/filepath" - "go.uber.org/zap" + "github.com/sirupsen/logrus" "golang.org/x/sys/unix" ) -func getOverlay(logger *zap.SugaredLogger, cacheDir, sourceDir string) (func(), string, error) { +func getOverlay(logger *logrus.Logger, cacheDir, sourceDir string) (func(), string, error) { var stat unix.Statfs_t // Skip overlay on xfs and zfs @@ -59,23 +59,25 @@ func getOverlay(logger *zap.SugaredLogger, cacheDir, sourceDir string) (func(), err := unix.Unmount(overlayDir, 0) if err != nil { - logger.Warnw("Failed to unmount overlay", "err", err, "dir", overlayDir) + logger.WithFields(logrus.Fields{"err": err, "dir": overlayDir}).Warn("Failed to unmount overlay directory") } err = os.RemoveAll(upperDir) if err != nil { - logger.Warnw("Failed to remove upper directory", "err", err, "dir", upperDir) + logger.WithFields(logrus.Fields{"err": err, "dir": upperDir}).Warn("Failed to remove upper directory") } err = os.RemoveAll(workDir) if err != nil { - logger.Warnw("Failed to remove work directory", "err", err, "dir", workDir) + logger.WithFields(logrus.Fields{"err": err, "dir": workDir}).Warn("Failed to remove work directory") + } err = os.Remove(overlayDir) if err != nil { - logger.Warnw("Failed to remove overlay directory", "err", err, "dir", overlayDir) + logger.WithFields(logrus.Fields{"err": err, "dir": overlayDir}).Warn("Failed to remove overlay directory") + } } diff --git a/distrobuilder/main.go b/distrobuilder/main.go index bd452ca7..6a53fffc 100644 --- a/distrobuilder/main.go +++ b/distrobuilder/main.go @@ -66,8 +66,8 @@ import ( "time" lxd "github.com/lxc/lxd/shared" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "go.uber.org/zap" "gopkg.in/yaml.v2" "github.com/lxc/distrobuilder/managers" @@ -109,7 +109,7 @@ type cmdGlobal struct { sourceDir string targetDir string interrupt chan os.Signal - logger *zap.SugaredLogger + logger *logrus.Logger overlayCleanup func() ctx context.Context cancel context.CancelFunc @@ -347,7 +347,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error { return fmt.Errorf("Failed to manage repositories: %w", err) } - c.logger.Infow("Running hooks", "trigger", "post-unpack") + c.logger.WithField("trigger", "post-unpack").Info("Running hooks") // Run post unpack hook for _, hook := range c.definition.GetRunnableActions("post-unpack", imageTargets) { @@ -365,7 +365,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error { return fmt.Errorf("Failed to manage packages: %w", err) } - c.logger.Infow("Running hooks", "trigger", "post-packages") + c.logger.WithField("trigger", "post-packages").Info("Running hooks") // Run post packages hook for _, hook := range c.definition.GetRunnableActions("post-packages", imageTargets) { @@ -411,10 +411,6 @@ func (c *cmdGlobal) preRunPack(cmd *cobra.Command, args []string) error { func (c *cmdGlobal) postRun(cmd *cobra.Command, args []string) error { hasLogger := c.logger != nil - if hasLogger { - defer c.logger.Sync() - } - // exit all chroots otherwise we cannot remove the cache directory for _, exit := range shared.ActiveChroots { if exit != nil { @@ -470,7 +466,7 @@ func (c *cmdGlobal) getOverlayDir() (string, func(), error) { } else { cleanup, overlayDir, err = getOverlay(c.logger, c.flagCacheDir, c.sourceDir) if err != nil { - c.logger.Warnw("Failed to create overlay", "err", err) + c.logger.WithField("err", err).Warn("Failed to create overlay") overlayDir = filepath.Join(c.flagCacheDir, "overlay") diff --git a/distrobuilder/main_build-dir.go b/distrobuilder/main_build-dir.go index a6a64483..859eb634 100644 --- a/distrobuilder/main_build-dir.go +++ b/distrobuilder/main_build-dir.go @@ -34,7 +34,7 @@ func (c *cmdBuildDir) command() *cobra.Command { return fmt.Errorf("Failed to load generator %q: %w", file.Generator, err) } - c.global.logger.Infow("Running generator", "generator", file.Generator) + c.global.logger.WithField("generator", file.Generator).Info("Running generator") err = generator.Run() if err != nil { diff --git a/distrobuilder/main_lxc.go b/distrobuilder/main_lxc.go index 5ca6402d..ecc91baf 100644 --- a/distrobuilder/main_lxc.go +++ b/distrobuilder/main_lxc.go @@ -119,7 +119,7 @@ func (c *cmdLXC) runPack(cmd *cobra.Command, args []string, overlayDir string) e return fmt.Errorf("Failed to manage repositories: %w", err) } - c.global.logger.Infow("Running hooks", "trigger", "post-unpack") + c.global.logger.WithField("trigger", "post-unpack").Info("Running hooks") // Run post unpack hook for _, hook := range c.global.definition.GetRunnableActions("post-unpack", imageTargets) { @@ -137,7 +137,7 @@ func (c *cmdLXC) runPack(cmd *cobra.Command, args []string, overlayDir string) e return fmt.Errorf("Failed to manage packages: %w", err) } - c.global.logger.Infow("Running hooks", "trigger", "post-packages") + c.global.logger.WithField("trigger", "post-packages").Info("Running hooks") // Run post packages hook for _, hook := range c.global.definition.GetRunnableActions("post-packages", imageTargets) { @@ -156,7 +156,8 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string, overlayDir string) error for _, file := range c.global.definition.Files { if !shared.ApplyFilter(&file, c.global.definition.Image.Release, c.global.definition.Image.ArchitectureMapped, c.global.definition.Image.Variant, c.global.definition.Targets.Type, shared.ImageTargetUndefined|shared.ImageTargetAll|shared.ImageTargetContainer) { - c.global.logger.Infow("Skipping generator", "generator", file.Generator) + c.global.logger.WithField("generator", file.Generator).Info("Skipping generator") + continue } @@ -165,7 +166,7 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string, overlayDir string) error return fmt.Errorf("Failed to load generator %q: %w", file.Generator, err) } - c.global.logger.Infow("Running generator", "generator", file.Generator) + c.global.logger.WithField("generator", file.Generator).Info("Running generator") err = generator.RunLXC(img, c.global.definition.Targets.LXC) if err != nil { @@ -181,7 +182,7 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string, overlayDir string) error addSystemdGenerator() - c.global.logger.Infow("Running hooks", "trigger", "post-files") + c.global.logger.WithField("trigger", "post-files").Info("Running hooks") // Run post files hook for _, action := range c.global.definition.GetRunnableActions("post-files", shared.ImageTargetUndefined|shared.ImageTargetAll|shared.ImageTargetContainer) { @@ -194,7 +195,7 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string, overlayDir string) error exitChroot() - c.global.logger.Infow("Creating LXC image", "compression", c.flagCompression) + c.global.logger.WithField("compression", c.flagCompression).Info("Creating LXC image") err = img.Build(c.flagCompression) if err != nil { diff --git a/distrobuilder/main_lxd.go b/distrobuilder/main_lxd.go index 7fb5b8e2..6df2a277 100644 --- a/distrobuilder/main_lxd.go +++ b/distrobuilder/main_lxd.go @@ -11,6 +11,7 @@ import ( client "github.com/lxc/lxd/client" lxd "github.com/lxc/lxd/shared" "github.com/lxc/lxd/shared/api" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "golang.org/x/sys/unix" @@ -179,7 +180,7 @@ func (c *cmdLXD) runPack(cmd *cobra.Command, args []string, overlayDir string) e return fmt.Errorf("Failed to manage repositories: %w", err) } - c.global.logger.Infow("Running hooks", "trigger", "post-unpack") + c.global.logger.WithField("trigger", "post-unpack").Info("Running hooks") // Run post unpack hook for _, hook := range c.global.definition.GetRunnableActions("post-unpack", imageTargets) { @@ -232,7 +233,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error return fmt.Errorf("Failed to load generator %q: %w", file.Generator, err) } - c.global.logger.Infow("Running generator", "generator", file.Generator) + c.global.logger.WithField("generator", file.Generator).Info("Running generator") err = generator.RunLXD(img, c.global.definition.Targets.LXD) if err != nil { @@ -346,7 +347,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error addSystemdGenerator() - c.global.logger.Infow("Running hooks", "trigger", "post-files") + c.global.logger.WithField("trigger", "post-files").Info("Running hooks") // Run post files hook for _, action := range c.global.definition.GetRunnableActions("post-files", imageTargets) { @@ -372,7 +373,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error } } - c.global.logger.Infow("Creating LXD image", "type", c.flagType, "vm", c.flagVM, "compression", c.flagCompression) + c.global.logger.WithFields(logrus.Fields{"type": c.flagType, "vm": c.flagVM, "compression": c.flagCompression}).Info("Creating LXD image") imageFile, rootfsFile, err := img.Build(c.flagType == "unified", c.flagCompression, c.flagVM) if err != nil { diff --git a/distrobuilder/main_repack-windows.go b/distrobuilder/main_repack-windows.go index 76409907..6872d13b 100644 --- a/distrobuilder/main_repack-windows.go +++ b/distrobuilder/main_repack-windows.go @@ -17,6 +17,7 @@ import ( "github.com/flosch/pongo2" lxd "github.com/lxc/lxd/shared" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "golang.org/x/sys/unix" @@ -56,13 +57,13 @@ func (c *cmdRepackWindows) command() *cobra.Command { err := unix.Statfs(dir, &stat) if err != nil { - c.global.logger.Warnw("Failed to get directory information", "directory", dir, "err", err) + c.global.logger.WithFields(logrus.Fields{"dir": dir, "err": err}).Warn("Failed to get directory information") continue } // Since there's no magic number for virtiofs, we need to check FUSE_SUPER_MAGIC (which is not defined in the unix package). if stat.Type == 0x65735546 { - c.global.logger.Warnw("FUSE filesystem detected, disabling overlay") + c.global.logger.Warn("FUSE filesystem detected, disabling overlay") c.global.flagDisableOverlay = true break } @@ -375,7 +376,7 @@ func (c *cmdRepackWindows) modifyWim(path string, index int) error { return errors.New("Failed to determine windows/system32/drivers path") } - logger.Infow("Modifying WIM file", "file", filepath.Base(path), "index", index) + logger.WithFields(logrus.Fields{"file": filepath.Base(path), "index": index}).Info("Modifying WIM file") // Create registry entries and copy files err = c.injectDrivers(dirs) @@ -511,7 +512,7 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error { softwareRegistry := "Windows Registry Editor Version 5.00" for driver, info := range windows.Drivers { - logger.Debugw("Injecting driver", "driver", driver) + logger.WithField("driver", driver).Debug("Injecting driver") ctx := pongo2.Context{ "infFile": fmt.Sprintf("oem%d.inf", i), @@ -535,7 +536,7 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error { // Copy driver files if lxd.StringInSlice(ext, []string{".cat", ".dll", ".inf", ".sys"}) { - logger.Debugw("Copying file", "src", path, "dest", targetPath) + logger.WithFields(logrus.Fields{"src": path, "dest": targetPath}).Debug("Copying file") err := shared.Copy(path, targetPath) if err != nil { @@ -546,7 +547,7 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error { // Copy .inf file if ext == ".inf" { target := filepath.Join(dirs["inf"], ctx["infFile"].(string)) - logger.Debugw("Copying file", "src", path, "dest", target) + logger.WithFields(logrus.Fields{"src": path, "dest": target}).Debug("Copying file") err = shared.Copy(path, target) if err != nil { @@ -581,7 +582,7 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error { // Copy .sys and .dll files if ext == ".dll" || ext == ".sys" { target := filepath.Join(dirs["drivers"], filepath.Base(path)) - logger.Debugw("Copying file", "src", path, "dest", target) + logger.WithFields(logrus.Fields{"src": path, "dest": target}).Debug("Copying file") err = shared.Copy(path, target) if err != nil { @@ -643,21 +644,21 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error { i++ } - logger.Debugw("Updating Windows registry", "hivefile", "DRIVERS") + logger.WithField("hivefile", "DRIVERS").Debug("Updating Windows registry") err := shared.RunCommand(c.global.ctx, strings.NewReader(driversRegistry), nil, "hivexregedit", "--merge", "--prefix='HKEY_LOCAL_MACHINE\\DRIVERS'", filepath.Join(dirs["config"], "DRIVERS")) if err != nil { return fmt.Errorf("Failed to edit Windows DRIVERS registry: %w", err) } - logger.Debugw("Updating Windows registry", "hivefile", "SYSTEM") + logger.WithField("hivefile", "SYSTEM").Debug("Updating Windows registry") err = shared.RunCommand(c.global.ctx, strings.NewReader(systemRegistry), nil, "hivexregedit", "--merge", "--prefix='HKEY_LOCAL_MACHINE\\SYSTEM'", filepath.Join(dirs["config"], "SYSTEM")) if err != nil { return fmt.Errorf("Failed to edit Windows SYSTEM registry: %w", err) } - logger.Debugw("Updating Windows registry", "hivefile", "SOFTWARE") + logger.WithField("hivefile", "SOFTWARE").Debug("Updating Windows registry") err = shared.RunCommand(c.global.ctx, strings.NewReader(softwareRegistry), nil, "hivexregedit", "--merge", "--prefix='HKEY_LOCAL_MACHINE\\SOFTWARE'", filepath.Join(dirs["config"], "SOFTWARE")) if err != nil { diff --git a/generators/common.go b/generators/common.go index a108db9b..26b025e9 100644 --- a/generators/common.go +++ b/generators/common.go @@ -1,19 +1,18 @@ package generators import ( - "go.uber.org/zap" - "github.com/lxc/distrobuilder/shared" + "github.com/sirupsen/logrus" ) type common struct { - logger *zap.SugaredLogger + logger *logrus.Logger cacheDir string sourceDir string defFile shared.DefinitionFile } -func (g *common) init(logger *zap.SugaredLogger, cacheDir string, sourceDir string, defFile shared.DefinitionFile, def shared.Definition) { +func (g *common) init(logger *logrus.Logger, cacheDir string, sourceDir string, defFile shared.DefinitionFile, def shared.Definition) { g.logger = logger g.cacheDir = cacheDir g.sourceDir = sourceDir @@ -26,7 +25,7 @@ func (g *common) init(logger *zap.SugaredLogger, cacheDir string, sourceDir stri out, err := shared.RenderTemplate(val, def) if err != nil { - logger.Warnw("Failed to render template", "err", err) + logger.WithField("err", err).Warn("Failed to render template") return val } diff --git a/generators/generators.go b/generators/generators.go index 66f0ebb2..e8bf6a9a 100644 --- a/generators/generators.go +++ b/generators/generators.go @@ -5,7 +5,7 @@ import ( "github.com/lxc/distrobuilder/image" "github.com/lxc/distrobuilder/shared" - "go.uber.org/zap" + "github.com/sirupsen/logrus" ) // ErrNotSupported returns a "Not supported" error @@ -15,7 +15,7 @@ var ErrNotSupported = errors.New("Not supported") var ErrUnknownGenerator = errors.New("Unknown generator") type generator interface { - init(logger *zap.SugaredLogger, cacheDir string, sourceDir string, defFile shared.DefinitionFile, def shared.Definition) + init(logger *logrus.Logger, cacheDir string, sourceDir string, defFile shared.DefinitionFile, def shared.Definition) Generator } @@ -40,7 +40,7 @@ var generators = map[string]func() generator{ } // Load loads and initializes a generator. -func Load(generatorName string, logger *zap.SugaredLogger, cacheDir string, sourceDir string, defFile shared.DefinitionFile, def shared.Definition) (Generator, error) { +func Load(generatorName string, logger *logrus.Logger, cacheDir string, sourceDir string, defFile shared.DefinitionFile, def shared.Definition) (Generator, error) { df, ok := generators[generatorName] if !ok { return nil, ErrUnknownGenerator diff --git a/managers/common.go b/managers/common.go index 2bcb0016..e68e5190 100644 --- a/managers/common.go +++ b/managers/common.go @@ -4,19 +4,19 @@ import ( "context" "github.com/lxc/distrobuilder/shared" - "go.uber.org/zap" + "github.com/sirupsen/logrus" ) type common struct { commands managerCommands flags managerFlags hooks managerHooks - logger *zap.SugaredLogger + logger *logrus.Logger definition shared.Definition ctx context.Context } -func (c *common) init(ctx context.Context, logger *zap.SugaredLogger, definition shared.Definition) { +func (c *common) init(ctx context.Context, logger *logrus.Logger, definition shared.Definition) { c.logger = logger c.definition = definition c.ctx = ctx diff --git a/managers/manager.go b/managers/manager.go index 1fd72bab..e07ca0ca 100644 --- a/managers/manager.go +++ b/managers/manager.go @@ -6,9 +6,8 @@ import ( "fmt" "strings" - "go.uber.org/zap" - "github.com/lxc/distrobuilder/shared" + "github.com/sirupsen/logrus" ) // ErrUnknownManager represents the unknown manager error @@ -47,7 +46,7 @@ type Manager struct { } type manager interface { - init(ctx context.Context, logger *zap.SugaredLogger, definition shared.Definition) + init(ctx context.Context, logger *logrus.Logger, definition shared.Definition) load() error manageRepository(repo shared.DefinitionPackagesRepository) error install(pkgs, flags []string) error @@ -73,7 +72,7 @@ var managers = map[string]func() manager{ } // Load loads and initializes a downloader. -func Load(ctx context.Context, managerName string, logger *zap.SugaredLogger, definition shared.Definition) (*Manager, error) { +func Load(ctx context.Context, managerName string, logger *logrus.Logger, definition shared.Definition) (*Manager, error) { df, ok := managers[managerName] if !ok { return nil, ErrUnknownManager diff --git a/shared/logger.go b/shared/logger.go index bb7641b9..dd0d92e1 100644 --- a/shared/logger.go +++ b/shared/logger.go @@ -1,47 +1,22 @@ package shared import ( - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "github.com/sirupsen/logrus" ) // GetLogger returns a new logger. -func GetLogger(debug bool) (*zap.SugaredLogger, error) { - encoderConfig := zapcore.EncoderConfig{ - TimeKey: "ts", - LevelKey: "level", - NameKey: "logger", - CallerKey: "caller", - FunctionKey: zapcore.OmitKey, - MessageKey: "msg", - StacktraceKey: "stacktrace", - LineEnding: zapcore.DefaultLineEnding, - EncodeLevel: zapcore.CapitalLevelEncoder, - EncodeCaller: zapcore.ShortCallerEncoder, - EncodeTime: zapcore.RFC3339TimeEncoder, - } +func GetLogger(debug bool) (*logrus.Logger, error) { + logger := logrus.StandardLogger() - config := zap.Config{ - Level: zap.NewAtomicLevelAt(zap.InfoLevel), - Development: false, - Encoding: "console", - EncoderConfig: encoderConfig, - OutputPaths: []string{"stdout"}, - ErrorOutputPaths: []string{"stderr"}, + formatter := logrus.TextFormatter{ + FullTimestamp: true, } - if debug { - config.Level.SetLevel(zap.DebugLevel) - } else { - config.EncoderConfig.TimeKey = "" - config.DisableCaller = true - config.DisableStacktrace = true - } + logger.Formatter = &formatter - logger, err := config.Build() - if err != nil { - return nil, err + if debug { + logger.Level = logrus.DebugLevel } - return logger.Sugar(), nil + return logger, nil } diff --git a/sources/almalinux-http.go b/sources/almalinux-http.go index 64989e31..de1c9368 100644 --- a/sources/almalinux-http.go +++ b/sources/almalinux-http.go @@ -46,7 +46,7 @@ func (s *almalinux) Run() error { stat, err := os.Stat(imagePath) if err == nil && stat.Size() > 0 { - s.logger.Infow("Unpacking raw image", "file", filepath.Join(fpath, strings.TrimSuffix(s.fname, ".xz"))) + s.logger.WithField("file", filepath.Join(fpath, strings.TrimSuffix(s.fname, ".xz"))).Info("Unpacking raw image") return s.unpackRaw(filepath.Join(fpath, strings.TrimSuffix(s.fname, ".xz")), s.rootfsDir, s.rawRunner) @@ -100,12 +100,12 @@ func (s *almalinux) Run() error { } if strings.HasSuffix(s.fname, ".raw.xz") || strings.HasSuffix(s.fname, ".raw") { - s.logger.Infow("Unpacking raw image", "file", filepath.Join(fpath, s.fname)) + s.logger.WithField("file", filepath.Join(fpath, s.fname)).Info("Unpacking raw image") return s.unpackRaw(filepath.Join(fpath, s.fname), s.rootfsDir, s.rawRunner) } - s.logger.Infow("Unpacking ISO", "file", filepath.Join(fpath, s.fname)) + s.logger.WithField("file", filepath.Join(fpath, s.fname)).Info("Unpacking ISO") return s.unpackISO(filepath.Join(fpath, s.fname), s.rootfsDir, s.isoRunner) } diff --git a/sources/alpine-http.go b/sources/alpine-http.go index f7534146..eb0013d3 100644 --- a/sources/alpine-http.go +++ b/sources/alpine-http.go @@ -80,7 +80,7 @@ func (s *alpineLinux) Run() error { } } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/alt-http.go b/sources/alt-http.go index 98b2f09a..5e179ab6 100644 --- a/sources/alt-http.go +++ b/sources/alt-http.go @@ -67,7 +67,7 @@ func (s *altLinux) Run() error { return fmt.Errorf("Failed to download %q: %w", baseURL+fname, err) } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/apertis-http.go b/sources/apertis-http.go index 5b8f6541..648e6645 100644 --- a/sources/apertis-http.go +++ b/sources/apertis-http.go @@ -78,7 +78,7 @@ func (s *apertis) Run() error { return fmt.Errorf("Failed to download %q: %w", baseURL+fname, err) } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/archlinux-http.go b/sources/archlinux-http.go index 4cfae3fd..23d4f74a 100644 --- a/sources/archlinux-http.go +++ b/sources/archlinux-http.go @@ -79,7 +79,7 @@ func (s *archlinux) Run() error { } } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/busybox.go b/sources/busybox.go index 19eb61f7..7aa1266a 100644 --- a/sources/busybox.go +++ b/sources/busybox.go @@ -44,7 +44,7 @@ func (s *busybox) Run() error { return fmt.Errorf("Failed to create directory %q: %w", sourceDir, err) } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), sourceDir, false, false, nil) diff --git a/sources/centos-http.go b/sources/centos-http.go index c778dbfd..f8b783a6 100644 --- a/sources/centos-http.go +++ b/sources/centos-http.go @@ -108,7 +108,7 @@ func (s *centOS) Run() error { source := filepath.Join(fpath, s.fname) - s.logger.Infow("Unpacking image", "file", source) + s.logger.WithField("file", source).Info("Unpacking image") if strings.HasSuffix(s.fname, ".raw.xz") || strings.HasSuffix(s.fname, ".raw") { err = s.unpackRaw(source, s.rootfsDir, s.rawRunner) diff --git a/sources/common.go b/sources/common.go index 94b35971..3ec9ea0b 100644 --- a/sources/common.go +++ b/sources/common.go @@ -17,11 +17,11 @@ import ( "github.com/lxc/distrobuilder/shared" lxd "github.com/lxc/lxd/shared" "github.com/lxc/lxd/shared/ioprogress" - "go.uber.org/zap" + "github.com/sirupsen/logrus" ) type common struct { - logger *zap.SugaredLogger + logger *logrus.Logger definition shared.Definition rootfsDir string cacheDir string @@ -29,7 +29,7 @@ type common struct { ctx context.Context } -func (s *common) init(ctx context.Context, logger *zap.SugaredLogger, definition shared.Definition, rootfsDir string, cacheDir string, sourcesDir string) { +func (s *common) init(ctx context.Context, logger *logrus.Logger, definition shared.Definition, rootfsDir string, cacheDir string, sourcesDir string) { s.logger = logger s.definition = definition s.rootfsDir = rootfsDir diff --git a/sources/fedora-http.go b/sources/fedora-http.go index a2a35f74..3dba7042 100644 --- a/sources/fedora-http.go +++ b/sources/fedora-http.go @@ -42,7 +42,7 @@ func (s *fedora) Run() error { return fmt.Errorf("Failed to download %q: %w", sourceURL, err) } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack the base image err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) @@ -94,7 +94,7 @@ func (s *fedora) unpackLayers(rootfsDir string) error { // remove files not relevant to the image. for _, manifest := range manifests { for _, layer := range manifest.Layers { - s.logger.Infow("Unpacking layer", "file", filepath.Join(rootfsDir, layer)) + s.logger.WithField("file", filepath.Join(rootfsDir, layer)).Info("Unpacking layer") err := lxd.Unpack(filepath.Join(rootfsDir, layer), rootfsDir, false, false, nil) if err != nil { diff --git a/sources/funtoo-http.go b/sources/funtoo-http.go index 44dcb6a7..196e92a4 100644 --- a/sources/funtoo-http.go +++ b/sources/funtoo-http.go @@ -109,7 +109,7 @@ func (s *funtoo) Run() error { } } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/gentoo.go b/sources/gentoo.go index c05c4c00..d85f2c9f 100644 --- a/sources/gentoo.go +++ b/sources/gentoo.go @@ -91,7 +91,7 @@ func (s *gentoo) Run() error { } } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/opensuse-http.go b/sources/opensuse-http.go index 36e879d7..6d579151 100644 --- a/sources/opensuse-http.go +++ b/sources/opensuse-http.go @@ -78,7 +78,7 @@ func (s *opensuse) Run() error { } } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/openwrt-http.go b/sources/openwrt-http.go index b1cdd8a7..a4d9a884 100644 --- a/sources/openwrt-http.go +++ b/sources/openwrt-http.go @@ -148,7 +148,7 @@ func (s *openwrt) Run() error { return fmt.Errorf("Failed to download %q: %w", baseURL+fname, err) } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil) diff --git a/sources/oraclelinux-http.go b/sources/oraclelinux-http.go index 1efccab9..2348bb21 100644 --- a/sources/oraclelinux-http.go +++ b/sources/oraclelinux-http.go @@ -81,7 +81,7 @@ func (s *oraclelinux) Run() error { return fmt.Errorf("Failed to download %q: %w", source, err) } - s.logger.Infow("Unpacking ISO", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking ISO") err = s.unpackISO(latestUpdate[1:], filepath.Join(fpath, fname), s.rootfsDir) if err != nil { @@ -135,7 +135,7 @@ func (s *oraclelinux) unpackISO(latestUpdate, filePath, rootfsDir string) error return fmt.Errorf("Failed to remove %q: %w", rootfsDir, err) } - s.logger.Infow("Unpacking root image", "file", rootfsImage) + s.logger.WithField("file", rootfsImage).Info("Unpacking root image") err = s.unpackRootfsImage(rootfsImage, tempRootDir) if err != nil { diff --git a/sources/rhel-common.go b/sources/rhel-common.go index de44e7ae..c52259a3 100644 --- a/sources/rhel-common.go +++ b/sources/rhel-common.go @@ -67,7 +67,7 @@ func (c *commonRHEL) unpackISO(filePath, rootfsDir string, scriptRunner func(str return fmt.Errorf("Failed to remove directory %q: %w", rootfsDir, err) } - c.logger.Infow("Unpacking root image", "file", rootfsImage) + c.logger.WithField("file", rootfsImage).Info("Unpacking root image") err = c.unpackRootfsImage(rootfsImage, tempRootDir) if err != nil { diff --git a/sources/rocky-http.go b/sources/rocky-http.go index 9ca9f5ec..58750f6c 100644 --- a/sources/rocky-http.go +++ b/sources/rocky-http.go @@ -66,7 +66,7 @@ func (s *rockylinux) Run() error { return fmt.Errorf("Failed to download %q: %w", baseURL+s.fname, err) } - s.logger.Infow("Unpacking ISO", "file", filepath.Join(fpath, s.fname)) + s.logger.WithField("file", filepath.Join(fpath, s.fname)).Info("Unpacking ISO") err = s.unpackISO(filepath.Join(fpath, s.fname), s.rootfsDir, s.isoRunner) if err != nil { diff --git a/sources/rootfs-http.go b/sources/rootfs-http.go index c291062f..8676619f 100644 --- a/sources/rootfs-http.go +++ b/sources/rootfs-http.go @@ -36,7 +36,7 @@ func (s *rootfs) Run() error { filename = path.Base(s.definition.Source.URL) } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, filename)) + s.logger.WithField("file", filepath.Join(fpath, filename)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, filename), s.rootfsDir, false, false, nil) diff --git a/sources/source.go b/sources/source.go index c597de34..f7bfc2aa 100644 --- a/sources/source.go +++ b/sources/source.go @@ -4,16 +4,15 @@ import ( "context" "errors" - "go.uber.org/zap" - "github.com/lxc/distrobuilder/shared" + "github.com/sirupsen/logrus" ) // ErrUnknownDownloader represents the unknown downloader error var ErrUnknownDownloader = errors.New("Unknown downloader") type downloader interface { - init(ctx context.Context, logger *zap.SugaredLogger, definition shared.Definition, rootfsDir string, cacheDir string, sourcesDir string) + init(ctx context.Context, logger *logrus.Logger, definition shared.Definition, rootfsDir string, cacheDir string, sourcesDir string) Downloader } @@ -48,7 +47,7 @@ var downloaders = map[string]func() downloader{ } // Load loads and initializes a downloader. -func Load(ctx context.Context, downloaderName string, logger *zap.SugaredLogger, definition shared.Definition, rootfsDir string, cacheDir string, sourcesDir string) (Downloader, error) { +func Load(ctx context.Context, downloaderName string, logger *logrus.Logger, definition shared.Definition, rootfsDir string, cacheDir string, sourcesDir string) (Downloader, error) { df, ok := downloaders[downloaderName] if !ok { return nil, ErrUnknownDownloader diff --git a/sources/springdalelinux-http.go b/sources/springdalelinux-http.go index a1a4e6c7..4f7a1611 100644 --- a/sources/springdalelinux-http.go +++ b/sources/springdalelinux-http.go @@ -32,7 +32,7 @@ func (s *springdalelinux) Run() error { return fmt.Errorf("Error downloading %q: %w", baseURL+s.fname, err) } - s.logger.Infow("Unpacking ISO", "file", filepath.Join(fpath, s.fname)) + s.logger.WithField("file", filepath.Join(fpath, s.fname)).Info("Unpacking ISO") err = s.unpackISO(filepath.Join(fpath, s.fname), s.rootfsDir, s.isoRunner) if err != nil { diff --git a/sources/ubuntu-http.go b/sources/ubuntu-http.go index bb3ed550..8c7bfea5 100644 --- a/sources/ubuntu-http.go +++ b/sources/ubuntu-http.go @@ -363,7 +363,7 @@ func (s ubuntu) unpack(filePath, rootDir string) error { return fmt.Errorf("Failed to create directory %q: %w", rootDir, err) } - s.logger.Infow("Unpacking file", "file", filePath) + s.logger.WithField("file", filePath).Info("Unpacking image") err = lxd.Unpack(filePath, rootDir, false, false, nil) if err != nil { diff --git a/sources/voidlinux-http.go b/sources/voidlinux-http.go index 0013977b..e3bb4083 100644 --- a/sources/voidlinux-http.go +++ b/sources/voidlinux-http.go @@ -80,7 +80,7 @@ func (s *voidlinux) Run() error { } } - s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, fname)) + s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image") // Unpack err = lxd.Unpack(filepath.Join(fpath, fname), s.rootfsDir, false, false, nil)