From 4ef088ab7c39f7eb25bce9147faf4e5d7a0604c5 Mon Sep 17 00:00:00 2001 From: ciarancourtney Date: Sat, 25 Aug 2018 10:56:51 +0100 Subject: [PATCH] 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 --- examples/EC2InstanceSample.py | 16 +++++++++++- examples/cloud-config.yaml | 26 +++++++++++++++++++ .../EC2InstanceSample.template | 2 +- tests/test_examples.py | 2 +- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 examples/cloud-config.yaml diff --git a/examples/EC2InstanceSample.py b/examples/EC2InstanceSample.py index 8c8b9b3e5..e71dca979 100644 --- a/examples/EC2InstanceSample.py +++ b/examples/EC2InstanceSample.py @@ -1,11 +1,25 @@ # Converted from EC2InstanceSample.template located at: # http://aws.amazon.com/cloudformation/aws-cloudformation-templates/ +from io import open +import os from troposphere import Base64, FindInMap, GetAtt from troposphere import Parameter, Output, Ref, Template import troposphere.ec2 as ec2 +def get_user_data(hostname, domain): + """ Open local YAML config template and substitute hostname & domain """ + dir_path = os.path.dirname(os.path.realpath(__file__)) + config_path = os.path.join(dir_path, 'cloud-config.yaml') + with open(config_path, encoding='utf-8') as f: + data = f.read() + data = data.replace('${InstanceHostname}', hostname) + data = data.replace('${PublicDomainName}', domain) + + return data + + template = Template() keyname_param = template.add_parameter(Parameter( @@ -31,7 +45,7 @@ InstanceType="t1.micro", KeyName=Ref(keyname_param), SecurityGroups=["default"], - UserData=Base64("80") + UserData=Base64(get_user_data('hostname', 'hostname.example.com')), )) template.add_output([ diff --git a/examples/cloud-config.yaml b/examples/cloud-config.yaml new file mode 100644 index 000000000..7d92bb799 --- /dev/null +++ b/examples/cloud-config.yaml @@ -0,0 +1,26 @@ +#cloud-config +hostname: ${InstanceHostname} +fqdn: ${InstanceHostname}.${PublicDomainName} +manage_etc_hosts: true +package_update: true +package_upgrade: true +packages: + - build-essential + - curl + - git +runcmd: + - | + echo "-----BEGIN RSA PRIVATE KEY----- + AAAAAAAAAAAAAAAAAAAAAAAAAA + -----END RSA PRIVATE KEY----- + " > /home/ubuntu/.ssh/id_rsa + - chmod 600 /home/ubuntu/.ssh/id_rsa + - chown ubuntu:ubuntu /home/ubuntu/.ssh/id_rsa + - ssh -oStrictHostKeyChecking=no -T git@github.com +ssh_authorized_keys: + - ssh-rsa AAAA== + - ssh-rsa AAAA== +power_state: + timeout: 120 + message: Rebooting to ensure hostname has stuck correctly and apply any kernel updates + mode: reboot diff --git a/tests/examples_output/EC2InstanceSample.template b/tests/examples_output/EC2InstanceSample.template index fae461178..1ea3dd3d0 100644 --- a/tests/examples_output/EC2InstanceSample.template +++ b/tests/examples_output/EC2InstanceSample.template @@ -103,7 +103,7 @@ "default" ], "UserData": { - "Fn::Base64": "80" + "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" } }, "Type": "AWS::EC2::Instance" diff --git a/tests/test_examples.py b/tests/test_examples.py index d760211cd..2beb82aa8 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -28,7 +28,7 @@ def test_example(self): sys.stdout = stdout with open(self.filename) as f: code = compile(f.read(), self.filename, 'exec') - exec(code, {'__name__': '__main__'}) + exec(code, {'__name__': '__main__', '__file__': self.filename}) finally: sys.stdout = saved # rewind fake stdout so we can read it