Exemple #1
0
    def update(self, att, val):
        """
        Update a specified column value for this Order object

        :param att: column to update
        :param val: new value
        :return: updated value from self
        """
        sql = 'update ordering_order set %s = %s where id = %s'

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, (db_extns.AsIs(att),
                                                  val, self.id))
                logger.info(log_sql)
                db.execute(sql, (db_extns.AsIs(att), val, self.id))
                db.commit()
        except DBConnectException as e:
            logger.debug('Error updating order: {}\nSQL: {}'
                         .format(e.message, log_sql))

        self.__setattr__(att, val)

        return self.__getattribute__(att)
Exemple #2
0
    def update(self, att, val):
        """
        Update a specified column value for this Order object

        :param att: column to update
        :param val: new value
        :return: updated value from self
        """
        sql = 'update ordering_order set %s = %s where id = %s'

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql,
                                            (db_extns.AsIs(att), val, self.id))
                logger.info(log_sql)
                db.execute(sql, (db_extns.AsIs(att), val, self.id))
                db.commit()
        except DBConnectException as e:
            logger.debug('Error updating order: {}\nSQL: {}'.format(
                e.message, log_sql))

        self.__setattr__(att, val)

        return self.__getattribute__(att)
Exemple #3
0
    def where(cls, params):
        """
        Query for a particular row in the ordering_oder table

        :param params: dictionary of column: value parameter to select on
        :return: list of matching Order objects
        """
        if not isinstance(params, dict):
            raise OrderException('Where arguments must be '
                                 'passed as a dictionary')

        sql, values = format_sql_params(cls.base_sql, params)

        ret = []
        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, values)
                logger.info('order.py where sql: {}'.format(log_sql))

                db.select(sql, values)

                for i in db:
                    od = dict(i)
                    obj = Order(**od)
                    ret.append(obj)
        except DBConnectException as e:
            logger.debug('Error order where: {}\n'
                         'sql: {}'.format(e.message, log_sql))
            raise OrderException(e)

        return ret
Exemple #4
0
    def find_or_create_user(cls, username, email, first_name, last_name,
                            contactid):
        user_id = None
        # username comes in not as a str from EE, which DBConnect takes issue with
        username = str(username)
        nownow = time.strftime('%Y-%m-%d %H:%M:%S')
        insert_stmt = "insert into auth_user (username, " \
                      "email, first_name, last_name, password, " \
                      "is_staff, is_active, is_superuser, " \
                      "last_login, date_joined, contactid) values " \
                      "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) " \
                      "on conflict (username) " \
                      "do update set (email, contactid) = (%s, %s) " \
                      "where auth_user.username = %s" \
                      "returning id"
        arg_tup = (username, email, first_name, last_name, 'pass', 'f', 't',
                   'f', nownow, nownow, contactid, email, contactid, username)

        with db_instance() as db:
            try:
                db.execute(insert_stmt, arg_tup)
                db.commit()
                user_id = db.fetcharr[0]['id']
            except:
                exc_type, exc_val, exc_trace = sys.exc_info()
                logger.debug("ERR user find_or_create args {0} {1} " \
                             "{2} {3}\n trace: {4}".format(username, email, first_name,
                                                           last_name, traceback.format_exc()))
                raise exc_type, exc_val, exc_trace

        return user_id
Exemple #5
0
    def where(cls, params):
        """
        Query for a particular row in the ordering_oder table

        :param params: dictionary of column: value parameter to select on
        :return: list of matching Order objects
        """
        if not isinstance(params, dict):
            raise OrderException('Where arguments must be '
                                 'passed as a dictionary')

        sql, values = format_sql_params(cls.base_sql, params)

        ret = []
        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, values)
                logger.info('order.py where sql: {}'.format(log_sql))

                db.select(sql, values)

                for i in db:
                    od = dict(i)
                    obj = Order(**od)
                    ret.append(obj)
        except DBConnectException as e:
            logger.debug('Error order where: {}\n'
                         'sql: {}'.format(e.message, log_sql))
            raise OrderException(e)

        return ret
