Commit ec33a316 by Chif Gergő

Can get VNC console, fix instance creation

parent c882edb7
......@@ -9,6 +9,8 @@ verify_ssl = true
openstacksdk = "*"
novaclient = "*"
python-novaclient = "*"
keystoneauth1 = "*"
keystoneauth = "*"
[requires]
python_version = "3.6"
from interface.vm.instance import InstanceInterface
from interface.vm.resources import Instance
from openstack.exceptions import SDKException
# from novaclient import client
from novaclient import client
import logging
from keystoneauth1.identity import v3
from keystoneauth1 import session
auth = v3.Password(auth_url="http://10.34.0.113/identity/v3",
username="admin",
password="64c7ee341d03844c548c",
user_domain_id='default',
project_id="2db5309f541c4466bc80bc534cf579d7")
sess = session.Session(auth=auth)
def openstackError(func):
......@@ -38,20 +48,20 @@ class OSVirtualMachineManager(InstanceInterface):
return new_server
@openstackError
def create_vm_from_template(self, image, resource, networks):
self.create_multiple_vm_from_template(image, resource, networks, 1)
def create_vm_from_template(self, name, image, resource, networks):
self.create_multiple_vm_from_template(name, image, resource, networks, 1)
@openstackError
def create_multiple_vm_from_template(self, image, resource, networks,
def create_multiple_vm_from_template(self, name, image, resource, networks,
number, **args):
compute = self.openstack.compute
flav = compute.find_flavor(resource['name'])
flav = compute.find_flavor(resource)
image = compute.find_image(image)
if not image:
raise ValueError("The template not found")
new_server = compute.create_server(name=image,
new_server = compute.create_server(name=name,
flavorRef=flav.id,
imageRef=image.id,
networks=networks,
......@@ -124,10 +134,6 @@ class OSVirtualMachineManager(InstanceInterface):
return self.openstack.compute.servers()
@openstackError
def get_vnc_console(self, name_or_id):
raise NotImplementedError
@openstackError
def resize_vm(self, name_or_id, resource):
if name_or_id:
instance = self.get_vm(name_or_id)
......@@ -141,22 +147,26 @@ class OSVirtualMachineManager(InstanceInterface):
instance = self.get_vm(name_or_id)
self.openstack.compute.create_server_image(instance, template_name, metadata)
# def get_vnc_console(self, name_or_id):
# with client.Client(2, "admin", "64c7ee341d03844c548c",
# "2db5309f541c4466bc80bc534cf579d7", "http://10.34.0.113/identity") as nova:
# if name_or_id:
# instance = nova.servers.get(name_or_id)
# return instance.get_vnc_console()
def get_vnc_console(self, name_or_id):
with client.Client("2", session=sess) as nova:
if name_or_id:
instance = nova.servers.get(name_or_id)
return instance.get_vnc_console("novnc")
def attach_volume(self, name_or_id, amount):
raise NotImplementedError
def convert_server_to_instance(self, server):
if not server.image:
image_id = None
else:
image_id = server.image.id
return Instance(id=server.id,
resource=server.flavor.id,
name=server.name,
image_id=server.image,
image_id=image_id,
disks=server.volumes,
interfaces=server.interfaces,
status=server.status,
launched_at=server.launched_at,
terminated_at=server.terminated_at,
addresses=server.addresses)
......@@ -7,9 +7,6 @@ It should be implemented for using other providers e. g. OpenStack
class InstanceInterface:
def create_vm(self, name, resource):
raise NotImplementedError
def create_vm_from_template(self, template, resource):
raise NotImplementedError
......
......@@ -10,19 +10,20 @@ class Instance:
ssh_keys = None
console_access_url = None
def __init__(self, id, name, resource, image_id, interfaces,
status, addresses, disks=None):
def __init__(self, id, name, resource, image_id,
status, addresses, launched_at, terminated_at, disks=None):
self.id = id
self.name = name
self.resource = resource
self.image = image_id
self.disks = disks
self.interfaces = interfaces
self.status = status
self.addresses = addresses
self.launched_at = launched_at
self.terminated_at = terminated_at
def JSON(self):
return json.dump(self)
return json.dumps(self.__dict__)
class Flavor:
......@@ -33,5 +34,6 @@ class Flavor:
self.vcpus = vcpus
self.initial_disk = disk
class Volume:
pass
......@@ -2,7 +2,7 @@
from implementation.vm.instance import OSVirtualMachineManager
import openstack
openstack.enable_logging(debug=True)
# openstack.enable_logging(debug=True)
conn = openstack.connect(cloud='openstack')
......@@ -15,10 +15,12 @@ print(resource)
# interface.stop_vm('a6bc504f-a422-4492-b429-e5dad2df12f4')
server = interface.get_vm("New test")
print(server)
print(interface.convert_server_to_instance(server).JSON())
print(interface.get_status("Uborka Test"))
print(interface.get_vnc_console("8e94c162-f8ed-4872-b9e5-50bf33040b5b"))
# print(interface.convert_server_to_instance(server))
# print(interface.get_vnc_console("New test"))
......
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