58 lines
2.5 KiB
Python
58 lines
2.5 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
from SWSCloudCore import models
|
|
from SWSCloudCore.controllers.tasks import ControllerTasks
|
|
from SWSCloudCore.controllers.vms import ControllerVMS
|
|
|
|
nb = models.Settings.get_item('NEGATIVE_BALANCE')
|
|
|
|
if int(models.Settings.get_item('SERVICE_VMS_ENABLE')) == 1:
|
|
print 'total vms:', models.Vms.select().where(models.Vms.status == 1).count()
|
|
|
|
for vm in models.Vms.select().where(models.Vms.status == 1):
|
|
# Высчитываем, сколько стоит виртуальная машина 15 минут
|
|
price_quarter = vm.plan.price / 30 / 24 / 4
|
|
|
|
print dict(id=vm.id, user=vm.user.id, plan=vm.plan.id, cost_mo=vm.plan.price, cost_quarter=price_quarter)
|
|
|
|
# Списание средств
|
|
x = models.UsersBalance.update(
|
|
balance=models.UsersBalance.balance - price_quarter
|
|
).where(models.UsersBalance.user == vm.user.id)
|
|
x.execute()
|
|
|
|
# Shutting down all VMs by users who doesn't have money
|
|
user_balance = models.UsersBalance.select(
|
|
models.UsersBalance.balance).where(models.UsersBalance.user == vm.user.id).get().balance
|
|
if -500 > user_balance and models.Vms.get_state(vm.id) == 1:
|
|
print "user_balance", user_balance
|
|
ControllerVMS(vm.user.id).status_set(vm.id, 3)
|
|
# Создание задания
|
|
ControllerTasks(vm.user.id).create(vm.datacenter.id, vm.server.id, 'vm_stop', 0, vm_id=vm.id)
|
|
|
|
|
|
if int(models.Settings.get_item('SERVICE_CONTAINERS_ENABLE')) == 1:
|
|
print 'total containers:', models.Containers.select().count()
|
|
for container in models.Containers.select():
|
|
container_state = models.ContainersStatisticsState.select().where(
|
|
models.ContainersStatisticsState.container == container.id
|
|
).get()
|
|
min_price = 100 #+ (container_state.size * )
|
|
|
|
# # Высчитываем, сколько стоит виртуальная машина 15 минут
|
|
price_quarter = min_price / 30 / 24 / 4
|
|
#
|
|
# print dict(id=vm.id, user=vm.user.id, plan=vm.plan.id, cost_mo=vm.plan.price, cost_quarter=price_quarter)
|
|
#
|
|
# Списание средств
|
|
x = models.UsersBalance.update(
|
|
balance=models.UsersBalance.balance - price_quarter
|
|
).where(
|
|
models.UsersBalance.user == container.user.id
|
|
)
|
|
x.execute()
|
|
# Post operations
|
|
# Check user balance
|
|
# If user balance less that allowable limit
|
|
# then power off all virtual machines and containers
|