Files
Packer-Cn/builder/yandex/builder.go
T

101 lines
2.5 KiB
Go
Raw Normal View History

2019-03-26 12:29:15 +00:00
package yandex
import (
2019-04-12 01:10:52 +03:00
"context"
2019-03-26 12:29:15 +00:00
"fmt"
2019-09-23 21:03:17 +03:00
"github.com/google/uuid"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2019-03-26 12:29:15 +00:00
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
2019-04-09 17:46:41 +03:00
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
2019-09-23 21:03:17 +03:00
"github.com/yandex-cloud/go-sdk/pkg/requestid"
2019-03-26 12:29:15 +00:00
)
// The unique ID for this builder.
const BuilderID = "packer.yandex"
// Builder represents a Packer Builder.
type Builder struct {
2019-12-17 11:25:56 +01:00
config Config
2019-03-26 12:29:15 +00:00
runner multistep.Runner
}
2019-12-17 11:25:56 +01:00
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2019-12-17 11:25:56 +01:00
warnings, errs := b.config.Prepare(raws...)
2019-03-26 12:29:15 +00:00
if errs != nil {
return nil, warnings, errs
2019-03-26 12:29:15 +00:00
}
return nil, warnings, nil
2019-03-26 12:29:15 +00:00
}
// Run executes a yandex Packer build and returns a packer.Artifact
2019-04-09 17:46:41 +03:00
// representing a Yandex.Cloud compute image.
2019-04-12 01:10:52 +03:00
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
2019-12-17 11:25:56 +01:00
driver, err := NewDriverYC(ui, &b.config)
2019-09-23 21:03:17 +03:00
ctx = requestid.ContextWithClientTraceID(ctx, uuid.New().String())
2019-03-26 12:29:15 +00:00
if err != nil {
return nil, err
}
// Set up the state
state := &multistep.BasicStateBag{}
2019-12-17 11:25:56 +01:00
state.Put("config", &b.config)
2019-03-26 12:29:15 +00:00
state.Put("driver", driver)
state.Put("sdk", driver.SDK())
state.Put("hook", hook)
state.Put("ui", ui)
// Build the steps
steps := []multistep.Step{
&StepCreateSSHKey{
2019-03-26 12:29:15 +00:00
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("yc_%s.pem", b.config.PackerBuildName),
},
&StepCreateInstance{
2019-03-26 12:29:15 +00:00
Debug: b.config.PackerDebug,
SerialLogFile: b.config.SerialLogFile,
},
&stepInstanceInfo{},
&communicator.StepConnect{
Config: &b.config.Communicator,
Host: commHost,
2019-03-26 12:29:15 +00:00
SSHConfig: b.config.Communicator.SSHConfigFunc(),
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.Communicator,
},
&StepTeardownInstance{},
2019-03-26 12:29:15 +00:00
&stepCreateImage{},
}
// Run the steps
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
2019-04-12 01:10:52 +03:00
b.runner.Run(ctx, state)
2019-03-26 12:29:15 +00:00
// Report any errors
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
2019-04-09 17:46:41 +03:00
image, ok := state.GetOk("image")
if !ok {
return nil, fmt.Errorf("Failed to find 'image' in state. Bug?")
2019-03-26 12:29:15 +00:00
}
2019-04-09 17:46:41 +03:00
artifact := &Artifact{
image: image.(*compute.Image),
config: &b.config,
driver: driver,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
2019-03-26 12:29:15 +00:00
}
return artifact, nil
}