Changeset - 45f57c8cb5d5
[Not reviewed]
version_5
0 1 0
Dennis Fink - 10 years ago 2015-08-21 16:17:13
dennis.fink@c3l.lu
Remove offline mail feature
1 file changed with 0 insertions and 85 deletions:
0 comments (0 inline, 0 general)
ennstatus/status/functions.py
Show inline comments
 
@@ -13,91 +13,6 @@ from ..api.model import Server
 
mail = Mail()
 

	
 

	
 
def _send_mail(server_name, last_updated, subject=None):
 

	
 
    current_app.logger.info('Try sending mail')
 
    try:
 
        if subject is None:
 
            subject = '[Ennstatus] %s went offline'
 
        msg = Message(subject % server_name,
 
                      sender='ennstatus@enn.lu')
 
        recipient = current_app.config['SERVER_ADMINS'][server_name]
 
        msg.add_recipient(recipient)
 

	
 
        body = ('%s went offline. I received the last update at %s'
 
                % (server_name, last_updated))
 

	
 
        if current_app.extensions['gnupg']:
 
            body = str(current_app.extensions['gnupg'].encrypt(body, recipient,
 
                                                           always_trust=True))
 
        msg.body = body
 

	
 
        mail.send(msg)
 
    except KeyError:
 
        current_app.logger.error('Admin for %s not found!' % server_name)
 
    except AssertionError:
 
        pass
 
    except Exception as e:
 
        current_app.logger.error('Unexpected error: %s' % e,
 
                                 exc_info=True)
 
    current_app.logger.info('Finished trying!')
 

	
 

	
 
def _check_server(data):
 

	
 
    server_name = data['server_name']
 
    last_updated = data['last_updated']
 
    current_status = data['server_status']
 
    filename = os.path.join('data', '.'.join([server_name.lower(), 'json']))
 

	
 
    date = datetime.strptime(last_updated, '%d-%m-%Y %H:%M:%S')
 
    now = datetime.utcnow()
 
    delta = now - date
 

	
 
    if delta.days >= 7:
 
        os.remove(filename)
 
        current_app.logger.error('%s was removed!' % server_name)
 
        return False
 
    elif current_status == 'Offline':
 
        return data
 

	
 
    if delta.seconds >= 3600:
 
        status = 'Offline'
 
    elif delta.seconds >= 1200:
 
        status = 'Unknown'
 
    else:
 
        status = None
 

	
 
    if status is not None:
 
        current_app.logger.error('%s is set to %s' % (server_name,
 
                                 status))
 

	
 
        if (current_status == 'Online' or current_status == 'Unknown') and \
 
           status == 'Offline':
 
            _send_mail(server_name, last_updated)
 

	
 
        for key in ('server_status', 'tor_status'):
 
            data[key] = status
 

	
 
        with open(filename, mode='w', encoding='utf-8') as file_object:
 
            json.dump(data, file_object)
 

	
 
    return data
 

	
 

	
 
def _load_single_server(filename):
 

	
 
    current_app.logger.info('Loading {}'.format(filename))
 
    try:
 
        with open(filename, encoding='utf-8') as f:
 
            server = json.load(f)
 
    except (IOError, ValueError):
 
        return False
 

	
 
    server = _check_server(server)
 
    return server
 

	
 

	
 
def single_server(name):
 

	
 
    server = Server.from_file_by_name(name)
0 comments (0 inline, 0 general)