proxadmin/manage.py

181 lines
8.8 KiB
Python
Raw Normal View History

2017-03-08 13:53:09 -05:00
#!/usr/bin/env python
import os, sys
2017-03-08 13:53:09 -05:00
import subprocess, shlex
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-12-14 18:00:02 -05:00
from app.email import send_email
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():
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()
2017-12-14 18:00:02 -05:00
print('\n[{}] Scan for unprotected deployments, lower their days left by 1 and autodelete them if expired:'.format(today.replace(microsecond=0)))
2017-10-07 11:39:50 -04:00
drafts = Deployment.query.filter_by(deleted=False).filter_by(protected=False).all()
2017-08-01 07:21:22 -04:00
for draft in drafts:
daysleft = draft.daysleft
daysleft -= 1
draft.daysleft = daysleft
2017-12-14 19:36:32 -05:00
email_content = 'Draft {} has {} days left.'.format(draft.machine_alias, daysleft)
print(str(email_content))
send_email(app.config['MAIL_USERNAME'], str(email_content), 'email/adm_logger', user=draft.owner, content=str(email_content))
2017-08-01 07:21:22 -04:00
db.session.commit()
2017-03-08 13:53:09 -05:00
@manager.command
def autodisable():
from app.models import User, Deployment, Service, Domain
2017-08-01 07:21:22 -04:00
from sqlalchemy import and_, or_, not_
today = datetime.utcnow()
2017-12-14 18:00:02 -05:00
print('\n[{}] Scan for active expired items and set them as inactive:'.format(today.replace(microsecond=0)))
dep_c = 0
srv_c = 0
dom_c = 0
2017-10-07 11:39:50 -04:00
deployments = Deployment.query.filter_by(deleted=False).filter_by(enabled=True).filter_by(protected=True).all()
2017-08-01 07:21:22 -04:00
for deploy in deployments:
#print('Found {}.'.format(str(deploy.machine_alias)))
lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
2017-12-26 20:47:26 -05:00
if today >= expiry:
2017-12-26 11:00:53 -05:00
email_content = '[!] Deployment "' + deploy.machine_alias + '" is past expiration date and will be marked INACTIVE. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c')
2018-01-09 06:05:45 -05:00
print(str(email_content) + " [mail sent]")
#send_email(app.config['MAIL_USERNAME'], str(email_content), 'email/adm_logger', user=deploy.owner, content=str(email_content))
2017-12-26 20:47:26 -05:00
#send_email(str(deploy.owner.email), str(email_content), 'email/client_logger', content=str(email_content))
deploy.warning = False
deploy.enabled = False
deploy.daysleft = 0
db.session.commit()
else:
dep_c += 1
2017-10-07 11:39:50 -04:00
services_ena = Service.query.filter_by(deleted=False).filter_by(enabled=True).all()
for service in services_ena:
lastcharge = service.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
2017-12-26 20:47:26 -05:00
if today >= expiry:
2017-12-26 11:00:53 -05:00
email_content = '[!] Service "' + service.description + '" is past expiration date and will be marked INACTIVE. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c')
2018-01-09 06:05:45 -05:00
print(str(email_content) + " [mail sent]")
#send_email(app.config['MAIL_USERNAME'], str(email_content), 'email/adm_logger', user=service.owner, content=str(email_content))
2017-12-26 20:47:26 -05:00
#send_email(str(deploy.owner.email), str(email_content), 'email/client_logger', content=str(email_content))
service.warning = False
service.enabled = False
service.daysleft = 0
db.session.commit()
else:
srv_c += 1
2017-10-07 11:39:50 -04:00
domains_ena = Domain.query.filter_by(deleted=False).filter_by(enabled=True).all()
for domain in domains_ena:
expiry = domain.date_expire
2017-12-26 20:47:26 -05:00
if today >= expiry:
2017-12-26 11:00:53 -05:00
email_content = '[!] Domain "' + domain.fqdn + '" is past expiration date and will be marked as INACTIVE. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c')
2018-01-09 06:05:45 -05:00
print(str(email_content) + " [mail sent]")
#send_email(app.config['MAIL_USERNAME'], str(email_content), 'email/adm_logger', user=domain.owner, content=str(email_content))
2017-12-26 20:47:26 -05:00
#send_email(str(deploy.owner.email), str(email_content), 'email/client_logger', content=str(email_content))
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))
@manager.command
def autowarn():
from app.models import User, Deployment, Service, Domain, Transaction
today = datetime.utcnow()
2017-12-14 18:00:02 -05:00
print('\n[{}] Scan for enabled and protected deployments that will expire soon and enable the warning flag:'.format(today.replace(microsecond=0)))
2017-10-07 11:39:50 -04:00
deployments_ena = Deployment.query.filter_by(deleted=False).filter_by(enabled=True).filter_by(protected=True).all()
for deploy in deployments_ena:
lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
daysleft = expiry - today
2017-07-24 06:33:33 -04:00
deploy.daysleft = daysleft.days + 1
db.session.commit()
warndays = deploy.period * 5
2017-12-26 11:00:53 -05:00
email_content = 'Deployment "' + deploy.machine_alias + '" is ' + str(daysleft.days) + ' days until expiration. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c')
2018-01-09 06:05:45 -05:00
if daysleft.days == warndays or round(daysleft.days / 2) == warndays:
print(str(email_content) + " [mail sent]")
#send_email(app.config['MAIL_USERNAME'], str(email_content), 'email/adm_logger', user=deploy.owner, content=str(email_content))
2017-12-26 20:47:26 -05:00
#send_email(str(deploy.owner.email), str(email_content), 'email/client_logger', content=str(email_content))
deploy.warning = True
if daysleft.days < warndays:
2017-12-14 19:36:32 -05:00
print(str(email_content))
deploy.warning = True
2017-12-26 20:47:26 -05:00
db.session.commit()
2017-12-14 18:00:02 -05:00
print('\n[{}] Scan for enabled services that will expire soon and enable the warning flag:'.format(today.replace(microsecond=0)))
2017-10-07 11:39:50 -04:00
services_ena = Service.query.filter_by(deleted=False).filter_by(enabled=True).all()
for service in services_ena:
lastcharge = service.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
daysleft = expiry - today
2017-07-24 06:33:33 -04:00
service.daysleft = daysleft.days + 1
db.session.commit()
warndays = service.period * 5
2017-12-26 11:00:53 -05:00
email_content = 'Service "' + service.description + '" is ' + str(daysleft.days) + ' days until expiration. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c')
2018-02-18 16:28:14 -05:00
if daysleft.days == warndays or (daysleft.days * 2) == warndays:
2018-01-09 06:05:45 -05:00
print(str(email_content) + " [mail sent]")
2017-12-26 11:00:53 -05:00
send_email(app.config['MAIL_USERNAME'], str(email_content), 'email/adm_logger', user=service.owner, content=str(email_content))
2017-12-26 20:47:26 -05:00
send_email(str(service.owner.email), str(email_content), 'email/client_logger', content=str(email_content))
service.warning = True
if daysleft.days < warndays:
2017-12-14 19:36:32 -05:00
print(str(email_content))
service.warning = True
2017-12-26 20:47:26 -05:00
db.session.commit()
2017-10-07 11:39:50 -04:00
domains_ena = Domain.query.filter_by(deleted=False).filter_by(enabled=True).all()
for domain in domains_ena:
expiry = domain.date_expire
daysleft = expiry - today
2017-07-24 06:33:33 -04:00
domain.daysleft = daysleft.days + 1
db.session.commit()
warndays = domain.period * 5
2017-12-26 11:00:53 -05:00
email_content = 'Domain "' + domain.fqdn + '" is ' + str(daysleft.days)+ ' days until expiration. Expiry date: ' + expiry.strftime('%c')
2018-02-18 16:28:14 -05:00
if daysleft.days == warndays or (daysleft.days * 2) == warndays:
2018-01-09 06:05:45 -05:00
print(str(email_content) + " [mail sent]")
2017-12-26 11:00:53 -05:00
send_email(app.config['MAIL_USERNAME'], str(email_content), 'email/adm_logger', user=domain.owner, content=str(email_content))
2017-12-26 20:47:26 -05:00
send_email(str(domain.owner.email), str(email_content), 'email/client_logger', content=str(email_content))
domain.warning = True
if daysleft.days < warndays:
2017-12-14 19:36:32 -05:00
print(str(email_content))
domain.warning = True
2017-12-26 20:47:26 -05:00
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()