Exemple #6
0
    def update_product_details(self, action, params):
        """Update product details

        Args:
            action (str): name of the action to peform. valid values include:
                            update_status, set_product_error,
                            set_product_unavailable,
                            mark_product_complete
            params (dict): args for the action. valid keys: name, orderid,
                            processing_loc, status, error, note,
                            completed_file_location, cksum_file_location,
                            log_file_contents

        Returns:
            True if successful
        """
        try:
            response = self.production.update_product(action, **params)
        except:
            logger.debug(
                "ERR version0 update_product_details, params: {0}\ntrace: {1}\n"
                .format(params, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #7
0
    def where(cls, params):
        """
        Query for particular users

        :param params: dictionary of column: value parameters
        :return: list of matching User objects
        """
        if not isinstance(params, dict):
            raise UserException(
                'Where arguments must be passed as a dictionary')

        sql, values = format_sql_params(cls.base_sql, params)

        ret = []
        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, values)
                logger.info('user.py where sql: {}'.format(log_sql))
                db.select(sql, values)
                for i in db:
                    obj = User(i["username"], i["email"], i["first_name"],
                               i["last_name"], i["contactid"])
                    ret.append(obj)
        except DBConnectException as e:
            logger.debug('Error querying for users: {}\n'
                         'sql: {}'.format(e.message, log_sql))
            raise UserException(e)
        return ret
Exemple #8
0
    def order_attr(self, col):
        """
        Select the column value from the ordering_order table for this
        specific scene

        :param col: column to select on
        :return: value
        """
        sql = ('SELECT %s '
               'FROM ordering_scene JOIN ordering_order '
               'ON ordering_order.id = ordering_scene.order_id '
               'WHERE ordering_scene.id = %s')

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, (db_extns.AsIs(col), self.id))
                db.select(sql, (db_extns.AsIs(col), self.id))
                ret = db[0][col]

        except DBConnectException as e:
            logger.debug('Error retrieving order_attr: {}\n'
                         'sql: {} \n'.format(e.message, log_sql))
            raise SceneException(e)

        except KeyError as e:
            logger.debug('Error order_attr returned no results\n'
                         'sql: {}'.format(log_sql))

            raise SceneException('Key Error: {}'.format(e.message))

        return ret
    def update_system_status(params):

        if params.keys().sort() is not [
                'system_message_title', 'system_message_body',
                'display_system_message'
        ].sort():
            return {
                'msg':
                'Only 3 params are valid, and they must be present:'
                'system_message_title, system_message_body,'
                'display_system_message'
            }

        sql_dict = {
            'msg.system_message_title': params['system_message_title'],
            'msg.system_message_body': params['system_message_body'],
            'system.display_system_message': params['display_system_message']
        }
        sql = ""
        for k, v in sql_dict.iteritems():
            sql += "update ordering_configuration set value = '{0}' where key = '{1}';".format(
                v, k)

        try:
            with db_instance() as db:
                db.execute(sql)
                db.commit()
        except DBConnectException as e:
            logger.debug("error updating system status: {}".format(e))
            return {'msg': "error updating database: {}".format(e.message)}

        return True
Exemple #10
0
    def order_attr(self, col):
        """
        Select the column value from the ordering_order table for this
        specific scene

        :param col: column to select on
        :return: value
        """
        sql = ('SELECT %s '
               'FROM ordering_scene JOIN ordering_order '
               'ON ordering_order.id = ordering_scene.order_id '
               'WHERE ordering_scene.id = %s')

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, (db_extns.AsIs(col),
                                                  self.id))
                db.select(sql, (db_extns.AsIs(col), self.id))
                ret = db[0][col]

        except DBConnectException as e:
            logger.debug('Error retrieving order_attr: {}\n'
                         'sql: {} \n'.format(e.message, log_sql))
            raise SceneException(e)

        except KeyError as e:
            logger.debug('Error order_attr returned no results\n'
                         'sql: {}'.format(log_sql))

            raise SceneException('Key Error: {}'
                                 .format(e.message))

        return ret
Exemple #11
0
    def get_default_ee_options(item_ls):
        """
        Factory method to return default ESPA order options for orders
        originating through Earth Explorer

        :param item_ls: list of scenes received from EE for an order
                        structure: list({sceneid:, unit_num:})
        :return: dictionary representation of the EE order
        """
        ee_order = {'format': 'gtiff'}
        for item in item_ls:
            try:
                scene_info = sensor.instance(item['sceneid'])
            except sensor.ProductNotImplemented:
                log_msg = ('Received unsupported product via EE: {}'
                           .format(item['sceneid']))
                logger.debug(log_msg)
                continue

            short = scene_info.shortname

            if short in ee_order:
                ee_order[short]['inputs'].append(item['sceneid'])
            else:
                if isinstance(scene_info, sensor.Landsat):
                    ee_order[short] = {'inputs': [item['sceneid']],
                                       'products': ['sr']}
                elif isinstance(scene_info, sensor.Modis):
                    ee_order[short] = {'inputs': [item['sceneid']],
                                       'products': ['l1']}

        return ee_order
