Commit 4f568f49 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazon: extract StepSecurityGroup

parent 45096d07
package ebs package common
import ( import (
"cgl.tideland.biz/identifier" "cgl.tideland.biz/identifier"
...@@ -10,18 +10,20 @@ import ( ...@@ -10,18 +10,20 @@ import (
"log" "log"
) )
type stepSecurityGroup struct { type StepSecurityGroup struct {
groupId string SecurityGroupId string
SSHPort int
createdGroupId string
} }
func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepAction { func (s *StepSecurityGroup) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(config)
ec2conn := state["ec2"].(*ec2.EC2) ec2conn := state["ec2"].(*ec2.EC2)
ui := state["ui"].(packer.Ui) ui := state["ui"].(packer.Ui)
if config.SecurityGroupId != "" { if s.SecurityGroupId != "" {
log.Printf("Using specified security group: %s", config.SecurityGroupId) log.Printf("Using specified security group: %s", s.SecurityGroupId)
state["securityGroupId"] = config.SecurityGroupId state["securityGroupId"] = s.SecurityGroupId
return multistep.ActionContinue return multistep.ActionContinue
} }
...@@ -36,14 +38,14 @@ func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepActi ...@@ -36,14 +38,14 @@ func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepActi
} }
// Set the group ID so we can delete it later // Set the group ID so we can delete it later
s.groupId = groupResp.Id s.createdGroupId = groupResp.Id
// Authorize the SSH access // Authorize the SSH access
perms := []ec2.IPPerm{ perms := []ec2.IPPerm{
ec2.IPPerm{ ec2.IPPerm{
Protocol: "tcp", Protocol: "tcp",
FromPort: config.SSHPort, FromPort: s.SSHPort,
ToPort: config.SSHPort, ToPort: s.SSHPort,
SourceIPs: []string{"0.0.0.0/0"}, SourceIPs: []string{"0.0.0.0/0"},
}, },
} }
...@@ -57,13 +59,13 @@ func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepActi ...@@ -57,13 +59,13 @@ func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepActi
} }
// Set some state data for use in future steps // Set some state data for use in future steps
state["securityGroupId"] = s.groupId state["securityGroupId"] = s.createdGroupId
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepSecurityGroup) Cleanup(state map[string]interface{}) { func (s *StepSecurityGroup) Cleanup(state map[string]interface{}) {
if s.groupId == "" { if s.createdGroupId == "" {
return return
} }
...@@ -71,10 +73,10 @@ func (s *stepSecurityGroup) Cleanup(state map[string]interface{}) { ...@@ -71,10 +73,10 @@ func (s *stepSecurityGroup) Cleanup(state map[string]interface{}) {
ui := state["ui"].(packer.Ui) ui := state["ui"].(packer.Ui)
ui.Say("Deleting temporary security group...") 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 { if err != nil {
log.Printf("Error deleting security group: %s", err) log.Printf("Error deleting security group: %s", err)
ui.Error(fmt.Sprintf( 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))
} }
} }
...@@ -91,7 +91,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -91,7 +91,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps // Build the steps
steps := []multistep.Step{ steps := []multistep.Step{
&awscommon.StepKeyPair{}, &awscommon.StepKeyPair{},
&stepSecurityGroup{}, &awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId,
SSHPort: b.config.SSHPort,
},
&stepRunSourceInstance{}, &stepRunSourceInstance{},
&common.StepConnectSSH{ &common.StepConnectSSH{
SSHAddress: sshAddress, SSHAddress: sshAddress,
......
...@@ -70,6 +70,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -70,6 +70,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps // Build the steps
steps := []multistep.Step{ steps := []multistep.Step{
&awscommon.StepKeyPair{}, &awscommon.StepKeyPair{},
&awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId,
SSHPort: b.config.SSHPort,
},
} }
// Run! // Run!
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment