2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2018-11-29 17:32:52 -05:00
|
|
|
package breakpoint
|
|
|
|
|
|
|
|
import (
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2018-11-29 17:32:52 -05:00
|
|
|
"fmt"
|
|
|
|
|
2018-11-30 13:46:40 -05:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/common"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2018-11-29 17:32:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2018-11-29 18:09:14 -05:00
|
|
|
Note string `mapstructure:"note"`
|
|
|
|
Disable bool `mapstructure:"disable"`
|
2018-11-29 17:32:52 -05:00
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2018-11-29 17:32:52 -05:00
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: "breakpoint",
|
2018-11-29 17:32:52 -05:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, _ map[string]interface{}) error {
|
2018-11-29 18:09:14 -05:00
|
|
|
if p.config.Disable {
|
|
|
|
if p.config.Note != "" {
|
|
|
|
ui.Say(fmt.Sprintf(
|
|
|
|
"Breakpoint provisioner with note \"%s\" disabled; continuing...",
|
|
|
|
p.config.Note))
|
|
|
|
} else {
|
|
|
|
ui.Say("Breakpoint provisioner disabled; continuing...")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-29 17:32:52 -05:00
|
|
|
if p.config.Note != "" {
|
|
|
|
ui.Say(fmt.Sprintf("Pausing at breakpoint provisioner with note \"%s\".", p.config.Note))
|
|
|
|
} else {
|
|
|
|
ui.Say("Pausing at breakpoint provisioner.")
|
|
|
|
}
|
|
|
|
|
|
|
|
message := fmt.Sprintf(
|
|
|
|
"Press enter to continue.")
|
|
|
|
|
2018-11-30 13:46:40 -05:00
|
|
|
var g errgroup.Group
|
2018-11-29 17:32:52 -05:00
|
|
|
result := make(chan string, 1)
|
2018-11-30 13:46:40 -05:00
|
|
|
g.Go(func() error {
|
2018-11-29 17:32:52 -05:00
|
|
|
line, err := ui.Ask(message)
|
|
|
|
if err != nil {
|
2018-11-30 13:46:40 -05:00
|
|
|
return fmt.Errorf("Error asking for input: %s", err)
|
2018-11-29 17:32:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result <- line
|
|
|
|
return nil
|
2018-11-30 13:46:40 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err
|
2018-11-29 17:32:52 -05:00
|
|
|
}
|
2018-11-30 13:46:40 -05:00
|
|
|
return nil
|
2018-11-29 17:32:52 -05:00
|
|
|
}
|