-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvz.py
140 lines (109 loc) · 3.83 KB
/
vz.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
import subprocess
import json
from ContainerNotFound import ContainerNotFound
from IPy import IP
class VZ:
ctid = ""
def validate(self, val):
# check if the input is a ctid,ip or invalid
try:
ctid = int(val)
if ctid >= 100:
return "CTID"
except ValueError:
# not a ctid
pass
try:
ip = IP(val)
return "IP"
except:
# not an IP
pass
return None
def get_all_json(self):
# use ip to get the ctid of vm
# get list of all vms and their ctid + ips in json format
output = subprocess.check_output("/usr/sbin/vzlist -ao ctid,ip --json", shell=True)
# getting rid of binary string
output = output.decode('utf-8')
vms = json.loads(output)
return vms
def check_ctid(self):
vms = self.get_all_json()
# loop through all the vms checking all their ips
# if you find one that has the required ip, return its ctid
for vm in vms:
if str(vm['ctid']) == self.ctid:
return True
# if it cannot be found, return None
return None
def get_ctid(self, ip):
vms = self.get_all_json()
# loop through all the vms checking all their ips
# if you find one that has the required ip, return its ctid
for vm in vms:
for address in vm['ip']:
if address == ip:
return str(vm['ctid'])
# if it cannot be found, return None
return None
def enable_ppp(self):
# user vzctl to enable ppp
# shutdown vm
ret = subprocess.call("/usr/sbin/vzctl stop {}".format(self.ctid), shell=True)
if ret == 0:
# enable tun
ret = subprocess.call("/usr/sbin/vzctl set {} --features ppp:on --save".format(self.ctid), shell=True)
else:
return False
if ret == 0:
# start vm again
ret = subprocess.call("/usr/sbin/vzctl start {}".format(self.ctid), shell=True)
else:
return False
if ret == 0:
# enable devices
subprocess.call("/usr/sbin/vzctl set {} --devices c:108:0:rw --save".format(self.ctid), shell=True)
else:
return False
if ret == 0:
# not checking return code, it is always 8
subprocess.call("/usr/sbin/vzctl exec {} mknod /dev/ppp c 108 0".format(self.ctid), shell=True)
else:
# return False
ret = subprocess.call("/usr/sbin/vzctl exec {} chmod 600 /dev/ppp".format(self.ctid), shell=True)
if ret == 0:
return True
else:
return False
def enable_tun(self):
# use vzctl to enable tun
# shutdown vm
ret = subprocess.call("/usr/sbin/vzctl stop {}".format(self.ctid), shell=True)
if ret == 0:
# enable tun
ret = subprocess.call("/usr/sbin/vzctl set {} --devnodes net/tun:rw --capability net_admin:on --save".format
(self.ctid), shell=True)
else:
return False
if ret == 0:
# start vm again
ret = subprocess.call("/usr/sbin/vzctl start {}".format(self.ctid), shell=True)
else:
return False
if ret == 0:
return True
else:
return False
def __init__(self, vmid):
# check if vm id is valid
if self.validate(vmid) == "CTID":
self.ctid = vmid
if self.check_ctid() is None:
raise ContainerNotFound
elif self.validate(vmid) == "IP":
self.ctid = self.get_ctid(vmid)
if self.ctid is None:
raise ContainerNotFound
else:
raise ValueError