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