2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2015-07-26 19:22:46 -04:00
|
|
|
package artifice
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2015-07-26 19:22:46 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2015-07-26 19:22:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The artifact-override post-processor allows you to specify arbitrary files as
|
|
|
|
// artifacts. These will override any other artifacts created by the builder.
|
|
|
|
// This allows you to use a builder and provisioner to create some file, such as
|
|
|
|
// a compiled binary or tarball, extract it from the builder (VM or container)
|
|
|
|
// and then save that binary or tarball and throw away the builder.
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
|
|
|
Files []string `mapstructure:"files"`
|
|
|
|
Keep bool `mapstructure:"keep_input_artifact"`
|
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type PostProcessor struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2015-07-26 19:22:46 -04:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: "artifice",
|
2015-07-26 19:22:46 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.config.Files) == 0 {
|
|
|
|
return fmt.Errorf("No files specified in artifice configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifact packersdk.Artifact) (packersdk.Artifact, bool, bool, error) {
|
2015-07-26 19:22:46 -04:00
|
|
|
if len(artifact.Files()) > 0 {
|
2020-01-31 10:45:18 -05:00
|
|
|
ui.Say(fmt.Sprintf("Discarding files from artifact: %s", strings.Join(artifact.Files(), ", ")))
|
2015-07-26 19:22:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := NewArtifact(p.config.Files)
|
|
|
|
ui.Say(fmt.Sprintf("Using these artifact files: %s", strings.Join(artifact.Files(), ", ")))
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
return artifact, true, false, err
|
2015-07-26 19:22:46 -04:00
|
|
|
}
|