create-activate phase 3

This commit is contained in:
deflax 2017-12-15 01:00:17 +02:00
parent c5440e311e
commit 3ec5755715
2 changed files with 28 additions and 14 deletions

17
grid.py
View file

@ -60,8 +60,17 @@ def delete(data):
return None return None
def phyidgen(slave_name, unit_type): def phyidgen(slave_name, unit_type):
""" scans all current db files and generate new id within a range between 1000 and 9999, and avoid any duplicates """ """ scans all current db files and generate new id within a range between
full_list = list(range(1000,10000)) 1000 - 1999 for bridges,
2000 - 2999 for routers,
and 7000 - 7999 for application servers
Avoid any duplicates. """
if str(unit_type) == 'br':
full_list = list(range(1000,2999))
if str(unit_type) == 'lxc':
full_list = list(range(2000,2999))
if str(unit_type) == 'kvm':
full_list = list(range(7000,7999))
exclude_list = [] exclude_list = []
directory = 'db/' directory = 'db/'
for dbfile in os.listdir(directory): for dbfile in os.listdir(directory):
@ -76,7 +85,9 @@ def phyidgen(slave_name, unit_type):
valid_list = list(set(full_list) - set(exclude_list)) valid_list = list(set(full_list) - set(exclude_list))
if len(valid_list) > 1: if len(valid_list) > 1:
choice = random.choice(valid_list) choice = random.choice(valid_list)
logger.info('{}> physical id generated: {}'.format(slave_name, choice)) #logger.info('{}[{}]> unit type: {} physical id: {}'.format(str(json['clientemail']), str(slave_name), str(unit_type), str(choice)))
logger.info('[{}]> unit type: {} physical id: {}'.format(str(slave_name), str(unit_type), str(choice)))
print(str(exclude_list))
return choice return choice
else: else:
logger.critical('{}> no free physical ids!'.format(slave_name)) logger.critical('{}> no free physical ids!'.format(slave_name))

View file

@ -72,8 +72,10 @@ def create(json):
memory=json['mem'], memory=json['mem'],
scsihw='virtio-scsi-pci', scsihw='virtio-scsi-pci',
scsi0='file=lvm:' + image_name + ',discard=on', scsi0='file=lvm:' + image_name + ',discard=on',
net0='virtio,bridge=' + json['net0if'],
description=description) description=description)
except: except Exception as e:
print(e)
response = { 'status': 'kvm_create_failed' } response = { 'status': 'kvm_create_failed' }
return response return response
data = { 'unit_id': int(unit_id), data = { 'unit_id': int(unit_id),
@ -93,14 +95,14 @@ def create(json):
vm_name_utf8 = json['hostname'] vm_name_utf8 = json['hostname']
vm_name = unidecode(vm_name_utf8) vm_name = unidecode(vm_name_utf8)
image_name = 'vm-' + str(phy_id) + '-disk-1' image_name = 'vm-' + str(phy_id) + '-disk-1'
try: #try:
local_storage = proxobject.nodes(real_slave_name).storage('lvm') # local_storage = proxobject.nodes(real_slave_name).storage('lvm')
storage_create_result = local_storage.content.post(vmid=phy_id, filename=image_name, size=json['hdd'] + 'G') # storage_create_result = local_storage.content.post(vmid=phy_id, filename=image_name, size=json['hdd'] + 'G')
ioconfig.logger.info('%s[%s]> allocated %s as %s' % (json['clientemail'], slave_name, json['hdd'], image_name)) # ioconfig.logger.info('%s[%s]> allocated %s as %s' % (json['clientemail'], slave_name, json['hdd'], image_name))
except: #except:
ioconfig.logger.info('%s[%s]> unable to allocate %s' % (json['clientemail'], slave_name, image_name)) # ioconfig.logger.info('%s[%s]> unable to allocate %s' % (json['clientemail'], slave_name, image_name))
response = { 'status':'vol_alloc_failed' } # response = { 'status':'vol_alloc_failed' }
return response # return response
try: try:
vm_pass = json['rootpass'] vm_pass = json['rootpass']
except: except:
@ -113,9 +115,10 @@ def create(json):
password=vm_pass, password=vm_pass,
cores=json['cpu'], cores=json['cpu'],
memory=json['mem'], memory=json['mem'],
net0='name=eth0,bridge=' + json['net0if'] + ',gw=' + json['region_gw'] + ',hwaddr=' + json['macaddr'] + ',ip=' + json['ipv4addr'] + '/' + json['region_netmask'], net0='name=eth0,bridge=' + json['net0if'] + ',ip=' + json['net0ip'] + '/' + json['net0mask'],
net1='name=eth1,bridge=' + json['net1if'] + ',ip=' + json['net1ip'] + '/' + json['net1mask'] + ',gw=' + json['net1gw'],
ostemplate='backup:vztmpl/debian-9.0-standard_9.0-2_amd64.tar.gz', ostemplate='backup:vztmpl/debian-9.0-standard_9.0-2_amd64.tar.gz',
rootfs='volume=lvm:' + image_name, rootfs='volume=lvm:' + str(json['hdd']),
swap=32, swap=32,
description=description) description=description)
except: except: