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

Add filtering for environment variables and LXC target configs #750

Merged
merged 5 commits into from
Jul 17, 2023
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
2 changes: 1 addition & 1 deletion distrobuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
}

// Setup the mounts and chroot into the rootfs
exitChroot, err := shared.SetupChroot(c.sourceDir, c.definition.Environment, nil)
exitChroot, err := shared.SetupChroot(c.sourceDir, *c.definition, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot: %w", err)
}
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 @@ -49,7 +49,7 @@ func (c *cmdBuildDir) command() *cobra.Command {
}

exitChroot, err := shared.SetupChroot(c.global.targetDir,
c.global.definition.Environment, nil)
*c.global.definition, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot in %q: %w", c.global.targetDir, err)
}
Expand Down
4 changes: 2 additions & 2 deletions distrobuilder/main_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *cmdLXC) commandPack() *cobra.Command {

func (c *cmdLXC) runPack(cmd *cobra.Command, args []string, overlayDir string) error {
// Setup the mounts and chroot into the rootfs
exitChroot, err := shared.SetupChroot(overlayDir, c.global.definition.Environment, nil)
exitChroot, err := shared.SetupChroot(overlayDir, *c.global.definition, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot: %w", err)
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string, overlayDir string) error
}

exitChroot, err := shared.SetupChroot(overlayDir,
c.global.definition.Environment, nil)
*c.global.definition, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot in %q: %w", overlayDir, err)
}
Expand Down
4 changes: 2 additions & 2 deletions distrobuilder/main_lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (c *cmdLXD) commandPack() *cobra.Command {

func (c *cmdLXD) runPack(cmd *cobra.Command, args []string, overlayDir string) error {
// Setup the mounts and chroot into the rootfs
exitChroot, err := shared.SetupChroot(overlayDir, c.global.definition.Environment, nil)
exitChroot, err := shared.SetupChroot(overlayDir, *c.global.definition, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot: %w", err)
}
Expand Down Expand Up @@ -388,7 +388,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
}

exitChroot, err := shared.SetupChroot(rootfsDir,
c.global.definition.Environment, mounts)
*c.global.definition, mounts)
if err != nil {
return fmt.Errorf("Failed to chroot: %w", err)
}
Expand Down
6 changes: 6 additions & 0 deletions image/lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ func (l *LXCImage) Build(compression string) error {
func (l *LXCImage) createMetadata() error {
metaDir := filepath.Join(l.cacheDir, "metadata")

imageTargets := shared.ImageTargetUndefined | shared.ImageTargetContainer | shared.ImageTargetAll

for _, c := range l.definition.Targets.LXC.Config {
if !shared.ApplyFilter(&c, l.definition.Image.Release, l.definition.Image.ArchitectureMapped, l.definition.Image.Variant, l.definition.Targets.Type, imageTargets) {
continue
}

// If not specified, create files up to ${maxLXCCompatLevel}
if c.Before == 0 {
c.Before = maxLXCCompatLevel + 1
Expand Down
15 changes: 14 additions & 1 deletion shared/chroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func killChrootProcesses(rootfs string) error {
}

// SetupChroot sets up mount and files, a reverter and then chroots for you.
func SetupChroot(rootfs string, envs DefinitionEnv, m []ChrootMount) (func() error, error) {
func SetupChroot(rootfs string, definition Definition, m []ChrootMount) (func() error, error) {
// Mount the rootfs
err := unix.Mount(rootfs, rootfs, "", unix.MS_BIND, "")
if err != nil {
Expand Down Expand Up @@ -248,6 +248,7 @@ func SetupChroot(rootfs string, envs DefinitionEnv, m []ChrootMount) (func() err
}

var env Environment
envs := definition.Environment

if envs.ClearDefaults {
env = Environment{}
Expand All @@ -273,7 +274,19 @@ func SetupChroot(rootfs string, envs DefinitionEnv, m []ChrootMount) (func() err
}

if envs.EnvVariables != nil && len(envs.EnvVariables) > 0 {
imageTargets := ImageTargetUndefined | ImageTargetAll

if definition.Targets.Type == DefinitionFilterTypeContainer {
imageTargets |= ImageTargetContainer
} else if definition.Targets.Type == DefinitionFilterTypeVM {
imageTargets |= ImageTargetVM
}

for _, e := range envs.EnvVariables {
if !ApplyFilter(&e, definition.Image.Release, definition.Image.ArchitectureMapped, definition.Image.Variant, definition.Targets.Type, imageTargets) {
continue
}

entry, ok := env[e.Key]
if ok {
entry.Value = e.Value
Expand Down
14 changes: 8 additions & 6 deletions shared/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ type DefinitionSource struct {

// A DefinitionTargetLXCConfig represents the config part of the metadata.
type DefinitionTargetLXCConfig struct {
Type string `yaml:"type"`
Before uint `yaml:"before,omitempty"`
After uint `yaml:"after,omitempty"`
Content string `yaml:"content"`
DefinitionFilter `yaml:",inline"`
Type string `yaml:"type"`
Before uint `yaml:"before,omitempty"`
After uint `yaml:"after,omitempty"`
Content string `yaml:"content"`
}

// A DefinitionTargetLXC represents LXC specific files as part of the metadata.
Expand Down Expand Up @@ -240,8 +241,9 @@ type DefinitionMappings struct {

// DefinitionEnvVars defines custom environment variables.
type DefinitionEnvVars struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
DefinitionFilter `yaml:",inline"`
Key string `yaml:"key"`
Value string `yaml:"value"`
}

// DefinitionEnv represents the config part of the environment section.
Expand Down
2 changes: 1 addition & 1 deletion sources/alpine-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (s *alpineLinux) Run() error {
// Handle edge builds
if s.definition.Image.Release == "edge" {
// Upgrade to edge
exitChroot, err := shared.SetupChroot(s.rootfsDir, s.definition.Environment, nil)
exitChroot, err := shared.SetupChroot(s.rootfsDir, s.definition, nil)
if err != nil {
return fmt.Errorf("Failed to set up chroot: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion sources/oraclelinux-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (s *oraclelinux) unpackISO(latestUpdate, filePath, rootfsDir string) error
}

// Setup the mounts and chroot into the rootfs
exitChroot, err := shared.SetupChroot(tempRootDir, shared.DefinitionEnv{}, nil)
exitChroot, err := shared.SetupChroot(tempRootDir, shared.Definition{}, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions sources/rhel-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (c *commonRHEL) unpackISO(filePath, rootfsDir string, scriptRunner func(str
}

// Setup the mounts and chroot into the rootfs
exitChroot, err := shared.SetupChroot(tempRootDir, shared.DefinitionEnv{}, nil)
exitChroot, err := shared.SetupChroot(tempRootDir, shared.Definition{}, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot: %w", err)
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func (c *commonRHEL) unpackRaw(filePath, rootfsDir string, scriptRunner func() e
}

// Setup the mounts and chroot into the rootfs
exitChroot, err := shared.SetupChroot(tempRootDir, shared.DefinitionEnv{}, nil)
exitChroot, err := shared.SetupChroot(tempRootDir, shared.Definition{}, nil)
if err != nil {
return fmt.Errorf("Failed to setup chroot: %w", err)
}
Expand Down
Loading