2020-12-01 16:42:11 -05:00
|
|
|
// The scaleway package contains a packersdk.Builder implementation
|
2017-04-06 05:19:17 -04:00
|
|
|
// that builds Scaleway images (snapshots).
|
|
|
|
|
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2018-01-05 03:57:59 -05:00
|
|
|
"errors"
|
2017-04-06 05:19:17 -04:00
|
|
|
"fmt"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-01 18:30:31 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-09-09 04:27:48 -04:00
|
|
|
"github.com/scaleway/scaleway-sdk-go/scw"
|
2017-04-06 05:19:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique id for the builder
|
2017-07-11 02:43:04 -04:00
|
|
|
const BuilderId = "hashicorp.scaleway"
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2017-04-06 05:19:17 -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...)
|
2017-04-06 05:19:17 -04:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
return nil, warnings, nil
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2020-09-09 04:27:48 -04:00
|
|
|
scwZone, err := scw.ParseZone(b.config.Zone)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
clientOpts := []scw.ClientOption{
|
|
|
|
scw.WithDefaultProjectID(b.config.ProjectID),
|
|
|
|
scw.WithAuth(b.config.AccessKey, b.config.SecretKey),
|
|
|
|
scw.WithDefaultZone(scwZone),
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.APIURL != "" {
|
|
|
|
clientOpts = append(clientOpts, scw.WithAPIURL(b.config.APIURL))
|
|
|
|
}
|
2017-07-21 06:26:20 -04:00
|
|
|
|
2020-09-09 04:27:48 -04:00
|
|
|
client, err := scw.NewClient(clientOpts...)
|
2017-07-21 06:26:20 -04:00
|
|
|
if err != nil {
|
2018-01-05 04:07:03 -05:00
|
|
|
ui.Error(err.Error())
|
2017-07-21 06:26:20 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
state := new(multistep.BasicStateBag)
|
2019-12-19 11:01:55 -05:00
|
|
|
state.Put("config", &b.config)
|
2017-04-06 05:19:17 -04:00
|
|
|
state.Put("client", client)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
2020-09-18 15:37:48 -04:00
|
|
|
&stepPreValidate{
|
|
|
|
Force: b.config.PackerForce,
|
|
|
|
ImageName: b.config.ImageName,
|
|
|
|
SnapshotName: b.config.SnapshotName,
|
|
|
|
},
|
2017-04-06 05:19:17 -04:00
|
|
|
&stepCreateSSHKey{
|
2018-08-28 11:48:37 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName),
|
2017-04-06 05:19:17 -04:00
|
|
|
},
|
2019-07-18 16:42:18 -04:00
|
|
|
new(stepRemoveVolume),
|
2017-04-06 05:19:17 -04:00
|
|
|
new(stepCreateServer),
|
|
|
|
new(stepServerInfo),
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
2020-01-30 18:22:22 -05:00
|
|
|
Host: communicator.CommHost(b.config.Comm.Host(), "server_ip"),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2017-04-06 05:19:17 -04:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
new(commonsteps.StepProvision),
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2018-09-14 14:03:23 -04:00
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2017-04-06 05:19:17 -04:00
|
|
|
new(stepShutdown),
|
|
|
|
new(stepSnapshot),
|
2017-04-11 06:19:28 -04:00
|
|
|
new(stepImage),
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2017-04-06 05:19:17 -04:00
|
|
|
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:57:59 -05:00
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
2017-04-06 05:19:17 -04:00
|
|
|
if _, ok := state.GetOk("snapshot_name"); !ok {
|
2018-01-05 03:57:59 -05:00
|
|
|
return nil, errors.New("Cannot find snapshot_name in state.")
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
2017-04-11 06:19:28 -04:00
|
|
|
imageName: state.Get("image_name").(string),
|
|
|
|
imageID: state.Get("image_id").(string),
|
2017-04-06 05:19:17 -04:00
|
|
|
snapshotName: state.Get("snapshot_name").(string),
|
2017-04-11 06:19:28 -04:00
|
|
|
snapshotID: state.Get("snapshot_id").(string),
|
2020-09-09 04:27:48 -04:00
|
|
|
zoneName: b.config.Zone,
|
2017-04-06 05:19:17 -04:00
|
|
|
client: client,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2017-04-06 05:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|