-
Notifications
You must be signed in to change notification settings - Fork 0
/
vds_api.py
90 lines (78 loc) · 3.13 KB
/
vds_api.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
import requests
import paramiko
import config
from time import sleep
from models import Server
def ssh_conect_to_server(server_ip, login, password):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(server_ip, '3333', login, password)
return ssh_client
def get_balance():
response = requests.get(
'{api_uri}command={command}&login={api_login}&pass={api_pass}&json=1'.format(
command = 'getBalance',
api_uri = config.VDS_API_URL,
api_login = config.VDS_API_LOGIN,
api_pass = config.VDS_API_PASSWORD)).json()
return response
def get_orders():
response = requests.get(
'{api_uri}command={command}&login={api_login}&pass={api_pass}&json=1'.format(
command = 'getOrders',
api_uri = config.VDS_API_URL,
api_login = config.VDS_API_LOGIN,
api_pass = config.VDS_API_PASSWORD)).json()
return response
def get_status(order_id):
orders = get_orders()
for order in orders['orders']:
if order['orderid'] == order_id:
return order['status']
def create_order():
response = requests.get(
'{api_uri}command={command}&login={api_login}&pass={api_pass}&json=1&tarifid={tarifid}&period={period}&addons={addons}&locationid=104'.format(
command = 'createOrder',
api_uri = config.VDS_API_URL,
api_login = config.VDS_API_LOGIN,
api_pass = config.VDS_API_PASSWORD,
tarifid='193',
period='1',
addons='119,98')
).json()
new_server = {
'orderid': str(response['orderid']),
'serverlogin': str(response['serverlogin']),
'serverpassword': str(response['serverpassword']),
'serverip': 'localhost'}
status = get_status(new_server['orderid'])
while str(status) != '1':
sleep(60)
status = get_status(new_server['orderid'])
orders = get_orders()
for order in orders['orders']:
if order['orderid'] == new_server['orderid']:
new_server['serverip'] = str(order['serverip'])
server, is_new = Server.get_or_create(
order_id = new_server['orderid'],
server_login = new_server['serverlogin'],
server_password = new_server['serverpassword'],
server_ip = new_server['serverip']
)
ssh_client = ssh_conect_to_server(new_server['serverip'], new_server['serverlogin'], new_server['serverpassword'])
with ssh_client.open_sftp() as sftp:
sftp.put('add_user.sh','add_user.sh')
sftp.put('revoke_user.sh','revoke_user.sh')
stdin, stdout, stderr = ssh_client.exec_command('chmod +x add_user.sh')
stdin, stdout, stderr = ssh_client.exec_command('chmod +x revoke_user.sh')
print('Создан новый сервер: ')
print(new_server)
return new_server
def get_tarifs():
response = requests.get(
'{api_uri}command={command}&login={api_login}&pass={api_pass}&json=1'.format(
command = 'getTarifs',
api_uri = config.VDS_API_URL,
api_login = config.VDS_API_LOGIN,
api_pass = config.VDS_API_PASSWORD)).json()
return response