/
virtualizorAPI.py
392 lines (342 loc) · 14.6 KB
/
virtualizorAPI.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
########################################################################################################################
##### Python Virtualizor API by BoxControL https://www.boxcontrol.net
##### it is a port of enduser.php started by Alons
########################################################################################################################
from phpserialize import unserialize
import urllib.parse as urp
import pycurl
from io import BytesIO
class Virtualizor_Enduser_API():
def __init__(self, apikey, apipass, apiip, apiport='4083', apiprotocol="https", apierror=[]):
'''
Constructor
:param apikey: The API KEY of your account
:param apipass: The API password of your account
:param apiip: The IP of the Control Panel
:param apiport: Optional port to connect to, default is set to 4083 for SSL, for non SSL use 4082
:param apiprotocol: Default protocol set to https
:param apierror: empty array to store errors
:return:
'''
self.apikey = apikey
self.apipass = apipass
self.apiip = apiip
self.apiport = apiport
self.apiprotocol = apiprotocol
self.apierror = apierror
def __repr__(self):
'''
Dumps a variable
'''
return (str(self.apiip) + '; apikey=' + str(self.apikey) + '; apipass=' + str(self.apipass))
@staticmethod
def unserialize_(apistr):
'''
Unserializes a string
:param apistr: The serialized string
:return: the unserialized array on success or false on failure
'''
apistr = unserialize(apistr)
if not apistr:
apistr = unserialize(apistr)
if not apistr:
return False
else:
return apistr
def apicall(self, path, post=[], cookies=[]):
'''
Makes an API request to the server to do a particular task
:param path: The action you want to do
:param post: An array of DATA that should be posted
:param cookies: An array FOR SENDING COOKIES
:return: The unserialized array on success OR False on failure
'''
url = self.apiprotocol + "://" + self.apiip + ":" + self.apiport + "/" + path
url += "&api=serialize&apikey=" + urp.quote_plus(self.apikey) + "&apipass=" + urp.quote_plus(self.apipass)
# Set curl parameters.
ch = pycurl.Curl()
ch.setopt(ch.URL, url)
# time out
ch.setopt(ch.CONNECTTIMEOUT, 3)
# Turn off the server and peer verification (TrustManager Concept)
ch.setopt(ch.SSL_VERIFYPEER, 0)
ch.setopt(ch.SSL_VERIFYHOST, 0)
ch.setopt(ch.SSL_CIPHER_LIST, 'rsa_rc4_128_sha')
# user agent
ch.setopt(ch.USERAGENT, 'Virtualizor')
# Cookies
if cookies:
ch.setopt(ch.COOKIESESSION, True)
ch.setopt(ch.COOKIE, urp.urlencode(cookies, '', '; '))
if post:
ch.setopt(ch.POSTFIELDS, urp.urlencode(post))
ch.setopt(ch.POST, 1)
# get response from server
buffer = BytesIO()
ch.setopt(ch.WRITEDATA, buffer)
ch.perform()
ch.close()
resp = buffer.getvalue()
if not resp:
return False
r = self.unserialize_(resp)
if not r:
return False
return r
def listvs(self):
'''
List the Virtual Servers in your account
:return: The array containing a list of Virtual Servers one has in their account
'''
resp = self.apicall('index.php?act=listvs')
return resp.get(b'vs').keys()
def start(self, id):
'''
START a Virtual Server
:param id: The VM's ID
:return: bool True on success or False on failure
'''
res = self.apicall('index.php?svs=' + str(id) + '&act=start&do=1')
if res.get(b'done'):
return True
else:
return False
def stop(self, id):
'''
STOP a Virtual Server
:param id: The VM's ID
:return: bool True on success or False on failure
'''
res = self.apicall('index.php?svs=' + str(id) + '&act=stop&do=1')
if res.get(b'done'):
return True
else:
return False
def restart(self, id):
'''
RESTART a Virtual Server
:param id: The VM's ID
:return: bool True on success or False on failure
'''
res = self.apicall('index.php?svs=' + str(id) + '&act=restart&do=1')
if res.get(b'done'):
return True
else:
return False
def poweroff(self, id):
'''
POWEROFF a Virtual Server
:param id: The VM's ID
:return: bool True on success or False on failure
'''
res = self.apicall('index.php?svs=' + str(id) + '&act=poweroff&do=1')
if res.get(b'done'):
return True
else:
return False
def status(self, id):
'''
STATUS of a Virtual Server
:param id: The VM's ID
:return: status of the server
'''
res = self.apicall('index.php?svs=' + str(id) + '&act=start')
if res.get(b'status') == 1:
return 'Online'
else:
return 'Offline'
def hostname(self, id, newhostname=None):
'''
GET or SET the hostname of a VM. To get the current hostname dont pass newhostname parameter
:param id: The VMs ID
:param newhostname: The new HOSTNAME of the virtual server.
:return: The CURRENT hostname is returned if $newhostname is NULL.
* FALSE is returned if there was an error while setting the new hostname
* 'onboot' is returned if the new hostname will be set when the VPS is STOPPED and STARTED
* 'done' is returned if the new hostname has been set right now - Mainly OpenVZ
'''
# are we going to change hostname ?
if newhostname != None:
post = (('newhost', newhostname.encode()), ('changehost', 'Change Hostname'))
resp = self.apicall('index.php?svs=' + str(id) + '&act=hostname', post)
# was there an error
if resp.get(b'error') != None:
error = resp(b'error')
return false
# will it be done when the VPS is STOPED and STARTED ?
elif resp.get(b'onboot') != None:
return 'onboot'
# It was done successfully
elif resp.get(b'done') != None:
return 'done'
# just return hostname
else:
resp = self.apicall('index.php?svs' + str(id) + '&act=hostname')
#return resp(b'current').json()
return resp.get(b'current').decode()
def cpu(self, id):
'''
GET the CPU details of a VM. Incase of Xen / KVM, only information is available as usage cannot be sensed.
:param id: The VMs ID
:return: An array containing the details is returned. Usage details is available only in case of OpenVZ.
'''
resp = self.apicall('index.php?svs=' + str(id) + '&act=cpu')
return resp.get(b'cpu')
def ram(self, id):
'''
GET the RAM details of a VM. Incase of Xen / KVM, only information is available as usage cannot be sensed.
:param id: The VMs ID
:return: An array containing the details is returned. Usage details is available only in case of OpenVZ.
'''
resp = self.apicall('index.php?svs=' + str(id) + '&act=ram')
return resp.get(b'ram')
def disk(self, id):
'''
GET the Disk details of a VM. Incase of Xen / KVM, only information is available as usage cannot be sensed.
:param id: The VMs ID
:return: An array containing the details is returned. Usage details is available only in case of OpenVZ.
'''
resp = self.apicall('index.php?svs=' + str(id) + '&act=disk')
return [resp.get(b'disk'), resp.get(b'inodes')]
def bandwidth(self, id, month=None):
'''
GET the Bandwidth Usage of a VM.
:param id: The VMs ID
:param month: The month in the format YYYYMM e.g. 201205 is for the Month of May, 2012
:return: Returns an array of Bandwidth Information for the Month GIVEN.
By Default the CURRENT MONTH details are returned
'''
if month != None:
resp = self.apicall('index.php?svs=' + str(id) + '&act=bandwidth' + '&show=' + month)
return resp.get(b'bandwidth')
else:
resp = self.apicall('index.php?svs=' + str(id) + '&act=bandwidth')
return resp.get(b'bandwidth')
def processes(self, id):
'''
List the Processes in a VPS - Only OpenVZ
:param id: The VMs ID
:return: An array containing all the processes is returned
'''
resp = self.apicall('index.php?svs=' + str(id) + '&act=processes')
return resp.get(b'processes')
def services(self, id):
'''
List the Services in a VPS - Only OpenVZ
:param id: The VMs ID
:return: An array containing all the services is returned
'''
resp = self.apicall('index.php?svs=' + str(id) + '&act=services')
return [resp.get(b'services'), resp.get(b'autostart'), resp.get(b'running')]
def changepassword(self, id, password):
'''
Changes the root password of a VPS
:param id: The VMs ID
:param password: The new password to set
:return: FALSE is returned if there was an error while setting the new password
'onboot' is returned if the new password will be set when the VPS is STOPPED and STARTED
'done' is returned if the new password has been set right now - Mainly OpenVZ
'''
post = (('newpass', password),
('conf', password),
('changepass', 'Change Password'))
resp = self.apicall('index.php?svs=' + str(id) + '&act=changepassword', post)
#return resp
# was there an error
if resp.get(b'error') != None:
error = resp.get(b'error')
return False
# will it be done when the VPS is STOPPED and STARTED ?
elif resp.get(b'onboot') != None:
return 'onboot'
# it was done successfully
elif resp.get(b'done') != None:
return 'done'
def vnc(self, id):
'''
Get the VNC Details like PORT, IP, VNC Password. Only available in case of Xen and KVM VPS if VNC is enabled.
:param id: The VMs ID
:return: An array containing all the VNC Details
'''
resp = self.apicall('index.php?svs=' + str(id) + '&act=vnc')
if resp == False:
return 'VNC is not enabled'
elif resp.get(b'info') != None:
return resp.get(b'info')
def vncpass(self, id, password):
'''
Change the VNC Password. Only available in case of Xen and KVM VPS if VNC is enabled.
:param id: The VMs ID
:param password: The new password to set
:return: FALSE is returned if there was an error while setting the new password
'onboot' is returned if the new password will be set when the VPS is STOPPED and STARTED
'''
post = (('newpass', password.encode()),
('conf', password.encode()),
('vncpass', 'Change Password'))
resp = self.apicall('index.php?svs=' + str(id) + '&act=vncpass', post)
# was there an error
if resp.get(b'error') != None:
return False
elif resp.get(b'onboot') != None or resp.get(b'done') != None:
return 'onboot'
def ostemplate(self, vid, newosid=None, newpass=None):
'''
Re-installs a VPS if the newosid is specified. If the newosid is not passed,
then this function will return an array of available templates.
:param vid: The VMs ID
:param newosid: The Operating System ID (you got from the list) that will be installed on the VPS.
:param newpass: The new root password to set
:return: FALSE is returned if there was an error while setting the new password
string 'onboot' is returned if the new password will be set when the VPS is STOPPED and STARTED
string 'done' is returned if the new password has been set right now - Mainly OpenVZ
array An array of the list of available OS Templates is returned if newosid is None
'''
# get the list of OS Templates
resp = self.apicall('index.php?svs=' + str(vid) + '&act=ostemplate')
# Get a list of Virtual Servers
listvs = self.listvs()
virtualization = listvs.get(vid).get(b'virt').decode()
availabledistros = resp.get(b'oslist').get(virtualization.encode())
ostemplate = []
for distro in availabledistros.values():
for key, value in distro.items():
ostemplate.append((key, value.get(b'name').decode()))
if newosid != None:
post = (('newos', str(newosid)),
('newpass', newpass.encode()),
('conf', newpass.encode()),
('reinsos', 'Reinstall'))
resp = self.apicall('index.php?svs=' + str(vid) + '&act=ostemplate', post)
# was there an error
if resp.get(b'error') != None:
return resp.get(b'error')
# will it be done when the VPS is STOPPED and STARTED ?
elif resp.get(b'onboot') != None:
return 'onboot'
# It was done successfully
elif resp.get(b'done') != None:
return 'done'
else:
return ostemplate
def controlpanel(self, id, panel):
'''
Install a Control Panel
:param id: The VMs ID
:param panel: The Name of the Panel you want to install. Options - cpanel, plesk, webuzo, kloxo, webmin
:return: FALSE is returned if there was an error while installing the control panel
'onboot' is returned if the control panel will be installed when the VPS is STOPPED and STARTED
'done' is returned if the control panel has been installed right now - Mainly OpenVZ
'''
post = (('ins', panel), ('', str(1)))
resp = self.apicall('index.php?svs=' + str(id) + '&act=controlpanel', post)
# Was there an error
if resp.get(b'error') != None:
#return False
return resp.get(b'error')
# will it be done when the VPS is STOPPED and STARTED ?
elif resp.get(b'onboot') != None:
return 'onboot'
# It was done successfully
elif resp.get(b'done') != None:
return 'done'