-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_functions.py
83 lines (64 loc) · 1.96 KB
/
redis_functions.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
import socket
import sys
import os
import uuid
from datetime import datetime
from typing import List
from redis_config import rds
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def get_ip():
"""
:return: Returns the IP Address of current system
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(('10.254.254.254', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
def get_vm_id_with_rpc_port(rpc_port: int) -> str:
d = rds.hgetall('rpc_ports')
vm_id = ''
for k, v in d.items():
if v == rpc_port:
vm_id = k
return vm_id
def get_vm_host_id(vm_id: str) -> int:
d = rds.hgetall(name=f'vm_configs:{vm_id}')
host_id = d['host_id']
assert isinstance(host_id, int)
return host_id
def get_current_host_id():
host_ip = get_ip()
host_id = -1
d = rds.hgetall(f'mon_proxy_addr')
for k, v in d.items():
if v == host_ip:
host_id = k
break
if host_id == -1:
eprint(f'This host with {host_ip} is not stored in database. '
f'So, can\'t create a VM')
return host_id
def get_vm_ids() -> List[str]:
host_id = get_current_host_id()
vm_ids = rds.smembers(f"vms_in_host:{host_id}")
return [vm_id.decode('utf-8') for vm_id in vm_ids]
def get_vm_pid(vm_id: str) -> int:
a = int(rds.hget(f"vm_configs:{vm_id}", 'pid'))
return a
def get_vm_tap_device(vm_id: str) -> str:
a = rds.hget(f"vm_configs:{vm_id}", 'tap_device').decode('utf-8')
return a
def get_host_tap_device() -> str:
host_id = get_current_host_id()
tap_device = rds.hget(f"host_configs:{host_id}", 'tap_device').decode('utf-8')
return tap_device
# Add other things needed, this will be replaced later,
# possibly with database calls as we get info. from other teams