-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxmox_tools
executable file
·80 lines (64 loc) · 1.95 KB
/
proxmox_tools
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
#!/usr/bin/python
import sys
import socket
import json
import requests
import itertools
import subprocess
username='vmoper@pve'
password='vmoper'
localnode = socket.gethostname()
urlapi = 'https://'+localnode+':8006/api2/json'
def geTicket():
"""Function to get ticket from proxmox
Connect to local node to the API and
get one ticket for one session"""
authdata = {"username": username, "password": password}
geticket = json.loads(s.post(urlapi+'/access/ticket',authdata ,verify=False).text)
ticket = geticket['data']['ticket']
csrfprev = geticket['data']['CSRFPreventionToken']
return [ticket, csrfprev]
def getNodes():
"""Function to get list of nodes on proxmox cluster"""
listnodes = json.loads(s.get(urlapi+'/nodes/', cookies=cookies, verify=False).text)['data']
nodes = []
for node in listnodes:
nodes.append(node['node'])
nodes.sort()
return nodes
def getVMnum():
"""Function to get num of running vm on each
proxmox node"""
nodes = getNodes()
for node in nodes:
listvm = json.loads(s.get(urlapi+'/nodes/'+node+'/qemu', cookies=cookies, verify=False).text)['data']
print "Nodo: " + node + " Running VM: ", len(listvm)
def getVMids(node):
"""Function to get running vm on
proxmox node"""
#node = self.node
listvm = json.loads(s.get(urlapi+'/nodes/'+node+'/qemu', cookies=cookies, verify=False).text)['data']
vmids = []
for id in iter(listvm):
vmids.append(id['vmid'])
vmids.sort()
return vmids
def migLocalNode():
vmids = getVMids(localnode)
nodes = getNodes()
nodes.pop(nodes.index(localnode))
cyrnodes = itertools.cycle(nodes)
for n in range(len(vmids)):
node = cyrnodes.next()
print "Migrating vm: %s to node %s" % (vmids[n], node)
subprocess.check_call(['qm', 'migrate', vmids[n], node, '-online'])
# Start session and get ticket
s = requests.Session()
ticket = geTicket()[0]
csrfprev = geTicket()[1]
cookies = dict(PVEAuthCookie=ticket)
#nodes = getNodes()
#print nodes
#vms = getVMids(localnode)
#print vms
migLocalNode()