diff --git a/builder/lxd/artifact.go b/builder/lxd/artifact.go index e04b081..5fc6b22 100644 --- a/builder/lxd/artifact.go +++ b/builder/lxd/artifact.go @@ -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{} @@ -20,7 +21,7 @@ func (*Artifact) BuilderId() string { } func (a *Artifact) Files() []string { - return nil + return []string{ a.f } } func (a *Artifact) Id() string { @@ -28,7 +29,7 @@ func (a *Artifact) Id() string { } 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{} { @@ -36,6 +37,5 @@ func (a *Artifact) State(name string) interface{} { } func (a *Artifact) Destroy() error { - _, err := LXDCommand("image", "delete", a.id) - return err + return os.RemoveAll(a.f) } diff --git a/builder/lxd/builder.go b/builder/lxd/builder.go index fe8ba9a..30ae559 100644 --- a/builder/lxd/builder.go +++ b/builder/lxd/builder.go @@ -5,6 +5,7 @@ package lxd import ( "context" + "fmt" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer-plugin-sdk/multistep" @@ -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 @@ -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")}, } diff --git a/builder/lxd/step_export.go b/builder/lxd/step_export.go new file mode 100644 index 0000000..dd782f8 --- /dev/null +++ b/builder/lxd/step_export.go @@ -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) {} diff --git a/builder/lxd/step_publish.go b/builder/lxd/step_publish.go index b06cef7..ad9ee6c 100644 --- a/builder/lxd/step_publish.go +++ b/builder/lxd/step_publish.go @@ -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 {