-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.py
387 lines (327 loc) · 13.1 KB
/
aws.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import sys
import time
from datetime import datetime
from signal import signal, SIGINT
from subprocess import call
from sys import exit
from typing import List, Optional, Dict, Tuple
import boto3
import click
import paramiko
from paramiko import SSHException
from paramiko.ssh_exception import NoValidConnectionsError
from prettytable import PrettyTable
from aws_config import loadconfig, Params
from aws_utils import _get_instance_by_name, _collect_instances, _get_tag_val
from launch import (
upload_and_run,
start,
make_script,
make_parallel_commands,
cleanup_instance, _compress_folder, _random_name,
)
from machine_monitor import MachineMonitor
@click.group()
def cli():
pass
@cli.command()
@click.argument("name")
@click.option("--owner", default=None, help="Owner of the machine or default one")
def kill(name: str, owner: Optional[str]):
config = loadconfig()
if owner is not None:
config.owner = owner
instance = _get_instance_by_name(name, config)
if instance is None:
print(f"Could not find instance called {name}")
return
client = boto3.client("ec2", region_name=config.region)
response = client.terminate_instances(InstanceIds=[instance["InstanceId"]])
print(f"Terminate response: {response}")
print(f"\n Instance terminated")
@cli.command()
@click.option("--owner", default=None, help="Custom owner of the machines")
@click.option("--sleep", default=0.1, help="How long to sleep between updates")
@click.option("--debug/--no_debug", help="print all lines?")
def monitor(owner: Optional[str], sleep: float, debug: bool):
"""Monitor the state of running instances of a given owner"""
config = loadconfig()
if owner is not None:
config.owner = owner
def clear_console():
_ = call("clear" if os.name == "posix" else "cls", shell=True)
def handler(signal_received, frame):
# https://www.devdungeon.com/content/python-catch-sigint-ctrl-c
print(
"\n\nSIGINT or CTRL-C detected, joining threads.. "
+ "\n\t..might take time if some monitor is trying to connect\n"
)
# join the threads and exit
for name, thread in threads.items():
thread.comm["should_stop"] = True
for name, thread in threads.items():
if thread.is_alive():
thread.join()
else:
print(f"WARNING, a dead thread named {name} found!")
print(f"\nAll threads ended, exiting, bye\n")
exit(0)
def get_machines(config: Params) -> Dict[str, str]:
instances = _collect_instances(config)
machines = {
_get_tag_val(ins["Tags"], "Name"): ins["PrivateIpAddress"]
for ins in instances
}
return machines
def _all_gpus_same_memory(received: List[Dict[str, str]]) -> bool:
first_mem = received[0]["total_mem"]
for rec in received:
if first_mem != rec["total_mem"]:
return False
return True
def make_gpu_fields(received: List[Dict[str, str]]) -> Tuple[str, str]:
loads = []
mems = []
all_same = _all_gpus_same_memory(received)
for gpu in received:
loads.append(gpu["util"])
if all_same:
mems.append(gpu["used_mem"])
else:
mems.append(gpu["used_mem"] + " /" + gpu["total_mem"] + "G")
# if all memories are the same, append total memory at the end
memory = ",".join(mems)
if all_same:
memory += " /" + received[0]["total_mem"] + "G"
return ",".join(loads), memory
# Tell Python to run the handler() function when SIGINT is recieved
signal(SIGINT, handler)
threads: Dict[str, MachineMonitor] = {}
while True:
# go through the machines, launch new monitors
machines = get_machines(config)
for name, ip in machines.items():
if not name in threads:
threads[name] = MachineMonitor(name, sleep, config)
threads[name].start()
# go through existing monitors, delete the old ones (note monitors end themselves)
to_remove = []
for name, _ in threads.items():
if name not in machines:
to_remove.append(name)
for remove in to_remove:
threads.pop(remove)
t = PrettyTable()
t.field_names = [
"Name",
"Ip",
"Load",
"GPU",
"GPU mem",
"EXPID",
"stdout",
] # "Heartbeat"
for name, ip in machines.items():
comm = threads[name].comm
load = comm["load"]
gpu_utils, gpu_mems = make_gpu_fields(comm["gpu"])
last_lines, heartbeat = comm["result"]
if len(last_lines) == 0:
t.add_row(
[name, ip, load, gpu_utils, gpu_mems, "-", "WARNING: NO LINE READ"]
)
else:
for line_id, (exp_id, last_line) in enumerate(last_lines):
if line_id == 0:
t.add_row(
[name, ip, load, gpu_utils, gpu_mems, exp_id, last_line]
)
else:
t.add_row(["", "", "", "", "", exp_id, last_line])
t.align = "l"
if not debug:
clear_console()
print(f"Found {len(machines)} machines owned by {config.owner}:\n")
print(t)
time.sleep(sleep)
@cli.command()
@click.option("--owner", default=None, help="Custom owner of the machines")
def list(owner: Optional[str] = None):
"""List currently running machines (of the owner, subgroup, group..)"""
config = loadconfig()
if owner is not None:
config.owner = owner
instances = _collect_instances(config)
addresses = [ins["PrivateIpAddress"] for ins in instances]
names = [_get_tag_val(ins["Tags"], "Name") for ins in instances]
machines = [{name: address} for name, address in zip(names, addresses)]
print(f"running machines are: , names {machines}")
@cli.command()
@click.argument("name")
@click.option("--owner", default=None, help="Custom owner of the machines")
@click.option("--setup/--no_setup", help="show the setup log?")
@click.option("--debug/--no_debug", help="print all lines?")
@click.option("--sleep", default=0.2, help="how long to sleep between updates")
@click.option(
"--group",
default=0,
help="experiment group (when more experiments ran in parallel)",
)
def lastline(
name: str, owner: Optional[str], setup: bool, debug: bool, sleep: float, group: int
):
"""Show the last line of the experiment_[group].log or setup.log of a given machine"""
config = loadconfig()
if owner is not None:
config.owner = owner
instance = _get_instance_by_name(name, config)
if instance is None:
return
instance_ip = instance["PrivateIpAddress"]
print(f"Connecting to {name} with IP: {instance_ip}")
# prepare the connection
key = paramiko.RSAKey.from_private_key_file(config.pem_file)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
file = "setup.log" if setup else f"experiment_{group}.log"
try:
client.connect(hostname=instance_ip, username="ubuntu", pkey=key)
print(f"DONE, connected")
while True:
stdin, stdout, stderr = client.exec_command(f"tail -n 2 {file}")
read_bytes = stdout.read()
last_line = MachineMonitor.extract_last_line(read_bytes)
print(last_line)
if not debug:
sys.stdout.write("\033[F") # Cursor up one line
time.sleep(sleep)
except SSHException or NoValidConnectionsError:
print(f"Could not connect to: {name} with IP {instance_ip}")
@cli.command()
@click.argument("name")
@click.option("--owner", default=None, help="Custom owner of the machines")
@click.option("--setup/--no_setup", help="show the setup log?")
@click.option(
"--group",
default=0,
help="experiment group (when more experiments ran in parallel)",
)
def tail(name: str, setup: bool, owner: Optional[str], group: int):
"""Tail the contents of experiment.log / setup.log of a given machine"""
config = loadconfig()
if owner is not None:
config.owner = owner
instance = _get_instance_by_name(name, config)
if instance is None:
return
instance_ip = instance["PrivateIpAddress"]
print(f"Connecting to {name} with IP: {instance_ip}")
file = "setup.log" if setup else f"experiment_{group}.log"
command = " ".join(
[
"ssh",
f"-i {config.pem_file} " "ubuntu@" + instance_ip,
f"'tail -f -n 2000 {file} && exec bash -l'",
]
)
print(command)
os.system(command)
return
@cli.command()
@click.argument("commands")
@click.option("--repeats", default=1, help="How many times to repeat given command (s)")
@click.option("--parallel", default=1, help="How many experiments to run in parallel?")
@click.option(
"--instance", default=None, help="instance type to be used (e.g. p2.xlarge)"
)
@click.option("--ami", default=None, help="ami_id to be used")
@click.option("--owner", default=None, help="custom owner of the machine")
@click.option("--noshutdown/--shutdown", help="Disable the auto-shutdown (debug)" +
"If the instance is manually shutdown, it still will be auto-terminated")
@click.option("--delay", default=20,
help="Delay in seconds between launching parallel experiments")
def launch(
commands: str,
repeats: int,
parallel: int,
instance: Optional[str],
owner: Optional[str],
ami: Optional[str],
noshutdown: bool,
delay: int,
):
"""Launch machine which will execute given commands (splitted by |) and auto-terminate after"""
config = loadconfig()
if instance is not None:
config.instance_type = instance
if ami is not None:
config.ami_id = ami
if owner is not None:
config.owner = owner
if noshutdown:
print(
f'\n\n\txxxxxxxxxxx WARNING xxxxxxxxx This instance will not be automatically shut down!\n\n')
parallel_commands = make_parallel_commands(commands, repeats, parallel)
print(f"Will launch the following commands:")
for id, coms in enumerate(parallel_commands):
print(f"Iteration {id}:")
formatted = "\n\t\t".join(coms)
print(f"\t\t{formatted}")
instance, name = start(config)
upload_and_run(instance, parallel_commands, config, noshutdown, config.repo_name, delay, name)
print(f"ALL DONE, instance name: {name}")
@cli.command()
@click.argument("name")
def makeimage(name: str):
"""Make the AMI - image from the currently running instance of a given name"""
config = loadconfig()
instance = _get_instance_by_name(name, config)
if instance is None:
print(f"ERROR: running instance named {name} not found!")
return
choice = input(
"This will cleanup currently running machine and will break logs of any currently running "
"experiments.\n\n\t PRESS y and enter for continue..."
)
if choice == "y":
cleanup_instance(instance, name, config)
else:
print(f"Aborting")
return
date_time = datetime.utcnow().strftime("%Y-%m-%d--%H-%M")
image_name = f"i-{config.repo_name}-{date_time}"
client = boto3.client("ec2", region_name=config.region)
result = client.create_image(InstanceId=instance["InstanceId"], Name=image_name)
print(f"result: {result}")
image_id = result["ImageId"]
print(
f"\n\nDone, resulting ImageId: {image_id}\t Change this in the config.yaml when ready."
)
@cli.command()
@click.argument("commands")
@click.option("--repeats", default=1, help="How many times to repeat given command (s)")
@click.option("--parallel", default=1, help="How many experiments to run in parallel?")
@click.option("--noshutdown/--shutdown", help="Disable the auto-shutdown (debug)")
@click.option("--delay", default=0, help="Delay in seconds between launching parallel experiments")
def debugscript(commands: str, repeats: int, parallel: int, noshutdown: bool, delay: int):
"""Debug purposes, prints out the whole script"""
config = loadconfig()
if noshutdown:
print(
f'\n\n\txxxxxxxxxxx WARNING xxxxxxxxx This instance will not be automatically shut down!\n\n')
machine_name = _random_name()
print(f'compressing the repository...')
filename = _compress_folder(machine_name)
print(f'compressed sources to: {filename}')
parallel_commands = make_parallel_commands(commands, repeats, parallel)
print(f"Will launch the following commands:")
for id, coms in enumerate(parallel_commands):
print(f"Iteration {id}:")
formatted = "\n\t\t".join(coms)
print(f"\t\t{formatted}")
script = make_script(parallel_commands, config.repo_name, noshutdown, delay=delay)
print(f"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
print(script)
if __name__ == "__main__":
cli()