Add --amazonec2-private-address-only

Signed-off-by: Simon Thulbourn <simon+github@thulbourn.com>
This commit is contained in:
Simon Thulbourn 2015-04-28 16:10:37 +01:00
parent c247c7ee9b
commit d0651d0273
4 changed files with 19 additions and 3 deletions

View File

@ -739,6 +739,7 @@ Options:
- `--amazonec2-subnet-id`: AWS VPC subnet id - `--amazonec2-subnet-id`: AWS VPC subnet id
- `--amazonec2-vpc-id`: **required** Your VPC ID to launch the instance in. - `--amazonec2-vpc-id`: **required** 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). Default: `a` - `--amazonec2-zone`: The AWS zone launch the instance in (i.e. one of a,b,c,d,e). Default: `a`
- `--amazonec2-private-address-only`: Use the private IP address only
By default, the Amazon EC2 driver will use a daily image of Ubuntu 14.04 LTS. By default, the Amazon EC2 driver will use a daily image of Ubuntu 14.04 LTS.

View File

@ -69,6 +69,7 @@ type Driver struct {
keyPath string keyPath string
RequestSpotInstance bool RequestSpotInstance bool
SpotPrice string SpotPrice string
PrivateIPOnly bool
} }
func init() { func init() {
@ -164,6 +165,10 @@ func GetCreateFlags() []cli.Flag {
Usage: "AWS spot instance bid price (in dollar)", Usage: "AWS spot instance bid price (in dollar)",
Value: "0.50", Value: "0.50",
}, },
cli.BoolFlag{
Name: "amazonec2-private-address-only",
Usage: "Only use a private IP address",
},
} }
} }
@ -221,6 +226,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.SwarmDiscovery = flags.String("swarm-discovery") d.SwarmDiscovery = flags.String("swarm-discovery")
d.SSHUser = flags.String("amazonec2-ssh-user") d.SSHUser = flags.String("amazonec2-ssh-user")
d.SSHPort = 22 d.SSHPort = 22
d.PrivateIPOnly = flags.Bool("amazonec2-private-address-only")
if d.AccessKey == "" { if d.AccessKey == "" {
return fmt.Errorf("amazonec2 driver requires the --amazonec2-access-key option") return fmt.Errorf("amazonec2 driver requires the --amazonec2-access-key option")
@ -372,7 +378,7 @@ func (d *Driver) Create() error {
return fmt.Errorf("Error get instance: %s", err) return fmt.Errorf("Error get instance: %s", err)
} }
} else { } else {
inst, err := d.getClient().RunInstance(d.AMI, d.InstanceType, d.Zone, 1, 1, d.SecurityGroupId, d.KeyName, d.SubnetId, bdm, d.IamInstanceProfile) inst, err := d.getClient().RunInstance(d.AMI, d.InstanceType, d.Zone, 1, 1, d.SecurityGroupId, d.KeyName, d.SubnetId, bdm, d.IamInstanceProfile, d.PrivateIPOnly)
if err != nil { if err != nil {
return fmt.Errorf("Error launching instance: %s", err) return fmt.Errorf("Error launching instance: %s", err)
} }
@ -427,6 +433,10 @@ func (d *Driver) GetIP() (string, error) {
return "", err return "", err
} }
if d.PrivateIPOnly {
return inst.PrivateIpAddress, nil
}
return inst.IpAddress, nil return inst.IpAddress, nil
} }

View File

@ -80,6 +80,7 @@ func getDefaultTestDriverFlags() *DriverOptionsMock {
"amazonec2-ssh-user": "ubuntu", "amazonec2-ssh-user": "ubuntu",
"amazonec2-request-spot-instance": false, "amazonec2-request-spot-instance": false,
"amazonec2-spot-price": "", "amazonec2-spot-price": "",
"amazonec2-private-address-only": false,
}, },
} }
} }

View File

@ -173,7 +173,7 @@ func (e *EC2) awsApiCall(v url.Values) (*http.Response, error) {
return resp, nil return resp, nil
} }
func (e *EC2) RunInstance(amiId string, instanceType string, zone string, minCount int, maxCount int, securityGroup string, keyName string, subnetId string, bdm *BlockDeviceMapping, role string) (EC2Instance, error) { func (e *EC2) RunInstance(amiId string, instanceType string, zone string, minCount int, maxCount int, securityGroup string, keyName string, subnetId string, bdm *BlockDeviceMapping, role string, privateIPOnly bool) (EC2Instance, error) {
instance := Instance{} instance := Instance{}
v := url.Values{} v := url.Values{}
v.Set("Action", "RunInstances") v.Set("Action", "RunInstances")
@ -186,7 +186,11 @@ func (e *EC2) RunInstance(amiId string, instanceType string, zone string, minCou
v.Set("NetworkInterface.0.DeviceIndex", "0") v.Set("NetworkInterface.0.DeviceIndex", "0")
v.Set("NetworkInterface.0.SecurityGroupId.0", securityGroup) v.Set("NetworkInterface.0.SecurityGroupId.0", securityGroup)
v.Set("NetworkInterface.0.SubnetId", subnetId) v.Set("NetworkInterface.0.SubnetId", subnetId)
if privateIPOnly {
v.Set("NetworkInterface.0.AssociatePublicIpAddress", "0")
} else {
v.Set("NetworkInterface.0.AssociatePublicIpAddress", "1") v.Set("NetworkInterface.0.AssociatePublicIpAddress", "1")
}
if len(role) > 0 { if len(role) > 0 {
v.Set("IamInstanceProfile.Name", role) v.Set("IamInstanceProfile.Name", role)