Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

add vpc-id and zone options for ec2 #98

Merged
merged 3 commits into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use vpc-id and zone instead of subnet-id for ec2
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
  • Loading branch information
ehazlett committed Dec 16, 2014
commit 888181245ed282bf4d88801a08bf185d8e83d5ac
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Options:

### Amazon EC2

Create machines on [Amazon Web Services](http://aws.amazon.com). You will need an Access Key ID, Secret Access Key and Subnet ID. To find the Subnet ID, login to the AWS console and go to Services -> VPC -> Subnets. Select the one where you would like to launch the instance.
Create machines on [Amazon Web Services](http://aws.amazon.com). You will need an Access Key ID, Secret Access Key and a VPC ID. To find the VPC ID, login to the AWS console and go to Services -> VPC -> Your VPCs. Select the one where you would like to launch the instance.

Options:

Expand All @@ -105,7 +105,8 @@ Options:
- `--amazonec2-region`: The region to use when launching the instance. Default: `us-east-1`
- `--amazonec2-root-size`: The root disk size of the instance (in GB). Default: `16`
- `--amazonec2-secret-key`: Your secret access key for the Amazon Web Services API.
- `--amazonec2-subnet-id`: Your VPC subnet ID to launch the instance in.
- `--amazonec2-vpc-id`: Your VPC ID to launch the instance in.
- `--amazonec2-zone`: The AWS zone launch the instance in (i.e. one of a,b,c,d,e).

## Contributing

Expand Down
60 changes: 42 additions & 18 deletions drivers/amazonec2/amazonec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type Driver struct {
InstanceId string
InstanceType string
IPAddress string
SubnetId string
SecurityGroupId string
ReservationId string
RootSize int64
VpcId string
Zone string
storePath string
keyPath string
}
Expand Down Expand Up @@ -89,9 +89,16 @@ func GetCreateFlags() []cli.Flag {
Value: defaultRegion,
},
cli.StringFlag{
Name: "amazonec2-subnet-id",
Usage: "AWS VPC subnet id",
Value: "",
Name: "amazonec2-vpc-id",
Usage: "AWS VPC id",
Value: "",
EnvVar: "AMAZONEC2_VPC_ID",
},
cli.StringFlag{
Name: "amazonec2-zone",
Usage: "AWS zone for instance (i.e. a,b,c,d,e)",
Value: "",
EnvVar: "AMAZONEC2_ZONE",
},
cli.StringFlag{
Name: "amazonec2-instance-type",
Expand All @@ -117,7 +124,9 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.AMI = flags.String("amazonec2-ami")
d.Region = flags.String("amazonec2-region")
d.InstanceType = flags.String("amazonec2-instance-type")
d.SubnetId = flags.String("amazonec2-subnet-id")
d.VpcId = flags.String("amazonec2-vpc-id")
zone := flags.String("amazonec2-zone")
d.Zone = zone[:]
d.RootSize = int64(flags.Int("amazonec2-root-size"))

if d.AccessKey == "" {
Expand All @@ -128,8 +137,12 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return fmt.Errorf("amazonec2 driver requires the --amazonec2-secret-key option")
}

if d.SubnetId == "" {
return fmt.Errorf("amazonec2 driver requires the --amazonec2-subnet-id option")
if d.VpcId == "" {
return fmt.Errorf("amazonec2 driver requires the --amazonec2-vpc-id option")
}

if d.Zone == "" {
return fmt.Errorf("amazonec2 driver requires the --amazonec2-zone option")
}

return nil
Expand Down Expand Up @@ -158,8 +171,27 @@ func (d *Driver) Create() error {
VolumeType: "gp2",
}

log.Debugf("launching instance")
instance, err := d.getClient().RunInstance(d.AMI, d.InstanceType, "a", 1, 1, group.GroupId, d.KeyName, d.SubnetId, bdm)
// get the subnet id
subnets, err := d.getClient().GetSubnets()
if err != nil {
return err
}

subnetId := ""
regionZone := d.Region + d.Zone
for _, s := range subnets {
if s.AvailabilityZone == regionZone {
subnetId = s.SubnetId
break
}
}

if subnetId == "" {
return fmt.Errorf("unable to find a subnet in the zone: %s", regionZone)
}

log.Debugf("launching instance in %s", regionZone)
instance, err := d.getClient().RunInstance(d.AMI, d.InstanceType, d.Zone, 1, 1, group.GroupId, d.KeyName, subnetId, bdm)

if err != nil {
return fmt.Errorf("Error launching instance: %s", err)
Expand Down Expand Up @@ -199,7 +231,7 @@ func (d *Driver) Create() error {

log.Debugf("HACK: Downloading version of Docker with identity auth...")

cmd, err = d.GetSSHCommand("sudo curl -sS -o /usr/bin/docker https://bfirsh.s3.amazonaws.com/docker/docker-1.3.1-dev-identity-auth")
cmd, err = d.GetSSHCommand("sudo curl -sS -o /usr/bin/docker https://ehazlett.s3.amazonaws.com/public/docker/linux/docker-1.4.0-dev-identity")
if err != nil {
return err
}
Expand Down Expand Up @@ -456,14 +488,6 @@ func (d *Driver) terminate() error {
}

func (d *Driver) createSecurityGroup() (*amz.SecurityGroup, error) {
subnets, err := d.getClient().GetSubnets()
if err != nil {
return nil, err
}
vpcId := subnets[0].VpcId

d.VpcId = vpcId

log.Debugf("creating security group in %s", d.VpcId)

grpName := fmt.Sprintf("docker-machine-%s", d.Id)
Expand Down