console/cloud-cron-balance.py

47 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# coding: utf-8
from SWSCloudCore import models
if int(models.Settings.select().where(models.Settings.key == 'SERVICE_VMS_ENABLE').get().val) == 1:
print 'total vms:', models.Vms.select().count()
for vm in models.Vms.select():
# Высчитываем, сколько стоит виртуальная машина 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()
if int(models.Settings.select().where(models.Settings.key == 'SERVICE_CONTAINERS_ENABLE').get().val) == 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 = 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()
# Post operations
# Check user balance
# If user balance less that allowable limit
# then power off all virtual machines and containers