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-07-31 14:38:30 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-11 17:37:10 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
2013-07-20 22:50:55 -04:00
|
|
|
type StepSecurityGroup struct {
|
2017-10-12 20:05:31 -04:00
|
|
|
CommConfig *communicator.Config
|
|
|
|
SecurityGroupIds []string
|
|
|
|
VpcId string
|
|
|
|
TemporarySGSourceCidr 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 {
|
2017-08-03 16:54:57 -04:00
|
|
|
if s.CommConfig.Type != "none" {
|
|
|
|
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
|
2017-06-09 15:05:59 -04:00
|
|
|
groupName := fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
|
|
|
|
ui.Say(fmt.Sprintf("Creating temporary security group for this instance: %s", groupName))
|
2015-04-05 17:58:48 -04:00
|
|
|
group := &ec2.CreateSecurityGroupInput{
|
|
|
|
GroupName: &groupName,
|
|
|
|
Description: aws.String("Temporary group for Packer"),
|
2013-07-22 01:46:11 -04:00
|
|
|
}
|
2017-05-17 12:45:20 -04:00
|
|
|
|
|
|
|
if s.VpcId != "" {
|
|
|
|
group.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)),
|
2017-10-12 20:05:31 -04:00
|
|
|
CidrIp: aws.String(s.TemporarySGSourceCidr),
|
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
|
2017-03-28 20:45:01 -04:00
|
|
|
// group isn't available immediately because AWS resources are eventually
|
2013-12-28 12:04:15 -05:00
|
|
|
// consistent.
|
2015-06-14 02:12:59 -04:00
|
|
|
ui.Say(fmt.Sprintf(
|
2017-09-25 20:57:56 -04:00
|
|
|
"Authorizing access to port %d from %s in the temporary security group...",
|
2017-10-12 20:05:31 -04:00
|
|
|
port, s.TemporarySGSourceCidr))
|
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 {
|
2017-07-31 14:38:30 -04:00
|
|
|
ctx := aws.BackgroundContext()
|
|
|
|
w := request.Waiter{
|
|
|
|
Name: "DescribeSecurityGroups",
|
2017-01-18 18:11:52 -05:00
|
|
|
MaxAttempts: 40,
|
2017-11-02 03:25:54 -04:00
|
|
|
Delay: request.ConstantWaiterDelay(5 * time.Second),
|
2017-07-31 14:38:30 -04:00
|
|
|
Acceptors: []request.WaiterAcceptor{
|
2017-01-18 18:11:52 -05:00
|
|
|
{
|
2017-07-31 14:38:30 -04:00
|
|
|
State: request.SuccessWaiterState,
|
|
|
|
Matcher: request.PathWaiterMatch,
|
2017-01-18 18:11:52 -05:00
|
|
|
Argument: "length(SecurityGroups[]) > `0`",
|
|
|
|
Expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-07-31 14:38:30 -04:00
|
|
|
State: request.RetryWaiterState,
|
|
|
|
Matcher: request.ErrorWaiterMatch,
|
2017-01-18 18:11:52 -05:00
|
|
|
Argument: "",
|
|
|
|
Expected: "InvalidGroup.NotFound",
|
|
|
|
},
|
|
|
|
{
|
2017-07-31 14:38:30 -04:00
|
|
|
State: request.RetryWaiterState,
|
|
|
|
Matcher: request.ErrorWaiterMatch,
|
2017-01-18 18:11:52 -05:00
|
|
|
Argument: "",
|
|
|
|
Expected: "InvalidSecurityGroupID.NotFound",
|
|
|
|
},
|
|
|
|
},
|
2017-07-31 14:38:30 -04:00
|
|
|
Logger: c.Config.Logger,
|
|
|
|
NewRequest: func(opts []request.Option) (*request.Request, error) {
|
|
|
|
var inCpy *ec2.DescribeSecurityGroupsInput
|
|
|
|
if input != nil {
|
|
|
|
tmp := *input
|
|
|
|
inCpy = &tmp
|
|
|
|
}
|
|
|
|
req, _ := c.DescribeSecurityGroupsRequest(inCpy)
|
|
|
|
req.SetContext(ctx)
|
|
|
|
req.ApplyOptions(opts...)
|
|
|
|
return req, nil
|
|
|
|
},
|
2017-01-18 18:11:52 -05:00
|
|
|
}
|
2017-07-31 14:38:30 -04:00
|
|
|
return w.WaitWithContext(ctx)
|
2017-01-18 18:11:52 -05:00
|
|
|
}
|