<commit_before>#!/usr/bin/env python
'''
 Copyright (C) <NAME> <<EMAIL>> - All Rights Reserved
 Unauthorized copying of this file, via any medium is strictly prohibited
 Proprietary and confidential
 Written by <NAME> <<EMAIL>>, September 2015
'''

import os
import logging
import bottle
import requests
import time
from boto.dynamodb2.fields import HashKey
from boto.dynamodb2.table import Table
import boto.dynamodb2 as ddb
import boto.ec2
import boto.sqs
import boto.sns
import boto.ses
from bottle import app, template
from boto.s3.connection import S3Connection
from datetime import datetime
from datetime import date
from dateutil.relativedelta import relativedelta

log_levels = { "DEBUG"   : logging.DEBUG,
               "INFO"    : logging.INFO,
               "WARNING" : logging.WARNING,
               "ERROR"   : logging.ERROR,
               "CRITICAL": logging.CRITICAL
           }


# Returns true if the credentials were updated
def update_creds_from_metadata_server(app):
    #Todo error check for timeout errors from http access
    #TOdo error catch for json decode failure

    if "keys.expiry" in app.config and app.config["keys.expiry"] > (datetime.now() + relativedelta(hours=1)):
        logging.debug("Update creds from metadata cancelled {0} < {1}".format(
            app.config["keys.expiry"],
            datetime.now()))
        return False

    URL  = app.config["metadata.credurl"]
    role = requests.get(URL).content
    URL  = URL + role
    data = requests.get(URL).json()

    app.config["keys.expiry"]     = datetime.strptime(str(data['Expiration']), '%Y-%m-%dT%H:%M:%SZ')
    app.config["keys.key_id"]     = str(data['AccessKeyId'])
    app.config["keys.key_secret"] = str(data['SecretAccessKey'])
    app.config["keys.key_token"]  = str(data['Token'])

    URL  = app.config["metadata.metaserver"]
    data = requests.get(URL+"instance-id").text
    app.config["instance_id"] = str(data)

    URL  = app.config["metadata.metaserver"]
    data = requests.get(URL+"placement/availability-zone/").text
    app.config["region"] = str(data)

    URL  = app.config["metadata.metaidentity"]
    data = requests.get(URL).json()
    app.config["identity"] = data

    if "doReload" in app.config and app.config["doReload"] == True:
        init(app)

    return True


##################################################################
# Annoy human with email
##################################################################
def send_success_mail(data, app):
    sesconn     =  app.config['ses.conn']
    job_id      =  data.get('job_id')
    rec_email   =  data.get('user_email')
    rec_name    =  data.get('username')
    src_email   =  app.config['ses.email_sender']
    url         =  app.config['server.url']

    body = template('./templates/completion_email.tpl',
                    username=rec_name,
                    job_id=job_id,
                    url=url)

    st = sesconn.send_email(src_email,
                            "[JES] Your Job has completed",
                            body,
                            [rec_email])
    print st
    return st

##################################################################
# Send condolences for job failure
##################################################################
def send_failure_mail(data, app):
    sesconn     =  app.config['ses.conn']
    job_id      =  data.get('job_id')
    rec_email   =  data.get('user_email')
    rec_name    =  data.get('username')
    src_email   =  app.config['ses.email_sender']
    url         =  app.config['server.url']

    body = template('./templates/failure_email.tpl',
                    username=rec_name,
                    job_id=job_id,
                    url=url)

    st = sesconn.send_email(src_email,
                            "[JES] Your Job has failed",
                            body,<commit_msg>Replacing JES with Turing.<commit_after>#!/usr/bin/env python
'''
 Copyright (C) <NAME> <<EMAIL>> - All Rights Reserved
 Unauthorized copying of this file, via any medium is strictly prohibited
 Proprietary and confidential
 Written by <NAME> <<EMAIL>>, September 2015
'''

import os
import logging
import bottle
import requests
import time
from boto.dynamodb2.fields import HashKey
from boto.dynamodb2.table import Table
import boto.dynamodb2 as ddb
import boto.ec2
import boto.sqs
import boto.sns
import boto.ses
from bottle import app, template
from boto.s3.connection import S3Connection
from datetime import datetime
from datetime import date
from dateutil.relativedelta import relativedelta

log_levels = { "DEBUG"   : logging.DEBUG,
               "INFO"    : logging.INFO,
               "WARNING" : logging.WARNING,
               "ERROR"   : logging.ERROR,
               "CRITICAL": logging.CRITICAL
           }


# Returns true if the credentials were updated
def update_creds_from_metadata_server(app):
    #Todo error check for timeout errors from http access
    #TOdo error catch for json decode failure

    if "keys.expiry" in app.config and app.config["keys.expiry"] > (datetime.now() + relativedelta(hours=1)):
        logging.debug("Update creds from metadata cancelled {0} < {1}".format(
            app.config["keys.expiry"],
            datetime.now()))
        return False

    URL  = app.config["metadata.credurl"]
    role = requests.get(URL).content
    URL  = URL + role
    data = requests.get(URL).json()

    app.config["keys.expiry"]     = datetime.strptime(str(data['Expiration']), '%Y-%m-%dT%H:%M:%SZ')
    app.config["keys.key_id"]     = str(data['AccessKeyId'])
    app.config["keys.key_secret"] = str(data['SecretAccessKey'])
    app.config["keys.key_token"]  = str(data['Token'])

    URL  = app.config["metadata.metaserver"]
    data = requests.get(URL+"instance-id").text
    app.config["instance_id"] = str(data)

    URL  = app.config["metadata.metaserver"]
    data = requests.get(URL+"placement/availability-zone/").text
    app.config["region"] = str(data)

    URL  = app.config["metadata.metaidentity"]
    data = requests.get(URL).json()
    app.config["identity"] = data

    if "doReload" in app.config and app.config["doReload"] == True:
        init(app)

    return True


##################################################################
# Annoy human with email
##################################################################
def send_success_mail(data, app):
    sesconn     =  app.config['ses.conn']
    job_id      =  data.get('job_id')
    rec_email   =  data.get('user_email')
    rec_name    =  data.get('username')
    src_email   =  app.config['ses.email_sender']
    url         =  app.config['server.url']

    body = template('./templates/completion_email.tpl',
                    username=rec_name,
                    job_id=job_id,
                    url=url)

    st = sesconn.send_email(src_email,
                            "[Turing] Your Job has completed",
                            body,
                            [rec_email])
    print st
    return st

##################################################################
# Send condolences for job failure
##################################################################
def send_failure_mail(data, app):
    sesconn     =  app.config['ses.conn']
    job_id      =  data.get('job_id')
    rec_email   =  data.get('user_email')
    rec_name    =  data.get('username')
    src_email   =  app.config['ses.email_sender']
    url         =  app.config['server.url']

    body = template('./templates/failure_email.tpl',
                    username=rec_name,
                    job_id=job_id,
                    url=url)

    st = sesconn.send_email(src_email,
                            "[Turing] Your Job has failed",
                            body,
{'fraction_non_alphanumeric': 0.17986352517059354, 'fraction_numerical': 0.0033474958156302303, 'mean_word_length': 2.707875894988067, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1153629', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2224, 'n_words': 504}