summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZane Bitter <zbitter@redhat.com>2014-10-01 15:16:45 -0400
committerEthan Lynn <xjunlin@cn.ibm.com>2015-06-08 11:10:55 +0800
commiteb22b015f5491e03e4a96bfa8c3b3d2d7f011ba0 (patch)
tree217e7af1d3db670ebcfaa464d41d59cda56ac606
parent4dbd3231adf474beecefa79ed35db43a6aa351d9 (diff)
downloadheat-stable/icehouse.tar.gz
Fix cloud-init Python syntax for Python < 2.6icehouse-eol2014.1.5stable/icehouse
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 (cherry picked from commit 95ec13c5725ccc19dfe910fe83eb9bc6799d482e) (cherry picked from commit 140d66e587bce27523ee7ebbeaecbced83082949)
-rwxr-xr-xheat/cloudinit/loguserdata.py15
-rw-r--r--heat/cloudinit/part_handler.py21
2 files changed, 26 insertions, 10 deletions
diff --git a/heat/cloudinit/loguserdata.py b/heat/cloudinit/loguserdata.py
index 38e443d5d..0378d757e 100755
--- a/heat/cloudinit/loguserdata.py
+++ b/heat/cloudinit/loguserdata.py
@@ -35,7 +35,7 @@ def init_logging():
LOG.setLevel(logging.INFO)
LOG.addHandler(logging.StreamHandler())
fh = logging.FileHandler("/var/log/heat-provision.log")
- os.chmod(fh.baseFilename, 0o600)
+ os.chmod(fh.baseFilename, int("600", 8))
LOG.addHandler(fh)
@@ -55,14 +55,16 @@ def call(args):
if data:
for x in data:
ls.write(x)
- except OSError as ex:
+ except OSError:
+ ex_type, ex, tb = sys.exc_info()
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:
+ except Exception:
+ ex_type, ex, tb = sys.exc_info()
LOG.error('Unknown error running userdata: %s\n' % str(ex))
return os.EX_SOFTWARE
return p.returncode
@@ -77,7 +79,7 @@ def main():
return -1
userdata_path = os.path.join(VAR_PATH, 'cfn-userdata')
- os.chmod(userdata_path, 0o700)
+ os.chmod(userdata_path, int("700", 8))
LOG.info('Provision began: %s\n' % datetime.datetime.now())
returncode = call([userdata_path])
@@ -96,5 +98,8 @@ if __name__ == '__main__':
provision_log = os.path.join(VAR_PATH, 'provision-finished')
# touch the file so it is timestamped with when finished
- with file(provision_log, 'a'):
+ pl = file(provision_log, 'a')
+ try:
os.utime(provision_log, None)
+ finally:
+ pl.close()
diff --git a/heat/cloudinit/part_handler.py b/heat/cloudinit/part_handler.py
index 64a24b470..5e8cc8461 100644
--- a/heat/cloudinit/part_handler.py
+++ b/heat/cloudinit/part_handler.py
@@ -15,6 +15,7 @@
import datetime
import errno
import os
+import sys
def list_types():
@@ -24,8 +25,9 @@ def list_types():
def handle_part(data, ctype, filename, payload):
if ctype == "__begin__":
try:
- os.makedirs('/var/lib/heat-cfntools', 0o700)
- except OSError as e:
+ 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
@@ -33,14 +35,23 @@ def handle_part(data, ctype, filename, payload):
if ctype == "__end__":
return
- with open('/var/log/part-handler.log', 'a') as log:
+ 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':
- with open('/var/lib/heat-cfntools/%s' % filename, 'w') as f:
+ 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
- with open('/var/lib/cloud/data/%s' % filename, 'w') as f:
+ f = open('/var/lib/cloud/data/%s' % filename, 'w')
+ try:
f.write(payload)
+ finally:
+ f.close()