builder/amazon: use helper/communicator
This commit is contained in:
parent
a1ceb5a7ef
commit
e557928119
|
@ -4,9 +4,9 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mitchellh/packer/common/uuid"
|
"github.com/mitchellh/packer/common/uuid"
|
||||||
|
"github.com/mitchellh/packer/helper/communicator"
|
||||||
"github.com/mitchellh/packer/template/interpolate"
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,11 +21,6 @@ type RunConfig struct {
|
||||||
SourceAmi string `mapstructure:"source_ami"`
|
SourceAmi string `mapstructure:"source_ami"`
|
||||||
SpotPrice string `mapstructure:"spot_price"`
|
SpotPrice string `mapstructure:"spot_price"`
|
||||||
SpotPriceAutoProduct string `mapstructure:"spot_price_auto_product"`
|
SpotPriceAutoProduct string `mapstructure:"spot_price_auto_product"`
|
||||||
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
|
||||||
SSHUsername string `mapstructure:"ssh_username"`
|
|
||||||
SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"`
|
|
||||||
SSHPrivateIp bool `mapstructure:"ssh_private_ip"`
|
|
||||||
SSHPort int `mapstructure:"ssh_port"`
|
|
||||||
SecurityGroupId string `mapstructure:"security_group_id"`
|
SecurityGroupId string `mapstructure:"security_group_id"`
|
||||||
SecurityGroupIds []string `mapstructure:"security_group_ids"`
|
SecurityGroupIds []string `mapstructure:"security_group_ids"`
|
||||||
SubnetId string `mapstructure:"subnet_id"`
|
SubnetId string `mapstructure:"subnet_id"`
|
||||||
|
@ -34,27 +29,19 @@ type RunConfig struct {
|
||||||
UserDataFile string `mapstructure:"user_data_file"`
|
UserDataFile string `mapstructure:"user_data_file"`
|
||||||
VpcId string `mapstructure:"vpc_id"`
|
VpcId string `mapstructure:"vpc_id"`
|
||||||
|
|
||||||
// Unexported fields that are calculated from others
|
// Communicator settings
|
||||||
sshTimeout time.Duration
|
Comm communicator.Config `mapstructure:",squash"`
|
||||||
|
SSHPrivateIp bool `mapstructure:"ssh_private_ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
|
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
|
||||||
// Defaults
|
|
||||||
if c.SSHPort == 0 {
|
|
||||||
c.SSHPort = 22
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.RawSSHTimeout == "" {
|
|
||||||
c.RawSSHTimeout = "5m"
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.TemporaryKeyPairName == "" {
|
if c.TemporaryKeyPairName == "" {
|
||||||
c.TemporaryKeyPairName = fmt.Sprintf(
|
c.TemporaryKeyPairName = fmt.Sprintf(
|
||||||
"packer %s", uuid.TimeOrderedUUID())
|
"packer %s", uuid.TimeOrderedUUID())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validation
|
// Validation
|
||||||
var errs []error
|
errs := c.Comm.Prepare(ctx)
|
||||||
if c.SourceAmi == "" {
|
if c.SourceAmi == "" {
|
||||||
errs = append(errs, errors.New("A source_ami must be specified"))
|
errs = append(errs, errors.New("A source_ami must be specified"))
|
||||||
}
|
}
|
||||||
|
@ -70,10 +57,6 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.SSHUsername == "" {
|
|
||||||
errs = append(errs, errors.New("An ssh_username must be specified"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.UserData != "" && c.UserDataFile != "" {
|
if c.UserData != "" && c.UserDataFile != "" {
|
||||||
errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
|
errs = append(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
|
||||||
} else if c.UserDataFile != "" {
|
} else if c.UserDataFile != "" {
|
||||||
|
@ -91,15 +74,5 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
|
||||||
c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
|
|
||||||
if err != nil {
|
|
||||||
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RunConfig) SSHTimeout() time.Duration {
|
|
||||||
return c.sshTimeout
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mitchellh/packer/helper/communicator"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -19,7 +21,10 @@ func testConfig() *RunConfig {
|
||||||
return &RunConfig{
|
return &RunConfig{
|
||||||
SourceAmi: "abcd",
|
SourceAmi: "abcd",
|
||||||
InstanceType: "m1.small",
|
InstanceType: "m1.small",
|
||||||
SSHUsername: "root",
|
|
||||||
|
Comm: communicator.Config{
|
||||||
|
SSHUsername: "foo",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,41 +67,28 @@ func TestRunConfigPrepare_SpotAuto(t *testing.T) {
|
||||||
|
|
||||||
func TestRunConfigPrepare_SSHPort(t *testing.T) {
|
func TestRunConfigPrepare_SSHPort(t *testing.T) {
|
||||||
c := testConfig()
|
c := testConfig()
|
||||||
c.SSHPort = 0
|
c.Comm.SSHPort = 0
|
||||||
if err := c.Prepare(nil); len(err) != 0 {
|
if err := c.Prepare(nil); len(err) != 0 {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.SSHPort != 22 {
|
if c.Comm.SSHPort != 22 {
|
||||||
t.Fatalf("invalid value: %d", c.SSHPort)
|
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.SSHPort = 44
|
c.Comm.SSHPort = 44
|
||||||
if err := c.Prepare(nil); len(err) != 0 {
|
if err := c.Prepare(nil); len(err) != 0 {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.SSHPort != 44 {
|
if c.Comm.SSHPort != 44 {
|
||||||
t.Fatalf("invalid value: %d", c.SSHPort)
|
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRunConfigPrepare_SSHTimeout(t *testing.T) {
|
|
||||||
c := testConfig()
|
|
||||||
c.RawSSHTimeout = ""
|
|
||||||
if err := c.Prepare(nil); len(err) != 0 {
|
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.RawSSHTimeout = "bad"
|
|
||||||
if err := c.Prepare(nil); len(err) != 1 {
|
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunConfigPrepare_SSHUsername(t *testing.T) {
|
func TestRunConfigPrepare_SSHUsername(t *testing.T) {
|
||||||
c := testConfig()
|
c := testConfig()
|
||||||
c.SSHUsername = ""
|
c.Comm.SSHUsername = ""
|
||||||
if err := c.Prepare(nil); len(err) != 1 {
|
if err := c.Prepare(nil); len(err) != 1 {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
||||||
"github.com/mitchellh/packer/common"
|
"github.com/mitchellh/packer/common"
|
||||||
|
"github.com/mitchellh/packer/helper/communicator"
|
||||||
"github.com/mitchellh/packer/helper/config"
|
"github.com/mitchellh/packer/helper/config"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"github.com/mitchellh/packer/template/interpolate"
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
|
@ -89,11 +90,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
|
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
|
||||||
KeyPairName: b.config.TemporaryKeyPairName,
|
KeyPairName: b.config.TemporaryKeyPairName,
|
||||||
PrivateKeyFile: b.config.SSHPrivateKeyFile,
|
PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey,
|
||||||
},
|
},
|
||||||
&awscommon.StepSecurityGroup{
|
&awscommon.StepSecurityGroup{
|
||||||
SecurityGroupIds: b.config.SecurityGroupIds,
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
||||||
SSHPort: b.config.SSHPort,
|
SSHPort: b.config.RunConfig.Comm.SSHPort,
|
||||||
VpcId: b.config.VpcId,
|
VpcId: b.config.VpcId,
|
||||||
},
|
},
|
||||||
&awscommon.StepRunSourceInstance{
|
&awscommon.StepRunSourceInstance{
|
||||||
|
@ -112,11 +113,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
BlockDevices: b.config.BlockDevices,
|
BlockDevices: b.config.BlockDevices,
|
||||||
Tags: b.config.RunTags,
|
Tags: b.config.RunTags,
|
||||||
},
|
},
|
||||||
&common.StepConnectSSH{
|
&communicator.StepConnect{
|
||||||
|
Config: &b.config.RunConfig.Comm,
|
||||||
SSHAddress: awscommon.SSHAddress(
|
SSHAddress: awscommon.SSHAddress(
|
||||||
ec2conn, b.config.SSHPort, b.config.SSHPrivateIp),
|
ec2conn,
|
||||||
SSHConfig: awscommon.SSHConfig(b.config.SSHUsername),
|
b.config.RunConfig.Comm.SSHPort,
|
||||||
SSHWaitTimeout: b.config.SSHTimeout(),
|
b.config.SSHPrivateIp),
|
||||||
|
SSHConfig: awscommon.SSHConfig(
|
||||||
|
b.config.RunConfig.Comm.SSHUsername),
|
||||||
},
|
},
|
||||||
&common.StepProvision{},
|
&common.StepProvision{},
|
||||||
&stepStopInstance{SpotPrice: b.config.SpotPrice},
|
&stepStopInstance{SpotPrice: b.config.SpotPrice},
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
awscommon "github.com/mitchellh/packer/builder/amazon/common"
|
||||||
"github.com/mitchellh/packer/common"
|
"github.com/mitchellh/packer/common"
|
||||||
|
"github.com/mitchellh/packer/helper/communicator"
|
||||||
"github.com/mitchellh/packer/helper/config"
|
"github.com/mitchellh/packer/helper/config"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"github.com/mitchellh/packer/template/interpolate"
|
"github.com/mitchellh/packer/template/interpolate"
|
||||||
|
@ -175,11 +176,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
|
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
|
||||||
KeyPairName: b.config.TemporaryKeyPairName,
|
KeyPairName: b.config.TemporaryKeyPairName,
|
||||||
PrivateKeyFile: b.config.SSHPrivateKeyFile,
|
PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey,
|
||||||
},
|
},
|
||||||
&awscommon.StepSecurityGroup{
|
&awscommon.StepSecurityGroup{
|
||||||
SecurityGroupIds: b.config.SecurityGroupIds,
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
||||||
SSHPort: b.config.SSHPort,
|
SSHPort: b.config.RunConfig.Comm.SSHPort,
|
||||||
VpcId: b.config.VpcId,
|
VpcId: b.config.VpcId,
|
||||||
},
|
},
|
||||||
&awscommon.StepRunSourceInstance{
|
&awscommon.StepRunSourceInstance{
|
||||||
|
@ -197,11 +198,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
BlockDevices: b.config.BlockDevices,
|
BlockDevices: b.config.BlockDevices,
|
||||||
Tags: b.config.RunTags,
|
Tags: b.config.RunTags,
|
||||||
},
|
},
|
||||||
&common.StepConnectSSH{
|
&communicator.StepConnect{
|
||||||
|
Config: &b.config.RunConfig.Comm,
|
||||||
SSHAddress: awscommon.SSHAddress(
|
SSHAddress: awscommon.SSHAddress(
|
||||||
ec2conn, b.config.SSHPort, b.config.SSHPrivateIp),
|
ec2conn,
|
||||||
SSHConfig: awscommon.SSHConfig(b.config.SSHUsername),
|
b.config.RunConfig.Comm.SSHPort,
|
||||||
SSHWaitTimeout: b.config.SSHTimeout(),
|
b.config.SSHPrivateIp),
|
||||||
|
SSHConfig: awscommon.SSHConfig(
|
||||||
|
b.config.RunConfig.Comm.SSHUsername),
|
||||||
},
|
},
|
||||||
&common.StepProvision{},
|
&common.StepProvision{},
|
||||||
&StepUploadX509Cert{},
|
&StepUploadX509Cert{},
|
||||||
|
|
Loading…
Reference in New Issue