-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
240 lines (216 loc) · 8.45 KB
/
app.py
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import traceback
from datetime import datetime
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_bolt.app import App
import config
from utils.log import setup_logger
from cluster.node import get_node_info_blocks, get_node_user_blocks, get_user_jobs_blocks
from utils.slack2unix import get_slack2unix_map
logger = setup_logger(output=config.LOGGER_OUTPUT, level=config.LOGGER_LEVEL)
app = App(token=os.environ["SLACK_BOT_TOKEN"],
signing_secret=os.environ["SLACK_APP_TOKEN"],
logger=logger)
def get_home_tab_blocks(user_id):
try:
unix_user = get_slack2unix_map().get(user_id, None)
blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Hi <@{user_id}>* :wave: ",
}
}, {
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"Last updated: {datetime.now().strftime('%m/%d/%Y, %H:%M:%S')} \n"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Refresh",
"emoji": True
},
"value": "refresh_home",
"action_id": "action_refresh_home"
}
}, {
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*GPU Cluster Summary:*",
}
}, *get_node_info_blocks()]
if unix_user:
blocks += [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*User Summary:*",
}
}, *get_node_user_blocks("All GPUs", limit=52),
*get_node_user_blocks("Non-preemptible GPUs", ignore_partition=["compute", "low-prio-gpu"], limit=12),
*get_node_user_blocks("`ddp-4way` GPUs", ignore_partition=["compute", "ddp-2way", "gpu", "low-prio-gpu"], limit=12),
*get_node_user_blocks("`ddp-2way` GPUs", ignore_partition=["compute", "ddp-4way", "gpu", "low-prio-gpu"], limit=12),
*get_node_user_blocks("`gpu` GPUs", ignore_partition=["compute", "ddp-4way", "ddp-2way", "low-prio-gpu"], limit=12),
*get_node_user_blocks("Preemptible GPUs", ignore_partition=["compute", "ddp-4way", "ddp-2way", "gpu"], limit=40),
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Your Jobs (`{unix_user}`):*\n",
}
}, *get_user_jobs_blocks(unix_user, state='RUNNING')
, *get_user_jobs_blocks(unix_user, state='PENDING'),
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Waiting in Cluster:*\n",
}
}, *get_user_jobs_blocks(unix_user_name=None, state='PENDING'),
]
else:
blocks.extend(get_no_account_found_blocks())
blocks.extend([
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": " ",
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Notes",
"emoji": True
},
"value": "readme",
"action_id": "action_readme",
}
}]
)
return blocks
except Exception:
logger.error(f"Failed to get home tab blocks")
traceback.print_exc()
return []
@app.action("action_refresh_home")
def refresh_home(ack, body, client):
user_id = body["user"]["id"]
ack()
client.views_update(
view_id=body["view"]["id"],
view={
"type": "home",
"blocks": get_home_tab_blocks(user_id)
}
)
@app.event("app_home_opened")
def update_home_tab(client, event, logger):
try:
user_id = event["user"]
# Call views.publish with the built-in client
client.views_publish(
# The user that opened your app's app home
user_id=user_id,
# The view object that appears in the app home
view={
"type": "home",
"blocks": get_home_tab_blocks(user_id)
}
)
except Exception:
logger.error(f"Failed to publish home tab")
traceback.print_exc()
def command_cluster_stats(user_id):
unix_user = get_slack2unix_map().get(user_id, None)
blocks = []
if unix_user:
return [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Hi <@{user_id}>* :wave:\nHere is the GPU availability summary ({datetime.now().strftime('%m/%d/%Y, %H:%M:%S')})",
}
}, *get_node_info_blocks()]
else:
return get_no_account_found_blocks()
def get_no_account_found_blocks():
return [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Error:*",
}
}, {
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": f"You do not seem to have a cluster account.\n"
f"Reason: Cannot find any account matching your Slack full name (not display name) in tritons's `/etc/passwd` database.\n"
f"Remedy: Run `getent passwd $USER` to look for your full name in triton and set the same on Slack."
}
]
}]
@app.command("/cluster")
def scan_cluster(ack, body):
ack(blocks=command_cluster_stats(body["user_id"]))
@app.message("cluster")
def say_hello_regex(message, say):
# logger.debug(message['text'])
say(blocks=command_cluster_stats(message["user"]))
# Listen for a shortcut invocation
@app.action("action_readme")
def open_modal(ack, body, client):
# Acknowledge the command request
ack()
# Call views_open with the built-in client
client.views_open(
# Pass a valid trigger_id within 3 seconds of receiving it
trigger_id=body["trigger_id"],
# View payload
view={
"type": "modal",
# View identifier
"callback_id": "readme",
"title": {
"type": "plain_text",
"text": "Notes"
},
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "1. The code is here: <https://github.com/subhc/susbot|github link>"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "2. If the app is not refreshing the server most likely is down. On clicking the `Refresh` button if an exclamation mark appears beside it the server is down. Ping me. \nIf you are curious run `ls -ltra "
"/work/subha/apps/VGGBot/logs` and check the latest log "
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "3. If your jobs don't show up or the displayed linux username is wrong then the app has failed to find any account matching your Slack full name (not display name) in tritons's `/etc/passwd` database. \nTo fix, "
"run `getent passwd $USER` to look for your full name in triton and set the same on Slack. Furthermore if you have multiple Slack accounts, (maybe one from before you formally joined VGG) your linux account can get mapped to the other Slack account.\nTake a look at the account matching logic <https://github.com/subhc/susbot/blob/main/utils/slack2unix.py|here> "
}
}
],
"type": "modal"
}
)
if __name__ == "__main__":
SocketModeHandler(app).start()