Commit 9e46f1ed by Belákovics Ádám

The VM implementation is needed for testing.

Merge remote-tracking branch 'origin/vm'
parents d2c380cf ec33a316
Pipeline #711 failed with stage
in 48 seconds
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don’t work, or not
# install all needed dependencies.
#Pipfile.lock
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files ✘ cgergo@cgergo  ~/mount/virtual/interface_openstack   vm ●  git status
On branch vm
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
deleted: implementation/instance.py
deleted: implementation/instance.pyc
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# Cloud configure
clouds.yaml
# pyc files
*.pyc
......@@ -7,6 +7,10 @@ verify_ssl = true
[packages]
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
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 wrap_OpenStackError(*args, **kw):
""" Decorator to wrap openstack error in simple Exception.
Return decorated function
"""
try:
return func(*args, **kw)
except SDKException as e:
logging.error(e.get_error_message())
new_e = Exception(e.get_error_message())
new_e.OpenStackError = True
raise new_e
return wrap_OpenStackError
class OSVirtualMachineManager(InstanceInterface):
def __init__(self, cloud):
super().__init__()
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,
flavorRef=flavor.id,
networks=networks,
block_device_mapping=block_dev_map
)
return new_server
@openstackError
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, name, image, resource, networks,
number, **args):
compute = self.openstack.compute
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=name,
flavorRef=flav.id,
imageRef=image.id,
networks=networks,
min_count=number,
)
new_server = self.openstack.compute.wait_for_server(new_server)
return new_server
@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)
if not server_instance:
raise ValueError("Could not get the vm")
return server_instance
@openstackError
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)
@openstackError
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)
@openstackError
def suspend_vm(self, name_or_id=None):
if name_or_id:
instance = self.get_vm(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)
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)
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)
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)
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)
return instance.status
@openstackError
def list_all_vm(self):
return self.openstack.compute.servers()
@openstackError
def resize_vm(self, name_or_id, resource):
if name_or_id:
instance = self.get_vm(name_or_id)
flavor = self.openstack.compute.find_flavor(resource['name'])
self.openstack.compute.resize_server(instance, flavor)
@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)
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=image_id,
disks=server.volumes,
status=server.status,
launched_at=server.launched_at,
terminated_at=server.terminated_at,
addresses=server.addresses)
......@@ -4,70 +4,71 @@ It should be implemented for using other providers e. g. OpenStack
"""
class InstanceInterface:
def create_vm(self, name, resource):
pass
class InstanceInterface:
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 get_status(self):
pass
def migrate_vm(self, name_or_id, to):
raise NotImplementedError
def list_all_vm(self):
pass
def get_status(self, name_or_id):
raise NotImplementedError
def install_ssh_key(self, key):
pass
def list_all_vm(self):
raise NotImplementedError
def save_as_template(self):
pass
def install_ssh_key(self, name_or_id, key):
raise NotImplementedError
def get_vnc_console(self):
pass
def save_as_template(self, name_or_id):
raise NotImplementedError
def change_password(self):
pass
def get_vnc_console(self, name_or_id):
raise NotImplementedError
def get_password(self):
pass
# def change_password(self, name_or_id):
# raise NotImplementedError
#
# 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
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, 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.status = status
self.addresses = addresses
self.launched_at = launched_at
self.terminated_at = terminated_at
def JSON(self):
return json.dumps(self.__dict__)
class Flavor:
def __init__(self, name, id, ram, vcpus, disk):
self.id = id
self.name = name
self.ram = ram
self.vcpus = vcpus
self.initial_disk = disk
class Volume:
pass
# This file is for testing the openstack api access
from implementation.vm.instance import OSVirtualMachineManager
import openstack
# openstack.enable_logging(debug=True)
conn = openstack.connect(cloud='openstack')
interface = OSVirtualMachineManager(conn)
resource = {"name": "m1.tiny", "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')
server = interface.get_vm("New test")
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"))
# interface.create_template("New test", "test_template")
# interface.create_vm_from_template("test_template", resource, [{"uuid": "c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"}])
# interface.resize_vm(server, resource)
# interface.reboot_vm(server.id)
# interface.reset_vm(server.id)
# interface.suspend_vm(server.id)
# interface.wake_up_vm(server.id)
# interface.destroy_vm(server.id)
# conn.compute.change_server_password(server, "root")
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