Exemple #1
0
def email_notifications(id, type):
    user = models.User.objects.get_queryset().filter(id=id).first()
    if not user:
        return
    if type == 'reset_password':
        connect_to_region(
            settings.AWS_REGION,
            aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
            aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
        ).send_email(
            settings.AWS_EMAIL,
            'Reset Password',
            u'''
Hi {first_name:s},

Please use the following link to reset your password.

https://invite.tellzone.com/reset-password/{id:d}/{hash:s}/

From,
Tellzone
            '''.strip().format(first_name=user.first_name,
                               id=user.id,
                               hash=user.hash),
            [
                user.email,
            ],
        )
    if type == 'verify':
        connect_to_region(
            settings.AWS_REGION,
            aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
            aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
        ).send_email(
            settings.AWS_EMAIL,
            'Verify',
            u'''
Hi {first_name:s},

Please use the following link to verify.

https://invite.tellzone.com/verify/{id:d}/{hash:s}/

From,
Tellzone
            '''.strip().format(first_name=user.first_name,
                               id=user.id,
                               hash=user.hash),
            [
                user.email,
            ],
        )
    def _send_email(self):
        try:
            if not self.accumulation:
                return
            with Closer(connect_to_region(
                self.settings.region,
                aws_access_key_id=unwrap(self.settings.aws_access_key_id),
                aws_secret_access_key=unwrap(self.settings.aws_secret_access_key)
            )) as conn:

                # WHO ARE WE SENDING TO
                emails = Data()
                for template, params in self.accumulation:
                    content = expand_template(template, params)
                    emails[literal_field(self.settings.to_address)] += [content]
                    for c in self.cc:
                        if any(c in params.params.error for c in c.contains):
                            emails[literal_field(c.to_address)] += [content]

                # SEND TO EACH
                for to_address, content in emails.items():
                    conn.send_email(
                        source=self.settings.from_address,
                        to_addresses=listwrap(to_address),
                        subject=self.settings.subject,
                        body="\n\n".join(content),
                        format="text"
                    )

            self.next_send = Date.now() + self.settings.max_interval
            self.accumulation = []
        except Exception as e:
            self.next_send = Date.now() + self.settings.max_interval
            Log.warning("Could not send", e)
Exemple #3
0
    def _send_email(self):
        try:
            if not self.accumulation:
                return
            with Closer(
                    connect_to_region(
                        self.settings.region,
                        aws_access_key_id=unwrap(
                            self.settings.aws_access_key_id),
                        aws_secret_access_key=unwrap(
                            self.settings.aws_secret_access_key))) as conn:

                # WHO ARE WE SENDING TO
                emails = Data()
                for template, params in self.accumulation:
                    content = expand_template(template, params)
                    emails[literal_field(
                        self.settings.to_address)] += [content]
                    for c in self.cc:
                        if any(c in params.params.error for c in c.contains):
                            emails[literal_field(c.to_address)] += [content]

                # SEND TO EACH
                for to_address, content in emails.items():
                    conn.send_email(source=self.settings.from_address,
                                    to_addresses=listwrap(to_address),
                                    subject=self.settings.subject,
                                    body="\n\n".join(content),
                                    format="text")

            self.next_send = Date.now() + self.settings.max_interval
            self.accumulation = []
        except Exception as e:
            self.next_send = Date.now() + self.settings.max_interval
            Log.warning("Could not send", e)
    def users_added_callback(self,users):
        '''
        Server url will be dynamic to work same code on different servers
        in case on user enrollment and iOS profile generation also.
        '''
        loader = Loader("/opt/toppatch/mv/media/app/")
        server_url = environ.get('SERVER_CNAME')
        ses_conn = ses.connect_to_region('us-east-1',
                    aws_access_key_id=environ.get('AWS_SES_ACCESS_KEY_ID'),
                    aws_secret_access_key=environ.get(
                        'AWS_SES_SECRET_ACCESS_KEY'))
        for user in users:

            link = str(server_url) + '/enroll/'+str(user.get('enrollment_id'))
            message = loader.load('user_enroll_mail.html').generate(
                        company_name=user.get('company_name'),
                    user_passwd=user.get('passcode'), activation_link=link)
            # message  = 'Your verification \
            #             link is : {0} and enrollment password is {1} . To ensure \
            #             your device os please open this link in your device \
            #             browser only. :)'.format(
            #                 str(server_url) + '/enroll/'+str(user['enrollment_id']), user['passcode'])
            #message  = message.replace('  ', '')

            try:
                ses_conn.send_email('*****@*****.**',
                        'MDM Enrollment verification', message,
                         [user['email']], format='html')
            except Exception,err:
                print repr(err)
