Skip to content

Commit

Permalink
Poll ansible subprocess (#711)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Berendt <berendt@osism.tech>
  • Loading branch information
berendt authored Nov 29, 2023
1 parent 9c6b71a commit 52267c0
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions osism/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# SPDX-License-Identifier: Apache-2.0

import io
import os
from pathlib import Path
import subprocess
Expand Down Expand Up @@ -114,19 +113,19 @@ def run_ansible_in_environment(
logger.info(f"RUN {command}")
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
env=env,
)
else:
command = f"/run.sh deploy {role} {joined_arguments}"
logger.info(f"RUN {command}")
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
env=env,
)

Expand All @@ -139,9 +138,9 @@ def run_ansible_in_environment(
logger.info(f"RUN {command}")
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
env=env,
)

Expand All @@ -155,9 +154,9 @@ def run_ansible_in_environment(
logger.info(f"RUN {command}")
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)

# execute local netbox playbooks
Expand All @@ -169,9 +168,9 @@ def run_ansible_in_environment(
logger.info(f"RUN {command}")
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)

# execute all other roles
Expand All @@ -183,9 +182,9 @@ def run_ansible_in_environment(
logger.info(f"RUN {command}")
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
env=env,
)

Expand All @@ -211,7 +210,8 @@ def run_ansible_in_environment(

# process all other results
else:
for line in io.TextIOWrapper(p.stdout, encoding="utf-8"):
while p.poll() is None:
line = p.stdout.readline().decode("utf-8")
if publish:
redis.xadd(request_id, {"type": "stdout", "content": line})
result += line
Expand Down Expand Up @@ -260,7 +260,7 @@ def handle_task(t, wait, format, timeout):
redis.xdel(str(t.task_id), last_id)

if message_type == "stdout":
print(message_content, end="")
print(message_content, end="", flush=True)
elif message_type == "rc":
rc = int(message_content)
elif message_type == "action" and message_content == "quit":
Expand Down

0 comments on commit 52267c0

Please sign in to comment.