Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
3a97bae0
Commit
3a97bae0
authored
May 21, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/amazonebs: Refactor into multiple steps
parent
559777e5
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
362 additions
and
180 deletions
+362
-180
builder/amazonebs/builder.go
builder/amazonebs/builder.go
+14
-180
builder/amazonebs/instance.go
builder/amazonebs/instance.go
+33
-0
builder/amazonebs/step.go
builder/amazonebs/step.go
+39
-0
builder/amazonebs/step_connect_ssh.go
builder/amazonebs/step_connect_ssh.go
+74
-0
builder/amazonebs/step_create_ami.go
builder/amazonebs/step_create_ami.go
+50
-0
builder/amazonebs/step_keypair.go
builder/amazonebs/step_keypair.go
+54
-0
builder/amazonebs/step_run_source_instance.go
builder/amazonebs/step_run_source_instance.go
+60
-0
builder/amazonebs/step_stop_instance.go
builder/amazonebs/step_stop_instance.go
+38
-0
No files found.
builder/amazonebs/builder.go
View file @
3a97bae0
...
@@ -6,19 +6,11 @@
...
@@ -6,19 +6,11 @@
package
amazonebs
package
amazonebs
import
(
import
(
"bufio"
"cgl.tideland.biz/identifier"
gossh
"code.google.com/p/go.crypto/ssh"
"encoding/hex"
"fmt"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/communicator/ssh"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer"
"log"
"log"
"net"
"time"
)
)
type
config
struct
{
type
config
struct
{
...
@@ -56,179 +48,21 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
...
@@ -56,179 +48,21 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
region
:=
aws
.
Regions
[
b
.
config
.
Region
]
region
:=
aws
.
Regions
[
b
.
config
.
Region
]
ec2conn
:=
ec2
.
New
(
auth
,
region
)
ec2conn
:=
ec2
.
New
(
auth
,
region
)
// Create a new keypair that we'll use to access the instance.
// Setup the state bag and initial state for the steps
keyName
:=
fmt
.
Sprintf
(
"packer %s"
,
hex
.
EncodeToString
(
identifier
.
NewUUID
()
.
Raw
()))
state
:=
make
(
map
[
string
]
interface
{})
ui
.
Say
(
"Creating temporary keypair for this instance..."
)
state
[
"config"
]
=
b
.
config
log
.
Printf
(
"temporary keypair name: %s"
,
keyName
)
state
[
"ec2"
]
=
ec2conn
keyResp
,
err
:=
ec2conn
.
CreateKeyPair
(
keyName
)
state
[
"hook"
]
=
hook
if
err
!=
nil
{
state
[
"ui"
]
=
ui
ui
.
Error
(
err
.
Error
())
return
}
// Make sure the keypair is properly deleted when we exit
defer
func
()
{
ui
.
Say
(
"Deleting temporary keypair..."
)
_
,
err
:=
ec2conn
.
DeleteKeyPair
(
keyName
)
if
err
!=
nil
{
ui
.
Error
(
"Error cleaning up keypair. Please delete the key manually: %s"
,
keyName
)
}
}()
runOpts
:=
&
ec2
.
RunInstances
{
KeyName
:
keyName
,
ImageId
:
b
.
config
.
SourceAmi
,
InstanceType
:
b
.
config
.
InstanceType
,
MinCount
:
0
,
MaxCount
:
0
,
}
ui
.
Say
(
"Launching a source AWS instance..."
)
runResp
,
err
:=
ec2conn
.
RunInstances
(
runOpts
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
}
instance
:=
&
runResp
.
Instances
[
0
]
log
.
Printf
(
"instance id: %s"
,
instance
.
InstanceId
)
// Make sure we clean up the instance by terminating it, no matter what
defer
func
()
{
// TODO: error handling
ui
.
Say
(
"Terminating the source AWS instance..."
)
ec2conn
.
TerminateInstances
([]
string
{
instance
.
InstanceId
})
}()
ui
.
Say
(
"Waiting for instance to become ready..."
)
instance
,
err
=
waitForState
(
ec2conn
,
instance
,
"running"
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
}
// Build the SSH configuration
keyring
:=
&
ssh
.
SimpleKeychain
{}
err
=
keyring
.
AddPEMKey
(
keyResp
.
KeyMaterial
)
if
err
!=
nil
{
ui
.
Say
(
"Error setting up SSH config: %s"
,
err
.
Error
())
return
}
sshConfig
:=
&
gossh
.
ClientConfig
{
User
:
"ubuntu"
,
Auth
:
[]
gossh
.
ClientAuth
{
gossh
.
ClientAuthKeyring
(
keyring
),
},
}
// Try to connect for SSH a few times
var
conn
net
.
Conn
for
i
:=
0
;
i
<
5
;
i
++
{
time
.
Sleep
(
time
.
Duration
(
i
)
*
time
.
Second
)
log
.
Printf
(
"Opening TCP conn for SSH to %s:22 (attempt %d)"
,
instance
.
DNSName
,
i
+
1
)
conn
,
err
=
net
.
Dial
(
"tcp"
,
fmt
.
Sprintf
(
"%s:22"
,
instance
.
DNSName
))
if
err
!=
nil
{
continue
}
defer
conn
.
Close
()
}
var
comm
packer
.
Communicator
if
err
==
nil
{
comm
,
err
=
ssh
.
New
(
conn
,
sshConfig
)
}
if
err
!=
nil
{
ui
.
Error
(
"Error connecting to SSH: %s"
,
err
.
Error
())
return
}
// XXX: TEST
remote
,
err
:=
comm
.
Start
(
"echo foo"
)
if
err
!=
nil
{
ui
.
Error
(
"Error: %s"
,
err
.
Error
())
return
}
remote
.
Wait
()
bufr
:=
bufio
.
NewReader
(
remote
.
Stdout
)
line
,
_
:=
bufr
.
ReadString
(
'\n'
)
ui
.
Say
(
line
)
// Stop the instance so we can create an AMI from it
_
,
err
=
ec2conn
.
StopInstances
(
instance
.
InstanceId
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
}
// Wait for the instance to actual stop
// Build the steps
// TODO: Handle diff source states, i.e. this force state sucks
steps
:=
[]
Step
{
ui
.
Say
(
"Waiting for the instance to stop..."
)
&
stepKeyPair
{},
instance
.
State
.
Name
=
"stopping"
&
stepRunSourceInstance
{},
instance
,
err
=
waitForState
(
ec2conn
,
instance
,
"stopped"
)
&
stepStopInstance
{},
if
err
!=
nil
{
&
stepCreateAMI
{},
ui
.
Error
(
err
.
Error
())
return
}
}
// Create the image
// Run!
ui
.
Say
(
"Creating the AMI..."
)
RunSteps
(
state
,
steps
)
createOpts
:=
&
ec2
.
CreateImage
{
InstanceId
:
instance
.
InstanceId
,
Name
:
b
.
config
.
AMIName
,
}
createResp
,
err
:=
ec2conn
.
CreateImage
(
createOpts
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
}
ui
.
Say
(
"AMI: %s"
,
createResp
.
ImageId
)
// Wait for the image to become ready
ui
.
Say
(
"Waiting for AMI to become ready..."
)
for
{
imageResp
,
err
:=
ec2conn
.
Images
([]
string
{
createResp
.
ImageId
},
ec2
.
NewFilter
())
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
}
if
imageResp
.
Images
[
0
]
.
State
==
"available"
{
break
}
}
}
func
waitForState
(
ec2conn
*
ec2
.
EC2
,
originalInstance
*
ec2
.
Instance
,
target
string
)
(
i
*
ec2
.
Instance
,
err
error
)
{
log
.
Printf
(
"Waiting for instance state to become: %s"
,
target
)
i
=
originalInstance
original
:=
i
.
State
.
Name
for
i
.
State
.
Name
==
original
{
var
resp
*
ec2
.
InstancesResp
resp
,
err
=
ec2conn
.
Instances
([]
string
{
i
.
InstanceId
},
ec2
.
NewFilter
())
if
err
!=
nil
{
return
}
i
=
&
resp
.
Reservations
[
0
]
.
Instances
[
0
]
time
.
Sleep
(
2
*
time
.
Second
)
}
if
i
.
State
.
Name
!=
target
{
err
=
fmt
.
Errorf
(
"unexpected target state '%s', wanted '%s'"
,
i
.
State
.
Name
,
target
)
return
}
return
}
}
builder/amazonebs/instance.go
0 → 100644
View file @
3a97bae0
package
amazonebs
import
(
"fmt"
"github.com/mitchellh/goamz/ec2"
"log"
"time"
)
func
waitForState
(
ec2conn
*
ec2
.
EC2
,
originalInstance
*
ec2
.
Instance
,
target
string
)
(
i
*
ec2
.
Instance
,
err
error
)
{
log
.
Printf
(
"Waiting for instance state to become: %s"
,
target
)
i
=
originalInstance
original
:=
i
.
State
.
Name
for
i
.
State
.
Name
==
original
{
var
resp
*
ec2
.
InstancesResp
resp
,
err
=
ec2conn
.
Instances
([]
string
{
i
.
InstanceId
},
ec2
.
NewFilter
())
if
err
!=
nil
{
return
}
i
=
&
resp
.
Reservations
[
0
]
.
Instances
[
0
]
time
.
Sleep
(
2
*
time
.
Second
)
}
if
i
.
State
.
Name
!=
target
{
err
=
fmt
.
Errorf
(
"unexpected target state '%s', wanted '%s'"
,
i
.
State
.
Name
,
target
)
return
}
return
}
builder/amazonebs/step.go
0 → 100644
View file @
3a97bae0
package
amazonebs
// A StepAction determines the next step to take regarding multi-step actions.
type
StepAction
uint
const
(
StepContinue
StepAction
=
iota
StepHalt
)
// Step is a single step that is part of a potentially large sequence
// of other steps, responsible for performing some specific action.
type
Step
interface
{
// Run is called to perform the action. The parameter is a "state bag"
// of untyped things. Please be very careful about type-checking the
// items in this bag.
//
// The return value determines whether multi-step sequences continue
// or should halt.
Run
(
map
[
string
]
interface
{})
StepAction
// Cleanup is called in reverse order of the steps that have run
// and allow steps to clean up after themselves.
//
// The parameter is the same "state bag" as Run.
Cleanup
(
map
[
string
]
interface
{})
}
// RunSteps runs a sequence of steps.
func
RunSteps
(
state
map
[
string
]
interface
{},
steps
[]
Step
)
{
for
_
,
step
:=
range
steps
{
action
:=
step
.
Run
(
state
)
defer
step
.
Cleanup
(
state
)
if
action
==
StepHalt
{
break
}
}
}
builder/amazonebs/step_connect_ssh.go
0 → 100644
View file @
3a97bae0
package
amazonebs
import
(
"fmt"
gossh
"code.google.com/p/go.crypto/ssh"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/packer/communicator/ssh"
"github.com/mitchellh/packer/packer"
"log"
"net"
"time"
)
type
stepConnectSSH
struct
{
conn
net
.
Conn
}
func
(
s
*
stepConnectSSH
)
Run
(
state
map
[
string
]
interface
{})
StepAction
{
instance
:=
state
[
"instance"
]
.
(
*
ec2
.
Instance
)
privateKey
:=
state
[
"privateKey"
]
.
(
string
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
// Build the keyring for authentication. This stores the private key
// we'll use to authenticate.
keyring
:=
&
ssh
.
SimpleKeychain
{}
err
:=
keyring
.
AddPEMKey
(
privateKey
)
if
err
!=
nil
{
ui
.
Say
(
"Error setting up SSH config: %s"
,
err
.
Error
())
return
StepHalt
}
// Build the actual SSH client configuration
sshConfig
:=
&
gossh
.
ClientConfig
{
User
:
"ubuntu"
,
Auth
:
[]
gossh
.
ClientAuth
{
gossh
.
ClientAuthKeyring
(
keyring
),
},
}
// Try to connect for SSH a few times
ui
.
Say
(
"Connecting to the instance via SSH..."
)
for
i
:=
0
;
i
<
5
;
i
++
{
time
.
Sleep
(
time
.
Duration
(
i
)
*
time
.
Second
)
log
.
Printf
(
"Opening TCP conn for SSH to %s:22 (attempt %d)"
,
instance
.
DNSName
,
i
+
1
)
s
.
conn
,
err
=
net
.
Dial
(
"tcp"
,
fmt
.
Sprintf
(
"%s:22"
,
instance
.
DNSName
))
if
err
!=
nil
{
continue
}
}
var
comm
packer
.
Communicator
if
err
==
nil
{
comm
,
err
=
ssh
.
New
(
s
.
conn
,
sshConfig
)
}
if
err
!=
nil
{
ui
.
Error
(
"Error connecting to SSH: %s"
,
err
.
Error
())
return
StepHalt
}
// Set the communicator on the state bag so it can be used later
state
[
"communicator"
]
=
comm
return
StepContinue
}
func
(
s
*
stepConnectSSH
)
Cleanup
(
map
[
string
]
interface
{})
{
if
s
.
conn
!=
nil
{
s
.
conn
.
Close
()
}
}
builder/amazonebs/step_create_ami.go
0 → 100644
View file @
3a97bae0
package
amazonebs
import
(
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/packer/packer"
)
type
stepCreateAMI
struct
{}
func
(
s
*
stepCreateAMI
)
Run
(
state
map
[
string
]
interface
{})
StepAction
{
config
:=
state
[
"config"
]
.
(
config
)
ec2conn
:=
state
[
"ec2"
]
.
(
*
ec2
.
EC2
)
instance
:=
state
[
"instance"
]
.
(
*
ec2
.
Instance
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
// Create the image
ui
.
Say
(
"Creating the AMI..."
)
createOpts
:=
&
ec2
.
CreateImage
{
InstanceId
:
instance
.
InstanceId
,
Name
:
config
.
AMIName
,
}
createResp
,
err
:=
ec2conn
.
CreateImage
(
createOpts
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
StepHalt
}
ui
.
Say
(
"AMI: %s"
,
createResp
.
ImageId
)
// Wait for the image to become ready
ui
.
Say
(
"Waiting for AMI to become ready..."
)
for
{
imageResp
,
err
:=
ec2conn
.
Images
([]
string
{
createResp
.
ImageId
},
ec2
.
NewFilter
())
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
StepHalt
}
if
imageResp
.
Images
[
0
]
.
State
==
"available"
{
break
}
}
return
StepContinue
}
func
(
s
*
stepCreateAMI
)
Cleanup
(
map
[
string
]
interface
{})
{
// No cleanup...
}
builder/amazonebs/step_keypair.go
0 → 100644
View file @
3a97bae0
package
amazonebs
import
(
"cgl.tideland.biz/identifier"
"encoding/hex"
"fmt"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/packer/packer"
"log"
)
type
stepKeyPair
struct
{
keyName
string
}
func
(
s
*
stepKeyPair
)
Run
(
state
map
[
string
]
interface
{})
StepAction
{
ec2conn
:=
state
[
"ec2"
]
.
(
*
ec2
.
EC2
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
ui
.
Say
(
"Creating temporary keypair for this instance..."
)
keyName
:=
fmt
.
Sprintf
(
"packer %s"
,
hex
.
EncodeToString
(
identifier
.
NewUUID
()
.
Raw
()))
log
.
Printf
(
"temporary keypair name: %s"
,
keyName
)
keyResp
,
err
:=
ec2conn
.
CreateKeyPair
(
keyName
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
StepHalt
}
// Set the keyname so we know to delete it later
s
.
keyName
=
keyName
// Set some state data for use in future steps
state
[
"keyPair"
]
=
keyName
state
[
"privateKey"
]
=
keyResp
.
KeyMaterial
return
StepContinue
}
func
(
s
*
stepKeyPair
)
Cleanup
(
state
map
[
string
]
interface
{})
{
// If no key name is set, then we never created it, so just return
if
s
.
keyName
==
""
{
return
}
ec2conn
:=
state
[
"ec2"
]
.
(
*
ec2
.
EC2
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
ui
.
Say
(
"Deleting temporary keypair..."
)
_
,
err
:=
ec2conn
.
DeleteKeyPair
(
s
.
keyName
)
if
err
!=
nil
{
ui
.
Error
(
"Error cleaning up keypair. Please delete the key manually: %s"
,
s
.
keyName
)
}
}
builder/amazonebs/step_run_source_instance.go
0 → 100644
View file @
3a97bae0
package
amazonebs
import
(
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/packer/packer"
"log"
)
type
stepRunSourceInstance
struct
{
instance
*
ec2
.
Instance
}
func
(
s
*
stepRunSourceInstance
)
Run
(
state
map
[
string
]
interface
{})
StepAction
{
config
:=
state
[
"config"
]
.
(
config
)
ec2conn
:=
state
[
"ec2"
]
.
(
*
ec2
.
EC2
)
keyName
:=
state
[
"keyPair"
]
.
(
string
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
runOpts
:=
&
ec2
.
RunInstances
{
KeyName
:
keyName
,
ImageId
:
config
.
SourceAmi
,
InstanceType
:
config
.
InstanceType
,
MinCount
:
0
,
MaxCount
:
0
,
}
ui
.
Say
(
"Launching a source AWS instance..."
)
runResp
,
err
:=
ec2conn
.
RunInstances
(
runOpts
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
StepHalt
}
s
.
instance
=
&
runResp
.
Instances
[
0
]
log
.
Printf
(
"instance id: %s"
,
s
.
instance
.
InstanceId
)
ui
.
Say
(
"Waiting for instance to become ready..."
)
s
.
instance
,
err
=
waitForState
(
ec2conn
,
s
.
instance
,
"running"
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
StepHalt
}
state
[
"instance"
]
=
s
.
instance
return
StepContinue
}
func
(
s
*
stepRunSourceInstance
)
Cleanup
(
state
map
[
string
]
interface
{})
{
if
s
.
instance
==
nil
{
return
}
ec2conn
:=
state
[
"ec2"
]
.
(
*
ec2
.
EC2
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
// TODO(mitchellh): error handling
ui
.
Say
(
"Terminating the source AWS instance..."
)
ec2conn
.
TerminateInstances
([]
string
{
s
.
instance
.
InstanceId
})
}
builder/amazonebs/step_stop_instance.go
0 → 100644
View file @
3a97bae0
package
amazonebs
import
(
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/packer/packer"
)
type
stepStopInstance
struct
{}
func
(
s
*
stepStopInstance
)
Run
(
state
map
[
string
]
interface
{})
StepAction
{
ec2conn
:=
state
[
"ec2"
]
.
(
*
ec2
.
EC2
)
instance
:=
state
[
"instance"
]
.
(
*
ec2
.
Instance
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
// Stop the instance so we can create an AMI from it
ui
.
Say
(
"Stopping the source instance..."
)
_
,
err
:=
ec2conn
.
StopInstances
(
instance
.
InstanceId
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
StepHalt
}
// Wait for the instance to actual stop
// TODO(mitchellh): Handle diff source states, i.e. this force state sucks
ui
.
Say
(
"Waiting for the instance to stop..."
)
instance
.
State
.
Name
=
"stopping"
instance
,
err
=
waitForState
(
ec2conn
,
instance
,
"stopped"
)
if
err
!=
nil
{
ui
.
Error
(
err
.
Error
())
return
StepHalt
}
return
StepContinue
}
func
(
s
*
stepStopInstance
)
Cleanup
(
map
[
string
]
interface
{})
{
// No cleanup...
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment