2016-12-09 00:52:27 +01:00
"""
2016-12-14 18:29:04 +01:00
. . module : lemur . endpoints . cli
2016-12-09 00:52:27 +01:00
: platform : Unix
2018-05-29 19:18:16 +02:00
: copyright : ( c ) 2018 by Netflix Inc . , see AUTHORS for more
2016-12-09 00:52:27 +01:00
: license : Apache , see LICENSE for more details .
. . moduleauthor : : Kevin Glisson < kglisson @netflix.com >
"""
from flask_script import Manager
import arrow
from datetime import timedelta
from sqlalchemy import cast
from sqlalchemy_utils import ArrowType
from lemur import database
2017-07-13 23:49:04 +02:00
from lemur . extensions import metrics , sentry
2016-12-09 00:52:27 +01:00
from lemur . endpoints . models import Endpoint
manager = Manager ( usage = " Handles all endpoint related tasks. " )
@manager.option ( ' -ttl ' , ' --time-to-live ' , type = int , dest = ' ttl ' , default = 2 , help = ' Time in hours, which endpoint has not been refreshed to remove the endpoint. ' )
def expire ( ttl ) :
"""
Removed all endpoints that have not been recently updated .
"""
2016-12-21 20:01:21 +01:00
print ( " [+] Staring expiration of old endpoints. " )
2016-12-09 00:52:27 +01:00
2017-07-13 23:49:04 +02:00
try :
now = arrow . utcnow ( )
expiration = now - timedelta ( hours = ttl )
endpoints = database . session_query ( Endpoint ) . filter ( cast ( Endpoint . last_updated , ArrowType ) < = expiration )
2016-12-21 20:01:21 +01:00
2017-07-13 23:49:04 +02:00
for endpoint in endpoints :
print ( " [!] Expiring endpoint: {name} Last Updated: {last_updated} " . format ( name = endpoint . name , last_updated = endpoint . last_updated ) )
database . delete ( endpoint )
metrics . send ( ' endpoint_expired ' , ' counter ' , 1 )
print ( " [+] Finished expiration. " )
except Exception as e :
sentry . captureException ( )