From 334715f12a72887f93d9414353716e58ecfe1ebe Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Thu, 15 Sep 2016 14:09:20 -0700 Subject: [PATCH] launch: Use popen rather than check_output This way we are able to stream the output from commands as they are received for better debugging. We can also move some new debug statements to inside of the new run() function so they are more automatic. Change-Id: I484f5cf70aa15923ea4bb866f3be536b2e8ed4ed --- launch/launch-node.py | 47 +++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/launch/launch-node.py b/launch/launch-node.py index 61b9d5315a..c88a302206 100755 --- a/launch/launch-node.py +++ b/launch/launch-node.py @@ -68,6 +68,23 @@ class JobDir(object): shutil.rmtree(self.root) +def run(cmd, **args): + args['stdout'] = subprocess.PIPE + args['stderr'] = subprocess.STDOUT + print "Running: %s" % (cmd,) + proc = subprocess.Popen(cmd, **args) + out = '' + for line in iter(proc.stdout.readline, ''): + sys.stdout.write(line) + sys.stdout.flush() + out += line + ret = proc.wait() + print "Return code: %s" % (ret,) + if ret != 0: + raise subprocess.CalledProcessError(ret, cmd, out) + return ret + + def bootstrap_server(server, key, name, volume_device, keep, mount_path, fs_label): @@ -137,20 +154,16 @@ def bootstrap_server(server, key, name, volume_device, keep, # Regenerate inventory cache, throwing an error if there is an issue # so that we don't generate a bogus groups file try: - subprocess.check_output( - ['/etc/ansible/hosts/openstack', '--list'], + run(['/etc/ansible/hosts/openstack', '--list'], env=expand_env) except subprocess.CalledProcessError as e: print "Inventory regeneration failed" print e.output raise - print "--- Running /usr/local/bin/expand-groups.sh ---" - print subprocess.check_output( - '/usr/local/bin/expand-groups.sh', + run('/usr/local/bin/expand-groups.sh', env=expand_env, stderr=subprocess.STDOUT) - print "--- done ---\n" # Write out the private SSH key we generated with open(jobdir.key, 'w') as key_file: @@ -176,22 +189,12 @@ def bootstrap_server(server, key, name, volume_device, keep, # Run the remote puppet apply playbook limited to just this server # we just created - try: - for playbook in [ - 'set_hostnames.yml', - 'remote_puppet_adhoc.yaml']: - print "--- Running : %s ---" % (ansible_cmd) - print subprocess.check_output( - ansible_cmd + [ - os.path.join( - SCRIPT_DIR, '..', 'playbooks', playbook)], - stderr=subprocess.STDOUT, - env=jobdir.env) - print "--- done ---\n" - except subprocess.CalledProcessError as e: - print "Subprocess failed" - print e.output - raise + for playbook in [ + 'set_hostnames.yml', + 'remote_puppet_adhoc.yaml']: + run(ansible_cmd + [ + os.path.join(SCRIPT_DIR, '..', 'playbooks', playbook)], + env=jobdir.env) try: ssh_client.ssh("reboot")