-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
741 lines (576 loc) · 25.1 KB
/
database.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
"""This module contains code to interact with a Cloud Spanner database.
This script is run on a VM instance, but is also used in the main application.
"""
from google.cloud import spanner
from google.api_core.exceptions import RetryError
try:
# This is used when running on a VM instance
from logging_utils import CloudAndConsoleLogger
except ImportError:
# This is used when running in the main application
from vmassign import CloudAndConsoleLogger
cnc_logger = CloudAndConsoleLogger(module_name=__name__)
class SpannerDatabase:
def __init__(self, project_id, instance_id, database_id, table_name):
"""Initializes a SpannerDatabase object.
Args:
project_id: The ID of the project that owns the Cloud Spanner instance.
instance_id: The ID of the Cloud Spanner instance.
database_id: The ID of the Cloud Spanner database.
table_name: The name of the table in the database (to be used in sql queries).
Returns:
A SpannerDatabase object.
"""
self.project_id = project_id
self.instance_id = instance_id
self.database_id = database_id
# TODO: There could be more than a single table in a database
self.table_name = table_name
# Latest query and results
self.query = None
# Instantiate a client and get a Cloud Spanner instance and database by ID.
self.spanner_client = spanner.Client(project=project_id)
self.instance = self.spanner_client.instance(instance_id)
self.database = self.instance.database(database_id)
# TODO: This really only makes this class compatible with a specific schema
# Get the column names of the database
self.column_names = self.get_column_names()
self.pin_column = "Pin"
self.crd_column = "CrdCmd"
self.hostname_column = "Hostname"
self.user_email_column = "UserEmail"
self.in_use_column = "inUse"
for column in [
self.pin_column,
self.crd_column,
self.hostname_column,
self.user_email_column,
self.in_use_column,
]:
if column not in self.column_names:
raise ValueError(f"Column {column} does not exist in the database")
def get_column_names(self, table_name=None):
"""Gets the column names of the database."""
if table_name is None:
table_name = self.table_name
query = f"""
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table_name
"""
params = {"table_name": table_name}
param_types = {"table_name": spanner.param_types.STRING}
try:
with self.database.snapshot() as snapshot:
results = snapshot.execute_sql(
query, params=params, param_types=param_types
)
except RetryError as e:
cnc_logger.error(f"Error: {e}")
cnc_logger(
"You may need to rerun authentication commands:"
"\n\t`gcloud auth application-default login`"
)
raise e
column_names = [r[0] for r in results]
return column_names
def process_results(self, results):
"""Processes the results of an SQL query.
Args:
results: The result set from the SQL query.
Returns:
A list of dictionaries containing the data from the database. Each dictionary
represents a row in the table with the column names as keys and the column values
as values.
"""
# Convert the result set to a list to populate the metadata
rows = list(results)
# Get the column names from the metadata
column_names = [field.name for field in results.metadata.row_type.fields]
# Convert all rows to dictionaries
data = [dict(zip(column_names, row)) for row in rows]
return data
def execute_sql_and_process_results(
self, query, params=None, param_types=None
) -> list:
"""Executes an SQL query and processes the results.
Args:
query: The SQL query to execute.
Returns:
A list of dictionaries containing the data from the database. Each dictionary
represents a row in the table with the column names as keys and the column values
as values.
"""
# Execute the SQL query
with self.database.snapshot() as snapshot:
results = snapshot.execute_sql(
query, params=params, param_types=param_types
)
# Process the results
data = self.process_results(results)
return data
def read_data(
self,
table_name=None,
):
"""Reads all data from the database for specified table.
Args:
table_name: The name of the table in the database (to be used in sql queries).
If None, the default table name is used (self.table_name).
Returns:
A list of dictionaries containing the data from the database. Each dictionary
represents a row in the table with the column names as keys and the column values
as values.
"""
if table_name is None:
table_name = self.table_name
query = f"SELECT * FROM {table_name}"
self.query = query
data = self.execute_sql_and_process_results(query)
cnc_logger.debug(data)
return data
def vm_exists(self, hostname, table_name=None):
"""Checks if the given hostname exists in the database
Args:
hostname: The hostname to check
Returns:
True if the hostname exists in the database, False otherwise
"""
if table_name is None:
table_name = self.table_name
query = f"SELECT * FROM {table_name} WHERE {self.hostname_column} = @hostname"
self.query = query
with self.database.snapshot() as snapshot:
result = snapshot.execute_sql(
query,
params={"hostname": hostname},
param_types={"hostname": spanner.param_types.STRING},
)
for row in result:
return True
return False
def check_vm_exists(self, hostname, table_name=None):
"""Checks if the given hostname exists in the database.
Args:
hostname: The hostname to check
table_name: The name of the table in the database (to be used in sql queries).
Raises:
ValueError: If the hostname does not exist in the database
Returns:
None
"""
if not self.vm_exists(hostname, table_name):
raise ValueError(f"The hostname {hostname} does not exist in the database")
# Used on local VM
def get_pin_and_crd(self, hostname, table_name=None):
"""Gets the pin and command for the given hostname from the database
Args:
hostname: The hostname of the VM to get the pin and command from
Returns:
A tuple containing the pin and command for the given hostname. For example:
(pin, cmd)
Raises:
ValueError: If the hostname does not exist in the database
"""
if table_name is None:
table_name = self.table_name
# Check if the hostname exists in the database
self.check_vm_exists(hostname, table_name)
query = f"SELECT * FROM {table_name} WHERE {self.hostname_column} = @hostname"
self.query = query
data = self.execute_sql_and_process_results(
query,
params={"hostname": hostname},
param_types={"hostname": spanner.param_types.STRING},
)
pin, command = data[0][self.pin_column], data[0][self.crd_column]
cnc_logger.debug(f"Pin and CRD for {hostname}: {pin}, {command}")
return pin, command
def add_crd_and_pin(self, hostname, pin, cmd, table_name=None, override=False):
"""Adds a command and pin to the given hostname in the database when a user connects to the VM with the corresponding hostname
Args:
hostname: The hostname of the VM to be added to the database
pin: The pin that user sets to be added to the database
cmd: The CRD command to be added to the database
table_name: The name of the table in the database (to be used in sql queries).
override: If True, the pin and command will be updated even if the hostname
already has a pin or command. If False, a ValueError will be raised if the
hostname already has a pin or command.
Raises:
ValueError: If the hostname does not exist in the database
ValueError: If the hostname already has a pin or command
"""
if table_name is None:
table_name = self.table_name
# Checks if the hostname exists in the database
self.check_vm_exists(hostname, table_name)
# Checks if the hostname already has a pin or command)
if not override and self.get_pin_and_crd(hostname) != (None, None):
raise ValueError(f"The hostname {hostname} already has a pin or command")
# Adds the given CRD command and pin to the database
def update_cmd_and_pin(transaction):
query = (
f"UPDATE {table_name} SET {self.pin_column} = @pin, "
f"{self.crd_column} = @cmd WHERE {self.hostname_column} = @hostname"
)
self.query = query
row_ct = transaction.execute_update(
query,
params={"pin": pin, "cmd": cmd, "hostname": hostname},
param_types={
"pin": spanner.param_types.STRING,
"cmd": spanner.param_types.STRING,
"hostname": spanner.param_types.STRING,
},
)
cnc_logger.debug(
f"{row_ct} record(s) updated for {self.hostname_column}={hostname} "
f"with {self.pin_column}={pin} and {self.crd_column}={cmd}"
)
self.database.run_in_transaction(update_cmd_and_pin)
def unassign_vm(self, hostname, table_name=None):
"""Reset the given VM instance so that it can be assigned to another user.
Args:
hostname: The hostname of the VM to be reset
Raises:
ValueError: If the hostname does not exist in the database
"""
if table_name is None:
table_name = self.table_name
# Checks if the hostname exists in the database
self.check_vm_exists(hostname, table_name)
# Reset the VM instance
def reset(transaction):
query = (
f"UPDATE {table_name} SET {self.pin_column} = NULL, "
f"{self.crd_column} = NULL, {self.user_email_column} = NULL, "
f"{self.in_use_column} = FALSE "
f"WHERE {self.hostname_column} = @hostname"
)
self.query = query
row_ct = transaction.execute_update(
query,
params={"hostname": hostname},
param_types={"hostname": spanner.param_types.STRING},
)
cnc_logger.debug(
f"{row_ct} record(s) updated for {self.hostname_column}={hostname} "
f"with {self.pin_column}=NULL, {self.crd_column}=NULL, "
f"{self.user_email_column}=NULL"
)
self.database.run_in_transaction(reset)
# Only used in __main__ (for debug)
def assign_vm(self, hostname, user_email, table_name=None):
"""Assigns a VM to a user in the database
Args:
hostname: The hostname of the VM to be assigned to the user
user_email: The email of the user to assign the VM to
Raises:
ValueError: If the hostname does not exist in the database
ValueError: If the hostname already has a user assigned to it
"""
if table_name is None:
table_name = self.table_name
# Checks if the hostname exists in the database
self.check_vm_exists(hostname, table_name)
# Assigns the VM to the user
def update_user_email(transaction):
query = (
f"UPDATE {self.table_name} SET {self.user_email_column} = @user_email "
f"WHERE {self.hostname_column} = @hostname"
)
self.query = query
row_ct = transaction.execute_update(
query,
params={"user_email": user_email, "hostname": hostname},
param_types={
"user_email": spanner.param_types.STRING,
"hostname": spanner.param_types.STRING,
},
)
cnc_logger.debug(
f"{row_ct} record(s) updated for {self.hostname_column}={hostname} "
f"with {self.user_email_column}={user_email}"
)
self.database.run_in_transaction(update_user_email)
def find_and_assign_vm(self, user_email):
"""This method finds and assigns a VM to a user using a transaction.
Args:
user_email: The user's email to link to a VM instance.
"""
def transaction(transaction):
# Query for an unassigned row
query = (
f"SELECT {self.hostname_column} FROM {self.table_name} "
f"WHERE {self.user_email_column} IS NULL LIMIT 1"
)
results = transaction.execute_sql(query)
data = self.process_results(results=results)
# rows = list(transaction.execute_sql(query))
if not data:
cnc_logger.error(
f"User {user_email} requested a VM, "
"but no unassigned VMs available"
)
raise ValueError("No unassigned VMs available :(")
# Assign the first unassigned row to the user
hostname = data[0][self.hostname_column]
update_statement = (
f"UPDATE {self.table_name} "
f"SET {self.user_email_column} = @user_email "
f"WHERE {self.hostname_column} = @hostname"
)
params = {"user_email": user_email, "hostname": hostname}
param_types = {
"user_email": spanner.param_types.STRING,
"hostname": spanner.param_types.STRING,
}
transaction.execute_update(
update_statement, params=params, param_types=param_types
)
cnc_logger.info(
f"VM with hostname [{hostname}] assigned to user [{user_email}]"
)
return hostname
# We will keep trying to assign a user a VM until we succeed!
while True:
try:
# Run the transaction
hostname = self.database.run_in_transaction(transaction)
return hostname
except Exception as e:
cnc_logger.error(f"Error: {e}")
raise e
# Used in manage
def get_assigned_vms(self, table_name=None):
"""Gets the hostnames of the VMs that are assigned to a user.
Args:
table_name: The name of the table in the database to run queries on. Default uses self.table_name.
Returns:
A list of hostnames of the VMs that have a user email assigned to them.
"""
if table_name is None:
table_name = self.table_name
query = (
f"SELECT {self.hostname_column} "
f"FROM {table_name} "
f"WHERE {self.user_email_column} IS NOT NULL"
)
self.query = query
data = self.execute_sql_and_process_results(query)
assigned_vms = [row[self.hostname_column] for row in data]
return assigned_vms
def get_assigned_vm_details(self, email: str, table_name=None) -> list:
"""Gets the hostname, pin, and crd of the assigned VM.
Args:
email: The email of the user to get the assigned VM details for.
Rasies:
ValueError: If the email does not exist in the database
Returns:
A tuple of (hostname, pin, command) for the VM assigned to a user email.
"""
if table_name is None:
table_name = self.table_name
query = (
f"SELECT * FROM {table_name} "
f"WHERE {self.user_email_column} = @user_email"
)
params = {"user_email": email}
param_types = {"user_email": spanner.param_types.STRING}
self.query = query
data = self.execute_sql_and_process_results(
query, params=params, param_types=param_types
)
if not data:
raise ValueError(f"The email {email} does not exist in the database")
hostname = data[0][self.hostname_column]
pin = data[0][self.pin_column]
command = data[0][self.crd_column]
cnc_logger.pprint(
{
"VM Details for": email,
self.hostname_column: hostname,
self.pin_column: pin,
self.crd_column: command,
}
)
return hostname, pin, command
# Unused in app or local vm
def get_unassigned_vms(self, table_name=None) -> list:
"""Gets the hostnames of the unassigned VMs.
Returns:
A list of hostnames of the VMs that do not have a user email, pin, or
command assigned to them.
"""
if table_name is None:
table_name = self.table_name
query = (
f"SELECT {self.hostname_column} FROM {table_name} "
f"WHERE {self.pin_column} IS NULL AND {self.crd_column} IS NULL "
f"AND {self.user_email_column} IS NULL"
)
self.query = query
data = self.execute_sql_and_process_results(query)
unassigned_vms = [row[self.hostname_column] for row in data]
cnc_logger.debug(f"Unassigned VMs: {unassigned_vms}")
return unassigned_vms
def get_unassigned_vms_count(self, table_name=None) -> int:
"""Gets the number of unassigned VMs.
Returns:
The number of VMs that do not have a user email, pin, or
command assigned to them.
"""
if table_name is None:
table_name = self.table_name
query = (
f"SELECT COUNT({self.hostname_column}) FROM {table_name} "
f"WHERE {self.pin_column} IS NULL AND {self.crd_column} IS NULL "
f"AND {self.user_email_column} IS NULL"
)
self.query = query
# Execute the SQL query
with self.database.snapshot() as snapshot:
results = snapshot.execute_sql(query)
rows = list(results)
unassigned_vms_count = rows[0][0] if rows else 0
cnc_logger.debug(f"Number of unassigned VMs: {unassigned_vms_count}")
return unassigned_vms_count
# Not used (yet)
def get_unused_vms(self, table_name=None):
"""Get the hostnames of the VMs that are not being used.
While the VMs are not being used, they can be assigned to a user. A VM is not
being used if the inUse column is set to False which is the default value. The
inUse column is set to True when the VM is running the SLEAP executable.
Args:
table_name: The name of the table in the database to run queries on. Default uses self.table_name.
Returns:
A list of hostnames of the VMs that are not being used.
"""
if table_name is None:
table_name = self.table_name
query = f"SELECT {self.hostname_column} FROM {table_name} WHERE {self.in_use_column} IS NOT TRUE"
self.query = query
data = self.execute_sql_and_process_results(query)
unused_vms = [row[self.hostname_column] for row in data]
cnc_logger.debug(f"Unused VMs: {unused_vms}")
return unused_vms
# Used on local VM
def set_in_use_status(self, hostname, in_use: bool, table_name=None):
"""Updates the inUse status of a given VM in the database.
The inUse status is set to True when the VM is running the SLEAP executable and
False when it is not.
Args:
hostname: The hostname of the VM to update the inUse status for
in_use: The boolean value to set the inUse column to.
table_name: The name of the table in the database to run queries on. Default uses self.table_name.
Default uses self.table_name.
Raises:
ValueError: If the hostname does not exist in the database
"""
if table_name is None:
table_name = self.table_name
# Checks if the hostname exists in the database
self.check_vm_exists(hostname, table_name)
# Update inUse status
def update_in_use(transaction):
query = (
f"UPDATE {table_name} SET {self.in_use_column} = @in_use "
f"WHERE {self.hostname_column} = @hostname"
)
self.query = query
row_ct = transaction.execute_update(
query,
params={"in_use": in_use, "hostname": hostname},
param_types={
"in_use": spanner.param_types.BOOL,
"hostname": spanner.param_types.STRING,
},
)
cnc_logger.debug(
f"{row_ct} record(s) updated for {self.hostname_column}={hostname} "
f"with {self.in_use_column}={in_use}"
)
self.database.run_in_transaction(update_in_use)
# Used in manage
def get_user_email(self, hostname, table_name=None):
"""Gets the email of the user assigned to the given hostname.
Args:
hostname: The hostname of the VM to get the user email for
table_name: The name of the table in the database (to be used in sql queries).
Returns:
The email of the user assigned to the given hostname.
"""
if table_name is None:
table_name = self.table_name
query = (
f"SELECT {self.user_email_column} "
f"FROM {table_name} "
f"WHERE {self.hostname_column} = @hostname"
)
self.query = query
with self.database.snapshot() as snapshot:
results = snapshot.execute_sql(
query,
params={"hostname": hostname},
param_types={"hostname": spanner.param_types.STRING},
)
for row in results:
return row[0]
# Used on local VM
@classmethod
def load_database(cls, project_id, instance_id, database_id, table_name=None):
"""Loads an existing database from Cloud Spanner.
Args:
project_id: The ID of the project that owns the Cloud Spanner instance.
instance_id: The ID of the Cloud Spanner instance.
database_id: The ID of the Cloud Spanner database.
table_name: The name of the table in the database (to be used in sql queries).
Returns:
A SpannerDatabase object.
"""
return cls(project_id, instance_id, database_id, table_name)
def manually_assign_and_crd_connect():
"""Run this with a command line argument to test the database. For example:
python database.py A/0asfhsadfh
"""
# Debug/demo code, not to be used on VM instances or in production
import getpass
import subprocess
try:
from vmassign import config
from vmassign.vm.local.crd_connect import reconstruct_command
except Exception as e:
message = (
"This script must be run from the main project directory, "
"not from a VM instance. If you are running from the main project "
"directory, make sure you have followed the installation instructions "
"in the README.md"
)
cnc_logger.error(f"Error: {e}\n{message}")
assign_vm = True
# Edit this info in config.py
project_id = config.PROJECT_ID
instance_id = config.DB_INSTANCE_ID
database_id = config.DB_DATABASE_ID
table_name = config.DB_TABLE_NAME
spanner_db = SpannerDatabase.load_database(
project_id, instance_id, database_id, table_name
)
unassigned_vms = spanner_db.get_unassigned_vms()
vm_hostname = getpass.getuser() + "-vm-1"
user_email = "[email protected]"
spanner_db.assign_vm(hostname=vm_hostname, user_email=user_email)
spanner_db.get_unassigned_vms()
if assign_vm:
pin = "723177"
command = reconstruct_command()
spanner_db.add_crd_and_pin(vm_hostname, pin, command, override=True)
pin_retrieved, command_retrieved = spanner_db.get_pin_and_crd(vm_hostname)
assert pin_retrieved == pin
assert command_retrieved == command
publish_cmd = f"gcloud pubsub topics publish {vm_hostname} --message='start'"
args = publish_cmd.split()
subprocess.run(args)
if __name__ == "__main__":
# Debug/demo code, not to be used on VM instances or in production
manually_assign_and_crd_connect()