Skip to content

Commit 7200400

Browse files
closes #735 Add cloud-config.yaml UserData to basic EC2 example
* Assumes cloud-config.yaml is in same dir as script * Example substitutes hostname and FQDN * yaml file also uses cf-style template strings for dual usage * Modify Test runner to pass __file__ global to exec
1 parent 35bdfbf commit 7200400

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

examples/EC2InstanceSample.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
# Converted from EC2InstanceSample.template located at:
22
# http://aws.amazon.com/cloudformation/aws-cloudformation-templates/
3+
from io import open
4+
from os import path
35

46
from troposphere import Base64, FindInMap, GetAtt
57
from troposphere import Parameter, Output, Ref, Template
68
import troposphere.ec2 as ec2
79

810

11+
def get_user_data(hostname, domain):
12+
""" Open local YAML config template and substitute hostname & domain """
13+
dir_path = path.dirname(path.realpath(__file__))
14+
config_path = path.join(dir_path, 'cloud-config.yaml')
15+
with open(config_path, encoding='utf-8') as f:
16+
data = f.read()
17+
data = data.replace('${InstanceHostname}', hostname)
18+
data = data.replace('${PublicDomainName}', domain)
19+
20+
return data
21+
22+
923
template = Template()
1024

1125
keyname_param = template.add_parameter(Parameter(
@@ -31,7 +45,7 @@
3145
InstanceType="t1.micro",
3246
KeyName=Ref(keyname_param),
3347
SecurityGroups=["default"],
34-
UserData=Base64("80")
48+
UserData=Base64(get_user_data('hostname', 'hostname.example.com')),
3549
))
3650

3751
template.add_output([

examples/cloud-config.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#cloud-config
2+
hostname: ${InstanceHostname}
3+
fqdn: ${InstanceHostname}.${PublicDomainName}
4+
manage_etc_hosts: true
5+
package_update: true
6+
package_upgrade: true
7+
packages:
8+
- build-essential
9+
- curl
10+
- git
11+
runcmd:
12+
- |
13+
echo "-----BEGIN RSA PRIVATE KEY-----
14+
AAAAAAAAAAAAAAAAAAAAAAAAAA
15+
-----END RSA PRIVATE KEY-----
16+
" > /home/ubuntu/.ssh/id_rsa
17+
- chmod 600 /home/ubuntu/.ssh/id_rsa
18+
- chown ubuntu:ubuntu /home/ubuntu/.ssh/id_rsa
19+
- ssh -oStrictHostKeyChecking=no -T git@github.com
20+
ssh_authorized_keys:
21+
- ssh-rsa AAAA==
22+
- ssh-rsa AAAA==
23+
power_state:
24+
timeout: 120
25+
message: Rebooting to ensure hostname has stuck correctly and apply any kernel updates
26+
mode: reboot

tests/examples_output/EC2InstanceSample.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"default"
104104
],
105105
"UserData": {
106-
"Fn::Base64": "80"
106+
"Fn::Base64": "#cloud-config\nhostname: hostname\nfqdn: hostname.hostname.example.com\nmanage_etc_hosts: true\npackage_update: true\npackage_upgrade: true\npackages:\n - build-essential\n - curl\n - git\nruncmd:\n - |\n echo \"-----BEGIN RSA PRIVATE KEY-----\n AAAAAAAAAAAAAAAAAAAAAAAAAA\n -----END RSA PRIVATE KEY-----\n \" > /home/ubuntu/.ssh/id_rsa\n - chmod 600 /home/ubuntu/.ssh/id_rsa\n - chown ubuntu:ubuntu /home/ubuntu/.ssh/id_rsa\n - ssh -oStrictHostKeyChecking=no -T git@github.com\nssh_authorized_keys:\n - ssh-rsa AAAA==\n - ssh-rsa AAAA==\npower_state:\n timeout: 120\n message: Rebooting to ensure hostname has stuck correctly and apply any kernel updates\n mode: reboot\n"
107107
}
108108
},
109109
"Type": "AWS::EC2::Instance"

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_example(self):
2828
sys.stdout = stdout
2929
with open(self.filename) as f:
3030
code = compile(f.read(), self.filename, 'exec')
31-
exec(code, {'__name__': '__main__'})
31+
exec(code, {'__name__': '__main__', '__file__': self.filename})
3232
finally:
3333
sys.stdout = saved
3434
# rewind fake stdout so we can read it

0 commit comments

Comments
 (0)