diff --git a/builder/amazon/ebs/step_security_group.go b/builder/amazon/common/step_security_group.go similarity index 71% rename from builder/amazon/ebs/step_security_group.go rename to builder/amazon/common/step_security_group.go index 4a056a1b3..5f3be3df9 100644 --- a/builder/amazon/ebs/step_security_group.go +++ b/builder/amazon/common/step_security_group.go @@ -1,4 +1,4 @@ -package ebs +package common import ( "cgl.tideland.biz/identifier" @@ -10,18 +10,20 @@ import ( "log" ) -type stepSecurityGroup struct { - groupId string +type StepSecurityGroup struct { + SecurityGroupId string + SSHPort int + + createdGroupId string } -func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepAction { - config := state["config"].(config) +func (s *StepSecurityGroup) Run(state map[string]interface{}) multistep.StepAction { ec2conn := state["ec2"].(*ec2.EC2) ui := state["ui"].(packer.Ui) - if config.SecurityGroupId != "" { - log.Printf("Using specified security group: %s", config.SecurityGroupId) - state["securityGroupId"] = config.SecurityGroupId + if s.SecurityGroupId != "" { + log.Printf("Using specified security group: %s", s.SecurityGroupId) + state["securityGroupId"] = s.SecurityGroupId return multistep.ActionContinue } @@ -36,14 +38,14 @@ func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepActi } // Set the group ID so we can delete it later - s.groupId = groupResp.Id + s.createdGroupId = groupResp.Id // Authorize the SSH access perms := []ec2.IPPerm{ ec2.IPPerm{ Protocol: "tcp", - FromPort: config.SSHPort, - ToPort: config.SSHPort, + FromPort: s.SSHPort, + ToPort: s.SSHPort, SourceIPs: []string{"0.0.0.0/0"}, }, } @@ -57,13 +59,13 @@ func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepActi } // Set some state data for use in future steps - state["securityGroupId"] = s.groupId + state["securityGroupId"] = s.createdGroupId return multistep.ActionContinue } -func (s *stepSecurityGroup) Cleanup(state map[string]interface{}) { - if s.groupId == "" { +func (s *StepSecurityGroup) Cleanup(state map[string]interface{}) { + if s.createdGroupId == "" { return } @@ -71,10 +73,10 @@ func (s *stepSecurityGroup) Cleanup(state map[string]interface{}) { ui := state["ui"].(packer.Ui) ui.Say("Deleting temporary security group...") - _, err := ec2conn.DeleteSecurityGroup(ec2.SecurityGroup{Id: s.groupId}) + _, err := ec2conn.DeleteSecurityGroup(ec2.SecurityGroup{Id: s.createdGroupId}) if err != nil { log.Printf("Error deleting security group: %s", err) ui.Error(fmt.Sprintf( - "Error cleaning up security group. Please delete the group manually: %s", s.groupId)) + "Error cleaning up security group. Please delete the group manually: %s", s.createdGroupId)) } } diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 81e839b9f..a0461efb6 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -91,7 +91,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps steps := []multistep.Step{ &awscommon.StepKeyPair{}, - &stepSecurityGroup{}, + &awscommon.StepSecurityGroup{ + SecurityGroupId: b.config.SecurityGroupId, + SSHPort: b.config.SSHPort, + }, &stepRunSourceInstance{}, &common.StepConnectSSH{ SSHAddress: sshAddress, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 8aa17633d..66680903c 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -70,6 +70,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps steps := []multistep.Step{ &awscommon.StepKeyPair{}, + &awscommon.StepSecurityGroup{ + SecurityGroupId: b.config.SecurityGroupId, + SSHPort: b.config.SSHPort, + }, } // Run!