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 ( )
2017-08-01 07:21:22 -04:00
@manager.command
def autoremove ( ) :
from app . models import User , Deployment
today = datetime . utcnow ( )
print ( ' \n Scan for unprotected deployments, lower their days left by 1 and autodelete them if expired. Script Started at {} ' . format ( today ) )
drafts = Deployment . query . filter_by ( cancelled = False ) . filter_by ( protected = False ) . all ( )
for draft in drafts :
daysleft = draft . daysleft
daysleft - = 1
draft . daysleft = daysleft
print ( ' Draft {} will be autoremoved after {} days. ' . format ( draft . machine_alias , daysleft ) )
db . session . commit ( )
2017-03-08 13:53:09 -05:00
@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
2017-08-01 07:21:22 -04:00
from sqlalchemy import and_ , or_ , not_
2017-07-06 17:15:02 -04:00
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-08-01 07:21:22 -04:00
deployments = Deployment . query . filter_by ( cancelled = False ) . filter_by ( enabled = True ) . filter_by ( protected = True ) . all ( )
for deploy in deployments :
#print('Found {}.'.format(str(deploy.machine_alias)))
2017-07-06 17:15:02 -04:00
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-08-01 07:21:22 -04:00
services_ena = Service . query . filter_by ( cancelled = False ) . filter_by ( enabled = True ) . all ( )
2017-07-06 17:15:02 -04:00
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
2017-08-01 07:21:22 -04:00
domains_ena = Domain . query . filter_by ( cancelled = False ) . 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-08-01 07:21:22 -04:00
deployments_ena = Deployment . query . filter_by ( cancelled = False ) . filter_by ( enabled = True ) . filter_by ( protected = True ) . all ( )
2017-07-06 17:15:02 -04:00
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
2017-07-24 06:33:33 -04:00
deploy . daysleft = daysleft . days + 1
2017-07-11 12:20:44 -04:00
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-20 19:30:09 -04:00
else :
deploy . warning = False
db . session . commit ( )
2017-07-11 12:20:44 -04:00
2017-08-01 07:21:22 -04:00
services_ena = Service . query . filter_by ( cancelled = False ) . filter_by ( enabled = True ) . all ( )
2017-07-06 17:15:02 -04:00
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
2017-07-24 06:33:33 -04:00
service . daysleft = daysleft . days + 1
2017-07-11 12:20:44 -04:00
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-20 19:30:09 -04:00
else :
service . warning = False
db . session . commit ( )
2017-07-11 12:20:44 -04:00
2017-08-01 07:21:22 -04:00
domains_ena = Domain . query . filter_by ( cancelled = False ) . filter_by ( enabled = True ) . all ( )
2017-07-06 17:15:02 -04:00
for domain in domains_ena :
2017-07-11 12:20:44 -04:00
expiry = domain . date_expire
daysleft = expiry - today
2017-07-24 06:33:33 -04:00
domain . daysleft = daysleft . days + 1
2017-07-11 12:20:44 -04:00
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 ( )