Skip to content

Commit

Permalink
WIP Add experimental pack cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
upils committed Oct 4, 2023
1 parent 6193013 commit 133efd9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/ubuntu-image/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
8 changes: 8 additions & 0 deletions internal/commands/pack.go
Original file line number Diff line number Diff line change
@@ -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"`
}
46 changes: 46 additions & 0 deletions internal/statemachine/pack.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions internal/statemachine/pack_states.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 133efd9

Please sign in to comment.