Collect child VM servers when refreshing a single host
This commit is contained in:
32
app/app.py
32
app/app.py
@@ -447,7 +447,7 @@ def api_refresh_stream():
|
||||
|
||||
@app.route('/api/servers/<int:server_id>/refresh/stream')
|
||||
def api_refresh_one_stream(server_id):
|
||||
"""SSE endpoint: collect a single server with progress updates."""
|
||||
"""SSE endpoint: collect a single server (and its child VMs) with progress updates."""
|
||||
def generate():
|
||||
with app.app_context():
|
||||
server = Server.query.get(server_id)
|
||||
@@ -462,6 +462,15 @@ def api_refresh_one_stream(server_id):
|
||||
'hostname': server.hostname,
|
||||
'url': server.url,
|
||||
}
|
||||
# Find child VMs configured under this host
|
||||
children = Server.query.filter_by(parent_hostname=hostname).all()
|
||||
child_entries = [{
|
||||
'id': c.id,
|
||||
'group': c.group_name,
|
||||
'username': c.username,
|
||||
'hostname': c.hostname,
|
||||
'url': c.url,
|
||||
} for c in children]
|
||||
|
||||
try:
|
||||
ssh_key = load_ssh_key()
|
||||
@@ -470,6 +479,7 @@ def api_refresh_one_stream(server_id):
|
||||
yield f"data: [DONE]\n\n"
|
||||
return
|
||||
|
||||
# Collect the host
|
||||
yield f"data: Connecting to {hostname}...\n\n"
|
||||
result = collect_one(entry, ssh_key)
|
||||
|
||||
@@ -490,6 +500,26 @@ def api_refresh_one_stream(server_id):
|
||||
else:
|
||||
yield f"data: {hostname} - offline: {result.get('error', 'unknown')}\n\n"
|
||||
|
||||
# Collect child VMs
|
||||
for child_entry in child_entries:
|
||||
child_host = child_entry['hostname']
|
||||
yield f"data: Connecting to {child_host}...\n\n"
|
||||
child_result = collect_one(child_entry, ssh_key)
|
||||
|
||||
with app.app_context():
|
||||
child_server = Server.query.get(child_entry['id'])
|
||||
_update_server_from_result(child_server, child_entry, child_result)
|
||||
db.session.commit()
|
||||
|
||||
if child_result.get('is_online'):
|
||||
child_ct = len(child_result.get('container', []))
|
||||
msg = f"{child_host} - online"
|
||||
if child_ct:
|
||||
msg += f", {child_ct} containers"
|
||||
yield f"data: {msg}\n\n"
|
||||
else:
|
||||
yield f"data: {child_host} - offline: {child_result.get('error', 'unknown')}\n\n"
|
||||
|
||||
yield f"data: [DONE]\n\n"
|
||||
|
||||
return Response(stream_with_context(generate()),
|
||||
|
||||
Reference in New Issue
Block a user