2013-07-20 22:50:55 -04:00
|
|
|
package common
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/multistep"
|
2013-10-16 22:21:14 -04:00
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
2013-06-11 17:37:10 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
2013-08-12 16:34:55 -04:00
|
|
|
"time"
|
2013-06-11 17:37:10 -04:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:50:55 -04:00
|
|
|
type StepSecurityGroup struct {
|
2013-10-02 13:52:16 -04:00
|
|
|
SecurityGroupIds []string
|
|
|
|
SSHPort int
|
|
|
|
VpcId string
|
2013-07-20 22:50:55 -04:00
|
|
|
|
|
|
|
createdGroupId string
|
2013-06-11 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSecurityGroup) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-11 17:37:10 -04:00
|
|
|
|
2013-10-02 13:52:16 -04:00
|
|
|
if len(s.SecurityGroupIds) > 0 {
|
|
|
|
log.Printf("Using specified security groups: %v", s.SecurityGroupIds)
|
|
|
|
state.Put("securityGroupIds", s.SecurityGroupIds)
|
2013-07-09 11:57:21 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:51:25 -04:00
|
|
|
if s.SSHPort == 0 {
|
|
|
|
panic("SSHPort must be set to a non-zero value.")
|
|
|
|
}
|
|
|
|
|
2013-06-11 17:37:10 -04:00
|
|
|
// Create the group
|
|
|
|
ui.Say("Creating temporary security group for this instance...")
|
2013-10-16 22:21:14 -04:00
|
|
|
groupName := fmt.Sprintf("packer %s", uuid.TimeOrderedUUID())
|
2013-06-11 17:37:10 -04:00
|
|
|
log.Printf("Temporary group name: %s", groupName)
|
2013-07-22 01:46:11 -04:00
|
|
|
group := ec2.SecurityGroup{
|
2013-07-22 01:46:58 -04:00
|
|
|
Name: groupName,
|
2013-07-22 01:46:11 -04:00
|
|
|
Description: "Temporary group for Packer",
|
2013-07-22 01:46:58 -04:00
|
|
|
VpcId: s.VpcId,
|
2013-07-22 01:46:11 -04:00
|
|
|
}
|
|
|
|
groupResp, err := ec2conn.CreateSecurityGroup(group)
|
2013-06-11 17:37:10 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the group ID so we can delete it later
|
2013-07-20 22:50:55 -04:00
|
|
|
s.createdGroupId = groupResp.Id
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
// Authorize the SSH access
|
|
|
|
perms := []ec2.IPPerm{
|
|
|
|
ec2.IPPerm{
|
|
|
|
Protocol: "tcp",
|
2013-07-20 22:50:55 -04:00
|
|
|
FromPort: s.SSHPort,
|
|
|
|
ToPort: s.SSHPort,
|
2013-06-11 17:37:10 -04:00
|
|
|
SourceIPs: []string{"0.0.0.0/0"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-12-28 12:04:15 -05:00
|
|
|
// We loop and retry this a few times because sometimes the security
|
|
|
|
// group isn't available immediately because AWS resources are eventaully
|
|
|
|
// consistent.
|
2013-06-11 17:37:10 -04:00
|
|
|
ui.Say("Authorizing SSH access on the temporary security group...")
|
2013-12-28 12:03:22 -05:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
_, err = ec2conn.AuthorizeSecurityGroup(groupResp.SecurityGroup, perms)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Error authorizing. Will sleep and retry. %s", err)
|
|
|
|
time.Sleep((time.Duration(i) * time.Second) + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2013-06-19 23:54:02 -04:00
|
|
|
err := fmt.Errorf("Error creating temporary security group: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-06-11 17:37:10 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some state data for use in future steps
|
2013-10-02 13:52:16 -04:00
|
|
|
state.Put("securityGroupIds", []string{s.createdGroupId})
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSecurityGroup) Cleanup(state multistep.StateBag) {
|
2013-07-20 22:50:55 -04:00
|
|
|
if s.createdGroupId == "" {
|
2013-06-11 17:37:10 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary security group...")
|
2013-08-12 16:34:55 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
_, err = ec2conn.DeleteSecurityGroup(ec2.SecurityGroup{Id: s.createdGroupId})
|
2013-08-12 16:43:52 -04:00
|
|
|
if err == nil {
|
|
|
|
break
|
2013-08-12 16:34:55 -04:00
|
|
|
}
|
2013-08-12 16:43:52 -04:00
|
|
|
|
|
|
|
log.Printf("Error deleting security group: %s", err)
|
|
|
|
time.Sleep(5 * time.Second)
|
2013-08-12 16:34:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 17:37:10 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
2013-07-20 22:50:55 -04:00
|
|
|
"Error cleaning up security group. Please delete the group manually: %s", s.createdGroupId))
|
2013-06-11 17:37:10 -04:00
|
|
|
}
|
|
|
|
}
|