Exemple #5
0
def send_bot_email(subject, message, emails):
    # send email
    c = ses.connect_to_region('us-east-1', 
        aws_access_key_id=app.config['AWS_KEY'],
        aws_secret_access_key=app.config['AWS_SECRET'])

    return c.send_email('*****@*****.**', subject, message, emails, html_body=models.parse_raw(message))
Exemple #6
0
    def users_added_callback(self, users):
        '''
        Server url will be dynamic to work same code on different servers
        in case on user enrollment and iOS profile generation also.
        '''
        loader = Loader("/opt/toppatch/mv/media/app/")
        server_url = environ.get('SERVER_CNAME')
        ses_conn = ses.connect_to_region(
            'us-east-1',
            aws_access_key_id=environ.get('AWS_SES_ACCESS_KEY_ID'),
            aws_secret_access_key=environ.get('AWS_SES_SECRET_ACCESS_KEY'))
        for user in users:

            link = str(server_url) + '/enroll/' + \
                str(user.get('enrollment_id'))
            message = loader.load('user_enroll_mail.html').generate(
                company_name=user.get('company_name'),
                user_passwd=user.get('passcode'), activation_link=link)

            try:
                ses_conn.send_email('*****@*****.**',
                                    'MDM Enrollment verification', message,
                                    [user['email']], format='html')
            except Exception as err:
                print repr(err)
Exemple #7
0
    def send_mail(self, From="", To="", subject="", body_text="", body_html="", attachments=None):
        attachments = attachments or []
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = From
        msg['To'] = To
        msg.preamble = 'Multipart message.\n'
        # the message body
        # part1 = MIMEText(body_text, 'plain')
        part2 = MIMEText(body_html, 'html')
        msg.attach(part2)
        # msg.attach(part1)
        # the attachment
        for attachment in attachments:
            log_path = attachment['original_log_path']
            with open(log_path, 'rb') as f_in:
                log_stat = os.stat(log_path)
                if log_stat.st_size > 512000:
                    gz_data = StringIO.StringIO()
                    with GzipFile(fileobj=gz_data, mode='wb', compresslevel=9, filename=attachment['filename']) as gz_out:
                        shutil.copyfileobj(f_in, gz_out)
                    part = MIMEApplication(gz_data.getvalue())
                    part.add_header('Content-Disposition', 'attachment', filename=attachment['filename']+'.gz')
                    gz_data.close()
                else:
                    part = MIMEApplication(f_in.read())
                    part.add_header('Content-Disposition', 'attachment', filename=attachment['filename'])
                msg.attach(part)

        # connect to SES
        connection = ses.connect_to_region(self._region, aws_access_key_id=self._aws_access_key, aws_secret_access_key=self._aws_secret_key)
        # and send the message
        result = connection.send_raw_email(msg.as_string(), source=msg['From'], destinations=[msg['To']])
        return(result)
 def configure(self, context):
     super().configure(context)
     self._conn = connect_to_region(
         re.sub('_', '-', self.region().name),
         aws_access_key_id=self.creds().access_key(),
         aws_secret_access_key=self.creds().access_secret()
     )
Exemple #9
0
 def send_message(self, message):
     session = ses.connect_to_region(
         self.aws_region_name,
         aws_access_key_id=self.aws_access_key,
         aws_secret_access_key=self.aws_secret_key,
     )
     session.send_raw_email(raw_message=message.as_string())
    def handle(self, *args, **options):
        # send a summary email
        email_subj = '[%s] SC Auto-tests: data daily summary updates'
        global_failed = False
        spiders_status = {}
        for spider in Spider.objects.filter(active=True):
            if spider.get_failed_test_runs_for_24_hours_with_missing_data(
                    threshold=4):
                global_failed = True
                spiders_status[spider.name] = 'failed'
            else:
                spiders_status[spider.name] = 'passed'

        email_template = """
%i spider(s) checked. %i Failed. %i Passed.

Detailed Results (tests passed / total):

"""  % (len(spiders_status.keys()),
        len([s for s in spiders_status.items() if s[1] == 'failed']),
        len([s for s in spiders_status.items() if s[1] == 'passed']))

        for spider_name, spider_status in spiders_status.items():
            spider = Spider.objects.get(name=spider_name)
            _total_tr = spider.get_total_test_runs_for_24_hours().count()
            _failed_tr = spider.get_failed_test_runs_for_24_hours_with_missing_data(
                threshold=4).count()
            _passed_tr = _total_tr - _failed_tr
            if spider_status == 'failed':
                email_template += "* [FAILED] - %i/%i - %s." % (
                    _passed_tr, _total_tr, spider.name)
            else:
                email_template += "* [PASSED] - %i/%i - %s." % (
                    _passed_tr, _total_tr, spider.name)

            email_template += " Details: %s" % spider.get_absolute_url()
            email_template += '\n\n'

        email_subj %= 'PASSED' if not global_failed else 'FAILED'
        email_subj += ", UTC time: %s" % datetime.datetime.utcnow()

        # send report email
        conn = ses.connect_to_region(
            'us-east-1',
            aws_access_key_id=settings.AMAZON_SES_KEY,
            aws_secret_access_key=settings.AMAZON_SES_SECRET,
        )
        to_email = ThresholdSettings.objects.all().first()
        if to_email:
            to_email = to_email.notify
            to_email = [e.strip() for e in to_email.split(',')]
            print "SENDING TO", to_email
            print conn.send_email(
                '*****@*****.**',
                email_subj,
                email_template,
                to_addresses=to_email,
                bcc_addresses=[]
            )
def horn_of_rohan(message):
    addresses = ['*****@*****.**', '*****@*****.**']
    connection = ses.connect_to_region('us-east-1')
    connection.send_email(
        'NPR News Apps <*****@*****.**>',
        'She Works: OVER LIMIT',
        None,
        addresses,
        html_body=message,
        format='html')
Exemple #12
0
    def send_email(self):
        conn = ses.connect_to_region(region_name=self.region)

        self.logger.info("Sending Email to %s", ", ".join(self.recipients))

        conn.send_email(
            source=self.sender,
            subject=self.subject,
            body=self.body,
            to_addresses=self.recipients)
def horn_of_rohan(message):
    addresses = ['*****@*****.**', '*****@*****.**']
    connection = ses.connect_to_region('us-east-1')
    connection.send_email(
        'NPR News Apps <*****@*****.**>',
        'She Works: OVER LIMIT',
        None,
        addresses,
        html_body=message,
        format='html')
Exemple #14
0
def func(self):
    from boto.ses import connect_to_region
    import json

    conn = connect_to_region('us-east-1',
                             aws_access_key_id=self.settings.AWS_KEY,
                             aws_secret_access_key=self.settings.AWS_SECRET)
    conn.send_email(
        self.settings.EMAIL_SENDER, 'apy.io: User is interested:',
        '%s | %s | %s' %
        (self.GET.get('?name'), self.GET.get('email'), self.GET.get('why')),
        [self.settings.EMAIL_RECIPIENT])
    return self.responses.JSONResponse(json.dumps(self.GET))
Exemple #15
0
    def __init__(self):

        conf = ConfigParser.ConfigParser()
        conf.read('etl.cfg')

        self.aws_id = conf.get('aws', 'aws_id')
        self.aws_key = conf.get('aws', 'aws_key')
        self.region = conf.get('aws', 'ses_region')
        self.def_sender = conf.get('aws', 'ses_def_sender')
        self.def_msg = conf.get('aws', 'ses_def_msg')
        self.def_recipients = conf.get('aws', 'ses_def_recipients').split(' ')

        self.conn = ses.connect_to_region(self.region, aws_access_key_id=self.aws_id, aws_secret_access_key=self.aws_key)
Exemple #16
0
def flush_and_email():
    global longs, seens, longs
    dakwargs = {'procs':longs}
    body = template.render(**dakwargs)

    tos = ['*****@*****.**']
    #tos = ['*****@*****.**']
    title = "Long Running Procs (%s)" % (len(longs))
    conn = ses.connect_to_region('us-east-1', aws_access_key_id=aws_access, aws_secret_access_key=aws_secret)
    conn.send_email('*****@*****.**',title,body,tos,html_body=body)

    # reset the datas
    seens = defaultdict(dict)
    longs = []
    def handle(self, *args, **options):
        # send a summary email
        email_subj = '[%s] SC Auto-tests: daily summary updates'
        if self._what_is_broken():
            email_subj %= 'FAILED'
        else:
            email_subj %= 'PASSED'

        email_template = """
%i spider(s) checked. %i Failed. %i Passed.

Detailed Results (tests passed / total):
""" % (self._num_of_active_spiders(), self._num_of_failed_spiders(),
       self._num_of_passed_spiders())

        for spider in Spider.objects.filter(active=True).order_by('name'):
            _total_tr = spider.get_total_test_runs_for_24_hours().count()
            _passed_tr = spider.get_passed_test_runs_for_24_hours().count()
            if spider.is_error():
                email_template += "* [FAILED] - %i/%i - %s." % (
                    _passed_tr, _total_tr, spider.name)
            else:
                email_template += "* [PASSED] - %i/%i - %s." % (
                    _passed_tr, _total_tr, spider.name)

            email_template += " Details: %s" % spider.get_absolute_url()
            email_template += '\n\n'

        email_subj += ", UTC time: %s" % datetime.datetime.utcnow()

        print email_template

        # send report email
        conn = ses.connect_to_region(
            'us-east-1',
            aws_access_key_id=settings.AMAZON_SES_KEY,
            aws_secret_access_key=settings.AMAZON_SES_SECRET,
        )
        to_email = ThresholdSettings.objects.all().first()
        if to_email:
            to_email = to_email.notify
            to_email = [e.strip() for e in to_email.split(',')]
            print "SENDING TO", to_email
            print conn.send_email(
                '*****@*****.**',
                email_subj,
                email_template,
                to_addresses=to_email,
                bcc_addresses=[]
            )
Exemple #18
0
def send_email():
    """
    Sends a daily email update.
    """
    with app.app.test_request_context():
        payload = app._email()
        addresses = app_config.ADMIN_EMAILS
        connection = ses.connect_to_region('us-east-1')
        connection.send_email(
            'NPR News Apps <*****@*****.**>',
            'She Works: %s report' % (datetime.datetime.now(pytz.utc).replace(tzinfo=pytz.utc) - datetime.timedelta(days=1)).strftime('%m/%d'),
            None,
            addresses,
            html_body=payload,
            format='html')
Exemple #19
0
def sendmail(self, recipients, subject, message):
    from boto.ses import connect_to_region

    self.debug(self.rid, "sendmail to: "+str(recipients))

    conn = connect_to_region('us-east-1', aws_access_key_id=self.settings.AWS_KEY, aws_secret_access_key=self.settings.AWS_SECRET)
    result = conn.send_email(
            self.settings.EMAIL_SENDER, 
            subject,
            None,
            to_addresses=recipients,
            format="html",
            html_body=message
        )

    return result
Exemple #20
0
def sendmail(self, recipients, subject, message):
    from boto.ses import connect_to_region

    self.debug(self.rid, "sendmail to: " + str(recipients))

    conn = connect_to_region('us-east-1',
                             aws_access_key_id=self.settings.AWS_KEY,
                             aws_secret_access_key=self.settings.AWS_SECRET)
    result = conn.send_email(self.settings.EMAIL_SENDER,
                             subject,
                             None,
                             to_addresses=recipients,
                             format="html",
                             html_body=message)

    return result
Exemple #21
0
def send_email():
    """
    Sends a daily email update.
    """
    with app.app.test_request_context():
        payload = app._email()
        addresses = app_config.ADMIN_EMAILS
        connection = ses.connect_to_region('us-east-1')
        connection.send_email(
            'NPR News Apps <*****@*****.**>',
            'She Works: %s report' %
            (datetime.datetime.now(pytz.utc).replace(tzinfo=pytz.utc) -
             datetime.timedelta(days=1)).strftime('%m/%d'),
            None,
            addresses,
            html_body=payload,
            format='html')
Exemple #22
0
def send_emails(args):
    (aws_access_key_id, aws_secret_access_key) = get_aws_credentials()
    conn = ses.connect_to_region('us-east-1',
                                 aws_access_key_id=aws_access_key_id,
                                 aws_secret_access_key=aws_secret_access_key)

    pairings = load_encrypted_pairings(args.pairings_file)

    with open(args.email_template, 'r') as fp:
        template_string = fp.read()

    for (u1, u2) in pairings:
        emailbody = template_string.format(user_name=u1.name,
                                           target_name=u2.name)
        conn.send_email(
            source=args.from_email,
            subject=args.email_subject,
            body=emailbody,
            format="html",
            to_addresses=u1.email,
        )
        print("Sent email to {}".format(u1.email))
