2013-07-20 22:50:55 -04:00
|
|
|
package common
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2017-01-18 18:11:52 -05:00
|
|
|
"github.com/aws/aws-sdk-go/private/waiter"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2013-06-11 17:37:10 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-10-16 22:21:14 -04:00
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
2015-06-14 02:12:59 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2013-06-11 17:37:10 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2013-07-20 22:50:55 -04:00
|
|
|
type StepSecurityGroup struct {
|
2015-06-14 02:12:59 -04:00
|
|
|
CommConfig *communicator.Config
|
2013-10-02 13:52:16 -04:00
|
|
|
SecurityGroupIds []string
|
|
|
|
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 {
|
2017-01-18 18:25:31 -05:00
|
|
|
_, err := ec2conn.DescribeSecurityGroups(
|
|
|
|
&ec2.DescribeSecurityGroupsInput{
|
|
|
|
GroupIds: aws.StringSlice(s.SecurityGroupIds),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Couldn't find specified security group: %s", err)
|
|
|
|
log.Printf("[DEBUG] %s", err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-10-02 13:52:16 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-14 02:12:59 -04:00
|
|
|
port := s.CommConfig.Port()
|
|
|
|
if port == 0 {
|
|
|
|
panic("port must be set to a non-zero value.")
|
2013-07-20 22:51:25 -04:00
|
|
|
}
|
|
|
|
|
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)
|
2015-04-05 17:58:48 -04:00
|
|
|
group := &ec2.CreateSecurityGroupInput{
|
|
|
|
GroupName: &groupName,
|
|
|
|
Description: aws.String("Temporary group for Packer"),
|
2015-08-17 20:44:01 -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())
|
2015-04-06 22:11:34 -04:00
|
|
|
state.Put("error", err)
|
2013-06-11 17:37:10 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the group ID so we can delete it later
|
2015-08-17 20:44:01 -04:00
|
|
|
s.createdGroupId = *groupResp.GroupId
|
2015-04-05 17:58:48 -04:00
|
|
|
|
|
|
|
// Authorize the SSH access for the security group
|
|
|
|
req := &ec2.AuthorizeSecurityGroupIngressInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
GroupId: groupResp.GroupId,
|
|
|
|
IpProtocol: aws.String("tcp"),
|
2015-07-28 20:10:21 -04:00
|
|
|
FromPort: aws.Int64(int64(port)),
|
|
|
|
ToPort: aws.Int64(int64(port)),
|
2015-08-17 20:44:01 -04:00
|
|
|
CidrIp: aws.String("0.0.0.0/0"),
|
2013-06-11 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
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.
|
2015-06-14 02:12:59 -04:00
|
|
|
ui.Say(fmt.Sprintf(
|
|
|
|
"Authorizing access to port %d the temporary security group...",
|
|
|
|
port))
|
2013-12-28 12:03:22 -05:00
|
|
|
for i := 0; i < 5; i++ {
|
2015-04-05 17:58:48 -04:00
|
|
|
_, err = ec2conn.AuthorizeSecurityGroupIngress(req)
|
2013-12-28 12:03:22 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-18 18:11:52 -05:00
|
|
|
log.Printf("[DEBUG] Waiting for temporary security group: %s", s.createdGroupId)
|
|
|
|
err = waitUntilSecurityGroupExists(ec2conn,
|
|
|
|
&ec2.DescribeSecurityGroupsInput{
|
|
|
|
GroupIds: []*string{aws.String(s.createdGroupId)},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("[DEBUG] Found security group %s", s.createdGroupId)
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("Timed out waiting for security group %s: %s", s.createdGroupId, err)
|
|
|
|
log.Printf("[DEBUG] %s", err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-11 17:37:10 -04:00
|
|
|
// 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++ {
|
2015-08-17 20:44:01 -04:00
|
|
|
_, err = ec2conn.DeleteSecurityGroup(&ec2.DeleteSecurityGroupInput{GroupId: &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
|
|
|
}
|
|
|
|
}
|
2017-01-18 18:11:52 -05:00
|
|
|
|
|
|
|
func waitUntilSecurityGroupExists(c *ec2.EC2, input *ec2.DescribeSecurityGroupsInput) error {
|
|
|
|
waiterCfg := waiter.Config{
|
|
|
|
Operation: "DescribeSecurityGroups",
|
|
|
|
Delay: 15,
|
|
|
|
MaxAttempts: 40,
|
|
|
|
Acceptors: []waiter.WaitAcceptor{
|
|
|
|
{
|
|
|
|
State: "success",
|
|
|
|
Matcher: "path",
|
|
|
|
Argument: "length(SecurityGroups[]) > `0`",
|
|
|
|
Expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
State: "retry",
|
|
|
|
Matcher: "error",
|
|
|
|
Argument: "",
|
|
|
|
Expected: "InvalidGroup.NotFound",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
State: "retry",
|
|
|
|
Matcher: "error",
|
|
|
|
Argument: "",
|
|
|
|
Expected: "InvalidSecurityGroupID.NotFound",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
w := waiter.Waiter{
|
|
|
|
Client: c,
|
|
|
|
Input: input,
|
|
|
|
Config: waiterCfg,
|
|
|
|
}
|
|
|
|
return w.Wait()
|
|
|
|
}
|