create kvms with vlan tag

This commit is contained in:
deflax 2018-01-25 01:09:30 +02:00
parent 3ec5755715
commit f15ba13249
2 changed files with 31 additions and 29 deletions

11
grid.py
View file

@ -61,16 +61,15 @@ def delete(data):
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 """ scans all current db files and generate new id within a range between
1000 - 1999 for bridges, 101 - 150 for deployments and avoids any duplicates.
2000 - 2999 for routers, reserved ids:
and 7000 - 7999 for application servers 100 - FrankenROUTER instance """
Avoid any duplicates. """
if str(unit_type) == 'br': if str(unit_type) == 'br':
full_list = list(range(1000,2999)) full_list = list(range(1000,2999))
if str(unit_type) == 'lxc': if str(unit_type) == 'lxc':
full_list = list(range(2000,2999)) full_list = list(range(2000,2999))
if str(unit_type) == 'kvm': if str(unit_type) == 'kvm':
full_list = list(range(7000,7999)) full_list = list(range(100,150))
exclude_list = [] exclude_list = []
directory = 'db/' directory = 'db/'
for dbfile in os.listdir(directory): for dbfile in os.listdir(directory):
@ -91,7 +90,7 @@ def phyidgen(slave_name, unit_type):
return choice return choice
else: else:
logger.critical('{}> no free physical ids!'.format(slave_name)) logger.critical('{}> no free physical ids!'.format(slave_name))
return none return 99999
def analyze_happiness(region_id): def analyze_happiness(region_id):
""" analyzes grid data for the reuqested region and returns proposed slave_id, """ analyzes grid data for the reuqested region and returns proposed slave_id,

View file

@ -41,14 +41,17 @@ def create(json):
except Exception as e: except Exception as e:
ioconfig.logger.warning('grid> slave not predefined. I will query for a capable one.') ioconfig.logger.warning('grid> slave not predefined. I will query for a capable one.')
#slave_name = str(grid.query_happiness(region_id, weight)) #slave_name = str(grid.query_happiness(region_id, weight))
#slave_name = 'lexx' #slave_name = 'warrior'
slave_name = 'warrior' slave_name = 'lexx'
ioconfig.logger.info('{}> slave selected'.format(slave_name)) ioconfig.logger.info('{}> slave selected'.format(slave_name))
proxobject = auth(slave_name) proxobject = auth(slave_name)
real_slave_name = proxobject.cluster.status.get()[0]['name'] real_slave_name = proxobject.cluster.status.get()[0]['name']
unit_id = int(time.time() * 10000 * 10000) #currently unit_id is just a timestamp unit_id = int(time.time() * 10000 * 10000) #currently unit_id is just a timestamp
phy_id = grid.phyidgen(slave_name, json['type']) phy_id = grid.phyidgen(slave_name, json['type'])
if phy_id == 99999:
response = { 'status': 'phy_id_alloc_failed' }
return response
description = ' (' + str(unit_id) + ' - ' + str(phy_id) + ')\n' + 'owned by ' + json['clientemail'] + ' (' + json['clientid'] + ')\n' description = ' (' + str(unit_id) + ' - ' + str(phy_id) + ')\n' + 'owned by ' + json['clientemail'] + ' (' + json['clientid'] + ')\n'
if json['type'] == 'kvm': if json['type'] == 'kvm':
@ -72,7 +75,7 @@ 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'], net0='virtio,bridge=' + json['net0if'] + 'tag=' + str(phy_id),
description=description) description=description)
except Exception as e: except Exception as e:
print(e) print(e)
@ -84,12 +87,12 @@ def create(json):
'clientemail': json['clientemail'], 'clientemail': json['clientemail'],
'hostname': vm_name, 'hostname': vm_name,
'region': region_name, 'region': region_name,
'slave': slave_name, 'slave': real_slave_name,
'phy_id': phy_id, 'phy_id': phy_id,
'net0if': json['net0if'] 'net0if': json['net0if']
} }
grid.create(data) grid.create(data)
response = { 'status': 'kvm_created', 'unit_id': unit_id, 'hostname': vm_name, 'region': region_name, 'slave': real_slave_name } response = { 'status': 'kvm_created', 'unit_id': unit_id, 'region': region_name, 'slave': real_slave_name, 'vlanid': phy_id}
if json['type'] == 'lxc': if json['type'] == 'lxc':
vm_name_utf8 = json['hostname'] vm_name_utf8 = json['hostname']
@ -136,24 +139,24 @@ def create(json):
grid.create(data) grid.create(data)
response = { 'status': 'lxc_created', 'unit_id': unit_id, 'hostname': vm_name, 'region': region_name, 'slave': real_slave_name } response = { 'status': 'lxc_created', 'unit_id': unit_id, 'hostname': vm_name, 'region': region_name, 'slave': real_slave_name }
if json['type'] == 'br': #if json['type'] == 'br':
try: # try:
create_result = proxobject.nodes(real_slave_name).network.post(iface='vmbr' + str(phy_id), # create_result = proxobject.nodes(real_slave_name).network.post(iface='vmbr' + str(phy_id),
type='bridge', # type='bridge',
autostart=1) # autostart=1)
except Exception as e: # except Exception as e:
print(e) # print(e)
return { 'status': 'br_create_failed' } # return { 'status': 'br_create_failed' }
data = { 'unit_id': int(unit_id), # data = { 'unit_id': int(unit_id),
'type': 'br', # 'type': 'br',
'clientid': json['clientid'], # 'clientid': json['clientid'],
'clientemail': json['clientemail'], # 'clientemail': json['clientemail'],
'region': region_name, # 'region': region_name,
'slave': slave_name, # 'slave': slave_name,
'phy_id': phy_id # 'phy_id': phy_id
} # }
grid.create(data) # grid.create(data)
response = { 'status': 'bridge_created', 'unit_id': unit_id, 'region': region_name, 'slave': real_slave_name, 'phy_id': phy_id } # response = { 'status': 'bridge_created', 'unit_id': unit_id, 'region': region_name, 'slave': real_slave_name, 'phy_id': phy_id }
return response return response