Exemple #23
0
    def email_santas(self):
        """email_santas(santas = None) --> email secret santas where each santa is a santa to the next person in the list santas"""
        santas = self.calculate_santa_order()
        subject = "Your Secret Santa Assignment"
        sesconn = ses.connect_to_region('us-west-2',aws_access_key_id=self.AWS_ACCESS_KEY_ID,
                                               aws_secret_access_key=self.AWS_SECRET_ACCESS_KEY)

        print self.FROM
        for t in santas:
            assignment = (santas.index(t) + 1) % len(santas)
            message = ("Dear %s,\n"
                       "You have been assigned to be a secret santa to %s (email: %s).\n"
                       "Please check their address and wishlist. Check it twice.\n"
                       "You will not be told again who you are getting a gift for.\n"
                       "\n"
                       "Christmas is December 25, make me proud.\n"
                       "\n"
                       "Ho ho ho, DBNR\n"
                       "Santa Claus\n"
                      ) % (
                          t, santas[assignment], self.people[santas[assignment]])
            sesconn.send_email(self.FROM, subject, message, self.people[t])
Exemple #24
0
    def _send_email(self):
        try:
            if self.accumulation:
                conn = connect_to_region(
                    self.settings.region,
                    aws_access_key_id=unwrap(self.settings.aws_access_key_id),
                    aws_secret_access_key=unwrap(
                        self.settings.aws_secret_access_key))

                conn.send_email(source=self.settings.from_address,
                                to_addresses=listwrap(
                                    self.settings.to_address),
                                subject=self.settings.subject,
                                body="\n\n".join(self.accumulation),
                                format="text")

                conn.close()
            self.next_send = Date.now() + WAIT_TO_SEND_MORE
            self.accumulation = []
        except Exception, e:
            self.next_send = Date.now() + WAIT_TO_SEND_MORE
            Log.warning("Could not send", e)
Exemple #25
0
def send_email(aws_region,
               from_address,
               to_addresses,
               subject,
               body):
    """
    Send an email via AWS SES using boto with the specified subject/body/recipients.

    Args:
        aws_region (str): AWS region whose SES service will be used, e.g. "us-east-1".
        from_address (str): Email address to use as the From: address. Must be an SES verified address.
        to_addresses (list(str)): List of email addresses to which to send the email.
        subject (str): Subject to use in the email.
        body (str): Body to use in the email - text format.
    """
    ses_conn = ses.connect_to_region(aws_region)
    _send_email_with_retry(
        ses_conn,
        from_address,
        to_addresses,
        subject,
        body
    )
    def _send_email(self):
        try:
            if self.accumulation:
                conn = connect_to_region(
                    self.settings.region,
                    aws_access_key_id=unwrap(self.settings.aws_access_key_id),
                    aws_secret_access_key=unwrap(self.settings.aws_secret_access_key)
                )

                conn.send_email(
                    source=self.settings.from_address,
                    to_addresses=listwrap(self.settings.to_address),
                    subject=self.settings.subject,
                    body="\n\n".join(self.accumulation),
                    format="text"
                )

                conn.close()
            self.next_send = Date.now() + WAIT_TO_SEND_MORE
            self.accumulation = []
        except Exception, e:
            self.next_send = Date.now() + WAIT_TO_SEND_MORE
            Log.warning("Could not send", e)
Exemple #27
0
    def email_santas(self):
        """email_santas(santas = None) --> email secret santas where each santa is a santa to the next person in the list santas"""
        santas = self.calculate_santa_order()
        subject = "Your Secret Santa Assignment"
        sesconn = ses.connect_to_region(
            'us-west-2',
            aws_access_key_id=self.AWS_ACCESS_KEY_ID,
            aws_secret_access_key=self.AWS_SECRET_ACCESS_KEY)

        print self.FROM
        for t in santas:
            assignment = (santas.index(t) + 1) % len(santas)
            message = (
                "Dear %s,\n"
                "You have been assigned to be a secret santa to %s (email: %s).\n"
                "Please check their address and wishlist. Check it twice.\n"
                "You will not be told again who you are getting a gift for.\n"
                "\n"
                "Christmas is December 25, make me proud.\n"
                "\n"
                "Ho ho ho, DBNR\n"
                "Santa Claus\n") % (t, santas[assignment],
                                    self.people[santas[assignment]])
            sesconn.send_email(self.FROM, subject, message, self.people[t])
 def __init__(self, settings):
     self.resource = connect_to_region(
         settings.region,
         aws_access_key_id=unwrap(settings.aws_access_key_id),
         aws_secret_access_key=unwrap(settings.aws_secret_access_key))
Exemple #29
0
def connect(key,secret):
  return ses.connect_to_region(
    'us-east-1',
    aws_access_key_id=key, 
    aws_secret_access_key=secret)
Exemple #30
0
 def __init__(self, sqs, queue_name, pool_size):
     super(Sendmail, self).__init__(sqs, queue_name, pool_size)
     # connect to SES
     self.ses = connect_to_region(self.region, aws_access_key_id=config.aws_key, aws_secret_access_key=config.aws_secret)
Exemple #31
0
 def __init__(self, settings):
     self.resource = connect_to_region(
         settings.region,
         aws_access_key_id=unwrap(settings.aws_access_key_id),
         aws_secret_access_key=unwrap(settings.aws_secret_access_key)
     )
Exemple #32
0

app = Flask(__name__)
app.config.from_object('config')
app.config.from_pyfile('constants.py')
app.config.from_pyfile('messages.py')
# app.session_interface = RedisSessionInterface()
db = SQLAlchemy(app)
lm = LoginManager(app)
jsglue = JSGlue(app)
lm.login_view = 'login_get'
client = raygunprovider.RaygunSender(CONFIG.RAYGUN4PY_API_KEY)

# Initialize the Moment.js object for use throughout the app
moment = Moment(app)

email_connection = ses.connect_to_region(
                region_name=CONFIG.AWS_REGION,
                aws_access_key_id=CONFIG.AWS_ACCESS_KEY,
                aws_secret_access_key=CONFIG.AWS_ACCESS_SECRET)
cache = RedisCache(host=CONFIG.REDIS_ENGINE, port=CONFIG.REDIS_PORT,
                   db=CONFIG.REDIS_DB_MAPPING['cache'],
                   default_timeout=CONFIG.DEFAULT_TIMEOUT)

app.jinja_env.add_extension('jinja2.ext.do')
redis = redis.StrictRedis(host=CONFIG.REDIS_ENGINE,
                          port=CONFIG.REDIS_PORT,
                          db=CONFIG.REDIS_DB_MAPPING['cache'])

from app import views, models, errorpage_handler
from boto import ses
# import json
from itsdangerous import TimedJSONWebSignatureSerializer

from logger import Logger
from db.helpers.violations import ViolationsDBHelper
from db.helpers.device import DeviceDBHelper
from db.helpers.user import UserDBHelper
from db.helpers.company import CompanyDBHelper
from db.helpers.base import DBHelper
# from tornado.template import Template
from tornado.template import Loader


ses_conn = ses.connect_to_region(
    'us-east-1',
    aws_access_key_id=environ.get('AWS_SES_ACCESS_KEY_ID'),
    aws_secret_access_key=environ.get('AWS_SES_SECRET_ACCESS_KEY'))

loader = Loader("/opt/toppatch/mv/media/app/")


def admin_mailer(device_id, violation_id, *args, **kwargs):

    TAG = 'admin mailer'
    base = DBHelper()
    cur = base.cursor
    log = Logger('AdminMailer')

    device = DeviceDBHelper()
    violation = ViolationsDBHelper()
    user = UserDBHelper()
def run(timestamp, param_file, aws_file):
    '''
    Run Timing scripts on AWS. See README for more info.

    A parameters file is passed into the function the controls the stages of
    the processing. The script then uploads the data

    This function is intended to be used by itself, or through a web server
    where execution requests are submitted.

    Parameters
    ----------
    timestamp : str
        Timestamp created when the request was submitted.
    param_file : str or dict
        JSON file or a dictionary with parameter settings.
    aws_file : str or dict
        JSON file or a dictionary with settings for the AWS execution.
    '''

    # Read the param_file file
    if isinstance(param_file, dict):
        params = param_file
    else:
        with open(param_file) as f:
            params = json.load(f)

    # Read the AWS settings file.
    if isinstance(aws_file, dict):
        aws_settings = aws_file
    else:
        with open(aws_file) as f:
            aws_settings = json.load(f)

    proc_name = params['target'].lower() + "_" + timestamp
    key = aws_settings['key']
    secret = aws_settings['secret']
    region = aws_settings['region']

    # Create the queue and submit the message.
    queue_name = proc_name
    resp_queue_name = "resp_" + proc_name

    queue = sqs.connect_to_region(region,
                                  aws_access_key_id=key,
                                  aws_secret_access_key=secret).create_queue(queue_name)

    msg = json_message(params, proc_name)
    queue.write(queue.new_message(body=msg))

    # Launch instance

    user_data = WORKER_SCRIPT % \
        {"USER": '******',
         "QUEUE_NAME": proc_name,
         "KEY": key,
         "SECRET": secret,
         "REGION": region,
         "RESP_QUEUE_NAME": resp_queue_name,
         "CUSTOM_LINES": "/usr/bin/git clone https://github.com/Astroua/AstroCompute_Scripts.git"}
         # "CUSTOM_LINES": 'ssh-keyscan github.com >> /home/ubuntu/.ssh/known_hosts\nsu - ubuntu -c "/usr/bin/git clone [email protected]:e-koch/AstroCompute_Scripts.git"\nexport USER="******"'}

    inst = launch(key_name=None, region=region,
                  image_id=aws_settings['image_id'],
                  instance_type=aws_settings['instance_type'],
                  security_groups=aws_settings['security_groups'],
                  initial_check=False, user_data=user_data)

    # Wait for return message
    # Total wait time set to 1 day
    time_max = 3600 * 24
    time_wait = 200
    t0 = time()
    while time() < t0 + time_max:
        update = inst.update()
        if update in [u"stopping", u"stopped"]:
            break
        sleep(time_wait)
    else:
        print("Reached time limit. Terminating.")

    inst.terminate()

    # Connect to the response queue
    resp_queue = sqs.connect_to_region(region,
                                       aws_access_key_id=key,
                                       aws_secret_access_key=secret).create_queue(resp_queue_name)

    # Check the response message
    mess = resp_queue.read(10)
    if mess is not None:
        content = json.loads(mess.get_body())
        print("Saving content.")
        with open("test_response.txt", "w") as f:
            json.dump(content, f)

    # Clean-up queues
    queue.delete()
    resp_queue.delete()

    # Send out email to the user.
    email_service = ses.connect_to_region(region,
                                          aws_access_key_id=key,
                                          aws_secret_access_key=secret)

    # The parameters should have the user's database values.
    email_address = params["user_email"]
    username = params["user_name"]
    time_limit = params["time_limit"]
    summary_url = params["summary_url"]
    project_email = params["project_email"]

    subject = "AstroCompute Timing Script Job: {} Completion".format(proc_name)
    body = email_body(username, proc_name, summary_url, time_limit,
                      project_email)
    email_service.send_email(project_email, subject, body, email_address,
                             format='text')

    # Set the expiration on the S3 bucket.
    set_bucket_lifetime(proc_name, days=time_limit,
                        aws_access={"aws_access_key_id": key,
                                    "aws_secret_access_key": secret})

    # Return success/failure

    # Add in the job name to parameters
    params["job_name"] = proc_name

    return params
Exemple #35
0
from os import environ
import tornado.ioloop
import tornado.web
from tornado.web import asynchronous
from boto import ses
from logger import Logger
#from send_enroll_mail import *
from db.constants import Constants as c
from db.helpers.enrollment import EnrollmentDBHelper
from db.helpers.user import UserDBHelper
from handlers.super import SuperHandler
from .markov_passwords import generate_password
from tornado.template import Loader

ses_conn = ses.connect_to_region(
    'us-east-1',
    aws_access_key_id=environ.get('AWS_SES_ACCESS_KEY_ID'),
    aws_secret_access_key=environ.get('AWS_SES_SECRET_ACCESS_KEY'))


# Async class to get the enroll request by using the POST method
class EnrollDeviceRequestHandler(SuperHandler):
    def options(self, data):
        self.add_header('Access-Control-Allow-Methods',
                        'GET,POST,PUT,OPTIONS, DELETE')
        self.add_header('Access-Control-Allow-Headers',
                        'Origin, X-Requested-With, Content-Type, Accept')
        #self.add_header('Access-Control-Allow-Origin', '*')

    @tornado.web.authenticated
    @asynchronous
    def post(self, data):
Exemple #36
0
 def __init__(self, aws_region, access_key, secret_key):
     self.conn = ses.connect_to_region(aws_region,
                                       aws_access_key_id=access_key,
                                       aws_secret_access_key=secret_key)
