Commit 43003cf9 by Belákovics Ádám

Base vm creation now works, create methods for flavor operations, rename some parameters

parent b77968ab
Pipeline #708 failed with stage
in 37 seconds
from interface.vm.instance import InstanceInterface
from interface.vm.resources import Instance
from interface.vm.resources import Instance, Flavor
from openstack.exceptions import SDKException
from novaclient import client
import logging
......@@ -24,8 +24,8 @@ def openstackError(func):
try:
return func(*args, **kw)
except SDKException as e:
logging.error(e.get_error_message())
new_e = Exception(e.get_error_message())
logging.error(e)
new_e = Exception(e)
new_e.OpenStackError = True
raise new_e
return wrap_OpenStackError
......@@ -38,24 +38,27 @@ class OSVirtualMachineManager(InstanceInterface):
self.openstack = cloud
@openstackError
def create_base_vm(self, name, resource, networks, block_dev_map):
flavor = self.get_flavor(resource)
new_server = self.compute.create_server(name=name,
def create_base_vm(self, name, flavor, networks, block_dev_map):
devices = []
b_device = block_dev_map.__dict__
devices.append(b_device)
flavor = self.get_flavor(flavor)
new_server = self.openstack.compute.create_server(name=name,
flavorRef=flavor.id,
networks=networks,
block_device_mapping=block_dev_map
block_device_mapping=devices
)
return new_server
return self.convert_server_to_instance(new_server)
@openstackError
def create_vm_from_template(self, name, image, resource, networks):
self.create_multiple_vm_from_template(name, image, resource, networks, 1)
def create_vm_from_template(self, name, image, flavor, networks):
self.create_multiple_vm_from_template(name, image, flavor, networks, 1)
@openstackError
def create_multiple_vm_from_template(self, name, image, resource, networks,
def create_multiple_vm_from_template(self, name, image, flavor, networks,
number, **args):
compute = self.openstack.compute
flav = compute.find_flavor(resource)
flav = compute.find_flavor(flavor)
image = compute.find_image(image)
if not image:
......@@ -67,76 +70,104 @@ class OSVirtualMachineManager(InstanceInterface):
networks=networks,
min_count=number,
)
return self.convert_server_to_instance(new_server)
new_server = self.openstack.compute.wait_for_server(new_server)
@openstackError
def create_flavor(self, name, ram, vcpus, initial_disk):
flavor = self.openstack.compute.create_flavor(name=name,
ram=ram,
vcpus=vcpus,
disk=initial_disk)
return Flavor(flavor.name, flavor.id, flavor.ram,
flavor.vcpus, flavor.disk)
return new_server
@openstackError
def get_flavor(self, flavor_id):
flavor = self.openstack.compute.find_flavor(flavor_id)
return Flavor(flavor.name, flavor.id, flavor.ram,
flavor.vcpus, flavor.disk)
@openstackError
def delete_flavor(self, flavor_id):
flavor = self.openstack.compute.find_flavor(flavor_id)
self.openstack.compute.delete_flavor(flavor)
@openstackError
def list_flavors(self):
flavors = []
for flavor in self.openstack.compute.flavors():
flavors.append(Flavor(flavor.name, flavor.id, flavor.ram,
flavor.vcpus, flavor.disk))
return flavors
@openstackError
def get_vm(self, name_or_id=None):
if not name_or_id:
raise ValueError("Name or id doesn't given")
server_instance = self.openstack.get_server(name_or_id)
server_instance = self.openstack.compute.get_server(name_or_id)
if not server_instance:
raise ValueError("Could not get the vm")
return server_instance
return self.convert_server_to_instance(server_instance)
@openstackError
def start_vm(self, name_or_id=None):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.start_server(instance)
@openstackError
def stop_vm(self, name_or_id=None):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.stop_server(instance)
@openstackError
def suspend_vm(self, name_or_id=None):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.suspend_server(instance)
@openstackError
def wake_up_vm(self, name_or_id=None):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.resume_server(instance)
@openstackError
def reboot_vm(self, name_or_id):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='SOFT')
@openstackError
def reset_vm(self, name_or_id):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='HARD')
@openstackError
def destroy_vm(self, name_or_id):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.delete_server(instance)
@openstackError
def get_status(self, name_or_id):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
return instance.status
@openstackError
def list_all_vm(self):
return self.openstack.compute.servers()
servers = []
for server in self.openstack.compute.servers():
servers.append(self.convert_server_to_instance(server))
return servers
@openstackError
def resize_vm(self, name_or_id, resource):
if name_or_id:
instance = self.get_vm(name_or_id)
instance = self.openstack.get_server(name_or_id)
flavor = self.openstack.compute.find_flavor(resource['name'])
self.openstack.compute.resize_server(instance, flavor)
......@@ -144,17 +175,30 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError
def create_template(self, name_or_id, template_name, metadata=None):
if name_or_id:
instance = self.get_vm(name_or_id)
self.openstack.compute.create_server_image(instance, template_name, metadata)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.create_server_image(instance,
template_name,
metadata)
def get_vnc_console(self, name_or_id):
def get_vnc_console(self, server_id):
with client.Client("2", session=sess) as nova:
if name_or_id:
instance = nova.servers.get(name_or_id)
if server_id:
instance = nova.servers.get(server_id)
return instance.get_vnc_console("novnc")
def attach_volume(self, name_or_id, amount):
raise NotImplementedError
@openstackError
def attach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.create_volume_attachment(server_id,
{"volumeId": volume_id,
"device": device
})
@openstackError
def detach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.delete_volume_attachment(server_id,
{"volumeId": volume_id,
"device": device
})
def convert_server_to_instance(self, server):
if not server.image:
......@@ -162,7 +206,7 @@ class OSVirtualMachineManager(InstanceInterface):
else:
image_id = server.image.id
return Instance(id=server.id,
resource=server.flavor.id,
flavor=server.flavor.id,
name=server.name,
image_id=image_id,
disks=server.volumes,
......
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 sign in to comment