2019-03-26 08:29:15 -04:00
|
|
|
package yandex
|
|
|
|
|
|
|
|
import (
|
2019-04-11 18:10:52 -04:00
|
|
|
"context"
|
2019-03-26 08:29:15 -04:00
|
|
|
"fmt"
|
|
|
|
|
2019-09-23 14:03:17 -04:00
|
|
|
"github.com/google/uuid"
|
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/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
|
2019-04-09 10:46:41 -04:00
|
|
|
|
|
|
|
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
|
2019-09-23 14:03:17 -04:00
|
|
|
"github.com/yandex-cloud/go-sdk/pkg/requestid"
|
2019-03-26 08:29:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder.
|
|
|
|
const BuilderID = "packer.yandex"
|
|
|
|
|
|
|
|
// Builder represents a Packer Builder.
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2019-03-26 08:29:15 -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-03-26 08:29:15 -04:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
2020-07-08 18:46:05 -04:00
|
|
|
generatedData := []string{
|
|
|
|
"ImageID",
|
|
|
|
"ImageName",
|
|
|
|
"ImageFamily",
|
|
|
|
"ImageDescription",
|
|
|
|
"ImageFolderID",
|
|
|
|
"SourceImageID",
|
|
|
|
"SourceImageName",
|
|
|
|
"SourceImageDescription",
|
|
|
|
"SourceImageFamily",
|
|
|
|
"SourceImageFolderID",
|
|
|
|
}
|
|
|
|
return generatedData, warnings, nil
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
// Run executes a yandex Packer build and returns a packersdk.Artifact
|
2019-04-09 10:46:41 -04:00
|
|
|
// representing a Yandex.Cloud compute image.
|
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-08-31 09:29:20 -04:00
|
|
|
driver, err := NewDriverYC(ui, &b.config.AccessConfig)
|
2019-09-23 14:03:17 -04:00
|
|
|
ctx = requestid.ContextWithClientTraceID(ctx, uuid.New().String())
|
2019-03-26 08:29:15 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state
|
|
|
|
state := &multistep.BasicStateBag{}
|
2019-12-17 05:25:56 -05:00
|
|
|
state.Put("config", &b.config)
|
2019-03-26 08:29:15 -04:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("sdk", driver.SDK())
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2020-11-04 18:44:05 -05:00
|
|
|
generatedData := &packerbuilderdata.GeneratedData{State: state}
|
2019-03-26 08:29:15 -04:00
|
|
|
|
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
2020-04-26 19:19:08 -04:00
|
|
|
&StepCreateSSHKey{
|
2019-03-26 08:29:15 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
DebugKeyPath: fmt.Sprintf("yc_%s.pem", b.config.PackerBuildName),
|
|
|
|
},
|
2020-04-26 19:19:08 -04:00
|
|
|
&StepCreateInstance{
|
2019-03-26 08:29:15 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
SerialLogFile: b.config.SerialLogFile,
|
2020-07-08 18:46:05 -04:00
|
|
|
GeneratedData: generatedData,
|
2019-03-26 08:29:15 -04:00
|
|
|
},
|
2020-12-09 10:51:34 -05:00
|
|
|
&StepInstanceInfo{},
|
2019-03-26 08:29:15 -04:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Communicator,
|
2020-12-09 10:51:34 -05:00
|
|
|
Host: CommHost,
|
2019-03-26 08:29:15 -04:00
|
|
|
SSHConfig: b.config.Communicator.SSHConfigFunc(),
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2019-03-26 08:29:15 -04:00
|
|
|
Comm: &b.config.Communicator,
|
|
|
|
},
|
2020-11-30 05:27:45 -05:00
|
|
|
&StepTeardownInstance{
|
|
|
|
SerialLogFile: b.config.SerialLogFile,
|
|
|
|
},
|
2020-07-08 18:46:05 -04:00
|
|
|
&stepCreateImage{
|
|
|
|
GeneratedData: generatedData,
|
|
|
|
},
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-04-11 18:10:52 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2019-03-26 08:29:15 -04:00
|
|
|
|
|
|
|
// Report any errors
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
2019-04-09 10:46:41 -04:00
|
|
|
|
|
|
|
image, ok := state.GetOk("image")
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Failed to find 'image' in state. Bug?")
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 10:46:41 -04:00
|
|
|
artifact := &Artifact{
|
2020-07-08 12:32:20 -04:00
|
|
|
Image: image.(*compute.Image),
|
2020-01-30 05:27:58 -05:00
|
|
|
config: &b.config,
|
2020-04-26 19:19:08 -04:00
|
|
|
driver: driver,
|
2020-01-30 05:27:58 -05:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|