-
Notifications
You must be signed in to change notification settings - Fork 11
customization: add firstboot #23
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package blueprint | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| type FirstbootCustomization struct { | ||
| Scripts []FirstbootScriptCustomization `json:"scripts,omitempty" toml:"scripts,omitempty"` | ||
| } | ||
|
|
||
| type FirstbootScriptCustomization struct { | ||
| union json.RawMessage | ||
| } | ||
|
|
||
| // FirstbootCommonCustomization contains common fields for all firstboot customizations. | ||
| type FirstbootCommonCustomization struct { | ||
| // Type of the firstboot customization. Supported values are: | ||
| // "custom", "satellite", and "aap". | ||
| Type string `json:"type,omitempty" toml:"type,omitempty"` | ||
|
|
||
| // Optional firstboot name. Must be unique within the blueprint and only | ||
| // alphanumeric characters with dashes and underscores are allowed. | ||
| Name string `json:"name,omitempty" toml:"name,omitempty"` | ||
|
|
||
| // 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 `json:"ignore_failure,omitempty" toml:"ignore_failure,omitempty"` | ||
| } | ||
|
|
||
| // CustomFirstbootCustomization contains fields specific to custom firstboot | ||
| // customizations. | ||
| type CustomFirstbootCustomization struct { | ||
| FirstbootCommonCustomization | ||
|
|
||
| // 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 `json:"contents" toml:"contents"` | ||
| } | ||
|
|
||
| // SatelliteFirstbootCustomization contains fields specific to satellite firstboot | ||
| // customizations. | ||
| type SatelliteFirstbootCustomization struct { | ||
| FirstbootCommonCustomization | ||
|
|
||
| // Optional CA certificate to enroll into the system before executing the | ||
| // firstboot script. | ||
| CACerts []string `json:"cacerts,omitempty" toml:"cacerts,omitempty"` | ||
|
|
||
| // Registration command as generated by the Satellite server. Required, if | ||
| // type is set to "satellite". | ||
| Command string `json:"command,omitempty" toml:"command,omitempty"` | ||
| } | ||
|
|
||
| // AAPFirstbootCustomization contains fields specific to AAP firstboot | ||
| // customizations. | ||
| type AAPFirstbootCustomization struct { | ||
| FirstbootCommonCustomization | ||
|
|
||
| // Optional CA certificate to enroll into the system before executing the | ||
| // firstboot script. | ||
| CACerts []string `json:"cacerts,omitempty" toml:"cacerts,omitempty"` | ||
|
|
||
| // 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 `json:"job_template_url,omitempty" toml:"job_template_url,omitempty"` | ||
|
|
||
| // The host config key. Required if type is set to "aap". | ||
| HostConfigKey string `json:"host_config_key,omitempty" toml:"host_config_key,omitempty"` | ||
| } | ||
|
|
||
| func (t FirstbootScriptCustomization) MarshalJSON() ([]byte, error) { | ||
| b, err := t.union.MarshalJSON() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("FirstbootScriptCustomization marshalling error: %w", err) | ||
| } | ||
| return b, nil | ||
| } | ||
|
|
||
| func (t *FirstbootScriptCustomization) UnmarshalJSON(b []byte) error { | ||
| err := t.union.UnmarshalJSON(b) | ||
| if err != nil { | ||
| return fmt.Errorf("FirstbootScriptCustomization unmarshalling error: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (t FirstbootScriptCustomization) MarshalTOML() ([]byte, error) { | ||
| b, err := t.union.MarshalJSON() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return jsonToToml(b) | ||
| } | ||
|
|
||
| func (t *FirstbootScriptCustomization) UnmarshalTOML(data any) error { | ||
| return unmarshalTOMLviaJSON(t, data) | ||
| } | ||
|
|
||
| var ErrMissingCustomContents = errors.New("missing contents field for custom firstboot customization") | ||
| var ErrUnknownFirstbootCustomization = errors.New("unknown firstboot customization: missing or invalid type field") | ||
| var ErrMissingAAPFields = errors.New("missing job_template_url or host_config_key field for aap firstboot customization") | ||
| var ErrMissingSatelliteCommand = errors.New("missing command field for satellite firstboot customization") | ||
|
|
||
| func unmarshalFirstbootScript[S any](data json.RawMessage) (*S, error) { | ||
| var script S | ||
| jd := json.NewDecoder(bytes.NewReader(data)) | ||
| jd.DisallowUnknownFields() | ||
| err := jd.Decode(&script) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &script, nil | ||
| } | ||
|
|
||
| // SelectUnion returns the specific firstboot customization types. The detection | ||
| // is based on the "type" field in the JSON payload. Returns nil for irrelevant types, | ||
| // or if the type is unknown or if there is an error during unmarshaling. | ||
| func (sp FirstbootScriptCustomization) SelectUnion() (*CustomFirstbootCustomization, *SatelliteFirstbootCustomization, *AAPFirstbootCustomization, error) { | ||
| var fcc FirstbootCommonCustomization | ||
| err := json.Unmarshal(sp.union, &fcc) | ||
| if err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| switch strings.ToLower(fcc.Type) { | ||
| case "custom": | ||
| cc, err := unmarshalFirstbootScript[CustomFirstbootCustomization](sp.union) | ||
| if err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
| if cc.Contents == "" { | ||
| return nil, nil, nil, ErrMissingCustomContents | ||
| } | ||
| return cc, nil, nil, nil | ||
| case "satellite": | ||
| sc, err := unmarshalFirstbootScript[SatelliteFirstbootCustomization](sp.union) | ||
| if err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
| if sc.Command == "" { | ||
| return nil, nil, nil, ErrMissingSatelliteCommand | ||
| } | ||
| return nil, sc, nil, err | ||
| case "aap": | ||
| ac, err := unmarshalFirstbootScript[AAPFirstbootCustomization](sp.union) | ||
| if err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
| if ac.JobTemplateURL == "" || ac.HostConfigKey == "" { | ||
| return nil, nil, nil, ErrMissingAAPFields | ||
| } | ||
| return nil, nil, ac, err | ||
| default: | ||
| return nil, nil, nil, ErrUnknownFirstbootCustomization | ||
| } | ||
| } | ||
|
|
||
| func FirstbootScriptCustomizationFromCustom(node CustomFirstbootCustomization) FirstbootScriptCustomization { | ||
| u, _ := json.Marshal(node) | ||
| return FirstbootScriptCustomization{union: u} | ||
| } | ||
|
|
||
| func FirstbootScriptCustomizationFromSatellite(node SatelliteFirstbootCustomization) FirstbootScriptCustomization { | ||
| u, _ := json.Marshal(node) | ||
| return FirstbootScriptCustomization{union: u} | ||
| } | ||
|
|
||
| func FirstbootScriptCustomizationFromAAP(node AAPFirstbootCustomization) FirstbootScriptCustomization { | ||
| u, _ := json.Marshal(node) | ||
| return FirstbootScriptCustomization{union: u} | ||
| } | ||
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.