Skip to content

Commit

Permalink
feat: add extract in build cli
Browse files Browse the repository at this point in the history
  • Loading branch information
erpz22 committed Aug 7, 2024
1 parent 104afab commit beb2562
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/eib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func main() {
app := cmd.NewApp()
app.Commands = []*cli.Command{
cmd.NewBuildCommand(build.Run),
cmd.NewExtractCommand(build.Extract),
cmd.NewValidateCommand(build.Validate),
cmd.NewVersionCommand(build.Version),
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/build/extract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package build

import (
"fmt"

"github.com/suse-edge/edge-image-builder/pkg/log"
)


Check failure on line 9 in pkg/build/extract.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` `-r 'interface{} -> any'` (gofmt)
func (b *Builder) Extract() error {
log.Audit("Generating image customization components...")

if err := b.configureCombustion(b.context); err != nil {
log.Audit("Error configuring customization components, check the logs under the build directory for more information.")
return fmt.Errorf("configuring combustion: %w", err)
}


log.Audit("Extract complete!")
return nil
}
119 changes: 119 additions & 0 deletions pkg/cli/build/extract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package build

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

"github.com/suse-edge/edge-image-builder/pkg/build"
"github.com/suse-edge/edge-image-builder/pkg/cache"
"github.com/suse-edge/edge-image-builder/pkg/cli/cmd"
"github.com/suse-edge/edge-image-builder/pkg/combustion"
"github.com/suse-edge/edge-image-builder/pkg/helm"
"github.com/suse-edge/edge-image-builder/pkg/image"
"github.com/suse-edge/edge-image-builder/pkg/kubernetes"
"github.com/suse-edge/edge-image-builder/pkg/log"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
)

const (
extractLogFilename = "eib-extract.log"
checkExtractLogMessage = "Please check the eib-extract.log file under the build directory for more information."
)

func Extract(_ *cli.Context) error {
args := &cmd.BuildArgs

rootBuildDir := args.RootBuildDir
if rootBuildDir == "" {
const defaultBuildDir = "_extract"

rootBuildDir = filepath.Join(args.ConfigDir, defaultBuildDir)
if err := os.MkdirAll(rootBuildDir, os.ModePerm); err != nil {
log.Auditf("The root build directory could not be set up under the configuration directory '%s'.", args.ConfigDir)
return err
}
}

buildDir, err := build.SetupBuildDirectory(rootBuildDir)
if err != nil {
log.Audit("The build directory could not be set up.")
return err
}

// This needs to occur as early as possible so that the subsequent calls can use the log
log.ConfigureGlobalLogger(filepath.Join(buildDir, extractLogFilename))

if cmdErr := imageConfigDirExists(args.ConfigDir); cmdErr != nil {
cmd.LogError(cmdErr, checkExtractLogMessage)
os.Exit(1)
}

imageDefinition, cmdErr := parseImageDefinition(args.ConfigDir, args.DefinitionFile)
if cmdErr != nil {
cmd.LogError(cmdErr, checkExtractLogMessage)
os.Exit(1)
}

combustionDir, artefactsDir, err := build.SetupCombustionDirectory(buildDir)
if err != nil {
log.Auditf("Setting up the combustion directory failed. %s", checkExtractLogMessage)
zap.S().Fatalf("Failed to create combustion directories: %s", err)
}

ctx := buildContext(buildDir, combustionDir, artefactsDir, args.ConfigDir, imageDefinition)

ctx.ImageDefinition.OperatingSystem.Packages = image.Packages{}
if cmdErr = validateImageDefinition(ctx); cmdErr != nil {
cmd.LogError(cmdErr, checkExtractLogMessage)
os.Exit(1)
}

appendHelm(ctx)

if cmdErr = bootstrapExtractDependencyServices(ctx, rootBuildDir); cmdErr != nil {
cmd.LogError(cmdErr, checkExtractLogMessage)
os.Exit(1)
}

defer func() {
if r := recover(); r != nil {
log.AuditInfo("Extract failed unexpectedly, check the logs under the extract directory for more information.")
zap.S().Fatalf("Unexpected error occurred: %s", r)
}
}()

builder := build.NewBuilder(ctx)
if err = builder.Extract(); err != nil {
zap.S().Fatalf("An error occurred extracting the image: %s", err)
}

return nil
}


Check failure on line 95 in pkg/cli/build/extract.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` `-r 'interface{} -> any'` (gofmt)
// If the image definition requires it, starts the necessary services, returning an error in the event of failure.
func bootstrapExtractDependencyServices(ctx *image.Context, rootDir string) *cmd.Error {
if combustion.IsEmbeddedArtifactRegistryConfigured(ctx) {
certsDir := filepath.Join(ctx.ImageConfigDir, combustion.K8sDir, combustion.HelmDir, combustion.CertsDir)
ctx.HelmClient = helm.New(ctx.BuildDir, certsDir)
}

if ctx.ImageDefinition.Kubernetes.Version != "" {
c, err := cache.New(rootDir)
if err != nil {
return &cmd.Error{
UserMessage: "Setting up file caching failed.",
LogMessage: fmt.Sprintf("Initializing cache instance failed: %v", err),
}
}

ctx.KubernetesScriptDownloader = kubernetes.ScriptDownloader{}
ctx.KubernetesArtefactDownloader = kubernetes.ArtefactDownloader{
Cache: c,
}
}

return nil
}
26 changes: 26 additions & 0 deletions pkg/cli/cmd/extract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"fmt"

"github.com/urfave/cli/v2"
)


Check failure on line 9 in pkg/cli/cmd/extract.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` `-r 'interface{} -> any'` (gofmt)
func NewExtractCommand(action func(*cli.Context) error) *cli.Command {
return &cli.Command{
Name: "extract",
Usage: "Extract new image",
UsageText: fmt.Sprintf("%s extract [OPTIONS]", appName),
Action: action,
Flags: []cli.Flag{
DefinitionFileFlag,
ConfigDirFlag,
&cli.StringFlag{
Name: "extract-dir",
Usage: "Full path to the directory to store extract artifacts",
Destination: &BuildArgs.RootBuildDir,
},
},
}
}

0 comments on commit beb2562

Please sign in to comment.