def run(timestamp, param_file, aws_file):
    '''
    Run Timing scripts on AWS. See README for more info.

    A parameters file is passed into the function the controls the stages of
    the processing. The script then uploads the data

    This function is intended to be used by itself, or through a web server
    where execution requests are submitted.

    Parameters
    ----------
    timestamp : str
        Timestamp created when the request was submitted.
    param_file : str or dict
        JSON file or a dictionary with parameter settings.
    aws_file : str or dict
        JSON file or a dictionary with settings for the AWS execution.
    '''

    # Read the param_file file
    if isinstance(param_file, dict):
        params = param_file
    else:
        with open(param_file) as f:
            params = json.load(f)

    # Read the AWS settings file.
    if isinstance(aws_file, dict):
        aws_settings = aws_file
    else:
        with open(aws_file) as f:
            aws_settings = json.load(f)

    proc_name = params['target'].lower() + "_" + timestamp
    key = aws_settings['key']
    secret = aws_settings['secret']
    region = aws_settings['region']

    # Create the queue and submit the message.
    queue_name = proc_name
    resp_queue_name = "resp_" + proc_name

    queue = sqs.connect_to_region(region,
                                  aws_access_key_id=key,
                                  aws_secret_access_key=secret).create_queue(queue_name)

    msg = json_message(params, proc_name)
    queue.write(queue.new_message(body=msg))

    # Launch instance

    user_data = WORKER_SCRIPT % \
        {"USER": '******',
         "QUEUE_NAME": proc_name,
         "KEY": key,
         "SECRET": secret,
         "REGION": region,
         "RESP_QUEUE_NAME": resp_queue_name,
         "CUSTOM_LINES": 'ssh-keyscan github.com >> /home/ubuntu/.ssh/known_hosts\nsu - ubuntu -c "/usr/bin/git clone [email protected]:Astroua/AstroCompute_Scripts.git"\nexport USER="******"'}

    inst = launch(key_name=None, region=region,
                  image_id=aws_settings['image_id'],
                  instance_type=aws_settings['instance_type'],
                  security_groups=aws_settings['security_groups'],
                  initial_check=False, user_data=user_data)

    # Wait for return message
    # Total wait time set to 1 day
    time_max = 3600 * 24
    time_wait = 200
    t0 = time()
    while time() < t0 + time_max:
        update = inst.update()
        if update in [u"stopping", u"stopped"]:
            break
        sleep(time_wait)
    else:
        print("Reached time limit. Terminating.")

    inst.terminate()

    # Connect to the response queue
    resp_queue = sqs.connect_to_region(region,
                                       aws_access_key_id=key,
                                       aws_secret_access_key=secret).create_queue(resp_queue_name)

    # Check the response message
    mess = resp_queue.read(10)
    if mess is not None:
        content = json.loads(mess.get_body())
        print("Saving content.")
        with open("tests/test_response.txt", "w") as f:
            json.dump(content, f)

    # Clean-up queues
    queue.delete()
    resp_queue.delete()

    # Send out email to the user.
    email_service = ses.connect_to_region(region,
                                          aws_access_key_id=key,
                                          aws_secret_access_key=secret)

    # The parameters should have the user's database values.
    email_address = params["user_email"]
    username = params["user_name"]
    time_limit = params["time_limit"]
    summary_url = params["summary_url"]
    project_email = params["project_email"]

    subject = "AstroCompute Timing Script Job: {} Completion".format(proc_name)
    body = email_body(username, proc_name, summary_url, time_limit,
                      project_email)
    email_service.send_email(project_email, subject, body, email_address,
                             format='text')

    # Set the expiration on the S3 bucket.
    set_bucket_lifetime(proc_name, days=time_limit,
                        aws_access={"aws_access_key_id": key,
                                    "aws_secret_access_key": secret})
                        dest="aws_secret", help="AWS secret key"),
        parser.add_option('-r', '--region', action="store",
                        dest="region", help="AWS secret key", default="us-east-1"),
        ]



(options, args) = parser.parse_args()

if not (options.critical_threshold and options.warning_threshold):
    print("Critical and warning thresholds are req")
    sys.exit(2)

if (options.aws_key and  options.aws_secret):
     conn = ses.connect_to_region(
                        options.region,
                        aws_access_key_id=options.aws_key,
                        aws_secret_access_key=options.aws_secret)
else:
     conn = ses.connect_to_region(options.region)

if not conn:
    print("CRITICAL: Could not connect to SES, please check credentials!")
    sys.exit(2)

try:
    quota = conn.get_send_quota()
except BotoServerError:
    print("CRITICAL: Could not connect to server")
    sys.exit(2)

max24h_send = float(quota['GetSendQuotaResponse']['GetSendQuotaResult']['Max24HourSend'])