Commit f8893ea0 by Chif Gergő

Start the InstanceInterface, OpenStack manager for the virtual machines

parent 2402a452
from interface.vm.instance import InstanceInterface
class OSVirtualMachineManager(InstanceInterface):
def __init__(self, cloud):
super().__init__()
self.openstack = cloud
def create_vm_from_template(self, name, resource, template):
flavor = self.openstack.compute.find_flavor('cirros256')
# TODO plan flavors
if flavor is None:
flavor = self.openstack.compute.create_flavor(name=name, ram=resource['ram'], vcpus=resource['cpu'], disk=20)
image = self.openstack.compute.find_image(template)
if not image:
raise ValueError("The template not found")
new_server = self.openstack.compute.create_server(name=name,
flavorRef=flavor.id,
imageRef=image.id)
new_server = self.openstack.compute.wait_for_server(new_server)
return new_server
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)
if not server_instance:
raise ValueError("Could not get the vm")
return server_instance
def start_vm(self, name_or_id=None):
if name_or_id:
instance = self.get_vm(name_or_id)
self.openstack.compute.start_server(instance)
def stop_vm(self, name_or_id=None):
if name_or_id:
instance = self.get_vm(name_or_id)
self.openstack.compute.stop_server(instance)
from ..client_interface.vm.instance import InstanceInterface
import openstack
class OSInstanceInterface(InstanceInterface):
def __init__(self, cloud):
super().__init__(self)
self.openstack = openstack.connect(cloud)
def create_vm_from_template(self, name, resource, template):
flavor = self.openstack.compute.find_flavor(name)
if not flavor:
flavor = self.openstack.compute.Flavor(nam=name, ram=resource.ram, vcpus=resource.cpu)
image = self.openstack.compute.find_image(template)
if not image:
raise ValueError("The template not found")
new_server = self.openstack.compute.create_server(name=name,
flavorRef=flavor.id,
imageRef=image.id)
new_server = conn.compute.wait_for_server(new_server)
return new_server
import json
class Instance:
''' The releveant data of the virtual machine instances
This data would be sent by the interface to the portal,
when it requests a virtual machine instance and vica versa
'''
ssh_keys = None
console_access_url = None
def __init__(self, id, name, resource, resource_name, image_id,
disks=None, interfaces=None):
self.id = id
self.name = name
self.resource = resource
self.resource_name = resource_name
self.image = image_id
self.disks = [disks]
self.interfaces = None
def JSON(self):
return json.dump(self)
......@@ -8,70 +8,70 @@ It should be implemented for using other providers e. g. OpenStack
class InstanceInterface:
def create_vm(self, name, resource):
pass
raise NotImplementedError
def create_vm_from_template(self, template, resource):
pass
raise NotImplementedError
def create_multiple_vm_from_template(self, template, resource, number):
pass
raise NotImplementedError
def get_vm_by_id(self, id):
pass
def get_vm(self, name_or_id):
raise NotImplementedError
def start_vm(self):
pass
def start_vm(self, name_or_id):
raise NotImplementedError
def stop_vm(self):
pass
def stop_vm(self, name_or_id):
raise NotImplementedError
def suspend_vm(self):
pass
def suspend_vm(self, name_or_id):
raise NotImplementedError
def wake_up_vm(self):
pass
def wake_up_vm(self, name_or_id):
raise NotImplementedError
def shut_off_vm(self):
pass
def shut_off_vm(self, name_or_id):
raise NotImplementedError
def reboot_vm(self):
pass
def reboot_vm(self, name_or_id):
raise NotImplementedError
def reset_vm(self):
pass
def reset_vm(self, name_or_id):
raise NotImplementedError
def destroy_vm(self):
pass
def destroy_vm(self, name_or_id):
raise NotImplementedError
def migrate_vm(self):
pass
def migrate_vm(self, name_or_id, to):
raise NotImplementedError
def get_status(self):
pass
def get_status(self, name_or_id):
raise NotImplementedError
def list_all_vm(self):
pass
def list_all_vm(self, name_or_id):
raise NotImplementedError
def install_ssh_key(self, key):
pass
def install_ssh_key(self, name_or_id, key):
raise NotImplementedError
def save_as_template(self):
pass
def save_as_template(self, name_or_id):
raise NotImplementedError
def get_vnc_console(self):
pass
def get_vnc_console(self, name_or_id):
raise NotImplementedError
def change_password(self):
pass
def change_password(self, name_or_id):
raise NotImplementedError
def get_password(self):
pass
def get_password(self, name_or_id):
raise NotImplementedError
def resize_vm(self):
pass
def resize_vm(self, name_or_id):
raise NotImplementedError
def create_template(self):
pass
def create_template(self, name_or_id, template_name):
raise NotImplementedError
def attach_volume(self):
pass
def attach_volume(self, name_or_id, amount):
raise NotImplementedError
# This file is for testing the openstack api access
from implementation.instance import OSVirtualMachineManager
import openstack
openstack.enable_logging(debug=True)
conn = openstack.connect(cloud='openstack')
interface = OSVirtualMachineManager(conn)
resource = {"ram": 1024, "cpu": 2}
print('#'*40)
print(resource)
# interface.create_vm_from_template('new_server', resource, "cirros-0.4.0-x86_64-disk")
interface.stop_vm('a6bc504f-a422-4492-b429-e5dad2df12f4')
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