119 lines
4.4 KiB
Python
119 lines
4.4 KiB
Python
# coding: utf-8
|
|
|
|
import json
|
|
|
|
from SWSCloudCore import models
|
|
from SWSCloudCore.controllers.containers.server import ControllerContainersServer
|
|
|
|
|
|
# from peewee import fn
|
|
|
|
|
|
class ControllerTasksServer:
|
|
def __init__(self, server_id, secret):
|
|
self.server_id = server_id
|
|
self.secret = secret
|
|
|
|
def auth(self):
|
|
if models.Servers.select().where(
|
|
models.Servers.id == self.server_id,
|
|
models.Servers.secret == self.secret
|
|
).count() == 0:
|
|
return False
|
|
return True
|
|
|
|
def count(self):
|
|
return models.Tasks.select(
|
|
models.Servers.id == self.server_id,
|
|
models.Servers.secret == self.secret
|
|
).count()
|
|
|
|
def get(self, status=0):
|
|
return models.Tasks.select().where(
|
|
models.Tasks.server == self.server_id,
|
|
models.Tasks.status == status
|
|
).execute()
|
|
|
|
def update(self, task_id, status):
|
|
# get origin status
|
|
if models.Tasks.select().where(models.Tasks.id == task_id).count() == 0:
|
|
return False
|
|
|
|
controllercontainer = ControllerContainersServer()
|
|
task = self.get_item(task_id)
|
|
|
|
# container statuses:
|
|
# 0 - inactive
|
|
# 1 - active
|
|
# 2 - activation
|
|
# 3 - deactivation
|
|
# 4 - creating
|
|
# 5 - deleting
|
|
|
|
if status == 1:
|
|
if task['task'] == 'container_start':
|
|
controllercontainer.status_set(task['plain']['container_id'], 2)
|
|
if task['task'] == 'container_stop':
|
|
controllercontainer.status_set(task['plain']['container_id'], 3)
|
|
|
|
if status == 2:
|
|
if task['task'] == 'container_create':
|
|
controllercontainer.status_set(task['plain']['container_id'], 1)
|
|
if task['task'] == 'container_start':
|
|
controllercontainer.status_set(task['plain']['container_id'], 1)
|
|
if task['task'] == 'container_stop':
|
|
controllercontainer.status_set(task['plain']['container_id'], 0)
|
|
if task['task'] == 'container_restart':
|
|
controllercontainer.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
|
|
delstats = models.ContainersStatistics.delete().where(
|
|
models.ContainersStatistics.container == task['plain']['container_id']
|
|
)
|
|
delstats.execute()
|
|
# delete stats state
|
|
delstatsstate = models.ContainersStatisticsState.delete().where(
|
|
models.ContainersStatisticsState.container == task['plain']['container_id']
|
|
)
|
|
delstatsstate.execute()
|
|
|
|
|
|
delcontainer = models.Containers.delete().where(models.Containers.id == task['plain']['container_id'])
|
|
delcontainer.execute()
|
|
|
|
task = models.Tasks.update(status=status).where(models.Tasks.id == task_id)
|
|
task.execute()
|
|
|
|
return True
|
|
|
|
def get_item(self, task_id):
|
|
get_task = models.Tasks.select().where(models.Tasks.id == task_id)[0]
|
|
return {
|
|
'id': get_task.id,
|
|
'datacenter': get_task.datacenter.id,
|
|
'server': get_task.server.id,
|
|
'user': get_task.user.id,
|
|
'created': get_task.created,
|
|
'task': get_task.task,
|
|
'status': get_task.status,
|
|
'plain': json.loads(get_task.plain)
|
|
}
|