Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,11 +7,12 @@ from openstack import exceptions
|
|
| 7 |
|
| 8 |
# GLOBAL SHARED STATE
|
| 9 |
active_user_count = 0
|
| 10 |
-
is_comfit_alive = False
|
| 11 |
clean_link = "comfit-copilot.mch250095.projects.jetstream-cloud.org:3000"
|
| 12 |
full_url = f"http://{clean_link}"
|
| 13 |
|
| 14 |
def get_conn():
|
|
|
|
| 15 |
return openstack.connect(
|
| 16 |
auth_url=os.getenv("OS_AUTH_URL"),
|
| 17 |
application_credential_id=os.getenv("OS_APPLICATION_CREDENTIAL_ID"),
|
|
@@ -22,6 +23,7 @@ def get_conn():
|
|
| 22 |
)
|
| 23 |
|
| 24 |
def get_current_status():
|
|
|
|
| 25 |
global active_user_count, is_comfit_alive
|
| 26 |
try:
|
| 27 |
conn = get_conn()
|
|
@@ -39,7 +41,7 @@ def get_current_status():
|
|
| 39 |
}
|
| 40 |
status_text = status_map.get(server.status, f"β STATE: {server.status}")
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if "RUNNING" in status_text:
|
| 44 |
try:
|
| 45 |
if requests.get(full_url, timeout=2).status_code < 500:
|
|
@@ -51,19 +53,15 @@ def get_current_status():
|
|
| 51 |
else:
|
| 52 |
is_comfit_alive = False
|
| 53 |
|
| 54 |
-
# Determine link visibility for the live timer update
|
| 55 |
link_update = gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=is_comfit_alive)
|
| 56 |
-
|
| 57 |
return status_text, f"π₯ Active Users: {active_user_count}", link_update
|
| 58 |
except Exception as e:
|
| 59 |
return f"β Error: {str(e)}", f"π₯ Active Users: {active_user_count}", gr.update()
|
| 60 |
|
| 61 |
def user_joined():
|
| 62 |
-
global active_user_count
|
| 63 |
active_user_count += 1
|
| 64 |
-
|
| 65 |
-
link_visibility = gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=is_comfit_alive)
|
| 66 |
-
return f"π₯ Active Users: {active_user_count}", link_visibility
|
| 67 |
|
| 68 |
def user_left():
|
| 69 |
global active_user_count
|
|
@@ -80,14 +78,14 @@ def manage_instance():
|
|
| 80 |
yield ("β Error: Instance Not Found", gr.update(visible=False))
|
| 81 |
return
|
| 82 |
|
|
|
|
| 83 |
if server.status != "ACTIVE":
|
| 84 |
-
yield ("β³ Milestone 1/3: Un-shelving...", gr.update(visible=False))
|
| 85 |
if server.status == "SHELVED_OFFLOADED":
|
| 86 |
conn.compute.unshelve_server(server)
|
| 87 |
conn.compute.wait_for_server(server, status='ACTIVE', wait=600)
|
| 88 |
-
yield ("β
Milestone 2/3: VM is ACTIVE.", gr.update(visible=False))
|
| 89 |
|
| 90 |
-
# Check software
|
| 91 |
yield ("π Milestone 3/3: Connecting to ComFit...", gr.update(visible=False))
|
| 92 |
for i in range(24):
|
| 93 |
try:
|
|
@@ -101,11 +99,11 @@ def manage_instance():
|
|
| 101 |
except Exception as e:
|
| 102 |
yield (f"β Error: {str(e)}", gr.update(visible=False))
|
| 103 |
|
| 104 |
-
#
|
| 105 |
combined_js = """
|
| 106 |
<script>
|
| 107 |
let idleTime = 0;
|
| 108 |
-
const MAX_IDLE = 10; // <--- CHANGE TIMEOUT HERE (
|
| 109 |
|
| 110 |
function playNotification(status) {
|
| 111 |
if (status.includes("RUNNING")) {
|
|
@@ -132,6 +130,8 @@ setInterval(function() {
|
|
| 132 |
|
| 133 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 134 |
gr.HTML(combined_js)
|
|
|
|
|
|
|
| 135 |
session_tracker = gr.State(value="active", delete_callback=user_left)
|
| 136 |
|
| 137 |
gr.Markdown("# π ComFit Copilot Shared Control Panel")
|
|
@@ -140,19 +140,43 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 140 |
live_status = gr.Textbox(label="Live Jetstream2 Status (5s refresh)", interactive=False)
|
| 141 |
user_display = gr.Textbox(label="Space Activity", interactive=False)
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 144 |
status_label = gr.Label(value="Ready")
|
| 145 |
link_box = gr.Markdown(visible=False)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
live_status.change(None, inputs=live_status, js="(s) => playNotification(s)")
|
| 148 |
activate_btn.click(fn=manage_instance, outputs=[status_label, link_box])
|
|
|
|
| 149 |
|
| 150 |
-
#
|
| 151 |
timer = gr.Timer(5)
|
| 152 |
timer.tick(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
demo.load(fn=user_joined, outputs=
|
| 156 |
demo.load(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
# GLOBAL SHARED STATE
|
| 9 |
active_user_count = 0
|
| 10 |
+
is_comfit_alive = False
|
| 11 |
clean_link = "comfit-copilot.mch250095.projects.jetstream-cloud.org:3000"
|
| 12 |
full_url = f"http://{clean_link}"
|
| 13 |
|
| 14 |
def get_conn():
|
| 15 |
+
"""Authenticates with Jetstream2 (OpenStack) using Application Credentials."""
|
| 16 |
return openstack.connect(
|
| 17 |
auth_url=os.getenv("OS_AUTH_URL"),
|
| 18 |
application_credential_id=os.getenv("OS_APPLICATION_CREDENTIAL_ID"),
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def get_current_status():
|
| 26 |
+
"""Background task to fetch status and sync shared link visibility."""
|
| 27 |
global active_user_count, is_comfit_alive
|
| 28 |
try:
|
| 29 |
conn = get_conn()
|
|
|
|
| 41 |
}
|
| 42 |
status_text = status_map.get(server.status, f"β STATE: {server.status}")
|
| 43 |
|
| 44 |
+
# Shared health check for the URL
|
| 45 |
if "RUNNING" in status_text:
|
| 46 |
try:
|
| 47 |
if requests.get(full_url, timeout=2).status_code < 500:
|
|
|
|
| 53 |
else:
|
| 54 |
is_comfit_alive = False
|
| 55 |
|
|
|
|
| 56 |
link_update = gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=is_comfit_alive)
|
|
|
|
| 57 |
return status_text, f"π₯ Active Users: {active_user_count}", link_update
|
| 58 |
except Exception as e:
|
| 59 |
return f"β Error: {str(e)}", f"π₯ Active Users: {active_user_count}", gr.update()
|
| 60 |
|
| 61 |
def user_joined():
|
| 62 |
+
global active_user_count
|
| 63 |
active_user_count += 1
|
| 64 |
+
return f"π₯ Active Users: {active_user_count}"
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def user_left():
|
| 67 |
global active_user_count
|
|
|
|
| 78 |
yield ("β Error: Instance Not Found", gr.update(visible=False))
|
| 79 |
return
|
| 80 |
|
| 81 |
+
# Multi-user bypass
|
| 82 |
if server.status != "ACTIVE":
|
| 83 |
+
yield ("β³ Milestone 1/3: Un-shelving Begin...", gr.update(visible=False))
|
| 84 |
if server.status == "SHELVED_OFFLOADED":
|
| 85 |
conn.compute.unshelve_server(server)
|
| 86 |
conn.compute.wait_for_server(server, status='ACTIVE', wait=600)
|
| 87 |
+
yield ("β
Milestone 2/3: Unshelved! VM is now ACTIVE.", gr.update(visible=False))
|
| 88 |
|
|
|
|
| 89 |
yield ("π Milestone 3/3: Connecting to ComFit...", gr.update(visible=False))
|
| 90 |
for i in range(24):
|
| 91 |
try:
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
yield (f"β Error: {str(e)}", gr.update(visible=False))
|
| 101 |
|
| 102 |
+
# JavaScript for sound + Inactivity Timeout
|
| 103 |
combined_js = """
|
| 104 |
<script>
|
| 105 |
let idleTime = 0;
|
| 106 |
+
const MAX_IDLE = 10; // <--- CHANGE TIMEOUT HERE (Minutes)
|
| 107 |
|
| 108 |
function playNotification(status) {
|
| 109 |
if (status.includes("RUNNING")) {
|
|
|
|
| 130 |
|
| 131 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 132 |
gr.HTML(combined_js)
|
| 133 |
+
|
| 134 |
+
# Session state for cleanup
|
| 135 |
session_tracker = gr.State(value="active", delete_callback=user_left)
|
| 136 |
|
| 137 |
gr.Markdown("# π ComFit Copilot Shared Control Panel")
|
|
|
|
| 140 |
live_status = gr.Textbox(label="Live Jetstream2 Status (5s refresh)", interactive=False)
|
| 141 |
user_display = gr.Textbox(label="Space Activity", interactive=False)
|
| 142 |
|
| 143 |
+
with gr.Row():
|
| 144 |
+
activate_btn = gr.Button("π Activate / Access ComFit Copilot", variant="primary")
|
| 145 |
+
refresh_btn = gr.Button("π Force Refresh Status", variant="secondary")
|
| 146 |
+
|
| 147 |
status_label = gr.Label(value="Ready")
|
| 148 |
link_box = gr.Markdown(visible=False)
|
| 149 |
|
| 150 |
+
# Instructions section
|
| 151 |
+
gr.Markdown("---")
|
| 152 |
+
gr.Markdown("""
|
| 153 |
+
### π Instructions for Visitors
|
| 154 |
+
|
| 155 |
+
**Step 1: Check Current Status**
|
| 156 |
+
Review the **Live Jetstream2 Status**. If it shows `SLEEPING (SHELVED)`, the server is off. If it shows `RUNNING (ACTIVE)`, the application is already live for everyone!
|
| 157 |
+
|
| 158 |
+
**Step 2: Access or Wake Up**
|
| 159 |
+
- If a **Visit Link** is already visible, simply click it to open ComFit Copilot.
|
| 160 |
+
- If no link is visible, click **Activate / Access ComFit Copilot**. This will trigger the un-shelving process.
|
| 161 |
+
|
| 162 |
+
**Step 3: Track Milestones**
|
| 163 |
+
The control panel will cycle through three milestones: Un-shelving, VM Activation, and Software Booting. Please be patient while the cloud prepares your environment.
|
| 164 |
+
|
| 165 |
+
**Step 4: Automatic Session Timeout**
|
| 166 |
+
If you do not interact with this page for **10 minutes**, your session will expire. The [Jetstream2 instance](https://docs.jetstream-cloud.org) will also auto-shelve after its own period of inactivity.
|
| 167 |
+
""")
|
| 168 |
+
|
| 169 |
+
# UI Event Listeners
|
| 170 |
live_status.change(None, inputs=live_status, js="(s) => playNotification(s)")
|
| 171 |
activate_btn.click(fn=manage_instance, outputs=[status_label, link_box])
|
| 172 |
+
refresh_btn.click(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 173 |
|
| 174 |
+
# Automatic 5-second timer
|
| 175 |
timer = gr.Timer(5)
|
| 176 |
timer.tick(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 177 |
|
| 178 |
+
# Initialization
|
| 179 |
+
demo.load(fn=user_joined, outputs=user_display)
|
| 180 |
demo.load(fn=get_current_status, outputs=[live_status, user_display, link_box])
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|