109 lines
4.3 KiB
Python
109 lines
4.3 KiB
Python
#!/usr/bin/env python
|
|
|
|
import os, sys
|
|
import subprocess, shlex
|
|
from datetime import date, time, datetime
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
from app import app, db
|
|
from flask_script import Manager, Shell, Command
|
|
from flask_migrate import Migrate, MigrateCommand
|
|
|
|
def make_shell_context():
|
|
from app.models import User, Role, Permission, Deployment
|
|
return dict(app=app,
|
|
db=db,
|
|
User=User,
|
|
Role=Role,
|
|
Permission=Permission,
|
|
Deployment=Deployment)
|
|
|
|
migrate = Migrate(app, db)
|
|
|
|
manager = Manager(app)
|
|
manager.add_command('shell', Shell(make_context=make_shell_context))
|
|
manager.add_command('db', MigrateCommand)
|
|
|
|
@manager.command
|
|
def deploy():
|
|
"""Run deployment tasks."""
|
|
from flask_migrate import upgrade
|
|
from app.models import Role, User
|
|
|
|
# migrate database to latest revision
|
|
upgrade()
|
|
|
|
# create user roles
|
|
Role.insert_roles()
|
|
|
|
@manager.command
|
|
def charge():
|
|
from app.models import User, Deployment, Service, Domain
|
|
|
|
today = datetime.utcnow()
|
|
print('[1] Scan for active expired items and set them as inactive {}\n'.format(today))
|
|
deployments_ena = Deployment.query.filter_by(enabled=True).all()
|
|
for deploy in deployments_ena:
|
|
lastcharge = deploy.date_last_charge
|
|
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
|
|
if today > expiry:
|
|
print('last charged: ' + lastcharge.strftime('%c') + 'expiry date: ' + expiry.strftime('%c'))
|
|
print(deploy.machine_alias + ' is past expiration date and will be marked INACTIVE')
|
|
services_ena = Service.query.filter_by(enabled=True).all()
|
|
for service in services_ena:
|
|
lastcharge = service.date_last_charge
|
|
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
|
|
if today > expiry:
|
|
print('last charged: ' + lastcharge.strftime('%c') + 'expiry date: ' + expiry.strftime('%c'))
|
|
print('Service "' + service.description + '" is past expiration date and will be marked INACTIVE')
|
|
for domain in domains_ena:
|
|
expiry = domain.date_expire
|
|
if today > expiry:
|
|
print('last charged: ' + lastcharge.strftime('%c') + 'expiry date: ' + expiry.strftime('%c'))
|
|
print('Domain "' + domain.fqdn + '" is past expiration date and will be marked as INACTIVE')
|
|
|
|
today = datetime.utcnow()
|
|
print('[2] Scan for items that will expire soon and try to autocharge them if they are not active. {}\n'.format(today))
|
|
deployments_ena = Deployment.query.filter_by(enabled=True).all()
|
|
for deploy in deployments_ena:
|
|
lastcharge = deploy.date_last_charge
|
|
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
|
|
daysleft = relativedelta(today, expiry).days()
|
|
warn = deploy.period * 5
|
|
print(daysleft)
|
|
if daysleft < 10:
|
|
print(deploy.machine_alias + ' is eligible for charging')
|
|
print('last charged: ' + lastcharge.strftime('%c'))
|
|
print('expiry date: ' + expiry.strftime('%c'))
|
|
cpu_cost = deploy.machine_cpu * app.config['CPU_RATIO']
|
|
mem_cost = ( deploy.machine_mem / 1024 ) * app.config['MEM_RATIO']
|
|
hdd_cost = deploy.machine_hdd * app.config['HDD_RATIO']
|
|
total = cpu_cost + mem_cost + hdd_cost
|
|
print('total: ' + str(total))
|
|
services_ena = Service.query.filter_by(enabled=True).all()
|
|
for service in services_ena:
|
|
lastcharge = service.date_last_charge
|
|
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
|
|
if today > expiry:
|
|
print(service.description + ' is eligible for charging')
|
|
print('last charged: ' + lastcharge.strftime('%c'))
|
|
print('expiry date: ' + expiry.strftime('%c'))
|
|
cost = service.price
|
|
print('total: ' + str(cost))
|
|
domains_ena = Domain.query.filter_by(enabled=True).all()
|
|
for domain in domains_ena:
|
|
expire = domain.date_expire
|
|
if expire - today < 60:
|
|
daysleft = relativedelta(now, expire).days
|
|
print(domain.fqdn + ' is expiring after ' + daysleft + ' for charging')
|
|
print('expiry date: ' + expire.strftime('%c'))
|
|
|
|
@manager.command
|
|
def runserver():
|
|
print('Starting Flask...')
|
|
app.run()
|
|
|
|
if __name__ == '__main__':
|
|
manager.run()
|
|
|