Skip to content

Commit

Permalink
feat: working scrolling ansible output
Browse files Browse the repository at this point in the history
  • Loading branch information
amunchet committed Nov 7, 2024
1 parent 8e53fe0 commit 28936fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
36 changes: 17 additions & 19 deletions backend/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,10 +1200,6 @@ def run_ansible(inp_data=""): # pragma: no cover
else: # pragma: no cover
return "Invalid data", 481

print("DDDD")
print(data)
print(request.data)
print("DDDD")
data = json.loads(data)
if (
"hosts" not in data
Expand All @@ -1230,6 +1226,7 @@ def run_ansible(inp_data=""): # pragma: no cover
private_data_dir=RUN_DIR,
playbook="{}.yml".format(playbook),
cmdline="-vvvvv --vault-password-file ../vault.pass",
quiet=True
)
except Exception as e:
# Delete Vault Password
Expand All @@ -1242,21 +1239,22 @@ def run_ansible(inp_data=""): # pragma: no cover
return f"Error: {e}", 200

def ansible_stream():
while thread.is_alive():
line = runner.stdout.readline()
if line:
yield line
else:
time.sleep(0.1) # Prevent busy waiting


if os.path.exists("/vault.pass"):
os.remove("/vault.pass")

# Delete all files
shutil.rmtree(RUN_DIR)

return Response(ansible_stream(), mimetype='text/plain')
try:
while thread.is_alive():
try:
for event in runner.events:
yield ("<div>" + str(event["stdout"]) + "</div>").encode("utf-8")
time.sleep(0.1)
except Exception as e:
yield f"Error: {e}".encode("utf-8")
finally:
if os.path.exists("/vault.pass"):
os.remove("/vault.pass")

# Delete all files
shutil.rmtree(RUN_DIR)

return ansible_stream(), {"Content-Type" : "text/plain"}


@app.route("/mac/<old_mac>/<new_mac>/")
Expand Down
2 changes: 1 addition & 1 deletion frontend/labyrinth/src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default {
Email: profile,
};

if (isUpload !== undefined) {
if (isUpload == true) {
headers["Content-Type"] = "multipart/form-data";
}

Expand Down
22 changes: 14 additions & 8 deletions frontend/labyrinth/src/views/Deploy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,9 @@
>Deploy to host<span v-if="ips.length != 0">s</span></b-button
>
<hr />
AAAA:
{{playbook_result}}
<div
class="playbook_result mb-4"
ref="playbookResultDiv"
v-html="$sanitize(playbook_result)"
v-if="running && playbook_result && playbook_loaded && ips.length == 0"
></div>
Expand Down Expand Up @@ -799,27 +798,30 @@ export default {
);
// Handle the streaming response
console.log(response)
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
let result = "";
let truth = 1
let truth = 1;
// Read the response stream
while (truth) {
const { done, value } = await reader.read();
if (done) break; // End of stream
result += decoder.decode(value, { stream: true });
this.playbook_result = result; // Update the result
this.$nextTick(() => {
// Scroll to the bottom of the div
const div = this.$refs.playbookResultDiv;
if (div) {
div.scrollTop = div.scrollHeight;
}
});
this.$forceUpdate(); // Re-render the component
}
} catch (error) {
// Handle any errors
console.log(error)
console.log(error);
this.$store.commit("updateError", error);
} finally {
this.running = false;
this.playbook_loaded = true;
}
},
Expand Down Expand Up @@ -959,6 +961,10 @@ export default {
padding: 1rem;
}
.playbook_result div{
margin-top: 0.5rem;
margin-bottom:0.5rem;
}
.text-underline {
font-weight: bold;
}
Expand Down

0 comments on commit 28936fa

Please sign in to comment.