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

systemd.unit.create: Add Environment and EnvironmentFile options (HMS-3814) #567

Merged
merged 5 commits into from
Apr 11, 2024
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 Schutzfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"fedora-39": {
"dependencies": {
"osbuild": {
"commit": "ec496769c5905bc07264ffdb26f6facb3cb3cdd6"
"commit": "6549bf1992b9731d52df5416584fab3f014a421f"
}
},
"repos": [
Expand Down
2 changes: 1 addition & 1 deletion pkg/distro/rhel/rhel10/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func baseEc2ImageConfig() *distro.ImageConfig {
Dropin: "10-rh-enable-for-ec2.conf",
Config: osbuild.SystemdServiceUnitDropin{
Service: &osbuild.SystemdUnitServiceSection{
Environment: "NM_CLOUD_SETUP_EC2=yes",
Environment: []osbuild.EnvironmentVariable{{Key: "NM_CLOUD_SETUP_EC2", Value: "yes"}},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/distro/rhel/rhel10/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ var defaultAzureImageConfig = &distro.ImageConfig{
Dropin: "10-rh-enable-for-azure.conf",
Config: osbuild.SystemdServiceUnitDropin{
Service: &osbuild.SystemdUnitServiceSection{
Environment: "NM_CLOUD_SETUP_AZURE=yes",
Environment: []osbuild.EnvironmentVariable{{Key: "NM_CLOUD_SETUP_AZURE", Value: "yes"}},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/distro/rhel/rhel8/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func baseEc2ImageConfig() *distro.ImageConfig {
Dropin: "10-rh-enable-for-ec2.conf",
Config: osbuild.SystemdServiceUnitDropin{
Service: &osbuild.SystemdUnitServiceSection{
Environment: "NM_CLOUD_SETUP_EC2=yes",
Environment: []osbuild.EnvironmentVariable{{Key: "NM_CLOUD_SETUP_EC2", Value: "yes"}},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/distro/rhel/rhel8/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ var defaultAzureImageConfig = &distro.ImageConfig{
Dropin: "10-rh-enable-for-azure.conf",
Config: osbuild.SystemdServiceUnitDropin{
Service: &osbuild.SystemdUnitServiceSection{
Environment: "NM_CLOUD_SETUP_AZURE=yes",
Environment: []osbuild.EnvironmentVariable{{Key: "NM_CLOUD_SETUP_AZURE", Value: "yes"}},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/distro/rhel/rhel9/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func baseEc2ImageConfig() *distro.ImageConfig {
Dropin: "10-rh-enable-for-ec2.conf",
Config: osbuild.SystemdServiceUnitDropin{
Service: &osbuild.SystemdUnitServiceSection{
Environment: "NM_CLOUD_SETUP_EC2=yes",
Environment: []osbuild.EnvironmentVariable{{Key: "NM_CLOUD_SETUP_EC2", Value: "yes"}},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/distro/rhel/rhel9/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ var defaultAzureImageConfig = &distro.ImageConfig{
Dropin: "10-rh-enable-for-azure.conf",
Config: osbuild.SystemdServiceUnitDropin{
Service: &osbuild.SystemdUnitServiceSection{
Environment: "NM_CLOUD_SETUP_AZURE=yes",
Environment: []osbuild.EnvironmentVariable{{Key: "NM_CLOUD_SETUP_AZURE", Value: "yes"}},
},
},
},
Expand Down
32 changes: 27 additions & 5 deletions pkg/osbuild/systemd_unit_create_stage.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package osbuild

import (
"fmt"
"regexp"
)

type serviceType string
type unitPath string

Expand All @@ -26,11 +31,13 @@ type Unit struct {
}

type Service struct {
Type serviceType `json:"Type,omitempty"`
RemainAfterExit bool `json:"RemainAfterExit,omitempty"`
ExecStartPre []string `json:"ExecStartPre,omitempty"`
ExecStopPost []string `json:"ExecStopPost,omitempty"`
ExecStart []string `json:"ExecStart,omitempty"`
Type serviceType `json:"Type,omitempty"`
RemainAfterExit bool `json:"RemainAfterExit,omitempty"`
ExecStartPre []string `json:"ExecStartPre,omitempty"`
ExecStopPost []string `json:"ExecStopPost,omitempty"`
ExecStart []string `json:"ExecStart,omitempty"`
Environment []EnvironmentVariable `json:"Environment,omitempty"`
EnvironmentFile []string `json:"EnvironmentFile,omitempty"`
}

type Install struct {
Expand All @@ -53,7 +60,22 @@ type SystemdUnitCreateStageOptions struct {

func (SystemdUnitCreateStageOptions) isStageOptions() {}

func (o *SystemdUnitCreateStageOptions) validate() error {
vre := regexp.MustCompile(envVarRegex)
if service := o.Config.Service; service != nil {
for _, envVar := range service.Environment {
if !vre.MatchString(envVar.Key) {
return fmt.Errorf("variable name %q doesn't conform to schema (%s)", envVar.Key, envVarRegex)
}
}
}
return nil
}

func NewSystemdUnitCreateStageOptions(options *SystemdUnitCreateStageOptions) *Stage {
if err := options.validate(); err != nil {
panic(err)
}
return &Stage{
Type: "org.osbuild.systemd.unit.create",
Options: options,
Expand Down
23 changes: 22 additions & 1 deletion pkg/osbuild/systemd_unit_stage.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package osbuild

import (
"fmt"
"regexp"
)

type unitType string

const (
Expand All @@ -16,7 +21,22 @@ type SystemdUnitStageOptions struct {

func (SystemdUnitStageOptions) isStageOptions() {}

func (o *SystemdUnitStageOptions) validate() error {
vre := regexp.MustCompile(envVarRegex)
if service := o.Config.Service; service != nil {
for _, envVar := range service.Environment {
if !vre.MatchString(envVar.Key) {
return fmt.Errorf("variable name %q doesn't conform to schema (%s)", envVar.Key, envVarRegex)
}
}
}
return nil
}

func NewSystemdUnitStage(options *SystemdUnitStageOptions) *Stage {
if err := options.validate(); err != nil {
panic(err)
}
return &Stage{
Type: "org.osbuild.systemd.unit",
Options: options,
Expand All @@ -32,7 +52,8 @@ type SystemdServiceUnitDropin struct {
// 'Service' configuration section of a unit file
type SystemdUnitServiceSection struct {
// Sets environment variables for executed process
Environment string `json:"Environment,omitempty"`
Environment []EnvironmentVariable `json:"Environment,omitempty"`
EnvironmentFile []string `json:"EnvironmentFile,omitempty"`
}

// 'Unit' configuration section of a unit file
Expand Down
Loading