Commit ec33a316 by Chif Gergő

Can get VNC console, fix instance creation

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