Skip to content

Commit

Permalink
Replace waterlog by slog
Browse files Browse the repository at this point in the history
Replace waterlog by `log/slog` now that it's in the standard library.
The output should be similar when using a TTY but log friendly when not.
  • Loading branch information
silkeh committed Sep 20, 2023
1 parent 3a551cd commit 525691a
Show file tree
Hide file tree
Showing 24 changed files with 307 additions and 229 deletions.
61 changes: 31 additions & 30 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package builder
import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"

log "github.com/DataDrake/waterlog"
"github.com/getsolus/libosdev/disk"
)

Expand Down Expand Up @@ -106,17 +106,17 @@ func (p *Package) BindSources(o *Overlay) error {
}

// Find the target path in the chroot
log.Debugf("Exposing source to container %s\n", bindConfig.BindTarget)
slog.Debug("Exposing source to container", "target", bindConfig.BindTarget)

if st, err := os.Stat(bindConfig.BindSource); err == nil && st != nil {
if st.IsDir() {
if err := os.MkdirAll(bindConfig.BindTarget, 0o0755); err != nil {
log.Errorf("Failed to create bind mount target %s, reason: %s\n", bindConfig.BindTarget, err)
slog.Error("Failed to create bind mount", "target", bindConfig.BindTarget, "reason", err)
return nil
}
} else {
if err := TouchFile(bindConfig.BindTarget); err != nil {
log.Errorf("Failed to create bind mount target %s, reason: %s\n", bindConfig.BindTarget, err)
slog.Error("Failed to create bind mount target", "target", bindConfig.BindTarget, "reason", err)
return nil
}
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (p *Package) BindCcache(o *Overlay) error {
ccacheSource = CcacheDirectory
}

log.Debugf("Exposing ccache to build %s\n", ccacheDir)
slog.Debug("Exposing ccache to build", "dir", ccacheDir)

// Bind mount local ccache into chroot
if err := mountMan.BindMount(ccacheSource, ccacheDir); err != nil {
Expand All @@ -170,7 +170,7 @@ func (p *Package) BindSccache(o *Overlay) error {
sccacheSource = SccacheDirectory
}

log.Debugf("Exposing sccache to build %s\n", sccacheDir)
slog.Debug("Exposing sccache to build", "dir", sccacheDir)

// Bind mount local sccache into chroot
if err := mountMan.BindMount(sccacheSource, sccacheDir); err != nil {
Expand Down Expand Up @@ -290,7 +290,7 @@ func (p *Package) CopyAssets(h *PackageHistory, o *Overlay) error {

// PrepYpkg will do the initial leg work of preparing us for a ypkg build.
func (p *Package) PrepYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager, overlay *Overlay, h *PackageHistory) error {
log.Debugln("Writing packager file")
slog.Debug("Writing packager file")

fp := filepath.Join(overlay.MountPoint, BuildUserHome, ".config", "solus", "packager")
fpd := filepath.Dir(fp)
Expand All @@ -314,7 +314,7 @@ func (p *Package) PrepYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager,
}

// Install build dependencies
log.Debugf("Installing build dependencies %s\n", ymlFile)
slog.Debug("Installing build dependencies", "file", ymlFile)

if err := ChrootExec(notif, overlay.MountPoint, cmd); err != nil {
return fmt.Errorf("Failed to install build dependencies %s, reason: %w\n", ymlFile, err)
Expand All @@ -323,7 +323,7 @@ func (p *Package) PrepYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager,
notif.SetActivePID(0)

// Cleanup now
log.Debugln("Stopping D-BUS")
slog.Debug("Stopping D-BUS")

if err := pman.StopDBUS(); err != nil {
return fmt.Errorf("Failed to stop d-bus, reason: %w\n", err)
Expand Down Expand Up @@ -358,7 +358,7 @@ func (p *Package) BuildYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager
return err
}
} else {
log.Warnln("Package has explicitly requested networking, sandboxing disabled")
slog.Warn("Package has explicitly requested networking, sandboxing disabled")
}

// Bring up sources
Expand Down Expand Up @@ -399,18 +399,18 @@ func (p *Package) BuildYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager
cmd += fmt.Sprintf(" -t %v", h.GetLastVersionTimestamp())
}

log.Infoln("Now starting build of package")
slog.Info("Now starting build of package")

if err := ChrootExec(notif, overlay.MountPoint, cmd); err != nil {
return fmt.Errorf("Failed to start build of package, reason: %w\n", err)
}

// Generate ABI Report
if !DisableABIReport {
log.Debugln("Attempting to generate ABI report")
slog.Debug("Attempting to generate ABI report")

if err := p.GenerateABIReport(notif, overlay); err != nil {
log.Warnf("Failed to generate ABI report, reason: %s\n", err)
slog.Warn("Failed to generate ABI report", "reason", err)
return nil
}
}
Expand All @@ -424,7 +424,7 @@ func (p *Package) BuildYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager
// by Build().
func (p *Package) BuildXML(notif PidNotifier, pman *EopkgManager, overlay *Overlay) error {
// Just straight up build it with eopkg
log.Warnln("Full sandboxing is not possible with legacy format")
slog.Warn("Full sandboxing is not possible with legacy format")

wdir := p.GetWorkDirInternal()
xmlFile := filepath.Join(wdir, filepath.Base(p.Path))
Expand All @@ -450,19 +450,19 @@ func (p *Package) BuildXML(notif PidNotifier, pman *EopkgManager, overlay *Overl
}

// Now build the package, ignore-sandbox in case someone is stupid
// and activates it in eopkg.conf..
// and activates it in eopkg.conf...
cmd := eopkgCommand(fmt.Sprintf("eopkg build --ignore-sandbox --yes-all -O %s %s", wdir, xmlFile))

log.Infof("Now starting build of package %s\n", p.Name)
slog.Info(fmt.Sprintf("Now starting build of package %s\n", p.Name))

if err := ChrootExec(notif, overlay.MountPoint, cmd); err != nil {
return fmt.Errorf("Failed to start build of package.\n")
}

notif.SetActivePID(0)

// Now we can stop dbus..
log.Debugln("Stopping D-BUS")
// Now we can stop dbus...
slog.Debug("Stopping D-BUS")

if err := pman.StopDBUS(); err != nil {
return fmt.Errorf("Failed to stop d-bus, reason: %w\n", err)
Expand All @@ -479,7 +479,7 @@ func (p *Package) GenerateABIReport(notif PidNotifier, overlay *Overlay) error {

cmd := fmt.Sprintf("cd %s; abi-wizard %s/YPKG/root/%s/install", wdir, BuildUserHome, p.Name)
if err := ChrootExec(notif, overlay.MountPoint, cmd); err != nil {
log.Warnf("Failed to generate abi report %s\n", err)
slog.Warn("Failed to generate abi report", "reason", err)
return nil
}

Expand All @@ -496,8 +496,8 @@ func (p *Package) CollectAssets(overlay *Overlay, usr *UserInfo, manifestTarget

collections, _ := filepath.Glob(filepath.Join(collectionDir, "*.eopkg"))
if len(collections) < 1 {
log.Errorln("Mysterious lack of eopkg files is mysterious")
return errors.New("Internal error: .eopkg files are missing")
slog.Error("Mysterious lack of eopkg files is mysterious")
return errors.New("internal error: .eopkg files are missing")
}

// Prior to blitting the files out, let's grab the manifest if requested
Expand Down Expand Up @@ -532,24 +532,24 @@ func (p *Package) CollectAssets(overlay *Overlay, usr *UserInfo, manifestTarget
collections = append(collections, pspecs...)
}

log.Debugf("Collecting files %d\n", len(collections))
slog.Debug("Collecting files", "len", len(collections))

for _, p := range collections {
tgt, err := filepath.Abs(filepath.Join(".", filepath.Base(p)))
if err != nil {
return fmt.Errorf("Unable to find working directory, reason: %w\n", err)
}

log.Debugf("Collecting build artifact %s\n", filepath.Base(p))
slog.Debug("Collecting build artifact", "path", filepath.Base(p))

if err = disk.CopyFile(p, tgt); err != nil {
return fmt.Errorf("Unable to collect build file, reason: %w\n", err)
}

log.Debugf("Setting file ownership for current user UID='%d' GID='%d' %s\n", usr.UID, usr.GID, filepath.Base(p))
slog.Debug("Setting file ownership for current user", "uid", usr.UID, "gid", usr.GID, "path", filepath.Base(p))

if err = os.Chown(tgt, usr.UID, usr.GID); err != nil {
log.Errorf("Error in restoring file ownership %s, reason: %s\n", filepath.Base(p), err)
slog.Error("Error in restoring file ownership", "path", filepath.Base(p), "reason", err)
}
}

Expand All @@ -558,7 +558,8 @@ func (p *Package) CollectAssets(overlay *Overlay, usr *UserInfo, manifestTarget

// Build will attempt to build the package in the overlayfs system.
func (p *Package) Build(notif PidNotifier, history *PackageHistory, profile *Profile, pman *EopkgManager, overlay *Overlay, manifestTarget string) error {
log.Debugf("Building package %s %s %d %s %s\n", p.Name, p.Version, p.Release, p.Type, overlay.Back.Name)
slog.Debug("Building package", "name", p.Name, "version", p.Version, "release", p.Release, "type", p.Type,
"profile", overlay.Back.Name)

usr := GetUserInfo()

Expand Down Expand Up @@ -586,7 +587,7 @@ func (p *Package) Build(notif PidNotifier, history *PackageHistory, profile *Pro
return fmt.Errorf("Failed to copy required source assets, reason: %w\n", err)
}

log.Debugln("Validating sources")
slog.Debug("Validating sources")

if err := p.FetchSources(overlay); err != nil {
return err
Expand All @@ -598,7 +599,7 @@ func (p *Package) Build(notif PidNotifier, history *PackageHistory, profile *Pro
}

// Bring up dbus to do Things
log.Debugln("Starting D-BUS")
slog.Debug("Starting D-BUS")

if err := pman.StartDBUS(); err != nil {
return fmt.Errorf("Failed to start d-bus, reason: %w\n", err)
Expand All @@ -609,13 +610,13 @@ func (p *Package) Build(notif PidNotifier, history *PackageHistory, profile *Pro
return fmt.Errorf("Configuring repositories failed, reason: %w\n", err)
}

log.Debugln("Upgrading system base")
slog.Debug("Upgrading system base")

if err := pman.Upgrade(); err != nil {
return fmt.Errorf("Failed to upgrade rootfs, reason: %w\n", err)
}

log.Debugln("Asserting system.devel component installation")
slog.Debug("Asserting system.devel component installation")

if err := pman.InstallComponent("system.devel"); err != nil {
return fmt.Errorf("Failed to assert system.devel, reason: %w\n", err)
Expand Down
9 changes: 5 additions & 4 deletions builder/chroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ package builder

import (
"fmt"
"log/slog"
"os"

log "github.com/DataDrake/waterlog"
"github.com/getsolus/libosdev/commands"
)

// Chroot will attempt to spawn a chroot in the overlayfs system.
func (p *Package) Chroot(notif PidNotifier, pman *EopkgManager, overlay *Overlay) error {
log.Debugf("Beginning chroot: profile='%s' version='%s' package='%s' type='%s' release='%d'\n", overlay.Back.Name, p.Version, p.Name, p.Type, p.Release)
slog.Debug("Beginning chroot", "profile", overlay.Back.Name, "version", p.Version,
"package", p.Name, "type", p.Type, "release", p.Release)

var env []string
if p.Type == PackageTypeXML {
Expand All @@ -53,11 +54,11 @@ func (p *Package) Chroot(notif PidNotifier, pman *EopkgManager, overlay *Overlay
return err
}
} else {
log.Warnln("Package has explicitly requested networking, sandboxing disabled")
slog.Warn("Package has explicitly requested networking, sandboxing disabled")
}
}

log.Debugln("Spawning login shell")
slog.Debug("Spawning login shell")
// Allow bash to work
commands.SetStdin(os.Stdin)

Expand Down
6 changes: 3 additions & 3 deletions builder/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package builder

import (
"fmt"
"log/slog"
"os"
"path/filepath"

log "github.com/DataDrake/waterlog"
"github.com/getsolus/libosdev/disk"
)

Expand Down Expand Up @@ -56,15 +56,15 @@ func CopyAll(source, destdir string) error {
}
} else {
if !PathExists(destdir) {
log.Debugf("Creating target directory: %s\n", destdir)
slog.Debug("Creating target directory", "dir", destdir)

if err = os.MkdirAll(destdir, 0o0755); err != nil {
return fmt.Errorf("Failed to create target directory: %s, reason: %w\n", destdir, err)
}
}

tgt := filepath.Join(destdir, filepath.Base(source))
log.Debugf("Copying source asset %s to %s\n", source, tgt)
slog.Debug("Copying source", "source", source, "target", tgt)

if err = disk.CopyFile(source, tgt); err != nil {
return fmt.Errorf("Failed to copy source asset to target: source='%s' target='%s', reason: %w\n", source, tgt, err)
Expand Down
12 changes: 6 additions & 6 deletions builder/eopkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package builder
import (
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strings"

log "github.com/DataDrake/waterlog"
"github.com/getsolus/libosdev/commands"
"github.com/getsolus/libosdev/disk"
)
Expand Down Expand Up @@ -86,14 +86,14 @@ func (e *EopkgManager) CopyAssets() error {

dirName := filepath.Dir(value)
if !PathExists(dirName) {
log.Debugf("Creating required directory: %s\n", dirName)
slog.Debug("Creating required directory", "path", dirName)

if err := os.MkdirAll(dirName, 0o0755); err != nil {
return fmt.Errorf("Failed to create required asset directory %s, reason %w\n", dirName, err)
}
}

log.Debugf("Copying host asset %s\n", key)
slog.Debug("Copying host asset", "key", key)

if err := disk.CopyFile(key, value); err != nil {
return fmt.Errorf("Failed to copy host asset %s, reason: %w\n", key, err)
Expand All @@ -118,7 +118,7 @@ func (e *EopkgManager) Init() error {

// Ensure system wide cache exists
if !PathExists(e.cacheSource) {
log.Debugf("Creating system-wide package cache: %s\n", e.cacheSource)
slog.Debug("Creating system-wide package cache", "path", e.cacheSource)

if err := os.MkdirAll(e.cacheSource, 0o0755); err != nil {
return fmt.Errorf("Failed to create package cache %s, reason: %w\n", e.cacheSource, err)
Expand Down Expand Up @@ -296,7 +296,7 @@ func (e *EopkgManager) GetRepos() ([]*EopkgRepo, error) {

var repoFiles []string

log.Debugln("Discovering repos in rootfs")
slog.Debug("Discovering repos in rootfs")

repoFiles, _ = filepath.Glob(globPat)
// No repos
Expand All @@ -309,7 +309,7 @@ func (e *EopkgManager) GetRepos() ([]*EopkgRepo, error) {
for _, repo := range repoFiles {
uri, err := readURIFile(repo)
if err != nil {
log.Errorf("Unable to read repository file %s, reason: %s\n", repo, err)
slog.Error("Unable to read repository file", "path", repo, "err", err)
return nil, err
}

Expand Down
Loading

0 comments on commit 525691a

Please sign in to comment.