Fix path to cloudinit directory

The various path manipulations designed to search for the cloudinit
directory do not work with the current installation script, or when
heat-jeos is also installed. They also perform gratuitous manipulation of
sys.path (causing warning messages) and may pick up the cloudinit files
from a different location to the code that is actually running. Python
already knows which heat package we are using - just look for the directory
inside there.

This patch also tidies up the code for generating the user data.

Change-Id: I14add5f1670aea2562329ff1a1303320a35ff428
Signed-off-by: Zane Bitter <zbitter@redhat.com>
This commit is contained in:
Zane Bitter 2012-06-22 17:30:28 +02:00
parent fa7d89e5ed
commit 893cda4314
1 changed files with 25 additions and 40 deletions

View File

@ -22,23 +22,11 @@ from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from novaclient.exceptions import NotFound from novaclient.exceptions import NotFound
import heat
from heat.engine.resources import Resource from heat.engine.resources import Resource
from heat.common import exception from heat.common import exception
logger = logging.getLogger('heat.engine.instance') logger = logging.getLogger('heat.engine.instance')
# If ../heat/__init__.py exists, add ../ to Python search path, so that
# it will override what happens to be installed in /usr/(local/)lib/python...
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
os.pardir,
os.pardir))
if os.path.exists(os.path.join(possible_topdir, 'heat', '__init__.py')):
sys.path.insert(0, possible_topdir)
cloudinit_path = '%s/heat/%s/' % (possible_topdir, "cloudinit")
else:
for p in sys.path:
if 'heat' in p:
cloudinit_path = '%s/heat/%s/' % (p, "cloudinit")
break
class Restarter(Resource): class Restarter(Resource):
@ -168,40 +156,37 @@ class Instance(Resource):
def _build_userdata(self, userdata): def _build_userdata(self, userdata):
if not self.mime_string: if not self.mime_string:
# Build mime multipart data blob for cloudinit userdata # Build mime multipart data blob for cloudinit userdata
mime_blob = MIMEMultipart()
fp = open('%s/%s' % (cloudinit_path, 'config'), 'r')
msg = MIMEText(fp.read(), _subtype='cloud-config')
fp.close()
msg.add_header('Content-Disposition', 'attachment',
filename='cloud-config')
mime_blob.attach(msg)
fp = open('%s/%s' % (cloudinit_path, 'part-handler.py'), 'r') def make_subpart(content, filename, subtype=None):
msg = MIMEText(fp.read(), _subtype='part-handler') if subtype is None:
fp.close() subtype = os.path.splitext(filename)[0]
msg.add_header('Content-Disposition', 'attachment', msg = MIMEText(content, _subtype=subtype)
filename='part-handler.py') msg.add_header('Content-Disposition', 'attachment',
mime_blob.attach(msg) filename=filename)
return msg
def read_cloudinit_file(fn):
with open(os.path.join(heat.__path__[0], 'cloudinit', fn),
'r') as fp:
return fp.read()
attachments = [(read_cloudinit_file('config'), 'cloud-config'),
(read_cloudinit_file('part-handler.py'),
'part-handler.py'),
(userdata, 'startup', 'x-shellscript')]
if 'Metadata' in self.t: if 'Metadata' in self.t:
metadata = self.parsed_template()['Metadata'] metadata = self.parsed_template()['Metadata']
msg = MIMEText(json.dumps(metadata), attachments.append((json.dumps(metadata),
_subtype='x-cfninitdata') 'cfn-init-data', 'x-cfninitdata'))
msg.add_header('Content-Disposition', 'attachment',
filename='cfn-init-data')
mime_blob.attach(msg)
if self.stack.metadata_server: if self.stack.metadata_server:
msg = MIMEText(self.stack.metadata_server, attachments.append((self.stack.metadata_server,
_subtype='x-cfninitdata') 'cfn-metadata-server', 'x-cfninitdata'))
msg.add_header('Content-Disposition', 'attachment',
filename='cfn-metadata-server') subparts = [make_subpart(*args) for args in attachments]
mime_blob.attach(msg) mime_blob = MIMEMultipart(_subparts=subparts)
msg = MIMEText(userdata, _subtype='x-shellscript')
msg.add_header('Content-Disposition', 'attachment',
filename='startup')
mime_blob.attach(msg)
self.mime_string = mime_blob.as_string() self.mime_string = mime_blob.as_string()
return self.mime_string return self.mime_string