Exemple #12
0
    def send_email(self, recipient, subject, body):
        '''Sends an email to a receipient on the behalf of espa'''
        def _validate(email):
            if not validate_email(email):
                raise TypeError("Invalid email address provided:%s" % email)

        to_header = recipient
        if isinstance(recipient, (list, tuple)):
            for r in recipient:
                _validate(r)
            to_header = ','.join(recipient)
        elif isinstance(recipient, basestring):
            _validate(recipient)
            recipient = [recipient]
        else:
            raise ValueError("Unsupported datatype for recipient:%s" %
                             type(recipient))

        logger.debug('Sending email: {} {}'.format(recipient, subject))
        msg = MIMEText(body)
        msg['Subject'] = subject
        msg['To'] = to_header
        msg['From'] = config.get('email.espa_address')
        s = SMTP(host=config.get('email.espa_server'),
                 timeout=float(config.get('email.smtp_timeout')))
        s.sendmail(msg['From'], recipient, msg.as_string())
        s.quit()

        return True
Exemple #13
0
    def backup_configuration(self, filepath=None):
        try:
            response = self.admin.backup_configuration(filepath)
        except:
            logger.debug('ERR version0 backup_configuration: '
                         '{}\ntrace: {}\n'.format(filepath, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #14
0
    def backup_configuration(self, filepath=None):
        try:
            response = self.admin.backup_configuration(filepath)
        except:
            logger.debug('ERR version0 backup_configuration: '
                         '{}\ntrace: {}\n'.format(filepath,
                                                  traceback.format_exc()))
            response = default_error_message

        return response
Exemple #15
0
    def available(self):
        """
        Checks the LTA API status endpoint

        :return: bool
        """
        url = self.base_url + 'login'
        logger.debug('HEAD {}'.format(url))
        resp = requests.head(url)
        return resp.ok
Exemple #16
0
 def get_system_config(self):
     """
     retrieve system configuration variables
     """
     try:
         return self.admin.get_system_config()
     except:
         exc_type, exc_val, exc_trace = sys.exc_info()
         logger.debug(
             "ERR retrieving system config: exception {0}".format(traceback.format_exc()))
         raise exc_type, exc_val, exc_trace
Exemple #17
0
 def get_stat(self, name):
     """
     retrieve requested statistic value
     :return: long
     """
     try:
         response = self.reporting.get_stat(name)
     except:
         logger.debug("ERR version0 get_stat name: {0}, traceback: {1}".format(name, traceback.format_exc()))
         response = default_error_message
     return response
Exemple #18
0
 def get_production_whitelist(self):
     """
     Returns list of ip addresses in hadoop cluster
     :return: list of strings
     """
     try:
         response = self.production.production_whitelist()
     except:
         logger.debug("ERR failure to generate production whitelist\ntrace: {}".format(traceback.format_exc()))
         response = default_error_message
     return response
Exemple #19
0
 def catch_orphaned_scenes(self):
     """
     Handler for marking queued scenes with no corresponding job in hadoop
     :return: true
     """
     try:
         response = self.production.catch_orphaned_scenes()
     except:
         logger.debug("ERR handling orphaned scenes\ntrace: {}".format(traceback.format_exc()))
         response = default_error_message
     return response
Exemple #20
0
 def get_system_config(self):
     """
     retrieve system configuration variables
     """
     try:
         return self.admin.get_system_config()
     except:
         exc_type, exc_val, exc_trace = sys.exc_info()
         logger.debug("ERR retrieving system config: exception {0}".format(
             traceback.format_exc()))
         raise exc_type, exc_val, exc_trace
Exemple #21
0
 def catch_orphaned_scenes(self):
     """
     Handler for marking queued scenes with no corresponding job in hadoop
     :return: true
     """
     try:
         response = self.production.catch_orphaned_scenes()
     except:
         logger.debug("ERR handling orphaned scenes\ntrace: {}".format(traceback.format_exc()))
         response = default_error_message
     return response
Exemple #22
0
 def get_production_whitelist(self):
     """
     Returns list of ip addresses in hadoop cluster
     :return: list of strings
     """
     try:
         response = self.production.production_whitelist()
     except:
         logger.debug("ERR failure to generate production whitelist\ntrace: {}".format(traceback.format_exc()))
         response = default_error_message
     return response
Exemple #23
0
    def update_system_status(self, params):
        """
        update system status attributes
        """
        try:
            response = self.admin.update_system_status(params)
        except:
            exc_type, exc_val, exc_trace = sys.exc_info()
            logger.debug("ERR updating system status params: {0}\n exception {1}".format(params, traceback.format_exc()))
            raise exc_type, exc_val, exc_trace

        return response
Exemple #24
0
    def get_system_status(self):
        """
        retrieve the system status message
        :return: str
        """
        try:
            response = self.admin.get_system_status()
        except:
            logger.debug("ERR version0 get_system_status. traceback {0}".format(traceback.format_exc()))
            response = default_error_message

        return response
Exemple #25
0
    def available_reports(self):
        """
        returns list of available reports
        :return: List
        """
        try:
            response = self.reporting.listing()
        except:
            logger.debug("ERR version0 available_reports traceback {0}".format(traceback.format_exc()))
            response = default_error_message

        return response
Exemple #26
0
 def get_stat(self, name):
     """
     retrieve requested statistic value
     :return: long
     """
     try:
         response = self.reporting.get_stat(name)
     except:
         logger.debug(
             "ERR version0 get_stat name: {0}, traceback: {1}".format(
                 name, traceback.format_exc()))
         response = default_error_message
     return response
Exemple #27
0
 def get_aux_report(self, group, year):
     """
     get data on gaps in available auxiliary data
     :param group: sensor group, L17 or L8
     :param year: year to report on
     :return: dict of missing days by year
     """
     try:
         response = self.reporting.missing_auxiliary_data(group, year)
     except:
         logger.debug("ERR retrieving auxiliary report for {} group, year {}\ntrace: {}".format(group, year, traceback.format_exc()))
         response = default_error_message
     return response
Exemple #28
0
    def available_reports(self):
        """
        returns list of available reports
        :return: List
        """
        try:
            response = self.reporting.listing()
        except:
            logger.debug("ERR version0 available_reports traceback {0}".format(
                traceback.format_exc()))
            response = default_error_message

        return response
Exemple #29
0
    def get_report(self, name):
        """
        retrieve a named report
        :param name:
        :return: OrderedDict
        """
        try:
            response = str(self.reporting.run(name))
        except:
            logger.debug("ERR version0 get_report name {0}\ntraceback {1}".format(name, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #30
0
    def execute_command(self, cmd):
        """
        Execute the given command on the cache

        :param cmd: cmd string to execute
        :return: results of the command
        """
        try:
            result = self.client.execute(cmd)
        except Exception, exception:
            logger.debug('Error executing command: {} '
                         'Raised exception: {}'.format(cmd, exception))
            raise OnlineCacheException(exception)
Exemple #31
0
    def update_system_status(self, params):
        """
        update system status attributes
        """
        try:
            response = self.admin.update_system_status(params)
        except:
            exc_type, exc_val, exc_trace = sys.exc_info()
            logger.debug(
                "ERR updating system status params: {0}\n exception {1}".
                format(params, traceback.format_exc()))
            raise exc_type, exc_val, exc_trace

        return response
Exemple #32
0
 def error_to(self, orderid, state):
     """
     flip scenes in error for given order to provided state
     :param orderid: order to work with
     :param state: value to set to
     :return: True
     """
     try:
         response = self.admin.error_to(orderid, state)
     except:
         logger.debug("ERR failure to reset to {} error scenes for {}\ntrace: {}".format(state, orderid,
                                                                                         traceback.format_exc()))
         response = default_error_message
     return response
Exemple #33
0
    def get_system_status(self):
        """
        retrieve the system status message
        :return: str
        """
        try:
            response = self.ordering.get_system_status()
        except:
            logger.debug(
                "ERR version1 get_system_status. traceback {0}".format(
                    traceback.format_exc()))
            response = default_error_message

        return response
Exemple #34
0
    def get(cls, col_name, scene_name, orderid):
        """
        Retrieve a value for a particular column based on
        the long name of the order

        :param col_name: column value to retrieve
        :param scene_name: scene/collection id
        :param orderid: long name for the related order,
         [email protected]
        :return: column value
        """
        sql = ('select %s '
               'from ordering_scene '
               'join ordering_order '
               'on ordering_order.id = ordering_scene.order_id '
               'where ordering_scene.name = %s '
               'and ordering_order.orderid = %s')

        if '.' in col_name:
            _, col = col_name.split('.')
        else:
            col = col_name

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = (db.cursor.
                           mogrify(sql, (db_extns.AsIs(col_name),
                                         scene_name, orderid)))
                logger.info(log_sql)
                db.select(sql, (db_extns.AsIs(col_name),
                                scene_name, orderid))
                ret = db[0][col]

        except DBConnectException as e:
            logger.debug('Error scene get\n'
                         'msg: {0}\n'
                         'sql: {1}'.format(e.message, log_sql))

            raise SceneException(e.message)

        except KeyError as e:
            logger.debug('Scene.get returned no results\n'
                         'sql: {}'.format(log_sql))

            raise SceneException('Key Error: {}'
                                 .format(e.message))

        return ret
Exemple #35
0
    def get(cls, col_name, scene_name, orderid):
        """
        Retrieve a value for a particular column based on
        the long name of the order

        :param col_name: column value to retrieve
        :param scene_name: scene/collection id
        :param orderid: long name for the related order,
         [email protected]
        :return: column value
        """
        sql = ('select %s '
               'from ordering_scene '
               'join ordering_order '
               'on ordering_order.id = ordering_scene.order_id '
               'where ordering_scene.name = %s '
               'and ordering_order.orderid = %s')

        if '.' in col_name:
            _, col = col_name.split('.')
        else:
            col = col_name

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = (db.cursor.
                           mogrify(sql, (db_extns.AsIs(col_name),
                                         scene_name, orderid)))
                logger.info(log_sql)
                db.select(sql, (db_extns.AsIs(col_name),
                                scene_name, orderid))
                ret = db[0][col]

        except DBConnectException as e:
            logger.debug('Error scene get\n'
                         'msg: {0}\n'
                         'sql: {1}'.format(e.message, log_sql))

            raise SceneException(e.message)

        except KeyError as e:
            logger.debug('Scene.get returned no results\n'
                         'sql: {}'.format(log_sql))

            raise SceneException('Key Error: {}'
                                 .format(e.message))

        return ret
Exemple #36
0
 def _api(self, verb, url, data=None, header=None):
     # certificate verification fails in dev/tst
     verify = True if cfg.mode == 'ops' else False
     try:
         logger.debug('[%s] %s', verb.upper(), self._host+url)
         resp = getattr(requests, verb)(self._host + url, data=data,
                                        headers=header, verify=verify)
         resp.raise_for_status()
     except Exception as e:
         raise ERSApiConnectionException(e)
     try:
         data = resp.json()
     except Exception as e:
         raise ERSApiErrorException(e)
     return data
Exemple #37
0
    def get_report(self, name):
        """
        retrieve a named report
        :param name:
        :return: OrderedDict
        """
        try:
            response = str(self.reporting.run(name))
        except:
            logger.debug(
                "ERR version0 get_report name {0}\ntraceback {1}".format(
                    name, traceback.format_exc()))
            response = default_error_message

        return response
    def stat_query(self, name):
        if name not in STATS:
            raise NotImplementedError("value: {0}".format(name))

        query = STATS[name]['query']

        if query is not None and len(query) > 0:
            with db_instance() as db:
                db.select(query)
                result = db.dictfetchall
                stat = result[0]['statistic']
            return stat
        else:
            logger.debug("Query was empty for {0}: {1}".format(name, query))
            return None
Exemple #39
0
    def handle_orders(self):
        """Handler for accepting orders and products into the processing system

        Args:
            none

        Returns:
            True if successful
        """
        try:
            response = self.production.handle_orders()
        except:
            logger.debug("ERR version1 handle_orders. trace: {0}".format(traceback.format_exc()))
            response = default_error_message

        return response
Exemple #40
0
    def roles(self):
        result = None
        with db_instance() as db:
            db.select(
                "select is_staff, is_active, is_superuser from auth_user where id = %s;"
                % self.id)
        try:
            result = db[0]
        except:
            exc_type, exc_val, exc_trace = sys.exc_info()
            logger.debug(
                "ERR retrieving roles for user. msg{0} trace{1}".format(
                    exc_val, traceback.format_exc()))
            raise exc_type, exc_val, exc_trace

        return result
Exemple #41
0
    def fetch_user_orders_feed(self, email):
        """
        returns order and scene details for a user formatted
        for an rss feed
        :param email:
        :return: dict
        """
        try:
            response = self.ordering.fetch_user_orders_feed(email)
        except:
            logger.debug(
                "ERR version1 fetch_user_orders_feed email: {0}\nexception: {1}"
                .format(email, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #42
0
    def handle_orders(self):
        """Handler for accepting orders and products into the processing system

        Args:
            none

        Returns:
            True if successful
        """
        try:
            response = self.production.handle_orders()
        except:
            logger.debug("ERR version0 handle_orders. trace: {0}".format(traceback.format_exc()))
            response = default_error_message

        return response
    def error_to(self, orderid, state):
        order = Order.find(orderid)
        err_scenes = order.scenes({'status': 'error'})
        try:
            for scene in err_scenes:
                scene.update('status', state)

            if state == 'submitted':
                order.status = 'ordered'
                order.completion_email_sent = None
                order.save()

            return True
        except SceneException as e:
            logger.debug('ERR admin provider error_to\ntrace: {}'.format(e.message))
            raise AdministrationProviderException('ERR updating with error_to')
Exemple #44
0
    def save(self):
        """
        Upsert self to the database
        """
        sql = ('INSERT INTO ordering_order %s VALUES %s '
               'ON CONFLICT (orderid) '
               'DO UPDATE '
               'SET %s = %s')

        attr_tup = ('orderid', 'status', 'order_source',
                    'product_options', 'product_opts', 'order_type',
                    'initial_email_sent', 'completion_email_sent',
                    'note', 'completion_date', 'order_date', 'user_id',
                    'ee_order_id', 'email', 'priority')

        vals = tuple(self.__getattribute__(v)
                     if v != 'product_opts'
                     else json.dumps(self.__getattribute__(v))
                     for v in attr_tup)

        cols = '({})'.format(','.join(attr_tup))

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, (db_extns.AsIs(cols),
                                                  vals,
                                                  db_extns.AsIs(cols),
                                                  vals))
                db.execute(sql, (db_extns.AsIs(cols), vals,
                                 db_extns.AsIs(cols), vals))
                db.commit()

                logger.info('Saved updates to order id: {}\n'
                            'order.id: {}\nsql: {}\nargs: {}'
                            .format(self.orderid, self.id, log_sql,
                                    zip(attr_tup, vals)))
        except DBConnectException as e:
            logger.debug('Error saving order: {}\nsql: {}'
                         .format(e.message, log_sql))

            raise OrderException(e)

        new = Order.find(self.id)

        for att in attr_tup:
            self.__setattr__(att, new.__getattribute__(att))
Exemple #45
0
    def create(cls, params):
        """
        Create a new scene entry in the ordering_scene table
        Also supports a bulk insert for large sets of scenes to insert

        dict{'name': ,
             'order_id': ,
             'status': ,
             'sensor_type': ,
             'ee_unit_id': }

        :param params: dictionary representation of a scene to insert
         into the system or a list of dictionary objects
        """
        if isinstance(params, (list, tuple)):
            template = ','.join(['%s'] * len(params))
            args = [(s['name'], s['order_id'],
                     s['status'], s['sensor_type'],
                     s['ee_unit_id'], '', '', '', '', '')
                    for s in params]
        else:
            template = '%s'
            args = [(params['name'], params['order_id'],
                     params['status'], params['sensor_type'],
                     params['ee_unit_id'], '', '', '', '', '')]

        sql = ('INSERT INTO ordering_scene '
               '(name, order_id, status, sensor_type, ee_unit_id, '
               'product_distro_location, product_dload_url, '
               'cksum_distro_location, cksum_download_url, '
               'processing_location) VALUES {}'.format(template))

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, args)
                logger.info('scene creation sql: {}'
                            .format(log_sql))
                db.execute(sql, args)
                db.commit()

        except DBConnectException as e:
            logger.debug('error creating new scene(s): {}\n'
                         'sql: {}\n'
                         .format(e.message, log_sql))
            raise SceneException(e.message)
Exemple #46
0
    def create(cls, params):
        """
        Create a new scene entry in the ordering_scene table
        Also supports a bulk insert for large sets of scenes to insert

        dict{'name': ,
             'order_id': ,
             'status': ,
             'sensor_type': ,
             'ee_unit_id': }

        :param params: dictionary representation of a scene to insert
         into the system or a list of dictionary objects
        """
        if isinstance(params, (list, tuple)):
            template = ','.join(['%s'] * len(params))
            args = [(s['name'], s['order_id'],
                     s['status'], s['sensor_type'],
                     s['ee_unit_id'], '', '', '', '', '')
                    for s in params]
        else:
            template = '%s'
            args = [(params['name'], params['order_id'],
                     params['status'], params['sensor_type'],
                     params['ee_unit_id'], '', '', '', '', '')]

        sql = ('INSERT INTO ordering_scene '
               '(name, order_id, status, sensor_type, ee_unit_id, '
               'product_distro_location, product_dload_url, '
               'cksum_distro_location, cksum_download_url, '
               'processing_location) VALUES {}'.format(template))

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, args)
                logger.info('scene creation sql: {}'
                            .format(log_sql))
                db.execute(sql, args)
                db.commit()

        except DBConnectException as e:
            logger.debug('error creating new scene(s): {}\n'
                         'sql: {}\n'
                         .format(e.message, log_sql))
            raise SceneException(e.message)
    def error_to(self, orderid, state):
        order = Order.find(orderid)
        err_scenes = order.scenes({'status': 'error'})
        try:
            for scene in err_scenes:
                scene.update('status', state)

            if state == 'submitted':
                order.status = 'ordered'
                order.completion_email_sent = None
                order.save()

            return True
        except SceneException as e:
            logger.debug('ERR admin provider error_to\ntrace: {}'.format(
                e.message))
            raise AdministrationProviderException('ERR updating with error_to')
Exemple #48
0
    def save(self):
        """
        Upsert self to the database
        """
        sql = ('INSERT INTO ordering_order %s VALUES %s '
               'ON CONFLICT (orderid) '
               'DO UPDATE '
               'SET %s = %s')

        attr_tup = ('orderid', 'status', 'order_source', 'product_options',
                    'product_opts', 'order_type', 'initial_email_sent',
                    'completion_email_sent', 'note', 'completion_date',
                    'order_date', 'user_id', 'ee_order_id', 'email',
                    'priority')

        vals = tuple(
            self.__getattribute__(v) if v != 'product_opts' else json.
            dumps(self.__getattribute__(v)) for v in attr_tup)

        cols = '({})'.format(','.join(attr_tup))

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(
                    sql,
                    (db_extns.AsIs(cols), vals, db_extns.AsIs(cols), vals))
                db.execute(
                    sql,
                    (db_extns.AsIs(cols), vals, db_extns.AsIs(cols), vals))
                db.commit()

                logger.info('Saved updates to order id: {}\n'
                            'order.id: {}\nsql: {}\nargs: {}'.format(
                                self.orderid, self.id, log_sql,
                                zip(attr_tup, vals)))
        except DBConnectException as e:
            logger.debug('Error saving order: {}\nsql: {}'.format(
                e.message, log_sql))

            raise OrderException(e)

        new = Order.find(self.id)

        for att in attr_tup:
            self.__setattr__(att, new.__getattribute__(att))
Exemple #49
0
    def get_production_key(self, key):
        """Returns value for given configuration key

        Arg:
            str representation of key

        Returns:
            dict: of key and value
        """
        try:
            response = {"msg": "'{0}' is not a valid key".format(key)}
            if key in self.configuration.configuration_keys:
                response = {key: self.configuration.get(key)}
        except:
            logger.debug("ERR version1 get_production_key, arg: {0}\ntrace: {1}\n".format(key, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #50
0
    def access_configuration(self, key=None, value=None, delete=False):
        """
        Provide access to the configuration keys

        :param key: configuration key to match on
        :param value: value to update key to
        :param delete: delete the matching key
        :return: configuration after operation
        """
        try:
            response = self.admin.access_configuration(key=key, value=value, delete=delete)
        except:
            logger.debug('ERR version0 configuration_management:'
                         ' {}\ntrace: {}\n'.format(','.join([key, value, delete]),
                                                   traceback.format_exc()))
            response = default_error_message

        return response
Exemple #51
0
    def fetch_order(self, ordernum):
        """ Returns details of a submitted order

        Args:
            ordernum (str): the order id of a submitted order

        Returns:
            dict: of order details
        """
        try:
            response = self.ordering.fetch_order(ordernum)
        except:
            logger.debug(
                "ERR version1 fetch_order arg: {0}\nexception {1}".format(
                    ordernum, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #52
0
    def queue_products(self, order_name_tuple_list, processing_location, job_name):
        """Place products into queued status in bulk

        Args:
            params (dict): {'order_name_list':[], 'processing_loc': '', 'job_name': ''}

        Returns:
            True if successful
        """
        try:
            response = self.production.queue_products(order_name_tuple_list, processing_location, job_name)
        except:
            logger.debug("ERR version0 queue_products"
                         " params: {0}\ntrace: {1}".format((order_name_tuple_list, processing_location, job_name),
                                                           traceback.format_exc()))
            response = default_error_message

        return response
Exemple #53
0
    def get_production_key(self, key):
        """Returns value for given configuration key

        Arg:
            str representation of key

        Returns:
            dict: of key and value
        """
        try:
            response = {"msg": "'{0}' is not a valid key".format(key)}
            if key in self.configuration.configuration_keys:
                response = {key: self.configuration.get(key)}
        except:
            logger.debug("ERR version0 get_production_key, arg: {0}\ntrace: {1}\n".format(key, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #54
0
    def __init__(self):
        self.orderpath = self.config.get(self.__order_path_key)

        if not self.orderpath:
            msg = '{} not defined in configurations'.format(self.__order_path_key)
            logger.debug(msg)
            raise OnlineCacheException(msg)

        host, user, pw = self.config.get([self.__host_key,
                                          self.__user_key,
                                          self.__pw_key])

        self.client = sshcmd.RemoteHost(host, user, pw, timeout=5)

        try:
            self.client.execute('ls')
        except Exception as e:
            logger.debug('No connection to OnlineCache host: {}'.format(e))
            raise OnlineCacheException(e)
Exemple #55
0
    def fetch_production_products(self, params):
        """Returns products ready for production

        Arg:
            params (dict): with the following keys:
                        for_user (str): username on the order
                        priority (str): 'high' | 'normal' | 'low'
                        product_types (str): 'modis,landsat'
                        encode_urls (bool): True | False

        Returns:
            list: list of products
        """
        try:
            response = self.production.get_products_to_process(**params)
        except:
            logger.debug("ERR version0 fetch_production_products, params: {0}\ntrace: {1}\n".format(params, traceback.format_exc()))
            response = default_error_message

        return response
Exemple #56
0
    def post(version):
        try:
            user = flask.g.user
            order = request.get_json(force=True)
            if not order:
                message = {"status": 400, "msg": "Unable to parse json data. Please ensure your order follows json "
                                                 "conventions and your http call is correct. If you believe this "
                                                 "message is in error please email customer service"}
            else:
                try:
                    order = lowercase_all(order)
                    orderid = espa.place_order(order, user)
                    if isinstance(orderid, str) and "@" in orderid:
                        # if order submission was successful, orderid is returned as a string
                        # which includes the submitters email address
                        message = {"status": 200, "orderid": orderid}
                    else:
                        # there was a problem, and orderid is a dict with the problem details
                        logger.info("problem with user submitted order. user: {0}\n details: {1}".format(user.username, orderid))
                        message = {"status": 400, "message": orderid}
                except Exception as e:
                    logger.debug("exception posting order: {0}\nuser: {1}\n msg: {2}".format(order, user.username, e.message))
                    message = {"status": 500, "msg": "the system experienced an exception. the admins have been notified"}
        except BadRequest as e:
            # request.get_json throws a BadRequest
            logger.debug("BadRequest, could not parse request into json {}\nuser: {}\nform data {}\n".format(e.description, user.username, request.form))
            message = {"status": 400, "msg": "Could not parse the request into json"}
        except Exception as e:
            logger.debug("ERR posting order. user: {0}\n error: {1}".format(user.username, e))
            message = {"status": 500, "msg": "the system has experienced an exception. the admins have been notified."}

        response = jsonify(message)
        response.status_code = message['status']
        return response
Exemple #57
0
    def execute(self, command):
        """ """
        try:
            if self.debug is True:
                logger.debug("Attempting to run [%s] on %s as %s" % (command,
                                                                     self.host,
                                                                     self.user))

            self.client = paramiko.SSHClient()
            self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

            if self.pw is not None:
                self.client.connect(self.host,
                                    username=self.user,
                                    password=self.pw,
                                    timeout=self.timeout)
            else:
                self.client.connect(self.host,
                                    username=self.user,
                                    timeout=self.timeout)

            stdin, stdout, stderr = self.client.exec_command(command)
            stdin.close()

            return {'stdout': stdout.readlines(), 'stderr': stderr.readlines()}

        except paramiko.SSHException as e:
            logger.debug('Failed running [{}]'
                         ' on {} as {} exception: {}'.format(command,
                                                             self.host,
                                                             self.user,
                                                             e))

            return e

        finally:
            if self.client is not None:
                self.client.close()
                self.client = None
Exemple #58
0
    def save(self):
        """
        Save the current configuration of the scene object to the DB
        """
        sql = 'UPDATE ordering_scene SET %s = %s WHERE id = %s'

        attr_tup = ('status', 'cksum_download_url', 'log_file_contents',
                    'processing_location', 'retry_after', 'job_name',
                    'note', 'retry_count', 'sensor_type',
                    'product_dload_url', 'tram_order_id',
                    'completion_date', 'ee_unit_id', 'retry_limit',
                    'cksum_distro_location', 'product_distro_location',
                    'reported_orphan', 'orphaned', 'failed_lta_status_update',
                    'download_size')

        vals = tuple(self.__getattribute__(v) for v in attr_tup)
        cols = '({})'.format(','.join(attr_tup))

        log_sql = ''
        try:
            with db_instance() as db:
                log_sql = db.cursor.mogrify(sql, (db_extns.AsIs(cols),
                                                  vals, self.id))

                db.execute(sql, (db_extns.AsIs(cols), vals, self.id))
                db.commit()
                logger.info('\n*** Saved updates to scene id: {}, name:{}\n'
                            'sql: {}\n args: {}\n***'
                            .format(self.id, self.name,
                                    log_sql, zip(attr_tup, vals)))
        except DBConnectException as e:
            logger.debug("Error saving scene: {}\n"
                         "sql: {}".format(e.message, log_sql))
            raise SceneException(e)

        new = Scene.where({'id': self.id})[0]

        for att in attr_tup:
            self.__setattr__(att, new.__getattribute__(att))