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

FR-5570 - Support the keep-enabled parameter in ubuntu-image extra-ppas #150

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/imagedefinition/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package imagedefinition

import "errors"

var (
ErrKeepEnabledNil = errors.New("KeepEnabled is nil. Thi value cannot be properly used.")
)
62 changes: 54 additions & 8 deletions internal/statemachine/classic_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,22 @@ func (stateMachine *StateMachine) calculateStates() error {
}
} else if classicStateMachine.ImageDef.Rootfs.Seed != nil {
rootfsCreationStates = append(rootfsCreationStates, rootfsSeedStates...)
if classicStateMachine.ImageDef.Customization != nil {
if len(classicStateMachine.ImageDef.Customization.ExtraPPAs) > 0 {
rootfsCreationStates = append(rootfsCreationStates,
stateFunc{"add_extra_ppas", (*StateMachine).addExtraPPAs})
}
if classicStateMachine.ImageDef.Customization != nil && len(classicStateMachine.ImageDef.Customization.ExtraPPAs) > 0 {
rootfsCreationStates = append(rootfsCreationStates,
[]stateFunc{
{"add_extra_ppas", (*StateMachine).addExtraPPAs},
{"install_packages", (*StateMachine).installPackages},
{"clean_extra_ppas", (*StateMachine).cleanExtraPPAs},
}...)

} else {
rootfsCreationStates = append(rootfsCreationStates,
stateFunc{"install_packages", (*StateMachine).installPackages},
)
}

rootfsCreationStates = append(rootfsCreationStates,
[]stateFunc{
{"install_packages", (*StateMachine).installPackages},
{"prepare_image", (*StateMachine).prepareClassicImage},
{"preseed_image", (*StateMachine).preseedClassicImage},
}...,
Expand Down Expand Up @@ -573,6 +580,9 @@ func (stateMachine *StateMachine) addExtraPPAs() (err error) {
}
}
}()

trustedGPGD := filepath.Join(classicStateMachine.tempDirs.chroot, "etc", "apt", "trusted.gpg.d")

for _, ppa := range classicStateMachine.ImageDef.Customization.ExtraPPAs {
ppaFileName, ppaFileContents := createPPAInfo(ppa,
classicStateMachine.ImageDef.Series)
Expand All @@ -598,8 +608,7 @@ func (stateMachine *StateMachine) addExtraPPAs() (err error) {
keyFileName := strings.Replace(ppaFileName, ".sources", ".gpg", 1)
*/
keyFileName := strings.Replace(ppaFileName, ".list", ".gpg", 1)
keyFilePath := filepath.Join(classicStateMachine.tempDirs.chroot,
"etc", "apt", "trusted.gpg.d", keyFileName)
keyFilePath := filepath.Join(trustedGPGD, keyFileName)
err = importPPAKeys(ppa, tmpGPGDir, keyFilePath, stateMachine.commonFlags.Debug)
if err != nil {
err = fmt.Errorf("Error retrieving signing key for ppa \"%s\": %s",
Expand All @@ -615,6 +624,43 @@ func (stateMachine *StateMachine) addExtraPPAs() (err error) {
return nil
}

// cleanExtraPPAs cleans previously added PPA to the source list
func (stateMachine *StateMachine) cleanExtraPPAs() (err error) {
classicStateMachine := stateMachine.parent.(*ClassicStateMachine)

sourcesListD := filepath.Join(stateMachine.tempDirs.chroot, "etc", "apt", "sources.list.d")

for _, ppa := range classicStateMachine.ImageDef.Customization.ExtraPPAs {
if ppa.KeepEnabled == nil {
return imagedefinition.ErrKeepEnabledNil
}

if *ppa.KeepEnabled {
continue
}

ppaFileName, _ := createPPAInfo(ppa, classicStateMachine.ImageDef.Series)

ppaFile := filepath.Join(sourcesListD, ppaFileName)
err = osRemove(ppaFile)
if err != nil {
err = fmt.Errorf("Error removing %s: %s", ppaFile, err.Error())
return err
}

keyFileName := strings.Replace(ppaFileName, ".list", ".gpg", 1)
keyFilePath := filepath.Join(classicStateMachine.tempDirs.chroot,
upils marked this conversation as resolved.
Show resolved Hide resolved
"etc", "apt", "trusted.gpg.d", keyFileName)
err = osRemove(keyFilePath)
if err != nil {
err = fmt.Errorf("Error removing %s: %s", keyFilePath, err.Error())
return err
}
}

return nil
}

// Install packages in the chroot environment. This is accomplished by
// running commands to do the following:
// 1. Mount /proc /sys /dev and /run in the chroot
Expand Down