Skip to content

Commit

Permalink
remove hashicorp/packer-plugin-sdk
Browse files Browse the repository at this point in the history
Replace it with simpler pkg/step.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Aug 22, 2023
1 parent 5cfa199 commit 3743163
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 869 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.18
require (
github.com/go-git/go-git/v5 v5.8.1
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/packer-plugin-sdk v0.5.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/packer-plugin-sdk v0.5.1 h1:ucXGWru98LsQrAWq8cEnaNN2Dvqw0HtyupAwNzrfy44=
github.com/hashicorp/packer-plugin-sdk v0.5.1/go.mod h1:OKziA4fQodpIq5HzOpRNt+vLpRJae61Z4uVFbeMLgd8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down
40 changes: 20 additions & 20 deletions pkg/images/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"

"github.com/cilium/little-vm-helper/pkg/kernels"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/cilium/little-vm-helper/pkg/step"
)

// ActionOp is the interface that actions operations need to implement.
Expand All @@ -20,7 +20,7 @@ import (
// works.
type ActionOp interface {
ActionOpName() string
ToSteps(s *StepConf) ([]multistep.Step, error)
ToSteps(s *StepConf) ([]step.Step, error)
}

type Action struct {
Expand Down Expand Up @@ -54,8 +54,8 @@ func (rc *RunCommand) ActionOpName() string {
return "run-command"
}

func (rc *RunCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (rc *RunCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--run-command", rc.Cmd},
}}, nil
Expand All @@ -71,8 +71,8 @@ func (c *CopyInCommand) ActionOpName() string {
return "copy-in"
}

func (c *CopyInCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (c *CopyInCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--copy-in", fmt.Sprintf("%s:%s", c.LocalPath, c.RemoteDir)},
}}, nil
Expand All @@ -87,8 +87,8 @@ func (c *SetHostnameCommand) ActionOpName() string {
return "set-hostname"
}

func (c *SetHostnameCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (c *SetHostnameCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--hostname", c.Hostname},
}}, nil
Expand All @@ -103,8 +103,8 @@ func (c *MkdirCommand) ActionOpName() string {
return "mkdir"
}

func (c *MkdirCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (c *MkdirCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--mkdir", c.Dir},
}}, nil
Expand All @@ -120,8 +120,8 @@ func (c *UploadCommand) ActionOpName() string {
return "upload"
}

func (c *UploadCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (c *UploadCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--upload", fmt.Sprintf("%s:%s", c.File, c.Dest)},
}}, nil
Expand All @@ -137,8 +137,8 @@ func (c *ChmodCommand) ActionOpName() string {
return "chmod"
}

func (c *ChmodCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (c *ChmodCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--chmod", fmt.Sprintf("%s:%s", c.Permissions, c.File)},
}}, nil
Expand All @@ -154,8 +154,8 @@ func (c *AppendLineCommand) ActionOpName() string {
return "append-line"
}

func (c *AppendLineCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (c *AppendLineCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--append-line", fmt.Sprintf("%s:%s", c.File, c.Line)},
}}, nil
Expand All @@ -171,8 +171,8 @@ func (c *LinkCommand) ActionOpName() string {
return "link"
}

func (c *LinkCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return []multistep.Step{&VirtCustomizeStep{
func (c *LinkCommand) ToSteps(s *StepConf) ([]step.Step, error) {
return []step.Step{&VirtCustomizeStep{
StepConf: s,
Args: []string{"--link", fmt.Sprintf("%s:%s", c.Target, c.Link)},
}}, nil
Expand All @@ -187,7 +187,7 @@ func (c *InstallKernelCommand) ActionOpName() string {
return "install-kernel"
}

func (c *InstallKernelCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
func (c *InstallKernelCommand) ToSteps(s *StepConf) ([]step.Step, error) {
installDir := c.KernelInstallDir
// NB(kkourt): quick hack for having a proper (independent of base
// directory) relative path for install dirs. Should figure out
Expand All @@ -203,7 +203,7 @@ func (c *InstallKernelCommand) ToSteps(s *StepConf) ([]multistep.Step, error) {
return nil, err
}
kernelPath := filepath.Join("/", kernel)
return []multistep.Step{
return []step.Step{
// boot files, configs, etc.
&VirtCustomizeStep{StepConf: s, Args: []string{"--copy-in", fmt.Sprintf("%s/boot:/", installDir)}},
// modules
Expand Down
15 changes: 6 additions & 9 deletions pkg/images/image_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"path/filepath"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/cilium/little-vm-helper/pkg/step"
"github.com/sirupsen/logrus"
)

Expand All @@ -28,9 +28,9 @@ func (f *ImageForest) doBuildImageDryRun(image string) error {
}

// merge act2 to act1, or return an error
func mergeSteps(step1, step2 multistep.Step) error {
func mergeSteps(step1, step2 step.Step) error {
mergable, ok := step1.(interface {
Merge(step multistep.Step) error
Merge(step step.Step) error
})
if !ok {
return fmt.Errorf("step1 (%v) not mergable", step1)
Expand Down Expand Up @@ -60,8 +60,7 @@ func (f *ImageForest) doBuildImage(
log: log,
}

state := new(multistep.BasicStateBag)
steps := make([]multistep.Step, 2, 2+len(cnf.Actions))
steps := make([]step.Step, 2, 2+len(cnf.Actions))

steps[0] = NewCreateImage(stepConf)
// NB: We might need an --chdir option or similar, but for now just
Expand Down Expand Up @@ -93,13 +92,11 @@ func (f *ImageForest) doBuildImage(
}
}

runner := &multistep.BasicRunner{Steps: steps}
runner.Run(ctx, state)
err := state.Get("err")
err := step.DoSteps(ctx, steps)
if err != nil {
imgFname := f.imageFilename(image)
log.Warnf("image file '%s' not deleted so that it can be inspected", imgFname)
return err.(error)
return err
}
return nil
}
12 changes: 6 additions & 6 deletions pkg/images/step_chdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"os"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/cilium/little-vm-helper/pkg/step"
)

type ChdirStep struct {
Expand All @@ -23,24 +23,24 @@ func NewChdirStep(cnf *StepConf, dir string) *ChdirStep {
}
}

func (s *ChdirStep) Run(_ context.Context, _ multistep.StateBag) multistep.StepAction {
func (s *ChdirStep) Do(ctx context.Context) (step.Result, error) {
var err error
s.oldDir, err = os.Getwd()
if err != nil {
s.log.Warnf("failed to get current directory: %v", err)
return multistep.ActionHalt
return step.Stop, err
}

err = os.Chdir(s.Dir)
if err != nil {
s.log.Warnf("failed to get change directory: %v", err)
return multistep.ActionHalt
return step.Stop, err
}
s.log.Infof("set current working dir to '%s'", s.Dir)
return multistep.ActionContinue
return step.Continue, nil
}

func (s *ChdirStep) Cleanup(_ multistep.StateBag) {
func (s *ChdirStep) Cleanup(ctx context.Context) {
err := os.Chdir(s.oldDir)
if err != nil {
s.log.Warnf("failed to get change to old directory: %v", err)
Expand Down
13 changes: 5 additions & 8 deletions pkg/images/step_create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"

"github.com/cilium/little-vm-helper/pkg/logcmd"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/cilium/little-vm-helper/pkg/step"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -228,8 +228,7 @@ func (s *CreateImage) makeDerivedImage(ctx context.Context) error {
return nil
}

func (s *CreateImage) Run(ctx context.Context, b multistep.StateBag) multistep.StepAction {

func (s *CreateImage) Do(ctx context.Context) (step.Result, error) {
var err error
if s.imgCnf.Parent == "" {
err = s.makeRootImage(ctx)
Expand All @@ -239,12 +238,10 @@ func (s *CreateImage) Run(ctx context.Context, b multistep.StateBag) multistep.S

if err != nil {
s.log.WithField("image", s.imgCnf.Name).WithError(err).Error("error buiding image")
b.Put("err", err)
return multistep.ActionHalt
return step.Stop, err
}
return multistep.ActionContinue

return step.Continue, nil
}

func (s *CreateImage) Cleanup(b multistep.StateBag) {
func (s *CreateImage) Cleanup(ctx context.Context) {
}
14 changes: 7 additions & 7 deletions pkg/images/step_guestfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path"

"github.com/cilium/little-vm-helper/pkg/logcmd"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/cilium/little-vm-helper/pkg/step"
)

// VirtCustomizeStep is a step implemented a set of arguments in virt-customize
Expand All @@ -25,24 +25,24 @@ type VirtCustomizeStep struct {
Args []string
}

func (s *VirtCustomizeStep) Run(ctx context.Context, b multistep.StateBag) multistep.StepAction {
func (s *VirtCustomizeStep) Do(ctx context.Context) (step.Result, error) {
imgFname := path.Join(s.imagesDir, s.imgCnf.Name)
args := []string{"-a", imgFname}
args = append(args, s.Args...)
cmd := exec.CommandContext(ctx, "virt-customize", args...)
err := logcmd.RunAndLogCommand(cmd, s.log)
if err != nil {
s.log.WithField("image", s.imgCnf.Name).WithError(err).Error("error executing command")
b.Put("err", err)
return multistep.ActionHalt
return step.Stop, err
}
return multistep.ActionContinue

return step.Continue, nil
}

func (s *VirtCustomizeStep) Cleanup(b multistep.StateBag) {
func (s *VirtCustomizeStep) Cleanup(ctx context.Context) {
}

func (s *VirtCustomizeStep) Merge(step multistep.Step) error {
func (s *VirtCustomizeStep) Merge(step step.Step) error {
vcs, ok := step.(*VirtCustomizeStep)
if !ok {
return fmt.Errorf("type %T cannot be merged to a VirtCustomizeStep", step)
Expand Down
6 changes: 3 additions & 3 deletions pkg/images/step_guestfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package images
import (
"testing"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/cilium/little-vm-helper/pkg/step"
"github.com/stretchr/testify/assert"
)

func TestACtionMerge(t *testing.T) {
func TestActionMerge(t *testing.T) {
cnf := StepConf{}

getSingleStep := func(op ActionOp) multistep.Step {
getSingleStep := func(op ActionOp) step.Step {
steps, err := op.ToSteps(&cnf)
assert.Nil(t, err)
assert.Len(t, steps, 1)
Expand Down
31 changes: 31 additions & 0 deletions pkg/step/step.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package step

import "context"

type Result int

const (
ResultInvalid Result = iota
Continue
Stop
)

type Step interface {
Do(ctx context.Context) (Result, error)
Cleanup(ctx context.Context)
}

func DoSteps(ctx context.Context, steps []Step) error {
for i := range steps {
res, err := steps[i].Do(ctx)
if res == Stop || err != nil {
return err
}
defer steps[i].Cleanup(ctx)
}

return nil
}
Loading

0 comments on commit 3743163

Please sign in to comment.