builder/amazon: extract StepKeyPair for both
This commit is contained in:
parent
5921492c8e
commit
e67e4cfa16
|
@ -1,4 +1,4 @@
|
||||||
package ebs
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cgl.tideland.biz/identifier"
|
"cgl.tideland.biz/identifier"
|
||||||
|
@ -10,11 +10,11 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepKeyPair struct {
|
type StepKeyPair struct {
|
||||||
keyName string
|
keyName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepKeyPair) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *StepKeyPair) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
ec2conn := state["ec2"].(*ec2.EC2)
|
ec2conn := state["ec2"].(*ec2.EC2)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state["ui"].(packer.Ui)
|
||||||
|
|
||||||
|
@ -23,9 +23,7 @@ func (s *stepKeyPair) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
log.Printf("temporary keypair name: %s", keyName)
|
log.Printf("temporary keypair name: %s", keyName)
|
||||||
keyResp, err := ec2conn.CreateKeyPair(keyName)
|
keyResp, err := ec2conn.CreateKeyPair(keyName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error creating temporary keypair: %s", err)
|
state["error"] = fmt.Errorf("Error creating temporary keypair: %s", err)
|
||||||
state["error"] = err
|
|
||||||
ui.Error(err.Error())
|
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +37,7 @@ func (s *stepKeyPair) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepKeyPair) Cleanup(state map[string]interface{}) {
|
func (s *StepKeyPair) Cleanup(state map[string]interface{}) {
|
||||||
// If no key name is set, then we never created it, so just return
|
// If no key name is set, then we never created it, so just return
|
||||||
if s.keyName == "" {
|
if s.keyName == "" {
|
||||||
return
|
return
|
|
@ -90,7 +90,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&stepKeyPair{},
|
&awscommon.StepKeyPair{},
|
||||||
&stepSecurityGroup{},
|
&stepSecurityGroup{},
|
||||||
&stepRunSourceInstance{},
|
&stepRunSourceInstance{},
|
||||||
&common.StepConnectSSH{
|
&common.StepConnectSSH{
|
||||||
|
|
|
@ -3,7 +3,11 @@
|
||||||
package instance
|
package instance
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/mitchellh/goamz/aws"
|
||||||
|
"github.com/mitchellh/goamz/ec2"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
||||||
|
"github.com/mitchellh/packer/builder/common"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
@ -14,6 +18,9 @@ const BuilderId = "mitchellh.amazon.instance"
|
||||||
// Config is the configuration that is chained through the steps and
|
// Config is the configuration that is chained through the steps and
|
||||||
// settable from the template.
|
// settable from the template.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
common.PackerConfig `mapstructure:",squash"`
|
||||||
|
awscommon.AccessConfig `mapstructure:",squash"`
|
||||||
|
awscommon.RunConfig `mapstructure:",squash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
|
@ -22,10 +29,71 @@ type Builder struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) Prepare(raws ...interface{}) error {
|
func (b *Builder) Prepare(raws ...interface{}) error {
|
||||||
|
md, err := common.DecodeConfig(&b.config, raws...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accumulate any errors
|
||||||
|
errs := common.CheckUnusedConfig(md)
|
||||||
|
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare()...)
|
||||||
|
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare()...)
|
||||||
|
|
||||||
|
if errs != nil && len(errs.Errors) > 0 {
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Config: %+v", b.config)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
||||||
|
region, ok := aws.Regions[b.config.Region]
|
||||||
|
if !ok {
|
||||||
|
panic("region not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
auth, err := b.config.AccessConfig.Auth()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ec2conn := ec2.New(auth, region)
|
||||||
|
|
||||||
|
// Setup the state bag and initial state for the steps
|
||||||
|
state := make(map[string]interface{})
|
||||||
|
state["config"] = b.config
|
||||||
|
state["ec2"] = ec2conn
|
||||||
|
state["hook"] = hook
|
||||||
|
state["ui"] = ui
|
||||||
|
|
||||||
|
// Build the steps
|
||||||
|
steps := []multistep.Step{
|
||||||
|
&awscommon.StepKeyPair{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run!
|
||||||
|
if b.config.PackerDebug {
|
||||||
|
b.runner = &multistep.DebugRunner{
|
||||||
|
Steps: steps,
|
||||||
|
PauseFn: common.MultistepDebugFn(ui),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.runner.Run(state)
|
||||||
|
|
||||||
|
// If there was an error, return that
|
||||||
|
if rawErr, ok := state["error"]; ok {
|
||||||
|
return nil, rawErr.(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there are no AMIs, then just return
|
||||||
|
if _, ok := state["amis"]; !ok {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,10 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func testConfig() map[string]interface{} {
|
||||||
|
return map[string]interface{}{}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
||||||
var raw interface{}
|
var raw interface{}
|
||||||
raw = &Builder{}
|
raw = &Builder{}
|
||||||
|
@ -12,3 +16,15 @@ func TestBuilder_ImplementsBuilder(t *testing.T) {
|
||||||
t.Fatalf("Builder should be a builder")
|
t.Fatalf("Builder should be a builder")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
||||||
|
var b Builder
|
||||||
|
config := testConfig()
|
||||||
|
|
||||||
|
// Add a random key
|
||||||
|
config["i_should_not_be_valid"] = true
|
||||||
|
err := b.Prepare(config)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("should have error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue