39 QEMU virtual machines
This commit is contained in:
parent
23b7670131
commit
b62e9433c4
9 changed files with 266 additions and 48 deletions
|
@ -3,6 +3,7 @@
|
||||||
import json
|
import json
|
||||||
from SWSCloudCore import models
|
from SWSCloudCore import models
|
||||||
from SWSCloudCore.controllers.containers.server import ControllerContainersServer
|
from SWSCloudCore.controllers.containers.server import ControllerContainersServer
|
||||||
|
from SWSCloudCore.controllers.vms.server import Controller_VMS_Server
|
||||||
|
|
||||||
|
|
||||||
class ControllerTasksServer:
|
class ControllerTasksServer:
|
||||||
|
@ -37,7 +38,8 @@ class ControllerTasksServer:
|
||||||
if models.Tasks.select().where(models.Tasks.id == task_id).count() == 0:
|
if models.Tasks.select().where(models.Tasks.id == task_id).count() == 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
controllercontainer = ControllerContainersServer()
|
controller_vm = Controller_VMS_Server()
|
||||||
|
controller_container = ControllerContainersServer()
|
||||||
task = self.get_item(task_id)
|
task = self.get_item(task_id)
|
||||||
|
|
||||||
# container statuses:
|
# container statuses:
|
||||||
|
@ -48,55 +50,113 @@ class ControllerTasksServer:
|
||||||
# 4 - creating
|
# 4 - creating
|
||||||
# 5 - deleting
|
# 5 - deleting
|
||||||
|
|
||||||
|
lxc_tasks_list = ['container_start', 'container_stop', 'container_create',
|
||||||
|
'container_restart', 'container_delete']
|
||||||
|
qemu_tasks_list = ['vm_create', 'vm_start', 'vm_stop', 'vm_restart', 'vm_delete']
|
||||||
|
|
||||||
if status == 1:
|
if status == 1:
|
||||||
if task['task'] == 'container_start':
|
if task['task'] in lxc_tasks_list:
|
||||||
controllercontainer.status_set(task['plain']['container_id'], 2)
|
if task['task'] == 'container_start':
|
||||||
if task['task'] == 'container_stop':
|
controller_container.status_set(task['plain']['container_id'], 2)
|
||||||
controllercontainer.status_set(task['plain']['container_id'], 3)
|
if task['task'] == 'container_stop':
|
||||||
|
controller_container.status_set(task['plain']['container_id'], 3)
|
||||||
|
|
||||||
|
if task['task'] in qemu_tasks_list:
|
||||||
|
if task['task'] == 'vm_start':
|
||||||
|
controller_vm.status_set(task['plain']['vm_id'], 2)
|
||||||
|
if task['task'] == 'vm_stop':
|
||||||
|
controller_vm.status_set(task['plain']['vm_id'], 3)
|
||||||
|
|
||||||
if status == 2:
|
if status == 2:
|
||||||
if task['task'] == 'container_create':
|
if task['task'] in qemu_tasks_list:
|
||||||
controllercontainer.status_set(task['plain']['container_id'], 1)
|
if task['task'] == 'vm_create':
|
||||||
if task['task'] == 'container_start':
|
controller_vm.status_set(task['plain']['vm_id'], 1)
|
||||||
controllercontainer.status_set(task['plain']['container_id'], 1)
|
if task['task'] == 'vm_start':
|
||||||
if task['task'] == 'container_stop':
|
controller_vm.status_set(task['plain']['vm_id'], 1)
|
||||||
controllercontainer.status_set(task['plain']['container_id'], 0)
|
if task['task'] == 'vm_stop':
|
||||||
if task['task'] == 'container_restart':
|
controller_vm.status_set(task['plain']['vm_id'], 0)
|
||||||
controllercontainer.status_set(task['plain']['container_id'], 1)
|
if task['task'] == 'vm_restart':
|
||||||
if task['task'] == 'container_delete':
|
controller_vm.status_set(task['plain']['vm_id'], 1)
|
||||||
# не имеет смысла менять статус, т.к. контейнер придётся удалять
|
if task['task'] == 'vm_delete':
|
||||||
# controllercontainer.status_set(task['plain']['container_id'], 2)
|
# не имеет смысла менять статус, т.к. контейнер придётся удалять
|
||||||
|
# controllercontainer.status_set(task['plain']['container_id'], 2)
|
||||||
|
|
||||||
ns = models.Ips.update(status=0).where(
|
ns = models.Ips.update(status=0).where(
|
||||||
models.Ips.ipv4 << models.Containers.select(models.Containers.ipv4).where(
|
models.Ips.ipv4 << models.Vms.select(models.Vms.ipv4).where(
|
||||||
models.Containers.id == task['plain']['container_id'],
|
models.Vms.id == task['plain']['vm_id'],
|
||||||
models.Containers.ipv4 != ''
|
models.Vms.ipv4 != ''
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
ns.execute()
|
||||||
ns.execute()
|
|
||||||
|
|
||||||
ns = models.Ips.update(status=0).where(
|
ns = models.Ips.update(status=0).where(
|
||||||
models.Ips.ipv6 << models.Containers.select(models.Containers.ipv6).where(
|
models.Ips.ipv6 << models.Vms.select(models.Vms.ipv6).where(
|
||||||
models.Containers.id == task['plain']['container_id'],
|
models.Vms.id == task['plain']['vm_id'],
|
||||||
models.Containers.ipv6 != ''
|
models.Vms.ipv6 != ''
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
ns.execute()
|
||||||
ns.execute()
|
|
||||||
|
|
||||||
# delete all container statistics
|
# delete all container statistics
|
||||||
delstats = models.ContainersStatistics.delete().where(
|
# del_stats = models.ContainersStatistics.delete().where(
|
||||||
models.ContainersStatistics.container == task['plain']['container_id']
|
# models.ContainersStatistics.container == task['plain']['container_id']
|
||||||
)
|
# )
|
||||||
delstats.execute()
|
# del_stats.execute()
|
||||||
# delete stats state
|
|
||||||
delstatsstate = models.ContainersStatisticsState.delete().where(
|
|
||||||
models.ContainersStatisticsState.container == task['plain']['container_id']
|
|
||||||
)
|
|
||||||
delstatsstate.execute()
|
|
||||||
|
|
||||||
|
# delete stats state
|
||||||
|
# del_stats_state = models.ContainersStatisticsState.delete().where(
|
||||||
|
# models.ContainersStatisticsState.container == task['plain']['container_id']
|
||||||
|
# )
|
||||||
|
# del_stats_state.execute()
|
||||||
|
|
||||||
delcontainer = models.Containers.delete().where(models.Containers.id == task['plain']['container_id'])
|
#
|
||||||
delcontainer.execute()
|
del_vm = models.Vms.delete().where(models.Vms.id == task['plain']['vm_id'])
|
||||||
|
del_vm.execute()
|
||||||
|
|
||||||
|
if task['task'] in lxc_tasks_list:
|
||||||
|
if task['task'] == 'container_create':
|
||||||
|
controller_container.status_set(task['plain']['container_id'], 1)
|
||||||
|
if task['task'] == 'container_start':
|
||||||
|
controller_container.status_set(task['plain']['container_id'], 1)
|
||||||
|
if task['task'] == 'container_stop':
|
||||||
|
controller_container.status_set(task['plain']['container_id'], 0)
|
||||||
|
if task['task'] == 'container_restart':
|
||||||
|
controller_container.status_set(task['plain']['container_id'], 1)
|
||||||
|
if task['task'] == 'container_delete':
|
||||||
|
# не имеет смысла менять статус, т.к. контейнер придётся удалять
|
||||||
|
# controllercontainer.status_set(task['plain']['container_id'], 2)
|
||||||
|
|
||||||
|
ns = models.Ips.update(status=0).where(
|
||||||
|
models.Ips.ipv4 << models.Containers.select(models.Containers.ipv4).where(
|
||||||
|
models.Containers.id == task['plain']['container_id'],
|
||||||
|
models.Containers.ipv4 != ''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
ns.execute()
|
||||||
|
|
||||||
|
ns = models.Ips.update(status=0).where(
|
||||||
|
models.Ips.ipv6 << models.Containers.select(models.Containers.ipv6).where(
|
||||||
|
models.Containers.id == task['plain']['container_id'],
|
||||||
|
models.Containers.ipv6 != ''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
ns.execute()
|
||||||
|
|
||||||
|
# delete all container statistics
|
||||||
|
del_stats = models.ContainersStatistics.delete().where(
|
||||||
|
models.ContainersStatistics.container == task['plain']['container_id']
|
||||||
|
)
|
||||||
|
del_stats.execute()
|
||||||
|
|
||||||
|
# delete stats state
|
||||||
|
del_stats_state = models.ContainersStatisticsState.delete().where(
|
||||||
|
models.ContainersStatisticsState.container == task['plain']['container_id']
|
||||||
|
)
|
||||||
|
del_stats_state.execute()
|
||||||
|
|
||||||
|
#
|
||||||
|
del_container = models.Containers.delete().where(models.Containers.id == task['plain']['container_id'])
|
||||||
|
del_container.execute()
|
||||||
|
|
||||||
task = models.Tasks.update(status=status).where(models.Tasks.id == task_id)
|
task = models.Tasks.update(status=status).where(models.Tasks.id == task_id)
|
||||||
task.execute()
|
task.execute()
|
||||||
|
|
|
@ -36,6 +36,23 @@ class ControllerVMS(object):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def set_status(self, vm_id, status):
|
def set_status(self, vm_id, status):
|
||||||
|
"""
|
||||||
|
DEPRECATED
|
||||||
|
:param vm_id:
|
||||||
|
:param status:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
x = Vms.update(status=status).where(Vms.id == vm_id)
|
x = Vms.update(status=status).where(Vms.id == vm_id)
|
||||||
x.execute()
|
x.execute()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def status_set(self, vm_id, status):
|
||||||
|
"""
|
||||||
|
|
||||||
|
:param vm_id:
|
||||||
|
:param status:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
x = Vms.update(status=status).where(Vms.id == vm_id)
|
||||||
|
x.execute()
|
||||||
|
return True
|
||||||
|
|
29
SWSCloudCore/controllers/vms/manage.py
Normal file
29
SWSCloudCore/controllers/vms/manage.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
from SWSCloudCore.models import Vms
|
||||||
|
|
||||||
|
|
||||||
|
class ControllerVMSManage(object):
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get(self, vm_id=None):
|
||||||
|
if vm_id:
|
||||||
|
return Vms.select().where(Vms.id == vm_id).get()
|
||||||
|
return Vms.select()
|
||||||
|
|
||||||
|
def exists(self, vm_id):
|
||||||
|
if Vms.select().where(Vms.id == vm_id).count() == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def status_set(self, vm_id, status):
|
||||||
|
"""
|
||||||
|
|
||||||
|
:param vm_id:
|
||||||
|
:param status:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
x = Vms.update(status=status).where(Vms.id == vm_id)
|
||||||
|
x.execute()
|
||||||
|
return True
|
27
SWSCloudCore/controllers/vms/server.py
Normal file
27
SWSCloudCore/controllers/vms/server.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
from SWSCloudCore.models import Vms
|
||||||
|
|
||||||
|
|
||||||
|
class Controller_VMS_Server(object):
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get(self, vm_id=None):
|
||||||
|
if vm_id:
|
||||||
|
return Vms.select().where(Vms.id == vm_id).get()
|
||||||
|
return Vms.select()
|
||||||
|
|
||||||
|
def exists(self, vm_id):
|
||||||
|
if Vms.select().where(Vms.id == vm_id).count() == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def status_set(self, vm_id, status):
|
||||||
|
"""
|
||||||
|
|
||||||
|
:param vm_id:
|
||||||
|
:param status:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
x = Vms.update(status=status).where(Vms.id == vm_id)
|
||||||
|
x.execute()
|
||||||
|
return True
|
|
@ -31,7 +31,7 @@
|
||||||
<li>Services
|
<li>Services
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="{{ url_for('administrator.containers') }}">Containers</a></li>
|
<li><a href="{{ url_for('administrator.containers') }}">Containers</a></li>
|
||||||
<li><a href="{{ url_for('administrator.containers') }}">VMs</a></li>
|
<li><a href="{{ url_for('administrator.vms') }}">VMs</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li>Infrastructure
|
<li>Infrastructure
|
||||||
|
|
43
SWSCloudCore/templates/administrator/vms/index.html
Normal file
43
SWSCloudCore/templates/administrator/vms/index.html
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
{% extends 'administrator/_layout.auth.html' %}
|
||||||
|
|
||||||
|
{% block title %}Virtual machines{% endblock %}
|
||||||
|
|
||||||
|
{% block subtitle %}List{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
Total: {{ vms|length }}
|
||||||
|
<table class="table table-bordered">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Id</th>
|
||||||
|
<th>Datacenter</th>
|
||||||
|
<th>Server</th>
|
||||||
|
<th>User</th>
|
||||||
|
<th>IPv4</th>
|
||||||
|
<th>IPv6</th>
|
||||||
|
<th>Status</th>
|
||||||
|
<th> </th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% if vms|length == 0 %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="6">Нет ни одного виртуального сервера</td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
{% for vm in vms %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ vm.id }}</td>
|
||||||
|
<td>{{ vm.datacenter.name }}</td>
|
||||||
|
<td>{{ vm.server.hostname }}</td>
|
||||||
|
<td>{{ vm.user.email }}</td>
|
||||||
|
<td>{{ vm.ipv4 }}</td>
|
||||||
|
<td>{{ vm.ipv6 }}</td>
|
||||||
|
<td>{{ vm.status }}</td>
|
||||||
|
<td><a href="#">Edit</a></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{% endblock %}
|
|
@ -0,0 +1,23 @@
|
||||||
|
{% extends "default/_layout.html" %}
|
||||||
|
|
||||||
|
{% block title %}Удаление виртуальной машины{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="row">
|
||||||
|
<div class="large-12 columns">
|
||||||
|
<h3>Удаление виртуальной машины</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="large-12 columns">
|
||||||
|
<form action="{{ url_for('vms.delete', vm_id=vm.id) }}" method="post">
|
||||||
|
<input type="hidden" name="id" value="{{ vm.id }}" />
|
||||||
|
<p>
|
||||||
|
Вы действительно хотите удалить виртуальной машины?<br/>
|
||||||
|
</p>
|
||||||
|
<input type="submit" value="Удалить" class="button alert" />
|
||||||
|
<a href="{{ url_for('vms.index') }}" class="">Отмена</a>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
|
@ -16,6 +16,7 @@ from SWSCloudCore.controllers.administrators import ControllerAdministrators
|
||||||
from SWSCloudCore.controllers.billing import ControllerBilling
|
from SWSCloudCore.controllers.billing import ControllerBilling
|
||||||
from SWSCloudCore.controllers.common import ControllerMessagesEmail
|
from SWSCloudCore.controllers.common import ControllerMessagesEmail
|
||||||
from SWSCloudCore.controllers.containers.manage import ControllerManageContainers
|
from SWSCloudCore.controllers.containers.manage import ControllerManageContainers
|
||||||
|
from SWSCloudCore.controllers.vms.manage import ControllerVMSManage
|
||||||
from SWSCloudCore.controllers.datacenters.manage import ControllerManageDatacenters
|
from SWSCloudCore.controllers.datacenters.manage import ControllerManageDatacenters
|
||||||
from SWSCloudCore.controllers.ips.manage import ControllerManageIPs
|
from SWSCloudCore.controllers.ips.manage import ControllerManageIPs
|
||||||
from SWSCloudCore.controllers.servers.manage import ControllerManageServer
|
from SWSCloudCore.controllers.servers.manage import ControllerManageServer
|
||||||
|
@ -142,6 +143,22 @@ def containers():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@viewAdministrator.route('/vms/')
|
||||||
|
def vms():
|
||||||
|
# check session
|
||||||
|
if not ControllerAdministrators().check_session():
|
||||||
|
return redirect(url_for("administrator.logout"))
|
||||||
|
# auth user
|
||||||
|
if not ControllerAdministrators().auth(session['admin_email'], session['admin_password']):
|
||||||
|
return redirect(url_for("administrator.logout"))
|
||||||
|
# формируем список правил
|
||||||
|
vms_items = ControllerVMSManage().get()
|
||||||
|
return render_template(
|
||||||
|
'administrator/vms/index.html',
|
||||||
|
vms=vms_items
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@viewAdministrator.route('/payments')
|
@viewAdministrator.route('/payments')
|
||||||
def payments():
|
def payments():
|
||||||
# check session
|
# check session
|
||||||
|
|
|
@ -192,6 +192,7 @@ def delete(vm_id):
|
||||||
return redirect(url_for("account.logout"))
|
return redirect(url_for("account.logout"))
|
||||||
#
|
#
|
||||||
vms = ControllerVMS(session['user_id'])
|
vms = ControllerVMS(session['user_id'])
|
||||||
|
tasks = ControllerTasks(session['user_id'])
|
||||||
# check the user have a selected rule
|
# check the user have a selected rule
|
||||||
# if user not have a container then redirect to the container list
|
# if user not have a container then redirect to the container list
|
||||||
if not vms.exists(vm_id):
|
if not vms.exists(vm_id):
|
||||||
|
@ -204,12 +205,13 @@ def delete(vm_id):
|
||||||
# Обновляем статус "5" для правила
|
# Обновляем статус "5" для правила
|
||||||
vms.set_status(vm_id, 5)
|
vms.set_status(vm_id, 5)
|
||||||
# Создание задания
|
# Создание задания
|
||||||
ControllerTasks(session['user_id']).create(
|
tasks.create(
|
||||||
vm_details.datacenter.id,
|
datacenter_id=vm_details.datacenter.id,
|
||||||
vm_details.server.id,
|
server_id=vm_details.server.id,
|
||||||
'vm_delete',
|
task='vm_delete',
|
||||||
0,
|
status=0,
|
||||||
vm_id=vm_id
|
vm_id=vm_id,
|
||||||
|
# hostname=vm_details.hostname
|
||||||
)
|
)
|
||||||
# TODO: send email container was deleted about
|
# TODO: send email container was deleted about
|
||||||
# Редиректим на страницу со всеми правилами
|
# Редиректим на страницу со всеми правилами
|
||||||
|
|
Loading…
Add table
Reference in a new issue