Skip to content

Instantly share code, notes, and snippets.

@richardraseley
Created February 9, 2015 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardraseley/5365787f560c5832ebb7 to your computer and use it in GitHub Desktop.
Save richardraseley/5365787f560c5832ebb7 to your computer and use it in GitHub Desktop.
Content-Type: multipart/mixed; boundary="===============8213883842430285090=="
MIME-Version: 1.0
--===============8213883842430285090==
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config"
user: ec2-user
# Capture all subprocess output into a logfile
# Useful for troubleshooting cloud-init issues
output: {all: '| tee -a /var/log/cloud-init-output.log'}
--===============8213883842430285090==
Content-Type: text/cloud-boothook; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="boothook.sh"
#!/bin/bash
# FIXME(shadower) this is a workaround for cloud-init 0.6.3 present in Ubuntu
# 12.04 LTS:
# https://bugs.launchpad.net/heat/+bug/1257410
#
# The old cloud-init doesn't create the users directly so the commands to do
# this are injected though nova_utils.py.
#
# Once we drop support for 0.6.3, we can safely remove this.
useradd -m ec2-user
echo -e 'ec2-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers
# in case heat-cfntools has been installed from package but no symlinks
# are yet in /opt/aws/bin/
cfn-create-aws-symlinks
# Do not remove - the cloud boothook should always return success
exit 0
--===============8213883842430285090==
Content-Type: text/part-handler; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="part-handler.py"
#part-handler
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import errno
import os
def list_types():
return(["text/x-cfninitdata"])
def handle_part(data, ctype, filename, payload):
if ctype == "__begin__":
try:
os.makedirs('/var/lib/heat-cfntools', 0o700)
except OSError as e:
if e.errno != errno.EEXIST:
raise
return
if ctype == "__end__":
return
with open('/var/log/part-handler.log', 'a') as log:
timestamp = datetime.datetime.now()
log.write('%s filename:%s, ctype:%s\n' % (timestamp, filename, ctype))
if ctype == 'text/x-cfninitdata':
with open('/var/lib/heat-cfntools/%s' % filename, 'w') as f:
f.write(payload)
# TODO(sdake) hopefully temporary until users move to heat-cfntools-1.3
with open('/var/lib/cloud/data/%s' % filename, 'w') as f:
f.write(payload)
--===============8213883842430285090==
Content-Type: text/x-cfninitdata; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cfn-userdata"
--===============8213883842430285090==
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="loguserdata.py"
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from distutils.version import LooseVersion
import errno
import logging
import os
import pkg_resources
import subprocess
import sys
VAR_PATH = '/var/lib/heat-cfntools'
LOG = logging.getLogger('heat-provision')
def chk_ci_version():
v = LooseVersion(pkg_resources.get_distribution('cloud-init').version)
return v >= LooseVersion('0.6.0')
def init_logging():
LOG.setLevel(logging.INFO)
LOG.addHandler(logging.StreamHandler())
fh = logging.FileHandler("/var/log/heat-provision.log")
os.chmod(fh.baseFilename, 0o600)
LOG.addHandler(fh)
def call(args):
class LogStream(object):
def write(self, data):
LOG.info(data)
LOG.info('%s\n' % ' '.join(args))
try:
ls = LogStream()
p = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
data = p.communicate()
if data:
for x in data:
ls.write(x)
except OSError as ex:
if ex.errno == errno.ENOEXEC:
LOG.error('Userdata empty or not executable: %s\n' % str(ex))
return os.EX_OK
else:
LOG.error('OS error running userdata: %s\n' % str(ex))
return os.EX_OSERR
except Exception as ex:
LOG.error('Unknown error running userdata: %s\n' % str(ex))
return os.EX_SOFTWARE
return p.returncode
def main():
if not chk_ci_version():
# pre 0.6.0 - user data executed via cloudinit, not this helper
LOG.error('Unable to log provisioning, need a newer version of'
' cloud-init\n')
return -1
userdata_path = os.path.join(VAR_PATH, 'cfn-userdata')
os.chmod(userdata_path, 0o700)
LOG.info('Provision began: %s\n' % datetime.datetime.now())
returncode = call([userdata_path])
LOG.info('Provision done: %s\n' % datetime.datetime.now())
if returncode:
return returncode
if __name__ == '__main__':
init_logging()
code = main()
if code:
LOG.error('Provision failed with exit code %s' % code)
sys.exit(code)
provision_log = os.path.join(VAR_PATH, 'provision-finished')
# touch the file so it is timestamped with when finished
with file(provision_log, 'a'):
os.utime(provision_log, None)
--===============8213883842430285090==
Content-Type: text/x-cfninitdata; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cfn-watch-server"
http://127.0.0.1:8003
--===============8213883842430285090==
Content-Type: text/x-cfninitdata; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cfn-metadata-server"
http://127.0.0.1:8000
--===============8213883842430285090==
Content-Type: text/x-cfninitdata; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cfn-boto-cfg"
[Boto]
debug = 0
is_secure = 0
https_validate_certificates = 1
cfn_region_name = heat
cfn_region_endpoint = 127.0.0.1
cloudwatch_region_name = heat
cloudwatch_region_endpoint = 127.0.0.1
--===============8213883842430285090==--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment