proxmaster/plugin.py

327 lines
14 KiB
Python
Raw Normal View History

2016-02-15 05:30:43 -05:00
#. -*- coding: utf-8 -
2016-11-11 09:56:35 -05:00
# required proxmox permissions: PVEAdmin
2016-02-15 05:30:43 -05:00
#
2017-09-20 20:07:56 -04:00
# afx 2015-2017
2016-02-15 05:30:43 -05:00
# site
from proxmoxer import ProxmoxAPI
import base64
import json
import time
import socket
2016-11-03 00:25:01 -04:00
import random
2016-05-08 08:44:19 -04:00
from unidecode import unidecode
2016-02-15 05:30:43 -05:00
#local
import grid
import utils
import ioconfig
import novnc
2016-11-03 00:05:51 -04:00
def auth(slave_name):
""" return control object from config slave names """
2016-02-15 05:30:43 -05:00
adminuser = ioconfig.parser.get('general', 'adminuser')
2016-11-03 00:05:51 -04:00
slaveip = ioconfig.parser.get(str(slave_name), 'ipv4')
slavepass = ioconfig.parser.get(str(slave_name), 'password')
slavetype = ioconfig.parser.get(str(slave_name), 'type')
2016-02-15 05:30:43 -05:00
#vendor specific
2016-11-03 12:56:26 -04:00
#if slavetype == 'proxmoxia':
# connection = lib_proxmoxia.Connector(slaveip)
# auth_token = connection.get_auth_token(adminuser, slavepass)
# proxobject = lib_proxmoxia.Proxmox(connection)
2016-11-03 00:05:51 -04:00
if slavetype == 'proxmox':
proxobject = ProxmoxAPI(slaveip, user=adminuser, password=slavepass, verify_ssl=False)
2016-11-03 12:56:26 -04:00
return proxobject
2016-02-15 05:30:43 -05:00
def vmlist(proxobject):
""" get vmlist """
#slave_name = proxobject.get('cluster/status')#'name']
2017-07-30 16:23:09 -04:00
#we keep a single node proxmoxes so node id = 0
2016-02-15 05:30:43 -05:00
slave_name = proxobject.cluster.status.get()[0]['name']
#query_kvm = proxobject.get('nodes/%s/qemu' % slave_name)
query_kvm = proxobject.nodes(slave_name).qemu.get()
query_lxc = proxobject.nodes(slave_name).lxc.get()
for kvm_dict in query_kvm:
kvm_dict['vmtype'] = 'kvm'
for lxc_dict in query_lxc:
lxc_dict['vmtype'] = 'lxc'
vmlist = query_kvm + query_lxc #merge machine list
return vmlist
def vmcreate(req):
2016-11-03 00:05:51 -04:00
""" create vm. returns JSON with data """
try:
region_id = ioconfig.parser.get(str(req['region']), 'regionid')
region_fullname = ioconfig.parser.get(str(req['region']), 'fullname')
except:
2016-10-28 22:57:57 -04:00
ioconfig.logger.error('grid> no region found')
2016-11-03 00:05:51 -04:00
return None
2016-05-08 08:44:19 -04:00
vm_name_utf8 = req['hostname']
vm_name = unidecode(vm_name_utf8)
2016-11-03 00:05:51 -04:00
try:
2017-07-30 16:23:09 -04:00
vm_pass = req['rootpass']
2016-11-03 00:05:51 -04:00
except:
2017-07-30 16:23:09 -04:00
vm_pass = 'datapoint'
2016-11-03 00:05:51 -04:00
#slave_name = str(grid.query_happiness(region_id, weight)) #TODO: provide weight parameters here and calculate route
2017-09-20 20:07:56 -04:00
#slave_name = 'lexx'
2017-04-08 19:56:09 -04:00
slave_name = 'warrior'
2016-11-03 10:07:28 -04:00
vm_id = random.randint(1000, 9999)
2016-11-03 01:06:46 -04:00
cubeid = int(time.time() * 10000 * 10000)
2016-11-03 00:38:26 -04:00
deploy = { 'cube': int(cubeid),
2017-07-30 21:39:18 -04:00
'type': req['type'],
2016-11-03 00:05:51 -04:00
'clientid': req['clientid'],
'clientemail': req['clientemail'],
2017-07-30 16:23:09 -04:00
'hostname': vm_name,
'region': region_fullname,
'slave': slave_name,
'vmid': vm_id,
'cpu': req['cpu'],
'mem': req['mem'],
'hdd': req['hdd']
}
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
2016-11-03 12:56:26 -04:00
real_slave_name = proxobject.cluster.status.get()[0]['name']
2017-07-30 21:39:18 -04:00
description = vm_name + ' (' + str(cubeid) + '/' + str(vm_id) + ')\n' + 'owned by ' + req['clientemail'] + ' (' + req['clientid'] + ')\n'
2016-02-15 05:30:43 -05:00
2017-07-30 21:15:14 -04:00
if req['type'] == 'kvm':
2017-02-28 19:56:03 -05:00
#create partition
image_name = 'vm-' + str(vm_id) + '-disk-1'
2017-04-08 19:56:09 -04:00
try:
local_storage = proxobject.nodes(real_slave_name).storage('lvm')
2017-07-30 21:15:14 -04:00
storage_create_result = local_storage.content.post(vmid=vm_id, filename=image_name, size=req['hdd'] + 'G')
ioconfig.logger.info('slave[%s]> allocated %s as %s' % (slave_name, req['hdd'], image_name))
2017-04-08 19:56:09 -04:00
except:
ioconfig.logger.info('slave[%s]> unable to allocate %s' % (slave_name, image_name))
response = { 'status':'FAIL' }
return response
2017-02-28 19:56:03 -05:00
2016-11-03 14:13:47 -04:00
create_result = proxobject.nodes(real_slave_name).qemu.post(vmid=int(vm_id),
2016-02-15 05:30:43 -05:00
name=vm_name,
sockets=1,
2017-07-30 21:15:14 -04:00
cores=req['cpu'],
memory=req['mem'],
2016-11-03 14:13:47 -04:00
virtio0='file=lvm:' + image_name,
2016-02-15 05:30:43 -05:00
onboot=1,
description=description)
2016-11-03 00:05:51 -04:00
2017-07-30 21:15:14 -04:00
if req['type'] == 'lxc':
2016-11-03 14:13:47 -04:00
create_result = proxobject.nodes(real_slave_name).lxc.post(vmid=int(vm_id),
2017-07-30 21:15:14 -04:00
cpus=req['cpu'],
memory=req['mem'],
2017-02-28 19:56:03 -05:00
swap=16,
ostemplate='backup:vztmpl/ubuntu-16.04-standard_16.04-1_amd64.tar.gz',
2016-11-03 00:05:51 -04:00
hostname=vm_name,
password=vm_pass,
2017-07-30 21:15:14 -04:00
rootfs='lvm:' + req['hdd'],
2016-11-03 14:13:47 -04:00
virtio0='file=lvm:' + image_name,
2016-11-03 00:05:51 -04:00
onboot=1,
description=description)
2017-04-08 19:56:09 -04:00
print(str(create_result))
2016-11-03 12:56:26 -04:00
2016-02-15 05:30:43 -05:00
#start the machihe
2017-09-20 20:07:56 -04:00
time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run
2016-11-03 14:13:47 -04:00
2017-07-30 19:15:40 -04:00
response = { 'status': 'CREATE', 'cube': cubeid, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name }
2016-11-03 14:13:47 -04:00
grid.writedb(deploy)
2016-02-15 05:30:43 -05:00
return response
2017-09-20 20:07:56 -04:00
def vmremove(cubeid):
""" terminate a vm """
slave_name, vm_type, vm_id, vmhost, vmowner = grid.queryvm(cubeid)
proxobject = auth(slave_name)
ioconfig.logger.info('%s[%s]> deleting %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
if vm_type == 'kvm':
result = proxobject.nodes(slave_name).qemu(vm_id).delete()
if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).delete()
grid.deletedb(cubeid)
return result
2016-02-15 05:30:43 -05:00
2016-11-03 00:05:51 -04:00
def vmstatus(cubeid):
2016-02-15 05:30:43 -05:00
""" returns the status of the machine """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
#slave_name = proxobject.c:luster.status.get()[0]['name']
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('%s[%s]> status of %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
2016-02-15 05:30:43 -05:00
if vm_type == 'kvm':
result = proxobject.nodes(slave_name).qemu(vm_id).status.current.get()
if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.current.get()
return result
2016-11-03 00:05:51 -04:00
def vmstart(cubeid):
2016-02-15 05:30:43 -05:00
""" starts a machine """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
#slave_name = proxobject.c:luster.status.get()[0]['name']
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('%s[%s]> starting %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
2016-02-15 05:30:43 -05:00
if vm_type == 'kvm':
result = proxobject.nodes(slave_name).qemu(vm_id).status.start.post()
if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.start.post()
2017-09-20 20:07:56 -04:00
#TODO: SET START AT BOOT FLAG
2016-02-16 14:12:12 -05:00
response = { 'status':'START' }
2016-02-15 05:30:43 -05:00
return response
2016-11-03 00:05:51 -04:00
def vmshutdown(cubeid):
2016-02-15 05:30:43 -05:00
""" acpi shutdown the machine.. """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
#slave_name = proxobject.c:luster.status.get()[0]['name']
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('%s[%s]> acpi shutdown %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
2016-02-15 05:30:43 -05:00
if vm_type == 'kvm':
2016-07-03 21:33:16 -04:00
result = proxobject.nodes(slave_name).qemu(vm_id).status.shutdown.post()
2016-02-15 05:30:43 -05:00
if vm_type == 'lxc':
2016-07-03 21:33:16 -04:00
result = proxobject.nodes(slave_name).lxc(vm_id).status.shutdown.post()
2017-09-20 20:07:56 -04:00
#TODO: REMOVE START AT BOOT FLAG
2016-03-31 10:40:40 -04:00
#ioconfig.logger.info('slave[{}]> {}'.format(slave_name, result))
2016-02-15 05:30:43 -05:00
response = { 'status':'SHUTDOWN', 'vmid':vm_id }
return response
2016-11-03 00:05:51 -04:00
def vmstop(cubeid):
2016-02-15 05:30:43 -05:00
""" poweroff the machine.. """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
#slave_name = proxobject.c:luster.status.get()[0]['name']
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('%s[%s]> power off %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
2016-02-15 05:30:43 -05:00
if vm_type == 'kvm':
result = proxobject.nodes(slave_name).qemu(vm_id).status.stop.post()
if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.stop.post()
2017-09-20 20:07:56 -04:00
#TODO: REMOVE START AT BOOT FLAG
2016-03-31 10:40:40 -04:00
#ioconfig.logger.info('slave[{}]> {}'.format(slave_name, result))
2016-02-16 14:12:12 -05:00
response = { 'status':'STOP', 'vmid':vm_id }
2016-02-15 05:30:43 -05:00
return response
2016-11-03 00:05:51 -04:00
def vmsuspend(cubeid):
2016-02-15 05:30:43 -05:00
""" suspend machine """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
#slave_name = proxobject.c:luster.status.get()[0]['name']
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('%s[%s]> suspending %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
2016-02-15 05:30:43 -05:00
if vm_type == 'kvm':
result = proxobject.nodes(slave_name).qemu(vm_id).status.suspend.post()
if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.suspend.post()
response = { 'status':'SUSPEND', 'vmid':vm_id }
return response
2016-11-03 00:05:51 -04:00
def vmresume(cubeid):
2016-02-15 05:30:43 -05:00
""" resume machine """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
#slave_name = proxobject.c:luster.status.get()[0]['name']
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('%s[%s]> resuming %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
2016-02-15 05:30:43 -05:00
if vm_type == 'kvm':
result = proxobject.nodes(slave_name).qemu(vm_id).status.resume.post()
if vm_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(vm_id).status.resume.post()
response = { 'status':'RESUME', 'vmid':vm_id }
return response
2016-11-03 00:05:51 -04:00
def vmrrd(cubeid):
2016-06-26 11:09:22 -04:00
""" retrieve rrd graphs (PNG) """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
2016-11-03 14:28:42 -04:00
proxobject.cluster.status.get()[0]['name']
2016-06-26 11:09:22 -04:00
result = {}
if vm_type == 'kvm':
2016-11-11 09:56:35 -05:00
statusquery = proxobject.nodes(slave_name).qemu(vm_id).status.current.get()
2016-06-26 12:41:01 -04:00
rcpu = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='cpu')
rmem = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='mem,maxmem')
rnet = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='netin,netout')
rhdd = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='diskread,diskwrite')
2016-11-11 09:56:35 -05:00
status = str(statusquery['qmpstatus'])
2016-06-26 11:09:22 -04:00
if vm_type == 'lxc':
2016-11-11 09:56:35 -05:00
status = proxobject.nodes(slave_name).lxc(vm_id).status.current.get()
2016-06-26 12:41:01 -04:00
rcpu = proxobject.nodes(slave_name).lxc(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='cpu')
rmem = proxobject.nodes(slave_name).lxc(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='mem,maxmem')
rnet = proxobject.nodes(slave_name).lxc(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='netin,netout')
rhdd = proxobject.nodes(slave_name).lxc(vm_id).rrd.get(timeframe='day', cf='AVERAGE', ds='diskread,diskwrite')
2017-02-28 19:56:03 -05:00
status = str(statusquery['qmpstatus']) #TODO: maybe change this?
2017-08-01 07:27:44 -04:00
#ioconfig.logger.info('%s[%s]> rrd of %s %s (%s). status: %s' % (vm_owner, slave_name, vm_type, vm_id, vm_host, status))
2016-11-11 09:56:35 -05:00
response = { 'status':status, 'cpu':rcpu, 'mem':rmem, 'net':rnet, 'hdd':rhdd }
2016-06-26 11:54:27 -04:00
return response
2016-06-26 12:42:00 -04:00
2016-11-04 22:07:04 -04:00
def vmvnc(cubeid):
2016-02-15 05:30:43 -05:00
""" invoke vnc ticket """
slave_name, vm_type, vm_id, vm_host, vm_owner = grid.queryvm(cubeid)
2016-11-03 00:05:51 -04:00
proxobject = auth(slave_name)
#slave_name = proxobject.c:luster.status.get()[0]['name']
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('%s[%s]> invoking vnc ticket for %s %s (%s)' % (vm_owner, slave_name, vm_type, vm_id, vm_host))
2016-02-15 05:30:43 -05:00
if vm_type == 'kvm':
ticket = proxobject.nodes(slave_name).qemu(vm_id).vncproxy.post(websocket=1)
#socket = proxobject.nodes(slave_name).qemu(vm_id).vncwebsocket.get(port=ticket['port'],
# vncticket=ticket['ticket'])
if vm_type == 'lxc':
ticket = proxobject.nodes(slave_name).lxc(vm_id).vncproxy.post()
#socket = proxobject.nodes(slave_name).lxc(vm_id).vncwebsocket.get(port=ticket['port'],
# vncticket=ticket['ticket'])
2016-11-03 00:05:51 -04:00
slaveip = ioconfig.parser.get(str(slave_name), 'ipv4')
2016-02-15 05:30:43 -05:00
#slaveport = socket['port']
slaveport = ticket['port']
2016-11-04 22:07:04 -04:00
slave_id = 1 #TODO: fix this
2016-11-11 09:56:35 -05:00
vnchost = ioconfig.parser.get('general', 'novnc_host')
2016-02-15 05:30:43 -05:00
listenport = str(int(slaveport) + 1000 + (int(slave_id) * 100)) #TODO: max 100 parallel connections/slave.
vnc_target = { 'target_host': slaveip,
'target_port': slaveport,
2016-11-11 09:56:35 -05:00
'listen_host': vnchost,
2016-02-15 06:14:19 -05:00
'listen_port': listenport
}
2016-07-03 21:33:16 -04:00
vnc_options = { 'idle-timeout': 20,
2016-11-11 09:56:35 -05:00
'verbose': True,
'cert': ioconfig.parser.get('general', 'ssl_cert'),
'key': ioconfig.parser.get('general', 'ssl_key'),
'ssl-only': True
2016-02-15 06:14:19 -05:00
}
2016-02-15 05:30:43 -05:00
novnc.spawn(vnc_target, vnc_options)
external_url = ioconfig.parser.get('general', 'novnc_url')
2016-11-11 09:56:35 -05:00
prefix = external_url + "?host=" + vnchost + "&port=" + listenport + "&view_only=false&encrypt=1&true_color=1&password="
2016-02-16 14:12:12 -05:00
vnc_url = prefix + ticket['ticket']
2016-02-15 05:30:43 -05:00
2017-09-20 20:07:56 -04:00
time.sleep(3) #wait few seconds for the parallel vncwebsocket
2017-07-19 15:28:26 -04:00
ioconfig.logger.info('{}[{}]> vnc port {} ready'.format(vm_owner, slave_name, listenport))
2016-06-26 11:09:22 -04:00
#response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] }
response = { 'status':'VNC', 'url':vnc_url }
2016-11-11 09:56:35 -05:00
#print(vnc_url)
2016-02-15 05:30:43 -05:00
return response
2016-11-11 09:56:35 -05:00
#def getmyip():
# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.connect(("gmail.com",80))
# myip = s.getsockname()[0]
# s.close
# return myip
2016-02-15 05:30:43 -05:00
if __name__ == '__main__':
#internal module tests
2016-05-08 08:44:19 -04:00
time.sleep(1)
#vmvnc(656758)
2016-02-15 05:30:43 -05:00