diff --git a/cmd/ubuntu-image/main.go b/cmd/ubuntu-image/main.go index dfa2f42a..a1d6e35f 100644 --- a/cmd/ubuntu-image/main.go +++ b/cmd/ubuntu-image/main.go @@ -41,6 +41,11 @@ func executeStateMachine(commonOpts *commands.CommonOpts, stateMachineOpts *comm stateMachine.Args = ubuntuImageCommand.Classic.ClassicArgsPassed stateMachine.SetCommonOpts(commonOpts, stateMachineOpts) stateMachineInterface = stateMachine + } else if imageType == "pack" { + stateMachine := new(statemachine.PackStateMachine) + stateMachine.Opts = ubuntuImageCommand.Pack.PackOptsPassed + stateMachine.SetCommonOpts(commonOpts, stateMachineOpts) + stateMachineInterface = stateMachine } // set up, run, and tear down the state machine diff --git a/internal/commands/common.go b/internal/commands/common.go index 3809f8f5..b008e6ae 100644 --- a/internal/commands/common.go +++ b/internal/commands/common.go @@ -34,4 +34,7 @@ type UbuntuImageCommand struct { ClassicArgsPassed ClassicArgs `positional-args:"true" required:"false"` ClassicOptsPassed ClassicOpts } `command:"classic"` + Pack struct { + PackOptsPassed PackOpts `required:"true"` + } `command:"pack" hidden:"true"` } diff --git a/internal/commands/pack.go b/internal/commands/pack.go new file mode 100644 index 00000000..3cc7c882 --- /dev/null +++ b/internal/commands/pack.go @@ -0,0 +1,8 @@ +package commands + +// PackOpts holds all flags that are specific to the pack command +type PackOpts struct { + ArtifactType string `long:"artifact-type" description:"Type of the resulting disk image file." required:"true" hidden:"true" default:"img"` + GadgetDir string `long:"gadget-dir" description:"Directory containing the gadget.yaml and the gadget tree." required:"true" hidden:"true"` + RootfsDir string `long:"rootfs-dir" description:"Directory containing the rootfs" required:"true" hidden:"true"` +} diff --git a/internal/statemachine/pack.go b/internal/statemachine/pack.go new file mode 100644 index 00000000..392dafb5 --- /dev/null +++ b/internal/statemachine/pack.go @@ -0,0 +1,46 @@ +package statemachine + +import ( + "fmt" + + "github.com/canonical/ubuntu-image/internal/commands" +) + +var packStates = []stateFunc{ + {"prepare_pack", (*StateMachine).preparePack}, + {"load_gadget_yaml", (*StateMachine).loadGadgetYaml}, + {"calculate_rootfs_size", (*StateMachine).calculateRootfsSize}, + {"populate_bootfs_contents", (*StateMachine).populateBootfsContents}, + {"populate_prepare_partitions", (*StateMachine).populatePreparePartitions}, + {"make_disk", (*StateMachine).makeDisk}, + {"update_bootloader", (*StateMachine).updateBootloader}, +} + +// PackStateMachine embeds StateMachine and adds the command line flags specific to pack images +type PackStateMachine struct { + StateMachine + Opts commands.PackOpts +} + +// Setup assigns variables and calls other functions that must be executed before Run() +func (packStateMachine *PackStateMachine) Setup() error { + fmt.Printf("WARNING: this is an experimental feature.") + + // set the parent pointer of the embedded struct + packStateMachine.parent = packStateMachine + + // set the beginning states that will be used by all pack image builds + packStateMachine.states = packStates + + // do the validation common to all image types + if err := packStateMachine.validateInput(); err != nil { + return err + } + + // if --resume was passed, figure out where to start + if err := packStateMachine.readMetadata(metadataStateFile); err != nil { + return err + } + + return nil +} diff --git a/internal/statemachine/pack_states.go b/internal/statemachine/pack_states.go new file mode 100644 index 00000000..32a28f3d --- /dev/null +++ b/internal/statemachine/pack_states.go @@ -0,0 +1,12 @@ +package statemachine + +import "path/filepath" + +func (stateMachine *StateMachine) preparePack() error { + packStateMachine := stateMachine.parent.(*PackStateMachine) + + packStateMachine.YamlFilePath = filepath.Join(packStateMachine.Opts.GadgetDir, "meta", "gadget.yaml") + + + return nil +}