Skip to content

Commit 44079bf

Browse files
committed
Added computed/sane defaults!!
Allows for computed defaults for a select list of pre-determined platforms. Currently a .kitchen.yml file of the following would work: --- driver_plugin: ec2 driver_config: # ... platforms: - name: ubuntu-10.04 - name: ubuntu-12.04 - name: ubuntu-12.10 - name: ubuntu-13.04 - name: centos-6.4 - name: debian-7.1.0 suites: - name: default run_list: - recipe[something]
1 parent 023288e commit 44079bf

File tree

3 files changed

+167
-6
lines changed

3 files changed

+167
-6
lines changed

README.md

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,46 @@ will need access to an [AWS][aws_site] account.
1818

1919
Please read the [Driver usage][driver_usage] page for more details.
2020

21+
## <a name="default-config"></a> Default Configuration
22+
23+
This driver can determine AMI and username login for a select number of
24+
platforms in each region. Currently, the following platform names are
25+
supported:
26+
27+
```ruby
28+
---
29+
platforms:
30+
- name: ubuntu-10.04
31+
- name: ubuntu-12.04
32+
- name: ubuntu-12.10
33+
- name: ubuntu-13.04
34+
- name: centos-6.4
35+
- name: debian-7.1.0
36+
```
37+
38+
This will effectively generate a configuration similar to:
39+
40+
```ruby
41+
---
42+
platforms:
43+
- name: ubuntu-10.04
44+
driver_config:
45+
image_id: ami-1ab3ce73
46+
username: ubuntu
47+
- name: ubuntu-12.04
48+
driver_config:
49+
image_id: ami-2f115c46
50+
username: ubuntu
51+
# ...
52+
- name: centos-6.4
53+
driver_config:
54+
image_id: ami-bf5021d6
55+
username: root
56+
# ...
57+
```
58+
59+
For specific default values, please consult [amis.json][amis_json].
60+
2161
## <a name="config"></a> Configuration
2262

2363
### <a name="config-az"></a> availability\_zone
@@ -30,19 +70,22 @@ The default is `"us-east-1b"`.
3070

3171
**Required** The AWS [access key id][credentials_docs] to use.
3272

33-
The default is unset, or `nil`.
73+
The default will be read from the `AWS_ACCESS_KEY` environment variable if set,
74+
or `nil` otherwise.
3475

3576
### <a name="config-aws-secret-access-key"></a> aws\_secret\_access\_key
3677

3778
**Required** The AWS [secret access key][credentials_docs] to use.
3879

39-
The default is unset, or `nil`.
80+
The default will be read from the `AWS_SECRET_KEY` environment variable if set,
81+
or `nil` otherwise.
4082

4183
### <a name="config-aws-ssh-key-id"></a> aws\_ssh\_key\_id
4284

4385
**Required** The EC2 [SSH key id][key_id_docs] to use.
4486

45-
The default is unset, or `nil`.
87+
The default will be read from the `AWS_SSH_KEY_ID` environment variable if set,
88+
or `nil` otherwise.
4689

4790
### <a name="config-flavor-id"></a> flavor\_id
4891

@@ -61,7 +104,9 @@ The default is `["default"]`.
61104

62105
**Required** The EC2 [AMI id][ami_docs] to use.
63106

64-
The default is unset, or `nil`.
107+
The default will be determined by the `aws_region` chosen and the Platform
108+
name, if a default exists (see [amis.json][ami_json]). If a default cannot be
109+
computed, then the default is `nil`.
65110

66111
### <a name="config-port"></a> port
67112

@@ -120,7 +165,9 @@ The default is `{ "created-by" => "test-kitchen" }`.
120165

121166
The SSH username that will be used to communicate with the instance.
122167

123-
The default is `"root"`.
168+
The default will be determined by the Platform name, if a default exists (see
169+
[amis.json][amis_json]). If a default cannot be computed, then the default is
170+
`"root"`.
124171

125172
## <a name="example"></a> Example
126173

@@ -214,6 +261,7 @@ Apache 2.0 (see [LICENSE][license])
214261
[driver_usage]: http://docs.kitchen-ci.org/drivers/usage
215262
[chef_omnibus_dl]: http://www.opscode.com/chef/install/
216263

264+
[amis_json]: https://github.com/opscode/kitchen-ec2/blob/master/data/amis.json
217265
[ami_docs]: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ComponentsAMIs.html
218266
[aws_site]: http://aws.amazon.com/
219267
[credentials_docs]: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/SettingUp_CommandLine.html#using-credentials-access-key

