Blame internal/osbuild2/stage.go

Packit Service 15f37d
package osbuild2
Packit Service 15f37d
Packit Service 15f37d
import (
Packit Service 15f37d
	"encoding/json"
Packit Service 15f37d
	"fmt"
Packit Service 15f37d
)
Packit Service 15f37d
Packit Service 15f37d
// Single stage of a pipeline executing one step
Packit Service 15f37d
type Stage struct {
Packit Service 15f37d
	// Well-known name in reverse domain-name notation, uniquely identifying
Packit Service 15f37d
	// the stage type.
Packit Service 15f37d
	Type string `json:"type"`
Packit Service 15f37d
	// Stage-type specific options fully determining the operations of the
Packit Service 15f37d
Packit Service 15f37d
	Inputs  Inputs       `json:"inputs,omitempty"`
Packit Service 15f37d
	Options StageOptions `json:"options,omitempty"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// Collection of Inputs for a Stage
Packit Service 15f37d
type Inputs interface {
Packit Service 15f37d
	isStageInputs()
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// Single Input for a Stage
Packit Service 15f37d
type Input interface {
Packit Service 15f37d
	isInput()
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// Fields shared between all Input types (should be embedded in each instance)
Packit Service 15f37d
type inputCommon struct {
Packit Service 15f37d
	Type string `json:"type"`
Packit Service 15f37d
	// Origin should be either 'org.osbuild.source' or 'org.osbuild.pipeline'
Packit Service 15f37d
	Origin string `json:"origin"`
Packit Service 15f37d
Packit Service 15f37d
	// References References `json:"references"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
type StageInput interface {
Packit Service 15f37d
	isStageInput()
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
type References interface {
Packit Service 15f37d
	isReferences()
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// StageOptions specify the operations of a given stage-type.
Packit Service 15f37d
type StageOptions interface {
Packit Service 15f37d
	isStageOptions()
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
type InputOptions interface {
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
type rawStage struct {
Packit Service 15f37d
	Type    string          `json:"type"`
Packit Service 15f37d
	Options json.RawMessage `json:"options"`
Packit Service 15f37d
	Inputs  json.RawMessage `json:"inputs"`
Packit Service 15f37d
}
Packit Service 15f37d
Packit Service 15f37d
// UnmarshalJSON unmarshals JSON into a Stage object. Each type of stage has
Packit Service 15f37d
// a custom unmarshaller for its options, selected based on the stage name.
Packit Service 15f37d
func (stage *Stage) UnmarshalJSON(data []byte) error {
Packit Service 15f37d
	var rawStage rawStage
Packit Service 15f37d
	if err := json.Unmarshal(data, &rawStage); err != nil {
Packit Service 15f37d
		return err
Packit Service 15f37d
	}
Packit Service 15f37d
	var options StageOptions
Packit Service 15f37d
	var inputs Inputs
Packit Service 15f37d
	switch rawStage.Type {
Packit Service 15f37d
	case "org.osbuild.fix-bls":
Packit Service 15f37d
		options = new(FixBLSStageOptions)
Packit Service 15f37d
	case "org.osbuild.fstab":
Packit Service 15f37d
		options = new(FSTabStageOptions)
Packit Service 15f37d
	case "org.osbuild.grub2":
Packit Service 15f37d
		options = new(GRUB2StageOptions)
Packit Service 15f37d
	case "org.osbuild.locale":
Packit Service 15f37d
		options = new(LocaleStageOptions)
Packit Service 15f37d
	case "org.osbuild.selinux":
Packit Service 15f37d
		options = new(SELinuxStageOptions)
Packit Service 15f37d
	case "org.osbuild.hostname":
Packit Service 15f37d
		options = new(HostnameStageOptions)
Packit Service 15f37d
	case "org.osbuild.users":
Packit Service 15f37d
		options = new(UsersStageOptions)
Packit Service 15f37d
	case "org.osbuild.groups":
Packit Service 15f37d
		options = new(GroupsStageOptions)
Packit Service 15f37d
	case "org.osbuild.timezone":
Packit Service 15f37d
		options = new(TimezoneStageOptions)
Packit Service 15f37d
	case "org.osbuild.chrony":
Packit Service 15f37d
		options = new(ChronyStageOptions)
Packit Service 15f37d
	case "org.osbuild.keymap":
Packit Service 15f37d
		options = new(KeymapStageOptions)
Packit Service 15f37d
	case "org.osbuild.firewall":
Packit Service 15f37d
		options = new(FirewallStageOptions)
Packit Service 15f37d
	case "org.osbuild.rhsm":
Packit Service 15f37d
		options = new(RHSMStageOptions)
Packit Service 15f37d
	case "org.osbuild.systemd":
Packit Service 15f37d
		options = new(SystemdStageOptions)
Packit Service 15f37d
	case "org.osbuild.script":
Packit Service 15f37d
		options = new(ScriptStageOptions)
Packit Service 15f37d
	case "org.osbuild.sysconfig":
Packit Service 15f37d
		options = new(SysconfigStageOptions)
Packit Service 15f37d
	case "org.osbuild.kernel-cmdline":
Packit Service 15f37d
		options = new(KernelCmdlineStageOptions)
Packit Service 15f37d
	case "org.osbuild.rpm":
Packit Service 15f37d
		options = new(RPMStageOptions)
Packit Service 15f37d
		inputs = new(RPMStageInputs)
Packit Service 15f37d
	case "org.osbuild.oci-archive":
Packit Service 15f37d
		options = new(OCIArchiveStageOptions)
Packit Service 15f37d
		inputs = new(OCIArchiveStageInputs)
Packit Service 15f37d
	case "org.osbuild.ostree.commit":
Packit Service 15f37d
		options = new(OSTreeCommitStageOptions)
Packit Service 15f37d
		inputs = new(OSTreeCommitStageInputs)
Packit Service 15f37d
	case "org.osbuild.ostree.pull":
Packit Service 15f37d
		options = new(OSTreePullStageOptions)
Packit Service 15f37d
		inputs = new(OSTreePullStageInputs)
Packit Service 15f37d
	case "org.osbuild.ostree.init":
Packit Service 15f37d
		options = new(OSTreeInitStageOptions)
Packit Service 15f37d
	case "org.osbuild.ostree.preptree":
Packit Service 15f37d
		options = new(OSTreePrepTreeStageOptions)
Packit Service 15f37d
	default:
Packit Service 15f37d
		return fmt.Errorf("unexpected stage type: %s", rawStage.Type)
Packit Service 15f37d
	}
Packit Service 15f37d
	if err := json.Unmarshal(rawStage.Options, options); err != nil {
Packit Service 15f37d
		return err
Packit Service 15f37d
	}
Packit Service 15f37d
	if inputs != nil && rawStage.Inputs != nil {
Packit Service 15f37d
		if err := json.Unmarshal(rawStage.Inputs, inputs); err != nil {
Packit Service 15f37d
			return err
Packit Service 15f37d
		}
Packit Service 15f37d
	}
Packit Service 15f37d
Packit Service 15f37d
	stage.Type = rawStage.Type
Packit Service 15f37d
	stage.Options = options
Packit Service 15f37d
	stage.Inputs = inputs
Packit Service 15f37d
Packit Service 15f37d
	return nil
Packit Service 15f37d
}