2017-03-08 13:53:09 -05:00
#!/usr/bin/env python
2017-07-06 17:15:02 -04:00
import os , sys
2017-03-08 13:53:09 -05:00
import subprocess , shlex
2017-07-06 17:15:02 -04:00
from datetime import date , time , datetime
from dateutil . relativedelta import relativedelta
2017-03-08 20:31:16 -05:00
from app import app , db
2017-03-08 13:53:09 -05:00
from flask_script import Manager , Shell , Command
from flask_migrate import Migrate , MigrateCommand
def make_shell_context ( ) :
2017-07-06 17:15:02 -04:00
from app . models import User , Role , Permission , Deployment
2017-03-08 13:53:09 -05:00
return dict ( app = app ,
db = db ,
User = User ,
Role = Role ,
Permission = Permission ,
Deployment = Deployment )
migrate = Migrate ( app , db )
2017-03-09 11:44:20 -05:00
2017-03-08 13:53:09 -05:00
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
2017-06-01 18:27:17 -04:00
from app . models import Role , User
2017-03-08 13:53:09 -05:00
# migrate database to latest revision
upgrade ( )
# create user roles
Role . insert_roles ( )
@manager.command
2017-07-11 12:20:44 -04:00
def autodisable ( ) :
2017-07-06 17:15:02 -04:00
from app . models import User , Deployment , Service , Domain
today = datetime . utcnow ( )
2017-07-11 12:20:44 -04:00
print ( ' \n Scan for active expired items and set them as inactive. Script started at {} ' . format ( today ) )
dep_c = 0
srv_c = 0
dom_c = 0
2017-07-06 17:15:02 -04:00
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 :
2017-07-11 12:20:44 -04:00
print ( ' Deployment " ' + deploy . machine_alias + ' " is past expiration date and will be marked INACTIVE. Last charged: ' + lastcharge . strftime ( ' %c ' ) + ' expiry date: ' + expiry . strftime ( ' %c ' ) )
deploy . warning = False
deploy . enabled = False
deploy . daysleft = 0
db . session . commit ( )
else :
dep_c + = 1
2017-07-06 17:15:02 -04:00
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 :
2017-07-11 12:20:44 -04:00
print ( ' Service " ' + service . description + ' " is past expiration date and will be marked INACTIVE. Last charged: ' + lastcharge . strftime ( ' %c ' ) + ' expiry date: ' + expiry . strftime ( ' %c ' ) )
service . warning = False
service . enabled = False
service . daysleft = 0
db . session . commit ( )
else :
srv_c + = 1
domains_ena = Domain . query . filter_by ( enabled = True ) . all ( )
2017-07-06 17:15:02 -04:00
for domain in domains_ena :
expiry = domain . date_expire
if today > expiry :
2017-07-11 12:20:44 -04:00
print ( ' Domain " ' + domain . fqdn + ' " is past expiration date and will be marked as INACTIVE. Last charged: ' + lastcharge . strftime ( ' %c ' ) + ' expiry date: ' + expiry . strftime ( ' %c ' ) )
domain . warning = False
domain . enabled = False
domain . daysleft = 0
db . session . commmit ( )
else :
dom_c + = 1
print ( ' Total Active Deployments: {} , Services: {} , Domains: {} ' . format ( dep_c , srv_c , dom_c ) )
2017-07-06 17:15:02 -04:00
2017-07-11 12:20:44 -04:00
@manager.command
def autowarn ( ) :
from app . models import User , Deployment , Service , Domain , Transaction
2017-07-06 17:15:02 -04:00
today = datetime . utcnow ( )
2017-07-11 12:20:44 -04:00
print ( ' \n Scan for items that will expire soon and enable the warning flag. Script started at {} ' . format ( today ) )
2017-07-06 17:15:02 -04:00
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 ) )
2017-07-11 12:20:44 -04:00
daysleft = expiry - today
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
deploy . daysleft = daysleft . days
deploy . price = total
db . session . commit ( )
warndays = deploy . period * 5
if daysleft . days < warndays :
print ( ' Deployment " ' + deploy . machine_alias + ' " is ' + str ( daysleft . days ) + ' days until expiration. Last charged: ' + lastcharge . strftime ( ' %c ' ) + ' expiry date: ' + expiry . strftime ( ' %c ' ) )
deploy . warning = True
db . session . commit ( )
2017-07-06 17:15:02 -04:00
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 ) )
2017-07-11 12:20:44 -04:00
daysleft = expiry - today
service . daysleft = daysleft . days
db . session . commit ( )
warndays = service . period * 5
if daysleft . days < warndays :
print ( ' Service " ' + service . description + ' " is ' + str ( daysleft . days ) + ' days until expiration. Last charged: ' + lastcharge . strftime ( ' %c ' ) + ' expiry date: ' + expiry . strftime ( ' %c ' ) )
service . warning = True
db . session . commit ( )
2017-07-06 17:15:02 -04:00
domains_ena = Domain . query . filter_by ( enabled = True ) . all ( )
for domain in domains_ena :
2017-07-11 12:20:44 -04:00
expiry = domain . date_expire
daysleft = expiry - today
domain . daysleft = daysleft . days
db . session . commit ( )
warndays = service . period * 5
if daysleft . days < warndays :
print ( ' Domain " ' + domain . fqdn + ' " is ' + str ( daysleft . days ) + ' days until expiration. Expiry date: ' + expiry . strftime ( ' %c ' ) )
domain . warning = True
db . session . commit ( )
2017-03-08 13:53:09 -05:00
@manager.command
def runserver ( ) :
print ( ' Starting Flask... ' )
app . run ( )
if __name__ == ' __main__ ' :
manager . run ( )