Skip to content

Commit

Permalink
Make output a file, not image in live LXD
Browse files Browse the repository at this point in the history
For KP Labs uses, it is easier to work with images exported to file.
  • Loading branch information
Novakov committed Sep 14, 2023
1 parent 2672e98 commit 35e0d22
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
10 changes: 5 additions & 5 deletions builder/lxd/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package lxd

import (
"fmt"
"os"
)

type Artifact struct {
id string

f string
// StateData should store data such as GeneratedData
// to be shared with post-processors
StateData map[string]interface{}
Expand All @@ -20,22 +21,21 @@ func (*Artifact) BuilderId() string {
}

func (a *Artifact) Files() []string {
return nil
return []string{ a.f }
}

func (a *Artifact) Id() string {
return a.id
}

func (a *Artifact) String() string {
return fmt.Sprintf("image: %s", a.id)
return fmt.Sprintf("image: %s in file %s", a.id, a.f)
}

func (a *Artifact) State(name string) interface{} {
return a.StateData[name]
}

func (a *Artifact) Destroy() error {
_, err := LXDCommand("image", "delete", a.id)
return err
return os.RemoveAll(a.f)
}
3 changes: 3 additions & 0 deletions builder/lxd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package lxd

import (
"context"
"fmt"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down Expand Up @@ -46,6 +47,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
&stepLxdLaunch{},
&StepProvision{},
&stepPublish{},
&stepExport{},
}

// Setup the state bag
Expand All @@ -71,6 +73,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)

artifact := &Artifact{
id: id,
f: fmt.Sprintf("%s.tar.gz", b.config.OutputImage),
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}

Expand Down
39 changes: 39 additions & 0 deletions builder/lxd/step_export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lxd

import (
"context"
"fmt"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)

type stepExport struct{}

func (s *stepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
config := state.Get("config").(*Config)
imageFingerprint := state.Get("imageFingerprint").(string)

ui.Say("Exporting image")
_, err := LXDCommand("image", "export", imageFingerprint, config.OutputImage)
if err != nil {
err := fmt.Errorf("Error exporting container: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

ui.Say("Deleting image")
_, err = LXDCommand("image", "delete", imageFingerprint)
if err != nil {
err := fmt.Errorf("Error deleting image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

return multistep.ActionContinue
}

func (s *stepExport) Cleanup(state multistep.StateBag) {}
2 changes: 1 addition & 1 deletion builder/lxd/step_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *stepPublish) Run(ctx context.Context, state multistep.StateBag) multist
}

publish_args := []string{
"publish", name, remote, "--alias", config.OutputImage,
"publish", name, remote,
}

for k, v := range config.PublishProperties {
Expand Down

0 comments on commit 35e0d22

Please sign in to comment.