Unverified Commit e1283b60 authored by efikalti's avatar efikalti Committed by Avraam Tsantekidis

moved all methods to the core provisioner class

parent 796501af
......@@ -9,7 +9,7 @@ from kamaki.clients import astakos, cyclades
from kamaki.clients import ClientError
from kamaki.clients.utils import https
from kamaki.cli.config import Config as KamakiConfig
from fokia.cluster_error_constants import *
from cluster_error_constants import *
# TODO: remove this and actually use ssl cert files
https.patch_ignore_ssl()
......@@ -120,6 +120,132 @@ class Provisioner:
raise ex
return okeanos_response
def create_lambda_cluster(self, vm_name, **kwargs):
"""
:param vm_name: hostname of the master
:param kwargs: contains specifications of the vms.
"""
quotas = self.get_quotas()
vcpus = kwargs['slaves'] * kwargs['vcpus_slave'] + kwargs['vcpus_master']
ram = kwargs['slaves'] * kwargs['ram_slave'] + kwargs['ram_master']
disk = kwargs['slaves'] * kwargs['disk_slave'] + kwargs['disk_master']
response = self.check_all_resources(quotas, cluster_size=kwargs['cluster_size'],
vcpus=vcpus,
ram=ram,
disk=disk,
ip_request=kwargs['ip_request'],
network_request=kwargs['network_request'],
project_name=kwargs['project_name'])
if response:
# Create master
master = self.create_vm(vm_name=vm_name, vcpus=kwargs['vcpus_master'], ram=kwargs['ram_master'], disk=kwargs['disk_master'], **kwargs)
ip = self.reserve_ip()
self.attach_public_ip(ip, master['id'])
# Create private network for cluster
vpn = self.create_vpn('lambda-vpn')
self.create_private_subnet(vpn['id'])
# Connect master to vpn
self.connect_vm(master['id'], vpn['id'])
# Create slaves
slaves = list()
for i in range(kwargs['slaves']):
slave_name = 'lambda-node' + str(i+1)
slave = self.create_vm(vm_name=slave_name, vcpus=kwargs['vcpus_slave'], ram=kwargs['ram_slave'], disk=kwargs['disk_slave'], **kwargs)
self.connect_vm(slave['id'], vpn['id'])
slaves.append(slave)
def create_vpn(self, network_name):
"""
Creates a virtual private network
:param network_name: name of the network
:return: the id of the network if successfull
"""
try:
# Create vpn with custom type and the name given as argument
vpn = self.network_client.create_network(
type=self.network_client.network_types[1],
name=network_name)
return vpn['id']
except ClientError as ex:
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):
"""
Reserve ip
:return: the ip object if successfull
"""
try:
ip = self.network_client.create_floatingip()
return ip
except ClientError as ex:
raise ex
return okeanos_response
def create_private_subnet(self, net_id):
"""
Creates a private subnets and connects it with this network
:param net_id: id of the network
:return: the id of the subnet if successfull
"""
cidr = "192.168.0.0/24"
gateway_ip = "192.168.0.1"
try:
subnet = self.network_client.create_subnet(net_id, cidr,
gateway_ip=gateway_ip,
enable_dhcp=True)
return subnet['id']
except ClientError as ex:
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
:param vm_id: id of the vm
:param net_id: id of the network
:return: returns True if successfull
"""
try:
port = self.network_client.create_port(network_id=net_id,
device_id=vm_id)
return True
except ClientError as ex:
raise ex
return okeanos_response
def attach_public_ip(self, ip, vm_id):
"""
Attach the public ip with this id to the vm
:param fnet_id: id of the floating network of the ip
:param vm_id: id of the vm
:return: returns True if successfull
"""
try:
port = self.network_client.create_port(network_id=ip['floating_network_id'],
device_id=vm_id,
fixed_ips=[dict(ip_address=ip['floating_ip_address']), ])
return True
except ClientError as ex:
raise ex
return okeanos_response
def get_quotas(self, **kwargs):
"""
Get the user quotas for the defined project.
......@@ -199,13 +325,38 @@ class Provisioner:
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Okeanos VM provisioning")
parser.add_argument('--cloud', type=str, dest="cloud", default="lambda")
parser.add_argument('--cloud', type=str, dest="cloud", default="~okeanos")
parser.add_argument('--project-name', type=str, dest="project_name",
default="lambda.grnet.gr")
parser.add_argument('--name', type=str, dest='name', default="to mikro debian sto livadi")
parser.add_argument('--slaves', type=int, dest='slaves', default=1)
parser.add_argument('--vcpus_master', type=int, dest='vcpus_master', default=4)
parser.add_argument('--vcpus_slave', type=int, dest='vcpus_slave', default=4)
parser.add_argument('--ram_master', type=int, dest='ram_master', default=4096) # in MB
parser.add_argument('--ram_slave', type=int, dest='ram_slave', default=4096) # in MB
parser.add_argument('--disk_master', type=int, dest='disk_master', default=40) # in GB
parser.add_argument('--disk_slave', type=int, dest='disk_slave', default=40) # in GB
parser.add_argument('--ip_request', type=int, dest='ip_request', default=1)
parser.add_argument('--network_request', type=int, dest='network_request', default=1)
parser.add_argument('--image_name', type=str, dest='image_name', default="debian")
parser.add_argument('--cluster_size', type=int, dest='cluster_size', default=2)
args = parser.parse_args()
provisioner = Provisioner(cloud_name=args.cloud)
print(provisioner.create_vm(vm_name=args.name, project_name=args.project_name,
image_name="debian"))
# print(provisioner.create_vm(vm_name=args.name, project_name=args.project_name,
# image_name="debian"))
provisioner.create_lambda_cluster(vm_name="test" , slaves=args.slaves,
image_name=args.image_name,
cluster_size=args.cluster_size,
vcpus_master=args.vcpus_master,
vcpus_slave=args.vcpus_slave,
ram_master=args.ram_master,
ram_slave=args.ram_slave,
disk_master=args.disk_master,
disk_slave=args.disk_slave,
ip_request=args.ip_request,
network_request=args.network_request,
project_name=args.project_name)
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