2013-05-09 17:16:39 -04:00
|
|
|
// The amazonebs package contains a packer.Builder implementation that
|
|
|
|
// builds AMIs for Amazon EC2.
|
|
|
|
//
|
|
|
|
// In general, there are two types of AMIs that can be created: ebs-backed or
|
|
|
|
// instance-store. This builder _only_ builds ebs-backed images.
|
2013-05-09 01:34:20 -04:00
|
|
|
package amazonebs
|
|
|
|
|
|
|
|
import (
|
2013-05-21 02:18:44 -04:00
|
|
|
"bufio"
|
2013-05-21 01:23:23 -04:00
|
|
|
"cgl.tideland.biz/identifier"
|
2013-05-21 02:18:44 -04:00
|
|
|
gossh "code.google.com/p/go.crypto/ssh"
|
2013-05-21 01:23:23 -04:00
|
|
|
"encoding/hex"
|
2013-05-10 18:21:11 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/aws"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2013-05-20 19:39:43 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-05-21 02:18:44 -04:00
|
|
|
"github.com/mitchellh/packer/communicator/ssh"
|
2013-05-09 01:34:20 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-05-09 16:26:40 -04:00
|
|
|
"log"
|
2013-05-21 02:18:44 -04:00
|
|
|
"net"
|
2013-05-10 18:21:11 -04:00
|
|
|
"time"
|
2013-05-09 01:34:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2013-05-20 19:50:35 -04:00
|
|
|
// Access information
|
2013-05-20 19:39:43 -04:00
|
|
|
AccessKey string `mapstructure:"access_key"`
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
2013-05-20 19:50:35 -04:00
|
|
|
|
2013-05-21 01:23:23 -04:00
|
|
|
// Information for the source instance
|
|
|
|
Region string
|
|
|
|
SourceAmi string `mapstructure:"source_ami"`
|
|
|
|
InstanceType string `mapstructure:"instance_type"`
|
2013-05-20 19:50:35 -04:00
|
|
|
|
|
|
|
// Configuration of the resulting AMI
|
|
|
|
AMIName string `mapstructure:"ami_name"`
|
2013-05-09 01:34:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config config
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:19:38 -04:00
|
|
|
func (b *Builder) Prepare(raw interface{}) (err error) {
|
2013-05-20 19:39:43 -04:00
|
|
|
err = mapstructure.Decode(raw, &b.config)
|
2013-05-09 16:19:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-21 02:43:37 -04:00
|
|
|
log.Printf("Config: %+v", b.config)
|
2013-05-10 16:01:54 -04:00
|
|
|
|
|
|
|
// TODO: Validate the configuration
|
2013-05-09 16:19:38 -04:00
|
|
|
return
|
2013-05-09 13:54:42 -04:00
|
|
|
}
|
2013-05-09 01:34:20 -04:00
|
|
|
|
2013-05-11 13:31:30 -04:00
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
|
2013-05-10 18:21:11 -04:00
|
|
|
auth := aws.Auth{b.config.AccessKey, b.config.SecretKey}
|
|
|
|
region := aws.Regions[b.config.Region]
|
|
|
|
ec2conn := ec2.New(auth, region)
|
|
|
|
|
2013-05-21 01:23:23 -04:00
|
|
|
// Create a new keypair that we'll use to access the instance.
|
|
|
|
keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw()))
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Creating temporary keypair for this instance...")
|
|
|
|
log.Printf("temporary keypair name: %s", keyName)
|
2013-05-21 02:18:44 -04:00
|
|
|
keyResp, err := ec2conn.CreateKeyPair(keyName)
|
2013-05-21 01:23:23 -04:00
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error(err.Error())
|
2013-05-21 01:23:23 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the keypair is properly deleted when we exit
|
|
|
|
defer func() {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Deleting temporary keypair...")
|
2013-05-21 01:23:23 -04:00
|
|
|
_, err := ec2conn.DeleteKeyPair(keyName)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(
|
|
|
|
"Error cleaning up keypair. Please delete the key manually: %s", keyName)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-05-10 18:21:11 -04:00
|
|
|
runOpts := &ec2.RunInstances{
|
2013-05-21 01:23:23 -04:00
|
|
|
KeyName: keyName,
|
2013-05-10 20:01:24 -04:00
|
|
|
ImageId: b.config.SourceAmi,
|
2013-05-21 01:23:23 -04:00
|
|
|
InstanceType: b.config.InstanceType,
|
2013-05-10 20:01:24 -04:00
|
|
|
MinCount: 0,
|
|
|
|
MaxCount: 0,
|
2013-05-10 18:21:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Launching a source AWS instance...")
|
2013-05-10 18:21:11 -04:00
|
|
|
runResp, err := ec2conn.RunInstances(runOpts)
|
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error(err.Error())
|
2013-05-10 18:21:11 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
instance := &runResp.Instances[0]
|
2013-05-21 02:43:37 -04:00
|
|
|
log.Printf("instance id: %s", instance.InstanceId)
|
2013-05-21 02:18:44 -04:00
|
|
|
|
|
|
|
// Make sure we clean up the instance by terminating it, no matter what
|
|
|
|
defer func() {
|
|
|
|
// TODO: error handling
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Terminating the source AWS instance...")
|
2013-05-21 02:18:44 -04:00
|
|
|
ec2conn.TerminateInstances([]string{instance.InstanceId})
|
|
|
|
}()
|
|
|
|
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Waiting for instance to become ready...")
|
2013-05-21 02:18:44 -04:00
|
|
|
instance, err = waitForState(ec2conn, instance, "running")
|
2013-05-10 18:21:11 -04:00
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error(err.Error())
|
2013-05-10 18:21:11 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-21 02:18:44 -04:00
|
|
|
// Build the SSH configuration
|
|
|
|
keyring := &ssh.SimpleKeychain{}
|
|
|
|
err = keyring.AddPEMKey(keyResp.KeyMaterial)
|
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Error setting up SSH config: %s", err.Error())
|
2013-05-21 02:18:44 -04:00
|
|
|
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 {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error("Error connecting to SSH: %s", err.Error())
|
2013-05-21 02:18:44 -04:00
|
|
|
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')
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say(line)
|
2013-05-21 02:18:44 -04:00
|
|
|
|
2013-05-10 18:21:11 -04:00
|
|
|
// Stop the instance so we can create an AMI from it
|
|
|
|
_, err = ec2conn.StopInstances(instance.InstanceId)
|
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error(err.Error())
|
2013-05-10 18:21:11 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the instance to actual stop
|
|
|
|
// TODO: Handle diff source states, i.e. this force state sucks
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Waiting for the instance to stop...")
|
2013-05-10 18:21:11 -04:00
|
|
|
instance.State.Name = "stopping"
|
2013-05-21 02:18:44 -04:00
|
|
|
instance, err = waitForState(ec2conn, instance, "stopped")
|
2013-05-10 18:21:11 -04:00
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error(err.Error())
|
2013-05-10 18:21:11 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the image
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Creating the AMI...")
|
2013-05-10 18:21:11 -04:00
|
|
|
createOpts := &ec2.CreateImage{
|
|
|
|
InstanceId: instance.InstanceId,
|
2013-05-10 20:01:24 -04:00
|
|
|
Name: b.config.AMIName,
|
2013-05-10 18:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
createResp, err := ec2conn.CreateImage(createOpts)
|
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error(err.Error())
|
2013-05-10 18:21:11 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("AMI: %s", createResp.ImageId)
|
2013-05-10 18:21:11 -04:00
|
|
|
|
2013-05-10 18:47:46 -04:00
|
|
|
// Wait for the image to become ready
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Say("Waiting for AMI to become ready...")
|
2013-05-10 18:47:46 -04:00
|
|
|
for {
|
|
|
|
imageResp, err := ec2conn.Images([]string{createResp.ImageId}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
2013-05-21 02:43:37 -04:00
|
|
|
ui.Error(err.Error())
|
2013-05-10 18:47:46 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if imageResp.Images[0].State == "available" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 18:21:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-21 02:18:44 -04:00
|
|
|
func waitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, target string) (i *ec2.Instance, err error) {
|
2013-05-21 02:43:37 -04:00
|
|
|
log.Printf("Waiting for instance state to become: %s", target)
|
2013-05-10 18:21:11 -04:00
|
|
|
|
2013-05-21 02:18:44 -04:00
|
|
|
i = originalInstance
|
2013-05-10 18:21:11 -04:00
|
|
|
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 {
|
2013-05-21 02:18:44 -04:00
|
|
|
err = fmt.Errorf("unexpected target state '%s', wanted '%s'", i.State.Name, target)
|
|
|
|
return
|
2013-05-10 18:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2013-05-10 16:01:54 -04:00
|
|
|
}
|