Commit f8e72e10 authored by Swann Perarnau's avatar Swann Perarnau
Browse files

[refactor] Isolate Popen, no recursive ansible

Recursive ansible was causing all kinds of problems, especially with IO.
Instead, we use ssh to launch a second round of ansible.

Instead of duplicating the popen handling, we then move it to a new
function.
parent 46f6e639
...@@ -35,6 +35,29 @@ def get_blazar_client(region=None): ...@@ -35,6 +35,29 @@ def get_blazar_client(region=None):
region_name=region) region_name=region)
def execute_cmd(args, env_update={}, prefix=''):
"""Execute a command and read stdout and stderr on the fly."""
print("Executing:", " ".join(args))
newenv = os.environ
newenv.update(env_update)
proc = subprocess.Popen(args, env=newenv, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
while True:
err = proc.poll()
if err is None:
line = proc.stdout.readline()
if line:
print(prefix, line, end='', flush=True)
else:
if err == 0:
print(prefix, "Command successful.")
else:
for l in proc.stderr.readlines():
print(l, end='', flush=True)
print(prefix, "Command failed.")
break
def do_create(argv): def do_create(argv):
"""Create an appliance inside a lease, based on template.""" """Create an appliance inside a lease, based on template."""
shade_client = get_shade_client(argv.region) shade_client = get_shade_client(argv.region)
...@@ -174,14 +197,7 @@ def do_configure(argv): ...@@ -174,14 +197,7 @@ def do_configure(argv):
' - name: Copy ansible configuration to the frontend\n' ' - name: Copy ansible configuration to the frontend\n'
' synchronize:\n' ' synchronize:\n'
' src: ' + confpath + '\n' ' src: ' + confpath + '\n'
' dest: ~/\n' ' dest: ~/\n')
' - name: Execute ansible on frontend\n'
' shell: ANSIBLE_HOST_KEY_CHECKING=False'
' ansible-playbook -i inventory.yaml ' + playpath + '\n'
' args:\n'
' chdir: ~/ansible\n'
' register: config\n'
' - debug: var=config.stdout_lines')
# generate files # generate files
remote_inv_path = os.path.join(confpath, "inventory.yaml") remote_inv_path = os.path.join(confpath, "inventory.yaml")
local_temp = NamedTemporaryFile(mode='w+', encoding='utf8', delete=False) local_temp = NamedTemporaryFile(mode='w+', encoding='utf8', delete=False)
...@@ -193,28 +209,23 @@ def do_configure(argv): ...@@ -193,28 +209,23 @@ def do_configure(argv):
yaml.dump(local_inventory, stream=local_temp) yaml.dump(local_inventory, stream=local_temp)
play_temp.write(playbook) play_temp.write(playbook)
# call the local ansible
try: try:
# call ansible cmd = ["ansible-playbook", "-i", local_temp.name, play_temp.name]
aargs = ["ansible-playbook", "-i", local_temp.name, play_temp.name] execute_cmd(cmd, {'ANSIBLE_HOST_KEY_CHECKING': 'False'}, 'localhost:')
proc = subprocess.Popen(aargs, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
while True:
err = proc.poll()
if err is None:
print(proc.stdout.readline(), end='', flush=True)
else:
if err == 0:
print("Configuration successful.")
else:
for l in proc.stderr.readlines():
print(l, end='', flush=True)
print("Configuration failed.")
break
finally: finally:
os.unlink(local_temp.name) os.unlink(local_temp.name)
os.unlink(play_temp.name) os.unlink(play_temp.name)
# call the remote ansible
remote_cmd = ('cd ansible;'
'ANSIBLE_HOST_KEY_CHECKING=False '
'ansible-playbook -i inventory.yaml ' + playpath)
# forward agent, auto accept host, don't save it in the host file
cmd = ["ssh", "-A", "-oUserKnownHostsFile=/dev/null",
"-oStrictHostKeyChecking=no", "cc@" + public_ip, remote_cmd]
execute_cmd(cmd, prefix='frontend:')
def main(): def main():
parser = argparse.ArgumentParser(description='Chameleon Appliance Helper') parser = argparse.ArgumentParser(description='Chameleon Appliance Helper')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment