From c5f335bceb121d2ebfeca0d0dfd4aea091189550 Mon Sep 17 00:00:00 2001 From: Simon de Vlieger Date: Thu, 25 Aug 2022 12:09:21 +0200 Subject: [PATCH] Add the `rhsm.facts` stage. We initially provide a Facts file that stores the `ApiType`. This is the API that was used to request the compose. --- Schutzfile | 24 ++++++------ internal/cloudapi/v2/handler.go | 4 ++ internal/distro/distro.go | 7 ++++ internal/distro/rhel8/pipelines.go | 8 ++++ internal/distro/rhel9/pipelines.go | 8 ++++ internal/osbuild/rhsm_facts_stage.go | 19 ++++++++++ internal/osbuild/rhsm_facts_stage_test.go | 46 +++++++++++++++++++++++ internal/weldr/api.go | 3 ++ osbuild-composer.spec | 8 ++-- 9 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 internal/osbuild/rhsm_facts_stage.go create mode 100644 internal/osbuild/rhsm_facts_stage_test.go diff --git a/Schutzfile b/Schutzfile index c337e9e34..581061e34 100644 --- a/Schutzfile +++ b/Schutzfile @@ -2,7 +2,7 @@ "fedora-35": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } }, "repos": [ @@ -79,7 +79,7 @@ "fedora-36": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } }, "repos": [ @@ -156,21 +156,21 @@ "rhel-8.4": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } } }, "rhel-8.6": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } } }, "rhel-8.7": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } }, "repos": [ @@ -216,14 +216,14 @@ "rhel-9.0": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } } }, "rhel-9.1": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } }, "repos": [ @@ -269,21 +269,21 @@ "centos-8": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } } }, "centos-9": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } } }, "centos-stream-9": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } }, "repos": [ @@ -329,7 +329,7 @@ "centos-stream-8": { "dependencies": { "osbuild": { - "commit": "376cbffd136bc4ba86fc7c63697fa5b88fe3acef" + "commit": "ba218f781d4c7455cd995eea55be09e902370905" } }, "repos": [ @@ -386,4 +386,4 @@ } ] } -} \ No newline at end of file +} diff --git a/internal/cloudapi/v2/handler.go b/internal/cloudapi/v2/handler.go index 9a9ecbcac..a4e32370b 100644 --- a/internal/cloudapi/v2/handler.go +++ b/internal/cloudapi/v2/handler.go @@ -269,6 +269,10 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error { } imageOptions := distro.ImageOptions{Size: imageType.Size(0)} + imageOptions.Facts = &distro.FactsImageOptions{ + ApiType: "cloudapi-v2", + } + if request.Customizations != nil && request.Customizations.Subscription != nil { imageOptions.Subscription = &distro.SubscriptionImageOptions{ Organization: request.Customizations.Subscription.Organization, diff --git a/internal/distro/distro.go b/internal/distro/distro.go index d27973bfe..f11a1cfd5 100644 --- a/internal/distro/distro.go +++ b/internal/distro/distro.go @@ -129,6 +129,7 @@ type ImageOptions struct { OSTree ostree.RequestParams Size uint64 Subscription *SubscriptionImageOptions + Facts *FactsImageOptions } // The SubscriptionImageOptions specify subscription-specific image options @@ -142,6 +143,12 @@ type SubscriptionImageOptions struct { Insights bool } +// The FactsImageOptions specify things to be stored into the Insights facts +// storage. This mostly relates to how the build of the image was performed. +type FactsImageOptions struct { + ApiType string +} + type BasePartitionTableMap map[string]disk.PartitionTable // A Manifest is an opaque JSON object, which is a valid input to osbuild diff --git a/internal/distro/rhel8/pipelines.go b/internal/distro/rhel8/pipelines.go index dfc0a27e3..8cf0ea557 100644 --- a/internal/distro/rhel8/pipelines.go +++ b/internal/distro/rhel8/pipelines.go @@ -690,6 +690,14 @@ func osPipeline(t *imageType, p.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false))) } + if options.Facts != nil { + p.AddStage(osbuild.NewRHSMFactsStage(&osbuild.RHSMFactsStageOptions{ + Facts: osbuild.RHSMFacts{ + ApiType: options.Facts.ApiType, + }, + })) + } + if t.rpmOstree { p.AddStage(osbuild.NewOSTreePrepTreeStage(&osbuild.OSTreePrepTreeStageOptions{ EtcGroupMembers: []string{ diff --git a/internal/distro/rhel9/pipelines.go b/internal/distro/rhel9/pipelines.go index 69bd32197..4105d211f 100644 --- a/internal/distro/rhel9/pipelines.go +++ b/internal/distro/rhel9/pipelines.go @@ -688,6 +688,14 @@ func osPipeline(t *imageType, p.AddStage(osbuild.NewSELinuxStage(selinuxStageOptions(false))) } + if options.Facts != nil { + p.AddStage(osbuild.NewRHSMFactsStage(&osbuild.RHSMFactsStageOptions{ + Facts: osbuild.RHSMFacts{ + ApiType: options.Facts.ApiType, + }, + })) + } + if t.rpmOstree { p.AddStage(osbuild.NewOSTreePrepTreeStage(&osbuild.OSTreePrepTreeStageOptions{ EtcGroupMembers: []string{ diff --git a/internal/osbuild/rhsm_facts_stage.go b/internal/osbuild/rhsm_facts_stage.go new file mode 100644 index 000000000..e4984338e --- /dev/null +++ b/internal/osbuild/rhsm_facts_stage.go @@ -0,0 +1,19 @@ +package osbuild + +type RHSMFactsStageOptions struct { + Facts RHSMFacts `json:"facts"` +} + +type RHSMFacts struct { + ApiType string `json:"image-builder.osbuild-composer.api-type"` +} + +func (RHSMFactsStageOptions) isStageOptions() {} + +// NewRHSMFactsStage creates a new RHSM stage +func NewRHSMFactsStage(options *RHSMFactsStageOptions) *Stage { + return &Stage{ + Type: "org.osbuild.rhsm.facts", + Options: options, + } +} diff --git a/internal/osbuild/rhsm_facts_stage_test.go b/internal/osbuild/rhsm_facts_stage_test.go new file mode 100644 index 000000000..579b98683 --- /dev/null +++ b/internal/osbuild/rhsm_facts_stage_test.go @@ -0,0 +1,46 @@ +package osbuild + +import ( + "encoding/json" + "fmt" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNewRHSMFactsStage(t *testing.T) { + expectedStage := &Stage{ + Type: "org.osbuild.rhsm.facts", + Options: &RHSMFactsStageOptions{}, + } + actualStage := NewRHSMFactsStage(&RHSMFactsStageOptions{}) + assert.Equal(t, expectedStage, actualStage) +} + +func TestRHSMFactsStageJson(t *testing.T) { + tests := []struct { + Options RHSMFactsStageOptions + JsonString string + }{ + { + Options: RHSMFactsStageOptions{ + Facts: RHSMFacts{ + ApiType: "test-api", + }, + }, + JsonString: fmt.Sprintf(`{"facts":{"image-builder.osbuild-composer.api-type":"%s"}}`, "test-api"), + }, + } + for _, test := range tests { + marshaledJson, err := json.Marshal(test.Options) + require.NoError(t, err, "failed to marshal JSON") + require.Equal(t, test.JsonString, string(marshaledJson)) + + var jsonOptions RHSMFactsStageOptions + err = json.Unmarshal([]byte(test.JsonString), &jsonOptions) + require.NoError(t, err, "failed to parse JSON") + require.True(t, reflect.DeepEqual(test.Options, jsonOptions)) + } +} diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 8992221eb..d50706d6d 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -2351,6 +2351,9 @@ func (api *API) composeHandler(writer http.ResponseWriter, request *http.Request URL: cr.OSTree.URL, }, } + options.Facts = &distro.FactsImageOptions{ + ApiType: "weldr", + } packageSets, err := api.depsolveBlueprintForImageType(*bp, options, imageType) if err != nil { diff --git a/osbuild-composer.spec b/osbuild-composer.spec index e43fd6f44..924d7cdae 100644 --- a/osbuild-composer.spec +++ b/osbuild-composer.spec @@ -309,10 +309,10 @@ The core osbuild-composer binary. This is suitable both for spawning in containe Summary: The worker for osbuild-composer Requires: systemd Requires: qemu-img -Requires: osbuild >= 62 -Requires: osbuild-ostree >= 62 -Requires: osbuild-lvm2 >= 62 -Requires: osbuild-luks2 >= 62 +Requires: osbuild >= 63 +Requires: osbuild-ostree >= 63 +Requires: osbuild-lvm2 >= 63 +Requires: osbuild-luks2 >= 63 Requires: %{name}-dnf-json = %{version}-%{release} %description worker