builder/amazon: various fixes (minor) to get things going

This commit is contained in:
Mitchell Hashimoto 2015-06-13 23:12:59 -07:00
parent dc8c94890a
commit 8f6ecfd9e3
6 changed files with 38 additions and 10 deletions

View File

@ -48,7 +48,9 @@ func (s *StepGetPassword) Run(state multistep.StateBag) multistep.StepAction {
ui.Say("Waiting for auto-generated password for instance...")
ui.Message(
"It is normal for this process to take up to 15 minutes,\n" +
"but it usually takes around 5. Please wait.")
"but it usually takes around 5. Please wait. After the\n" +
"password is read, it will printed out below. Since it should\n" +
"be a temporary password, this should be a minimal security risk.")
password, err = s.waitForPassword(state, cancel)
waitDone <- true
}()
@ -66,7 +68,7 @@ WaitLoop:
return multistep.ActionHalt
}
ui.Message("Password retrieved!")
ui.Message(fmt.Sprintf(" \nPassword retrieved: %s", password))
s.Comm.WinRMPassword = password
break WaitLoop
case <-timeout:
@ -121,6 +123,8 @@ func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-cha
return decryptedPassword, nil
}
log.Printf("[DEBUG] Password is blank, will retry...")
}
}

View File

@ -1,6 +1,7 @@
package common
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
@ -53,7 +54,14 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt
}
// Test if it is encoded already, and if not, encode it
if _, err := base64.StdEncoding.DecodeString(string(contents)); err != nil {
log.Printf("[DEBUG] base64 encoding user data...")
contents = []byte(base64.StdEncoding.EncodeToString(contents))
}
userData = string(contents)
}
ui.Say("Launching a source AWS instance...")

View File

@ -9,12 +9,13 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common/uuid"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/packer"
)
type StepSecurityGroup struct {
CommConfig *communicator.Config
SecurityGroupIds []string
SSHPort int
VpcId string
createdGroupId string
@ -30,8 +31,9 @@ func (s *StepSecurityGroup) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionContinue
}
if s.SSHPort == 0 {
panic("SSHPort must be set to a non-zero value.")
port := s.CommConfig.Port()
if port == 0 {
panic("port must be set to a non-zero value.")
}
// Create the group
@ -57,15 +59,17 @@ func (s *StepSecurityGroup) Run(state multistep.StateBag) multistep.StepAction {
req := &ec2.AuthorizeSecurityGroupIngressInput{
GroupID: groupResp.GroupID,
IPProtocol: aws.String("tcp"),
FromPort: aws.Long(int64(s.SSHPort)),
ToPort: aws.Long(int64(s.SSHPort)),
FromPort: aws.Long(int64(port)),
ToPort: aws.Long(int64(port)),
CIDRIP: aws.String("0.0.0.0/0"),
}
// We loop and retry this a few times because sometimes the security
// group isn't available immediately because AWS resources are eventaully
// consistent.
ui.Say("Authorizing SSH access on the temporary security group...")
ui.Say(fmt.Sprintf(
"Authorizing access to port %d the temporary security group...",
port))
for i := 0; i < 5; i++ {
_, err = ec2conn.AuthorizeSecurityGroupIngress(req)
if err == nil {

View File

@ -94,7 +94,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
&awscommon.StepSecurityGroup{
SecurityGroupIds: b.config.SecurityGroupIds,
SSHPort: b.config.RunConfig.Comm.SSHPort,
CommConfig: &b.config.RunConfig.Comm,
VpcId: b.config.VpcId,
},
&awscommon.StepRunSourceInstance{

View File

@ -179,8 +179,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey,
},
&awscommon.StepSecurityGroup{
CommConfig: &b.config.RunConfig.Comm,
SecurityGroupIds: b.config.SecurityGroupIds,
SSHPort: b.config.RunConfig.Comm.SSHPort,
VpcId: b.config.VpcId,
},
&awscommon.StepRunSourceInstance{

View File

@ -31,6 +31,18 @@ type Config struct {
WinRMTimeout time.Duration `mapstructure:"winrm_timeout"`
}
// Port returns the port that will be used for access based on config.
func (c *Config) Port() int {
switch c.Type {
case "ssh":
return c.SSHPort
case "winrm":
return c.WinRMPort
default:
return 0
}
}
func (c *Config) Prepare(ctx *interpolate.Context) []error {
if c.Type == "" {
c.Type = "ssh"