loguserdata: prevent shadowing of arguments and globals

cleaning this up for bug 1154641

Change-Id: I95271bdeba1bba29e78af79de4e2173d07750e4f
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
Angus Salkeld 2013-03-14 12:33:23 +11:00
parent a492c6e04f
commit fbea931550
1 changed files with 25 additions and 23 deletions

View File

@ -1,13 +1,15 @@
#!/usr/bin/env python
import sys
import os
import subprocess
import datetime
import pkg_resources
import os
import subprocess
import sys
from distutils.version import LooseVersion
path = '/var/lib/heat-cfntools'
VAR_PATH = '/var/lib/heat-cfntools'
def chk_ci_version():
@ -15,45 +17,45 @@ def chk_ci_version():
return v >= LooseVersion('0.6.0')
def create_log(path):
fd = os.open(path, os.O_WRONLY | os.O_CREAT, 0600)
def create_log(log_path):
fd = os.open(log_path, os.O_WRONLY | os.O_CREAT, 0600)
return os.fdopen(fd, 'w')
def call(args, log):
log.write('%s\n' % ' '.join(args))
p = subprocess.Popen(args, stdout=log, stderr=log)
def call(args, logger):
logger.write('%s\n' % ' '.join(args))
p = subprocess.Popen(args, stdout=logger, stderr=logger)
return p.returncode
def main(log):
def main(logger):
if not chk_ci_version():
# pre 0.6.0 - user data executed via cloudinit, not this helper
log.write('Unable to log provisioning, need a newer version of'
' cloud-init\n')
logger.write('Unable to log provisioning, need a newer version of'
' cloud-init\n')
return -1
userdata_path = os.path.join(path, 'cfn-userdata')
userdata_path = os.path.join(VAR_PATH, 'cfn-userdata')
os.chmod(userdata_path, 0700)
log.write('Provision began: %s\n' % datetime.datetime.now())
returncode = call([userdata_path], log)
log.write('Provision done: %s\n' % datetime.datetime.now())
logger.write('Provision began: %s\n' % datetime.datetime.now())
returncode = call([userdata_path], logger)
logger.write('Provision done: %s\n' % datetime.datetime.now())
if returncode:
return returncode
if __name__ == '__main__':
with create_log('/var/log/heat-provision.log') as log:
returncode = main(log)
if returncode:
code = main(log)
if code:
log.write('Provision failed')
userdata_path = os.path.join(path, 'provision-finished')
with create_log(userdata_path) as log:
provision_log = os.path.join(VAR_PATH, 'provision-finished')
with create_log(provision_log) as log:
log.write('%s\n' % datetime.datetime.now())