2020-12-01 16:42:11 -05:00
|
|
|
// The linode package contains a packersdk.Builder implementation
|
2019-04-15 17:22:25 -04:00
|
|
|
// that builds Linode images.
|
|
|
|
package linode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-01 22:02:52 -04:00
|
|
|
"log"
|
2019-04-15 17:22:25 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2019-04-15 17:22:25 -04:00
|
|
|
"github.com/linode/linodego"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-04-15 17:22:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder.
|
|
|
|
const BuilderID = "packer.linode"
|
|
|
|
|
|
|
|
// Builder represents a Packer Builder.
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2019-04-15 17:22:25 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2019-04-15 17:22:25 -04:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2019-04-15 17:22:25 -04:00
|
|
|
}
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2019-04-15 17:22:25 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (ret packersdk.Artifact, err error) {
|
2019-04-15 17:22:25 -04:00
|
|
|
ui.Say("Running builder ...")
|
|
|
|
|
|
|
|
client := newLinodeClient(b.config.PersonalAccessToken)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
state := new(multistep.BasicStateBag)
|
2019-12-19 11:01:55 -05:00
|
|
|
state.Put("config", &b.config)
|
2019-04-15 17:22:25 -04:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
|
|
|
&StepCreateSSHKey{
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("linode_%s.pem", b.config.PackerBuildName),
|
|
|
|
},
|
|
|
|
&stepCreateLinode{client},
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
2020-01-30 18:22:22 -05:00
|
|
|
Host: commHost(b.config.Comm.Host()),
|
2019-04-15 17:22:25 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2019-04-15 17:22:25 -04:00
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
|
|
|
&stepShutdownLinode{client},
|
|
|
|
&stepCreateImage{client},
|
|
|
|
}
|
|
|
|
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-04-15 17:22:25 -04:00
|
|
|
b.runner.Run(ctx, state)
|
|
|
|
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("image"); !ok {
|
|
|
|
return nil, errors.New("Cannot find image in state.")
|
|
|
|
}
|
|
|
|
|
|
|
|
image := state.Get("image").(*linodego.Image)
|
|
|
|
artifact := Artifact{
|
|
|
|
ImageLabel: image.Label,
|
|
|
|
ImageID: image.ID,
|
|
|
|
Driver: &client,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2019-04-15 17:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
2019-11-01 22:02:52 -04:00
|
|
|
|
|
|
|
func commHost(host string) func(multistep.StateBag) (string, error) {
|
|
|
|
return func(state multistep.StateBag) (string, error) {
|
|
|
|
if host != "" {
|
2020-01-30 18:22:22 -05:00
|
|
|
log.Printf("Using host value: %s", host)
|
2019-11-01 22:02:52 -04:00
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
instance := state.Get("instance").(*linodego.Instance)
|
|
|
|
if len(instance.IPv4) == 0 {
|
|
|
|
return "", fmt.Errorf("Linode instance %d has no IPv4 addresses!", instance.ID)
|
|
|
|
}
|
|
|
|
return instance.IPv4[0].String(), nil
|
|
|
|
}
|
|
|
|
}
|