diff --git a/pkg/osbuild/systemd_unit_create_stage.go b/pkg/osbuild/systemd_unit_create_stage.go new file mode 100644 index 0000000000..19eea44b80 --- /dev/null +++ b/pkg/osbuild/systemd_unit_create_stage.go @@ -0,0 +1,61 @@ +package osbuild + +type serviceType string +type unitPath string + +const ( + Simple serviceType = "simple" + Exec serviceType = "exec" + Forking serviceType = "forking" + Oneshot serviceType = "oneshot" + Dbus serviceType = "dbus" + Notify serviceType = "notify" + NotifyReloadservice serviceType = "notify-reload" + Idle serviceType = "idle" + Etc unitPath = "etc" + Usr unitPath = "usr" +) + +type Unit struct { + Description string `json:"Description,omitempty"` + DefaultDependencies bool `json:"DefaultDependencies,omitempty"` + ConditionPathExists []string `json:"ConditionPathExists,omitempty"` + ConditionPathIsDirectory []string `json:"ConditionPathIsDirectory,omitempty"` + Requires []string `json:"Requires,omitempty"` + Wants []string `json:"Wants,omitempty"` +} + +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 Install struct { + RequiredBy []string `json:"RequiredBy,omitempty"` + WantedBy []string `json:"WantedBy,omitempty"` +} + +type SystemdServiceUnit struct { + Unit *Unit `json:"Unit"` + Service *Service `json:"Service"` + Install *Install `json:"Install"` +} + +type SystemdUnitCreateStageOptions struct { + Filename string `json:"filename"` + UnitType unitType `json:"unit-type,omitempty"` // unitType defined in ./systemd_unit_stage.go + UnitPath unitPath `json:"unit-path,omitempty"` + Config SystemdServiceUnit `json:"config"` +} + +func (SystemdUnitCreateStageOptions) isStageOptions() {} + +func NewSystemdUnitCreateStageOptions(options *SystemdUnitCreateStageOptions) *Stage { + return &Stage{ + Type: "org.osbuild.systemd.unit.create", + Options: options, + } +} diff --git a/pkg/osbuild/systemd_unit_create_stage_test.go b/pkg/osbuild/systemd_unit_create_stage_test.go new file mode 100644 index 0000000000..e50b0f5de4 --- /dev/null +++ b/pkg/osbuild/systemd_unit_create_stage_test.go @@ -0,0 +1,70 @@ +package osbuild + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func createSystemdUnit() SystemdServiceUnit { + + var unit = Unit{ + Description: "Create directory and files", + ConditionPathExists: []string{"!/etc/myfile"}, + ConditionPathIsDirectory: []string{"!/etc/mydir"}, + Requires: []string{"dbus.service", "libvirtd.service"}, + Wants: []string{"local-fs.target"}, + } + var service = Service{ + Type: Oneshot, + RemainAfterExit: true, + ExecStartPre: []string{"echo creating_files"}, + ExecStopPost: []string{"echo done_creating_files"}, + ExecStart: []string{"mkdir -p /etc/mydir", "touch /etc/myfiles"}, + } + + var install = Install{ + RequiredBy: []string{"multi-user.target", "boot-complete.target"}, + WantedBy: []string{"sshd.service"}, + } + + var systemdUnit = SystemdServiceUnit{ + Unit: &unit, + Service: &service, + Install: &install, + } + + return systemdUnit +} + +func TestNewSystemdUnitCreateStage(t *testing.T) { + systemdServiceConfig := createSystemdUnit() + var options = SystemdUnitCreateStageOptions{ + Filename: "create-dir-files", + Config: systemdServiceConfig, + } + expectedStage := &Stage{ + Type: "org.osbuild.systemd.unit.create", + Options: &options, + } + + actualStage := NewSystemdUnitCreateStageOptions(&options) + assert.Equal(t, expectedStage, actualStage) +} + +func TestNewSystemdUnitCreateStageInEtc(t *testing.T) { + systemdServiceConfig := createSystemdUnit() + var options = SystemdUnitCreateStageOptions{ + Filename: "create-dir-files", + Config: systemdServiceConfig, + UnitPath: Etc, + UnitType: Global, + } + expectedStage := &Stage{ + Type: "org.osbuild.systemd.unit.create", + Options: &options, + } + + actualStage := NewSystemdUnitCreateStageOptions(&options) + assert.Equal(t, expectedStage, actualStage) +}