Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to logrus #590

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions distrobuilder/chroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

}
}

Expand Down
14 changes: 5 additions & 9 deletions distrobuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion distrobuilder/main_build-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions distrobuilder/main_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions distrobuilder/main_lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
21 changes: 11 additions & 10 deletions distrobuilder/main_repack-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions generators/common.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions generators/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
Loading