-
Notifications
You must be signed in to change notification settings - Fork 79
manifest: add firstboot support #1913
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
Open
lzap
wants to merge
3
commits into
osbuild:main
Choose a base branch
from
lzap:firstboot1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package firstboot | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "path/filepath" | ||
| "regexp" | ||
| "slices" | ||
|
|
||
| "github.com/osbuild/blueprint/pkg/blueprint" | ||
| "github.com/osbuild/images/pkg/shutil" | ||
| ) | ||
|
|
||
| type FirstbootOptions struct { | ||
| Scripts []Script | ||
| } | ||
|
|
||
| type Script struct { | ||
| Filename string | ||
| Contents string | ||
| IgnoreFailure bool | ||
| Certs []string | ||
| } | ||
|
|
||
| // FirstbootCommonOptions contains common fields for all firstboot options. | ||
| type FirstbootCommonOptions struct { | ||
| // Optional firstboot name. Must be unique within the blueprint and only | ||
| // alphanumeric characters with dashes and underscores are allowed. | ||
| Name string | ||
|
|
||
| // Ignore errors when executing the firstboot script and continue with | ||
| // execution of the following firstboot scripts, if any. By default, | ||
| // firstboot scripts are executed in order and if one of them fails, the | ||
| // execution stops immediately. | ||
| IgnoreFailure bool | ||
| } | ||
|
|
||
| // CustomFirstbootOptions contains fields specific to custom firstboot | ||
| // options. | ||
| type CustomFirstbootOptions struct { | ||
| FirstbootCommonOptions | ||
|
|
||
| // Strings without shebang will be interpreted as shell scripts, otherwise | ||
| // the script will be executed using the shebang interpreter. Required if | ||
| // type is set to "custom". | ||
| Contents string | ||
| } | ||
|
|
||
| // SatelliteFirstbootOptions contains fields specific to satellite firstboot | ||
| // options. | ||
| type SatelliteFirstbootOptions struct { | ||
| FirstbootCommonOptions | ||
|
|
||
| // Optional CA certificate to enroll into the system before executing the | ||
| // firstboot script. | ||
| CACerts []string | ||
|
|
||
| // Registration command as generated by the Satellite server. Required, if | ||
| // type is set to "satellite". | ||
| Command string | ||
| } | ||
|
|
||
| // AAPFirstbootOptions contains fields specific to AAP firstboot options. | ||
| type AAPFirstbootOptions struct { | ||
| FirstbootCommonOptions | ||
|
|
||
| // Optional CA certificate to enroll into the system before executing the | ||
| // firstboot script. | ||
| CACerts []string | ||
|
|
||
| // Job template URL as generated by the AAP server. Required if type is set | ||
| // to "aap". Example URLs are | ||
| // https://aap.example.com/api/controller/v2/job_templates/9/callback/ or | ||
| // https://aap.example.com/api/v2/job_templates/9/callback/ depending on the | ||
| // AAP version. | ||
| JobTemplateURL string | ||
|
|
||
| // The host config key. Required if type is set to "aap". | ||
| HostConfigKey string | ||
| } | ||
|
|
||
| // FirstbootOption is a union of all supported firstboot options. | ||
| type FirstbootOption interface { | ||
| isFirstbootOption() | ||
| } | ||
|
|
||
| func (CustomFirstbootOptions) isFirstbootOption() {} | ||
| func (SatelliteFirstbootOptions) isFirstbootOption() {} | ||
| func (AAPFirstbootOptions) isFirstbootOption() {} | ||
|
|
||
| var ErrFirstbootAlreadySet = errors.New("firstboot customization already set") | ||
|
|
||
| var reservedRegexp = regexp.MustCompile(`^(custom|satellite|aap)-\d+$`) | ||
|
|
||
| // FirstbootOptionsFromBP converts a blueprint FirstbootCustomization to | ||
| // FirstbootOptions. Validation is done in the blueprint package, so this function | ||
| // assumes the input is valid, however, JSON unmarshalling errors are possible. | ||
| func FirstbootOptionsFromBP(bpFirstboot blueprint.FirstbootCustomization) (*FirstbootOptions, error) { | ||
| fo := &FirstbootOptions{} | ||
| var satDone, aapDone bool | ||
| var ci int | ||
| var alreadyUsed []string | ||
|
|
||
| nameFunc := func(inputName, prefix string) string { | ||
| // Use number-based name if name was not provided, is a path, already used, | ||
| // or matches reserved pattern. | ||
| if inputName == "" || !filepath.IsLocal(inputName) || | ||
| slices.Contains(alreadyUsed, inputName) || reservedRegexp.MatchString(inputName) { | ||
| ci++ | ||
| return fmt.Sprintf("osbuild-first-%s-%d", prefix, ci) | ||
| } | ||
|
|
||
| // keep the naming convention consistent with the existing "osbuild-first-boot" | ||
| alreadyUsed = append(alreadyUsed, inputName) | ||
| return fmt.Sprintf("osbuild-first-%s", inputName) | ||
| } | ||
|
|
||
| for _, fbsc := range bpFirstboot.Scripts { | ||
| cust, sat, aap, err := fbsc.SelectUnion() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if cust != nil { | ||
| fo.Scripts = append(fo.Scripts, Script{ | ||
| Filename: nameFunc(cust.Name, "custom"), | ||
| Contents: cust.Contents, | ||
| IgnoreFailure: cust.IgnoreFailure, | ||
| }) | ||
| } | ||
|
|
||
| if sat != nil { | ||
| if satDone { | ||
| return nil, fmt.Errorf("%w: satellite", ErrFirstbootAlreadySet) | ||
| } | ||
| satDone = true | ||
|
|
||
| fo.Scripts = append(fo.Scripts, Script{ | ||
| Filename: nameFunc(sat.Name, "satellite"), | ||
| Contents: sat.Command, | ||
| IgnoreFailure: sat.IgnoreFailure, | ||
| Certs: sat.CACerts, | ||
| }) | ||
| } | ||
|
|
||
| if aap != nil { | ||
| if aapDone { | ||
| return nil, fmt.Errorf("%w: aap", ErrFirstbootAlreadySet) | ||
| } | ||
| aapDone = true | ||
|
|
||
| contents := fmt.Sprintf("#!/usr/bin/bash\ncurl -i --data %s %s\n", | ||
| shutil.Quote("host_config_key="+aap.HostConfigKey), | ||
| shutil.Quote(aap.JobTemplateURL), | ||
| ) | ||
|
|
||
| fo.Scripts = append(fo.Scripts, Script{ | ||
| Filename: nameFunc(aap.Name, "aap"), | ||
| Contents: contents, | ||
| IgnoreFailure: aap.IgnoreFailure, | ||
| Certs: aap.CACerts, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return fo, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.