console/SWSCloudCore/controllers/servers/server.py
2016-04-20 03:39:13 +03:00

94 lines
3.1 KiB
Python

import datetime
from SWSCloudCore import models
class ControllerServerServers:
def get_secret(self, server_id):
return models.Servers.select(models.Servers.secret).where(models.Servers.id == server_id).get().secret
def exists(self, server_id):
try:
if models.Servers.select().where(models.Servers.id == server_id).count() == 0:
return False
except Exception as e:
return False
else:
return True
class ControllerServerStatistics:
def write(self, container_id, cpu, memory, size, net_tx, net_rx, net_total):
models.ContainersStatistics.create(
container=str(container_id),
cpu=cpu,
memory=memory,
size=size,
net_tx=net_tx,
net_rx=net_rx,
net_total=net_total
)
return True
def state_exists(self, container_id):
if models.ContainersStatisticsState.select().where(
models.ContainersStatisticsState.container == container_id
).count() == 0:
return False
return True
def state_create(self, container_id, statistics):
if 'cpu_use' not in statistics:
statistics['cpu_use'] = 0
if 'memory_use' not in statistics:
statistics['memory_use'] = 0
if 'size' not in statistics:
statistics['size'] = 0
if 'tx_bytes' not in statistics:
statistics['tx_bytes'] = 0
if 'rx_bytes' not in statistics:
statistics['rx_bytes'] = 0
if 'total_bytes' not in statistics:
statistics['total_bytes'] = 0
try:
models.ContainersStatisticsState.create(
container=container_id,
cpu=statistics['cpu_use'],
memory=statistics['memory_use'],
size=statistics['size'],
net_tx=statistics['tx_bytes'],
net_rx=statistics['rx_bytes'],
net_total=statistics['total_bytes']
)
except Exception as e:
# TODO: report
return False
return True
def state_update(self, container_id, statistics):
if 'cpu_use' not in statistics:
statistics['cpu_use'] = 0
if 'memory_use' not in statistics:
statistics['memory_use'] = 0
if 'size' not in statistics:
statistics['size'] = 0
if 'tx_bytes' not in statistics:
statistics['tx_bytes'] = 0
if 'rx_bytes' not in statistics:
statistics['rx_bytes'] = 0
if 'total_bytes' not in statistics:
statistics['total_bytes'] = 0
state = models.ContainersStatisticsState.update(
updated=datetime.datetime.now(),
cpu=statistics['cpu_use'],
memory=statistics['memory_use'],
size=statistics['size'],
net_tx=statistics['tx_bytes'],
net_rx=statistics['rx_bytes'],
net_total=statistics['total_bytes']
).where(models.ContainersStatisticsState.container == container_id)
state.execute()
return True