Commit aec5cf35 authored by efikalti's avatar efikalti

reserve ip first from the pool of existing ips and then create more if needed

parent 51104772
......@@ -67,6 +67,9 @@ class Provisioner:
self.private_key = None
self.image_id = 'c6f5adce-21ad-4ce3-8591-acfe7eb73c02'
"""
FIND RESOURCES
"""
def find_flavor(self, **kwargs):
"""
......@@ -113,37 +116,9 @@ class Provisioner:
logger.info("Retrieving project")
return self.astakos.get_projects(**filter)[0]
def create_vm(self, vm_name=None, image_id=None, ip=None, personality=None, **kwargs):
"""
:param vm_name: Name of the virtual machine to create
:param image_id: image id if you want another image than the default
:param kwargs: passed to the functions called for detail options
:return:
"""
flavor_id = self.find_flavor(**kwargs)['id']
# Get image
if image_id == None:
image_id = self.image_id
else:
image_id = self.find_image(**kwargs)['id']
project_id = self.find_project_id(**kwargs)['id']
networks = list()
if ip != None:
ip_obj = dict()
ip_obj['uuid'] = ip['floating_network_id']
ip_obj['fixed_ip'] = ip['floating_ip_address']
networks.append(ip_obj)
networks.append({'uuid': kwargs['net_id']})
if personality == None:
personality = []
try:
okeanos_response = self.cyclades.create_server(name=vm_name, flavor_id=flavor_id,
image_id=image_id,
project_id=project_id,
networks=networks, personality=personality)
except ClientError as ex:
raise ex
return okeanos_response
"""
CREATE RESOURCES
"""
def create_lambda_cluster(self, vm_name, **kwargs):
"""
......@@ -239,56 +214,37 @@ class Provisioner:
inventory["slaves"] = self.slaves
return inventory
def get_cluster_details(self):
def create_vm(self, vm_name=None, image_id=None, ip=None, personality=None, **kwargs):
"""
:returns: dictionary of basic details for the cluster
:param vm_name: Name of the virtual machine to create
:param image_id: image id if you want another image than the default
:param kwargs: passed to the functions called for detail options
:return:
"""
details = dict()
nodes = dict()
master = dict()
master['id'] = self.master['id']
master['name'] = self.master['name']
master['adminPass'] = self.master['adminPass']
nodes['master'] = master
slaves = list()
for slave in self.slaves:
slave_obj = dict()
slave_obj['id'] = slave['id']
slave_obj['name'] = slave['name']
name = slave_obj['name']
slaves.append(slave_obj)
nodes['slaves'] = slaves
details['nodes'] = nodes
vpn = dict()
vpn['id'] = self.vpn['id']
vpn['type'] = self.vpn['type']
details['vpn'] = vpn
details['ips'] = self.ips
ips_list = list()
for ip in self.ips:
flavor_id = self.find_flavor(**kwargs)['id']
# Get image
if image_id == None:
image_id = self.image_id
else:
image_id = self.find_image(**kwargs)['id']
project_id = self.find_project_id(**kwargs)['id']
networks = list()
if ip != None:
ip_obj = dict()
ip_obj['floating_network_id'] = ip['floating_network_id']
ip_obj['floating_ip_address'] = ip['floating_ip_address']
ip_obj['id'] = ip['id']
ips_list.append(ip_obj)
details['ips'] = ips_list
subnet = dict()
subnet['id'] = self.subnet['id']
subnet['cidr'] = self.subnet['cidr']
subnet['gateway_ip'] = self.subnet['gateway_ip']
details['subnet'] = subnet
return details
def get_private_key(self):
"""
:returns: Private key of master
"""
return self.private_key
ip_obj['uuid'] = ip['floating_network_id']
ip_obj['fixed_ip'] = ip['floating_ip_address']
networks.append(ip_obj)
networks.append({'uuid': kwargs['net_id']})
if personality == None:
personality = []
try:
okeanos_response = self.cyclades.create_server(name=vm_name, flavor_id=flavor_id,
image_id=image_id,
project_id=project_id,
networks=networks, personality=personality)
except ClientError as ex:
raise ex
return okeanos_response
def create_vpn(self, network_name, project_id):
"""
......@@ -307,24 +263,15 @@ class Provisioner:
raise ex
return okeanos_response
def destroy_vpn(self, id):
"""
Destroy a virtual private network
:param id: id of the network we want to destroy
:return: True if successfull
"""
try:
self.network_client.delete_network(id)
return True
except ClientError as ex:
raise ex
return okeanos_response
def reserve_ip(self,project_id):
"""
Reserve ip
:return: the ip object if successfull
"""
list_float_ips = self.network_client.list_floatingips()
for ip in list_float_ips:
if ip['instance_id'] is None and ip['port_id'] is None:
return ip
try:
ip = self.network_client.create_floatingip(project_id=project_id)
return ip
......@@ -348,6 +295,7 @@ class Provisioner:
raise ex
return okeanos_response
def connect_vm(self, vm_id, net_id):
"""
Connects the vm with this id to the network with the net_id
......@@ -379,6 +327,111 @@ class Provisioner:
raise ex
return okeanos_response
"""
DELETE RESOURCES
"""
def delete_lambda_cluster(self, details):
"""
Delete a lambda cluster
:param details: details of the cluster we want to delete
:return: True if successfull
"""
def delete_vm(self, vm_id):
"""
Delete a vm
:param vm_id: id of the vm we want to delete
:return: True if successfull
"""
try:
self.cyclades.delete_server(vm_id)
return True
except ClientError as ex:
raise ex
return False
def delete_private_subnet(self, subnet_id):
"""
Delete a subnet
:param subnet_id: id of the subnet we want to delete
:return: True if successfull
"""
try:
self.network_client.delete_subnet(subnet_id)
return True
except ClientError as ex:
raise ex
return False
def delete_vpn(self, net_id):
"""
Delete a virtual private network
:param net_id: id of the network we want to delete
:return: True if successfull
"""
try:
self.network_client.delete_network(id)
return True
except ClientError as ex:
raise ex
return False
"""
GET RESOURCES
"""
def get_cluster_details(self):
"""
:returns: dictionary of basic details for the cluster
"""
details = dict()
nodes = dict()
master = dict()
master['id'] = self.master['id']
master['name'] = self.master['name']
master['adminPass'] = self.master['adminPass']
nodes['master'] = master
slaves = list()
for slave in self.slaves:
slave_obj = dict()
slave_obj['id'] = slave['id']
slave_obj['name'] = slave['name']
name = slave_obj['name']
slaves.append(slave_obj)
nodes['slaves'] = slaves
details['nodes'] = nodes
vpn = dict()
vpn['id'] = self.vpn['id']
vpn['type'] = self.vpn['type']
details['vpn'] = vpn
details['ips'] = self.ips
ips_list = list()
for ip in self.ips:
ip_obj = dict()
ip_obj['floating_network_id'] = ip['floating_network_id']
ip_obj['floating_ip_address'] = ip['floating_ip_address']
ip_obj['id'] = ip['id']
ips_list.append(ip_obj)
details['ips'] = ips_list
subnet = dict()
subnet['id'] = self.subnet['id']
subnet['cidr'] = self.subnet['cidr']
subnet['gateway_ip'] = self.subnet['gateway_ip']
details['subnet'] = subnet
return details
def get_private_key(self):
"""
:returns: Private key of master
"""
return self.private_key
def get_quotas(self, **kwargs):
"""
Get the user quotas for the defined project.
......@@ -415,6 +468,9 @@ class Provisioner:
return ip
return None
"""
CHECK RESOURCES
"""
def check_all_resources(self, quotas, **kwargs):
"""
Checks user's quota for every requested resource.
......@@ -526,5 +582,5 @@ if __name__ == "__main__":
network_request=args.network_request,
project_name=args.project_name)
# print(response)
# print(provisioner.get_cluster_details())
print(provisioner.get_cluster_details())
# print(provisioner.get_private_key())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment