Repository navigation
Expand file tree
/
Copy pathcloud-dash-tui.py
More file actions
201 lines (164 loc) · 6.77 KB
/
Copy pathcloud-dash-tui.py
File metadata and controls
201 lines (164 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os
import httpx
from dotenv import load_dotenv
from textual.app import App, ComposeResult
from textual.containers import Horizontal, VerticalScroll
from textual.widgets import Header, Footer, Static, Label, DataTable
DIGITALOCEAN_API = "https://api.digitalocean.com/v2"
class CloudDashTUI(App):
"""Simple Cloud Dashboard using Textual."""
CSS = """
Screen {
layout: vertical;
}
Horizontal {
height: 1fr;
}
VerticalScroll {
width: 1fr;
border: round $accent;
padding: 1 1;
}
"""
load_dotenv() # sets environmental variables with user's `.env` file
COMMAND_PALETTE_BINDING = "ctrl+backslash"
BINDINGS = [
("r", "refresh", "Refresh"),
("q", "quit", "Quit"),
]
def compose(self) -> ComposeResult:
yield Header()
yield Horizontal(
VerticalScroll(Static("Droplets", id="droplets_title"), id="droplets_panel"),
VerticalScroll(Static("Volumes", id="volumes_title"), id="volumes_panel"),
)
yield Footer()
# -----------------------------------------------------
# API Helpers
# -----------------------------------------------------
def get_token(self) -> str | None:
return os.environ["DIGITALOCEAN_TOKEN"]
def client(self) -> httpx.Client:
token = self.get_token()
return httpx.Client(
headers={"Authorization": f"Bearer {token}"},
timeout=10.0,
)
def fetch_droplets(self):
try:
r = self.client().get(f"{DIGITALOCEAN_API}/droplets")
r.raise_for_status()
return r.json().get("droplets", [])
except Exception as e:
return [{"name": f"Error: {e}"}]
def fetch_volumes(self):
try:
r = self.client().get(f"{DIGITALOCEAN_API}/volumes")
r.raise_for_status()
return r.json().get("volumes", [])
except Exception as e:
return [{"name": f"Error: {e}"}]
# -----------------------------------------------------
# Refactored update using provided data
# -----------------------------------------------------
def update_tables_with_data(self, droplets, volumes):
droplets_panel = self.query_one("#droplets_panel", VerticalScroll)
volumes_panel = self.query_one("#volumes_panel", VerticalScroll)
droplets_panel.remove_children()
volumes_panel.remove_children()
if droplets is None:
droplets_panel.mount(Label("[red]Missing env var DIGITALOCEAN_TOKEN[/]"))
volumes_panel.mount(Label("[red]Missing env var DIGITALOCEAN_TOKEN[/]"))
return
# ------ DROPLETS ------
droplets_panel.mount(Label("[bold underline]Droplets[/]\n"))
if not droplets:
droplets_panel.mount(Label("No droplets found."))
for d in droplets:
name = d.get("name", "unknown")
status = d.get("status", "unknown")
region = d.get("region", {}).get("slug", "unknown")
v4_list = d.get("networks", {}).get("v4", [])
public = next((n["ip_address"] for n in v4_list if n.get("type") == "public"), None)
private = next((n["ip_address"] for n in v4_list if n.get("type") == "private"), None)
ip4 = public or private or "—"
# 🆕 --- Hardware info ---
size = d.get("size", {})
vcpus = size.get("vcpus", "?")
memory_mb = size.get("memory", "?")
disk_gb = size.get("disk", "?")
# 🆕 --- OS info ---
image = d.get("image", {})
distro = image.get("distribution", "unknown")
os_name = image.get("name", "unknown")
droplets_panel.mount(
Static(
f"[bold]{name}[/]\n"
f"• Status: {status}\n"
f"• Region: {region}\n"
f"• IPv4 (public): {ip4}\n"
f"• CPU: {vcpus} vCPUs\n"
f"• RAM: {memory_mb} MB\n"
f"• Disk: {disk_gb} GB\n"
f"• OS: {distro} — {os_name}\n"
)
)
# ------ VOLUMES ------
volumes_panel.mount(Label("[bold underline]Volumes[/]\n"))
if not volumes:
volumes_panel.mount(Label("No volumes found."))
# Build droplet ID → name map
droplet_names = {
d["id"]: d.get("name", f"unknown-{d['id']}")
for d in droplets
if "id" in d
}
for v in volumes:
name = v.get("name", "unknown")
size = v.get("size_gigabytes", "?")
region = v.get("region", {}).get("slug", "unknown")
droplets_attached = v.get("droplet_ids", [])
attached_names = [droplet_names.get(did, f"{did}") for did in droplets_attached]
volumes_panel.mount(
Static(
f"[bold]{name}[/]\n"
f"• Size: {size} GB\n"
f"• Region: {region}\n"
f"• Attached to: {attached_names or 'None'}\n"
)
)
# -----------------------------------------------------
# Start background loading on startup
# -----------------------------------------------------
def on_mount(self):
droplets_panel = self.query_one("#droplets_panel", VerticalScroll)
volumes_panel = self.query_one("#volumes_panel", VerticalScroll)
droplets_panel.remove_children()
volumes_panel.remove_children()
droplets_panel.mount(Label("[bold]Loading droplets…[/]"))
volumes_panel.mount(Label("[bold]Loading volumes…[/]"))
self.run_worker(self.load_data, thread=True, exclusive=True) # will run load_data() in background
# -----------------------------------------------------
# Background thread: fetch droplets + volumes
# -----------------------------------------------------
def load_data(self):
"""Runs in a background thread (Textual worker)."""
token = self.get_token()
if not token:
self.call_from_thread(self.update_tables_with_data, None, None)
return
try:
droplets = self.fetch_droplets()
volumes = self.fetch_volumes()
except Exception as e:
droplets = [{"name": f"Error: {e}"}]
volumes = []
self.call_from_thread(self.update_tables_with_data, droplets, volumes) # updates UI on main thread
# -----------------------------------------------------
# Refresh (just restarts load)
# -----------------------------------------------------
def action_refresh(self):
self.on_mount()
self.bell()
if __name__ == "__main__":
CloudDashTUI().run()