data/amis.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"regions": {
3+
"ap-northeast-1": {
4+
"ubuntu-10.04": "ami-8326b382",
5+
"ubuntu-12.04": "ami-bb9a08ba",
6+
"ubuntu-12.10": "ami-4556c544",
7+
"ubuntu-13.04": "ami-97099a96",
8+
"centos-6.4": "ami-9ffa709e",
9+
"debian-7.1.0": "ami-f1f064f0"
10+
},
11+
"ap-southeast-1": {
12+
"ubuntu-10.04": "ami-34713866",
13+
"ubuntu-12.04": "ami-881c57da",
14+
"ubuntu-12.10": "ami-3ce0a86e",
15+
"ubuntu-13.04": "ami-4af5bd18",
16+
"centos-6.4": "ami-46f5bb14",
17+
"debian-7.1.0": "ami-fe8ac3ac"
18+
},
19+
"ap-southeast-2": {
20+
"ubuntu-10.04": "ami-2f009315",
21+
"ubuntu-12.04": "ami-cb26b4f1",
22+
"ubuntu-12.10": "ami-1703912d",
23+
"ubuntu-13.04": "ami-8f32a0b5",
24+
"centos-6.4": "ami-9352c1a9",
25+
"debian-7.1.0": "ami-4e099a74"
26+
},
27+
"eu-west-1": {
28+
"ubuntu-10.04": "ami-bbadb0cf",
29+
"ubuntu-12.04": "ami-d3b5aea7",
30+
"ubuntu-12.10": "ami-6b62791f",
31+
"ubuntu-13.04": "ami-6fd4cf1b",
32+
"centos-6.4": "ami-75190b01",
33+
"debian-7.1.0": "ami-954559e1"
34+
},
35+
"sa-east-1": {
36+
"ubuntu-10.04": "ami-962c898b",
37+
"ubuntu-12.04": "ami-c905a1d4",
38+
"ubuntu-12.10": "ami-e759fdfa",
39+
"ubuntu-13.04": "ami-4b2b8f56",
40+
"centos-6.4": "ami-a665c0bb",
41+
"debian-7.1.0": "ami-b03590ad"
42+
},
43+
"us-east-1": {
44+
"ubuntu-10.04": "ami-1ab3ce73",
45+
"ubuntu-12.04": "ami-2f115c46",
46+
"ubuntu-12.10": "ami-4d5b1824",
47+
"ubuntu-13.04": "ami-a73371ce",
48+
"centos-6.4": "ami-bf5021d6",
49+
"debian-7.1.0": "ami-50d9a439"
50+
},
51+
"us-west-1": {
52+
"ubuntu-10.04": "ami-848ba2c1",
53+
"ubuntu-12.04": "ami-eaf0daaf",
54+
"ubuntu-12.10": "ami-02220847",
55+
"ubuntu-13.04": "ami-fe052fbb",
56+
"centos-6.4": "ami-5d456c18",
57+
"debian-7.1.0": "ami-1a9bb25f"
58+
},
59+
"us-west-2": {
60+
"ubuntu-10.04": "ami-f19407c1",
61+
"ubuntu-12.04": "ami-e6f36fd6",
62+
"ubuntu-12.10": "ami-0c069b3c",
63+
"ubuntu-13.04": "ami-4ade427a",
64+
"centos-6.4": "ami-b3bf2f83",
65+
"debian-7.1.0": "ami-158a1925"
66+
}
67+
},
68+
"usernames": {
69+
"ubuntu-10.04": "ubuntu",
70+
"ubuntu-12.04": "ubuntu",
71+
"ubuntu-12.10": "ubuntu",
72+
"ubuntu-13.04": "ubuntu",
73+
"centos-6.4": "root",
74+
"debian-7.1.0": "admin"
75+
},
76+
"references": {
77+
"ubuntu": "http://uec-images.ubuntu.com/query/",
78+
"debian": "https://wiki.debian.org/Cloud/AmazonEC2Image",
79+
"centos": "http://wiki.centos.org/Cloud/AWS"
80+
}
81+
}

lib/kitchen/driver/ec2.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# limitations under the License.
1818

1919
require 'benchmark'
20+
require 'json'
2021
require 'fog'
2122

2223
require 'kitchen'
@@ -35,7 +36,21 @@ class Ec2 < Kitchen::Driver::SSHBase
3536
default_config :flavor_id, 'm1.small'
3637
default_config :groups, ['default']
3738
default_config :tags, { 'created-by' => 'test-kitchen' }
38-
default_config :username, 'root'
39+
default_config :aws_access_key_id do |driver|
40+
ENV['AWS_ACCESS_KEY']
41+
end
42+
default_config :aws_secret_access_key do |driver|
43+
ENV['AWS_SECRET_KEY']
44+
end
45+
default_config :aws_ssh_key_id do |driver|
46+
ENV['AWS_SSH_KEY_ID']
47+
end
48+
default_config :image_id do |driver|
49+
driver.default_ami
50+
end
51+
default_config :username do |driver|
52+
driver.default_username
53+
end
3954

4055
required_config :aws_access_key_id
4156
required_config :aws_secret_access_key
@@ -65,6 +80,15 @@ def destroy(state)
6580
state.delete(:hostname)
6681
end
6782

83+
def default_ami
84+
region = amis["regions"][config[:region]]
85+
region && region[instance.platform.name]
86+
end
87+
88+
def default_username
89+
amis["usernames"][instance.platform.name] || "root"
90+
end
91+
6892
private
6993

7094
def connection
@@ -100,6 +124,14 @@ def debug_server_config
100124
debug("ec2:key_name '#{config[:aws_ssh_key_id]}'")
101125
debug("ec2:subnet_id '#{config[:subnet_id]}'")
102126
end
127+
128+
def amis
129+
@amis ||= begin
130+
json_file = File.join(File.dirname(__FILE__),
131+
%w{.. .. .. data amis.json})
132+
JSON.load(IO.read(json_file))
133+
end
134+
end
103135
end
104136
end
105137
end

0 commit comments

Comments
 (0)