def add_instance_record(cfg, conn):
    log.debug('Adding instance record')

    instance_record = {}
    if cfg['proc_node_type'] != 'CLOUD':
        instance_record['instance_id'] = socket.gethostname()
        instance_record['instance_type'] = 'on-prem'
    else:
        instance_record['instance_id'] = get_instance_id()
        instance_record['instance_type'] = get_instance_type()
    instance_record['local_queue_id'] = cfg['id']
    cfg['instance_record'] = instance_record

    try:
        instance_record_sql = '''
            insert into instance_records (instance_id, local_queue_id, start_time, instance_type)
            values (%(instance_id)s, %(local_queue_id)s, current_timestamp, %(instance_type)s)
        '''
        query_database(conn=conn,
                       query=instance_record_sql,
                       params=instance_record,
                       commit=True)

    except Exception:
        log.exception("Instance record could not be inserted")
    else:
        log.info("Instance record of instance %s and job %s inserted",
                 instance_record['instance_id'],
                 instance_record['local_queue_id'])
Esempio n. 2
0
def create_one_time_hash(conn, action, user_id, params):
    sql = '''
        update one_time_actions set expires = now() + interval '30' day  where
            user_id=%(user_id)s and action=%(action)s and params=%(params)s
            returning id, hash
    '''
    vals = {'user_id': user_id, 'action': action, 'params': params}
    recs = query_database(conn, sql, vals, commit=True, returning=True)

    if recs and len(recs) > 0 and len(recs[0]) > 0:
        return recs[0][0], recs[0][1]

    hashval = uuid.uuid4().hex
    sql = '''
        insert into one_time_actions(hash,user_id,action,params,expires)
        values (%(hashval)s, %(user_id)s, %(action)s, %(params)s, current_timestamp + interval '30' day)
        returning id
    '''
    vals.update({'hashval': hashval})
    recs = query_database(conn, sql, vals, commit=True, returning=True)

    if not recs[0][0]:
        raise Exception("Error getting id")
    id_ = int(recs[0][0])
    log.debug('One-time hash id: {0} and hash: {1}'.format(id_, hashval))

    return id_, hashval
Esempio n. 3
0
def queue_email(conn, lqid, to_address, subject, body):
    sql = '''
    insert into email_queue(local_queue_id, status, recipients, subject, message, mime_type)
    values (%(local_queue_id)s, %(status)s, %(recipients)s, %(subject)s, %(message)s, %(mime_type)s)
    '''
    values = {
        "local_queue_id": lqid,
        "status": 'QUEUED',
        "recipients": to_address,
        "subject": subject,
        "message": body,
        "mime_type": 'html'
    }
    query_database(conn, sql, values, commit=True)
def log_instance_shutdown_in_hyp3_db(cfg):
    if cfg['proc_node_type'] != 'CLOUD':
        return

    instance = get_instance_info(cfg)
    try:
        with get_db_connection('hyp3-db') as hyp3db_conn:
            sql = "update instances set shutdown_time = current_timestamp where id = (%(instance_id)s)"
            query_database(hyp3db_conn, sql, instance, commit=True)
    except Exception as e:
        log.error("Instance %s could not be updated with shutdown time",
                  instance["instance_id"])
        log.error("Error was: %s", str(e))
    else:
        log.info("Instance %s was updated with shutdown time",
                 instance["instance_id"])
def add_instance_to_hyp3_db(cfg):
    if cfg['proc_node_type'] != 'CLOUD':
        return
    instance = get_instance_info(cfg)
    try:
        with get_db_connection('hyp3-db') as hyp3db_conn:
            sql = 'insert into instances (id, start_time, process_id) values (%(instance_id)s, current_timestamp, %(process_id)s);'
            query_database(conn=hyp3db_conn,
                           query=sql,
                           params=instance,
                           commit=True)
    except Exception as e:
        log.error("Instance %s could not be inserted into instances",
                  instance['instance_id'])
        log.error("Error was: %s", str(e))
    else:
        log.info("Instance %s was inserted into instances",
                 instance['instance_id'])
def update_instance_with_specific_gamma_id(cfg):
    if cfg['proc_node_type'] != 'CLOUD':
        return

    instance = get_instance_info(cfg)
    try:
        with get_db_connection('hyp3-db') as hyp3db_conn:
            sql = 'update instances set process_id = %(process_id)s where id = %(instance_id)s'
            query_database(conn=hyp3db_conn,
                           query=sql,
                           params=instance,
                           commit=True)
    except Exception as e:
        log.error(
            "any_gamma instance %s could not be updated with specific gamma process id, %s",
            instance['instance_id'], cfg['proc_name'])
        log.error("Error was: %s", str(e))
    else:
        log.info(
            "any_gamma instance %s was update with specific gamma process id, %s",
            instance['instance_id'], cfg['proc_name'])
def update_instance_record(cfg, conn):
    if 'instance_record' in cfg:
        instance_record = cfg['instance_record']
        try:
            instance_record_sql = 'update instance_records set end_time=current_timestamp where (instance_id=%(instance_id)s and local_queue_id=%(local_queue_id)s);'
            query_database(conn=conn,
                           query=instance_record_sql,
                           params=instance_record,
                           commit=True)
        except Exception:
            log.exception(
                "Instance record for instance %s and job %s could not be updated with job completion time",
                instance_record['instance_id'],
                instance_record['local_queue_id'])
        else:
            log.info(
                "Instance record for instance %s and job %s had end_time updated with job completion time",
                instance_record['instance_id'],
                instance_record['local_queue_id'])
    else:
        log.debug('No instance record found to update')
Esempio n. 8
0
def get_process_id_dict():
    process_id_dict = dict()
    process_names_ids_sql = 'select text_id, id FROM processes'
    process_names_ids = list()

    with get_db_connection('hyp3-db') as hyp3db_conn:
        process_names_ids = query_database(conn=hyp3db_conn,
                                           query=process_names_ids_sql,
                                           returning=True)
        for proc in process_names_ids:
            process_id_dict[proc[0]] = proc[1]

    return process_id_dict
Esempio n. 9
0
def send_queued_emails():
    with get_db_connection('hyp3-db') as conn:
        sql = '''
            select id, local_queue_id, recipients, subject, message, attachment_filename, attachment, mime_type
            from email_queue where status = 'QUEUED'
        '''
        recs = query_database(conn, sql)
        if len(recs) == 0:
            log.info('No emails to send')

        for r in recs:
            if r and r[0] and r[2] and len(r[2]) > 0:
                id_ = int(r[0])
                lqid = None
                if r[1] is not None:
                    lqid = int(r[1])
                to = r[2]
                subject = r[3]
                body = r[4]

                mime_type = "plain"
                if r[7] is not None:
                    mime_type = r[7]
                if mime_type == "text":
                    mime_type = "plain"

                log.info('Emailing ' + to + ' for lqid: ' + str(lqid))
                log.debug('Subject: ' + subject)

                ok, msg = send_email(to, subject, body, mime_type=mime_type)
                if ok:
                    status = 'SENT'
                else:
                    status = 'FAILED'

                log.debug('Updating status to ' + status)
                sql = "update email_queue set status = %(status)s, system_message = %(msg)s, processed_time = current_timestamp where id = %(id)s"
                query_database(conn, sql, {'status': status, 'msg': msg, 'id': id_}, commit=True)