This adds a method to the Driver: PreCreateCheck. This can be used
where you want to run some prerequisite checks before attempting to create the machine. In the case of EC2, this is a check for an existing keypair. This can be used in the other drivers in the future as well. Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
@@ -182,7 +182,28 @@ func (d *Driver) DriverName() string {
|
||||
return driverName
|
||||
}
|
||||
|
||||
func (d *Driver) checkPrereqs() error {
|
||||
// check for existing keypair
|
||||
key, err := d.getClient().GetKeyPair(d.MachineName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if key != nil {
|
||||
return fmt.Errorf("There is already a keypair with the name %s. Please either remove that keypair or use a different machine name.", d.MachineName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) PreCreateCheck() error {
|
||||
return d.checkPrereqs()
|
||||
}
|
||||
|
||||
func (d *Driver) Create() error {
|
||||
if err := d.checkPrereqs(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("Launching instance...")
|
||||
|
||||
if err := d.createKeyPair(); err != nil {
|
||||
|
||||
6
drivers/amazonec2/amz/describe_keypairs.go
Normal file
6
drivers/amazonec2/amz/describe_keypairs.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package amz
|
||||
|
||||
type DescribeKeyPairsResponse struct {
|
||||
RequestId string `xml:"requestId"`
|
||||
KeySet []KeyPair `xml:"keySet>item"`
|
||||
}
|
||||
@@ -438,6 +438,42 @@ func (e *EC2) GetSubnets() ([]Subnet, error) {
|
||||
return subnets, nil
|
||||
}
|
||||
|
||||
func (e *EC2) GetKeyPairs() ([]KeyPair, error) {
|
||||
keyPairs := []KeyPair{}
|
||||
resp, err := e.performStandardAction("DescribeKeyPairs")
|
||||
if err != nil {
|
||||
return keyPairs, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
contents, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return keyPairs, fmt.Errorf("Error reading AWS response body: %s", err)
|
||||
}
|
||||
|
||||
unmarshalledResponse := DescribeKeyPairsResponse{}
|
||||
if err = xml.Unmarshal(contents, &unmarshalledResponse); err != nil {
|
||||
return keyPairs, fmt.Errorf("Error unmarshalling AWS response XML: %s", err)
|
||||
}
|
||||
|
||||
keyPairs = unmarshalledResponse.KeySet
|
||||
|
||||
return keyPairs, nil
|
||||
}
|
||||
|
||||
func (e *EC2) GetKeyPair(name string) (*KeyPair, error) {
|
||||
keyPairs, err := e.GetKeyPairs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, key := range keyPairs {
|
||||
if key.KeyName == name {
|
||||
return &key, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (e *EC2) GetInstanceState(instanceId string) (state.State, error) {
|
||||
resp, err := e.performInstanceAction(instanceId, "DescribeInstances", nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -11,3 +11,8 @@ type ImportKeyPairResponse struct {
|
||||
KeyFingerprint string `xml:"keyFingerprint"`
|
||||
KeyMaterial []byte `xml:"keyMaterial"`
|
||||
}
|
||||
|
||||
type KeyPair struct {
|
||||
KeyFingerprint string `xml:"keyFingerprint"`
|
||||
KeyName string `xml:"keyName"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user