deb-heat/heat/cloudinit/part_handler.py
Zane Bitter 95ec13c572 Fix cloud-init Python syntax for Python < 2.6
The loguserdata.py file gets uploaded to the servers created by Heat to run
under cloud-init. Since the default versions of Python installed on the
user's server may be very old (e.g. RHEL 5 defaults to Python 2.4), avoid
using the octal syntax introduced for Python 3.0 and backported only as far
as Python 2.6. (Also avoid the old syntax, which will break on Python 3.x.)

Also remove use of the "with" statement from loguserdata.py and
part-handler.py. This statement is only available from Python 2.6 on (or in
Python 2.5 via "from __future__ import with_statement").

Finally, remove use of the "except ExceptionType as value" syntax for
catching exceptions. Again, this was only backported to Python 2.6.

Change-Id: I89e86d00993d51e2514b1e589503c6d966909403
Partial-Bug: #1375864
2014-10-01 15:16:45 -04:00

58 lines
1.6 KiB
Python

#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
import sys
def list_types():
return(["text/x-cfninitdata"])
def handle_part(data, ctype, filename, payload):
if ctype == "__begin__":
try:
os.makedirs('/var/lib/heat-cfntools', int("700", 8))
except OSError:
ex_type, e, tb = sys.exc_info()
if e.errno != errno.EEXIST:
raise
return
if ctype == "__end__":
return
log = open('/var/log/part-handler.log', 'a')
try:
timestamp = datetime.datetime.now()
log.write('%s filename:%s, ctype:%s\n' % (timestamp, filename, ctype))
finally:
log.close()
if ctype == 'text/x-cfninitdata':
f = open('/var/lib/heat-cfntools/%s' % filename, 'w')
try:
f.write(payload)
finally:
f.close()
# TODO(sdake) hopefully temporary until users move to heat-cfntools-1.3
f = open('/var/lib/cloud/data/%s' % filename, 'w')
try:
f.write(payload)
finally:
f.close()