Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Refactor enki build-uki command
Browse files Browse the repository at this point in the history
so that it can be used as a library from Auroraboot
as part of kairos-io/kairos#1633

The following anti-patterns make it very hard to re-use the enki
functions:

- viper is used as a global store, randomly accessing everything from anywhere
- we pass around too many "configs": the BuildConfig, the embedded
  kairos-agent config, the BuildUKIAction (which is essentially yet another config),

It's nearly impossible to construct the proper input for any function
because we don't know what part of the configs available, is actually needed
and used.

This is a first refactoring that should at least let the caller use the
BuildUKIAction without needing to populate a global "viper" object.

Signed-off-by: Dimitris Karakasilis <[email protected]>
  • Loading branch information
jimmykarily committed Nov 1, 2024
1 parent 9583560 commit 950d879
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
30 changes: 13 additions & 17 deletions cmd/build-uki.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ func NewBuildUKICmd() *cobra.Command {
if !ol.IsDir() {
return fmt.Errorf("overlay-rootfs is not a directory: %s", overlayRootfs)
}

// Transform it into absolute path
absolutePath, err := filepath.Abs(overlayRootfs)
if err != nil {
viper.Set("overlay-rootfs", absolutePath)
}
}
overlayIso, _ := cmd.Flags().GetString("overlay-iso")
if overlayIso != "" {
Expand All @@ -78,12 +72,6 @@ func NewBuildUKICmd() *cobra.Command {
if artifact != string(constants.IsoOutput) {
return fmt.Errorf("overlay-iso is only supported for iso artifacts")
}

// Transform it into absolute path
absolutePath, err := filepath.Abs(overlayIso)
if err != nil {
viper.Set("overlay-iso", absolutePath)
}
}

// Check if the keys directory exists
Expand Down Expand Up @@ -124,10 +112,19 @@ func NewBuildUKICmd() *cobra.Command {
}

flags := cmd.Flags()
outputDir, _ := flags.GetString("output-dir")
keysDir, _ := flags.GetString("keys")
outputType, _ := flags.GetString("output-type")
a := action.NewBuildUKIAction(cfg, imgSource, outputDir, keysDir, outputType)
cfg.Name, _ = flags.GetString("name")
cfg.OutDir, _ = flags.GetString("output-dir")
cfg.OutputType, _ = flags.GetString("output-type")
cfg.KeysDir, _ = flags.GetString("keys")
overlayRootfs, _ := flags.GetString("overlay-rootfs")
if absolutePath, err := filepath.Abs(overlayRootfs); err != nil {
cfg.OverlayRootFS = absolutePath
}
overlayISO, _ := flags.GetString("overlay-iso")
if absolutePath, err := filepath.Abs(overlayISO); err != nil {
cfg.OverlayISO = absolutePath
}
a := action.NewBuildUKIAction(cfg, imgSource)
err = a.Run()
if err != nil {
cfg.Logger.Errorf(err.Error())
Expand Down Expand Up @@ -158,7 +155,6 @@ func NewBuildUKICmd() *cobra.Command {
c.MarkFlagRequired("keys")
// Mark some flags as mutually exclusive
c.MarkFlagsMutuallyExclusive([]string{"extra-cmdline", "extend-cmdline"}...)
viper.BindPFlags(c.Flags())
return c
}

Expand Down
58 changes: 31 additions & 27 deletions pkg/action/build-uki.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/kairos-io/enki/pkg/constants"
"github.com/klauspost/compress/zstd"
"github.com/sanity-io/litter"
"github.com/spf13/viper"
"github.com/u-root/u-root/pkg/cpio"
"golang.org/x/exp/maps"

Expand All @@ -28,25 +27,32 @@ import (
)

type BuildUKIAction struct {
img *v1.ImageSource
e *elemental.Elemental
outputDir string
keysDirectory string
logger sdkTypes.KairosLogger
outputType string
version string
arch string
name string
img *v1.ImageSource
e *elemental.Elemental
outputDir string
overlayRootFS string
secureBootEnroll string
includeVersionInConfig bool
includeCmdLineInConfig bool
overlayISO string
defaultEntry string
splash string
keysDirectory string
logger sdkTypes.KairosLogger
outputType string
version string
arch string
name string
}

func NewBuildUKIAction(cfg *types.BuildConfig, img *v1.ImageSource, outputDir, keysDirectory, outputType string) *BuildUKIAction {
func NewBuildUKIAction(cfg *types.BuildConfig, img *v1.ImageSource) *BuildUKIAction {
b := &BuildUKIAction{
logger: cfg.Logger,
img: img,
e: elemental.NewElemental(&cfg.Config),
outputDir: outputDir,
keysDirectory: keysDirectory,
outputType: outputType,
outputDir: cfg.OutDir,
keysDirectory: cfg.KeysDir,
outputType: cfg.OutputType,
arch: cfg.Arch,
name: cfg.Name,
}
Expand Down Expand Up @@ -76,9 +82,9 @@ func (b *BuildUKIAction) Run() error {
}
defer os.RemoveAll(sourceDir)

if viper.GetString("overlay-rootfs") != "" {
b.logger.Infof("Adding files from %s to rootfs", viper.GetString("overlay-rootfs"))
overlay, err := v1.NewSrcFromURI(fmt.Sprintf("dir:%s", viper.GetString("overlay-rootfs")))
if b.overlayRootFS != "" {
b.logger.Infof("Adding files from %s to rootfs", b.overlayRootFS)
overlay, err := v1.NewSrcFromURI(fmt.Sprintf("dir:%s", b.overlayRootFS))
if err != nil {
b.logger.Errorf("error creating overlay image: %s", err)
return err
Expand Down Expand Up @@ -158,7 +164,7 @@ func (b *BuildUKIAction) Run() error {
SBCert: filepath.Join(b.keysDirectory, "db.pem"),
SdBootPath: systemdBoot,
OutSdBootPath: outputSystemdBootEfi,
Splash: viper.GetString("splash"),
Splash: b.splash,
}

if err := os.Chdir(sourceDir); err != nil {
Expand Down Expand Up @@ -215,7 +221,7 @@ func (b *BuildUKIAction) Run() error {
// createSystemdConf creates the generic conf that systemd-boot uses
func (b *BuildUKIAction) createSystemdConf(sourceDir string) error {
var finalEfiConf string
entry := viper.GetString("default-entry")
entry := b.defaultEntry
if entry != "" {
if !strings.HasSuffix(entry, ".conf") {
finalEfiConf = strings.TrimSuffix(entry, " ") + ".conf"
Expand All @@ -229,9 +235,8 @@ func (b *BuildUKIAction) createSystemdConf(sourceDir string) error {
finalEfiConf = utils.NameFromCmdline(constants.ArtifactBaseName, constants.UkiCmdline+" "+constants.UkiCmdlineInstall) + ".conf"
}

secureBootEnroll := viper.GetString("secure-boot-enroll")
// Set that as default selection for booting
data := fmt.Sprintf("default %s\ntimeout 5\nconsole-mode max\neditor no\nsecure-boot-enroll %s\n", finalEfiConf, secureBootEnroll)
data := fmt.Sprintf("default %s\ntimeout 5\nconsole-mode max\neditor no\nsecure-boot-enroll %s\n", finalEfiConf, b.secureBootEnroll)
err := os.WriteFile(filepath.Join(sourceDir, "loader.conf"), []byte(data), os.ModePerm)
if err != nil {
return fmt.Errorf("creating the loader.conf file: %s", err)
Expand Down Expand Up @@ -446,11 +451,11 @@ func (b *BuildUKIAction) createConfFiles(sourceDir, cmdline, title, finalEfiName

configData := fmt.Sprintf("title %s\nefi /EFI/kairos/%s.efi\n", title, finalEfiName)

if viper.GetBool("include-version-in-config") {
if b.includeVersionInConfig {
configData = fmt.Sprintf("%sversion %s\n", configData, b.version)
}

if viper.GetBool("include-cmdline-in-config") {
if b.includeCmdLineInConfig {
configData = fmt.Sprintf("%scmdline %s\n", configData, strings.Trim(extraCmdline, " "))
}

Expand Down Expand Up @@ -502,9 +507,9 @@ func (b *BuildUKIAction) createISO(sourceDir string) error {
return err
}

if viper.GetString("overlay-iso") != "" {
b.logger.Infof("Adding files from %s to iso", viper.GetString("overlay-iso"))
overlay, err := v1.NewSrcFromURI(fmt.Sprintf("dir:%s", viper.GetString("overlay-iso")))
if b.overlayISO != "" {
b.logger.Infof("Adding files from %s to iso", b.overlayISO)
overlay, err := v1.NewSrcFromURI(fmt.Sprintf("dir:%s", b.overlayISO))
if err != nil {
b.logger.Errorf("error creating overlay image: %s", err)
return err
Expand All @@ -515,7 +520,6 @@ func (b *BuildUKIAction) createISO(sourceDir string) error {
b.logger.Errorf("error copying overlay image: %s", err)
return err
}

}

isoName := fmt.Sprintf("kairos_%s.iso", b.version)
Expand Down
10 changes: 7 additions & 3 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ type LiveISO struct {

// BuildConfig represents the config we need for building isos, raw images, artifacts
type BuildConfig struct {
Date bool `yaml:"date,omitempty" mapstructure:"date"`
Name string `yaml:"name,omitempty" mapstructure:"name"`
OutDir string `yaml:"output,omitempty" mapstructure:"output"`
Date bool `yaml:"date,omitempty" mapstructure:"date"`
Name string `yaml:"name,omitempty" mapstructure:"name"`
OutDir string `yaml:"output,omitempty" mapstructure:"output"`
OutputType string `yaml:"output_type,omitempty" mapstructure:"output_type"`
KeysDir string `yaml:"keys_dir,omitempty" mapstructure:"keys_dir"`
OverlayRootFS string `yaml:"overlay_rootfs,omitempty" mapstructure:"overlay_rootfs"`
OverlayISO string `yaml:"overlay_iso,omitempty" mapstructure:"overlay_iso"`

// 'inline' and 'squash' labels ensure config fields
// are embedded from a yaml and map PoV
Expand Down

0 comments on commit 950d879

Please sign in to comment.