Ejemplo n.º 1
0
def nuvla_init(config):
    log.info("Authenticating with Nuvla...")
    nuvla = Nuvla(endpoint=config.get('nuvla', {}).get('endpoint'), insecure=True)
    n = 0
    ts = time.time()
    while True:
        try:
            nuvla.login_password(config['nuvla']['username'], config['nuvla']['password'])
            break
        except RETRY_EXCEPTIONS as ex:
            log.error("Authenticating with Nuvla... failed: {}".format(ex))
            st = 2 ** n
            log.info("Authenticating with Nuvla... re-attempting in {} sec.".format(st))
            time.sleep(st)
            n = (n < 7) and (n + 1) or 0
    log.info("Authenticating with Nuvla... done. (time took: {} sec)".format(int(time.time() - ts)))
    return nuvla
Ejemplo n.º 2
0
        import http.client as http_client
    except ImportError:
        # Python 2
        import httplib as http_client
    http_client.HTTPConnection.debuglevel = 1

    # You must initialize logging, otherwise you'll not see debug output.
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True


nuvla = Nuvla(endpoint='https://localhost', insecure=True)
res = nuvla.login_password('super', 'supeR8-supeR8')
if not res.ok:
    raise Exception('Login failed: {}'.format(res.json().get('message', '')))

dpl_api = Deployment(nuvla)

module_id = 'module/20b6ea7a-ee55-44e7-93e7-7f26b4caef18'

data_records = ['data-record/36fe11cc-7f4f-4ee5-a123-0fafacf4a24b',
                'data-record/6803e2e4-1db6-42b4-8b8c-b3273c938366']
data_objects = ['data-object/36fe11cc-7f4f-4ee5-a123-0fafacf4a24b',
                'data-object/6803e2e4-1db6-42b4-8b8c-b3273c938366']
data_sets = [{'id': 'data-set/31d8ce24-7567-455e-9d39-4763c1d4010e',
              'time-start': '2020-02-14T23:00:00Z',
              'time-end': '2020-03-16T22:45:00Z'},
             {'filter': "content-type='gnss/perf-test' and gnss:station='nuvlabox-esac'",
Ejemplo n.º 3
0
class Base(object):
    stop_event = threading.Event()

    def __init__(self):
        self.args = None
        self._init_args_parser()
        self._kz: KazooClient = None
        self.api: Api = None
        self.name = None
        self.statsd: StatsClient = None

        self._init_logger()

        signal.signal(signal.SIGTERM, Base.on_exit)
        signal.signal(signal.SIGINT, Base.on_exit)

    def _init_args_parser(self):
        parser = argparse.ArgumentParser(description='Process Nuvla jobs')
        required_args = parser.add_argument_group('required named arguments')

        parser.add_argument(
            '--zk-hosts', dest='zk_hosts', default=None, nargs='+', metavar='HOST',
            help='ZooKeeper list of hosts [localhost:port]. (default: 127.0.0.1:2181)')

        parser.add_argument('--api-url', dest='api_url', default='https://nuvla.io', metavar='URL',
                            help='Nuvla endpoint to connect to (default: https://nuvla.io)')

        required_args.add_argument('--api-user', dest='api_user', help='Nuvla Username',
                                   metavar='USERNAME')
        required_args.add_argument('--api-pass', dest='api_pass', help='Nuvla Password',
                                   metavar='PASSWORD')

        required_args.add_argument('--api-key', dest='api_key', help='Nuvla API Key Id',
                                   metavar='API_KEY')
        required_args.add_argument('--api-secret', dest='api_secret', help='Nuvla API Key Secret',
                                   metavar='API_SECRET')

        parser.add_argument('--api-insecure', dest='api_insecure', default=False,
                            action='store_true',
                            help='Do not check Nuvla certificate')

        parser.add_argument('--api-authn-header', dest='api_authn_header', default=None,
                            help='Set header for internal authentication')

        parser.add_argument('--name', dest='name', metavar='NAME', default=None,
                            help='Base name for this process')

        parser.add_argument('--statsd', dest='statsd', metavar='STATSD',
                            default=None, help=f'StatsD server as host[:{STATSD_PORT}].')

        self._set_command_specific_options(parser)

        self.args = parser.parse_args()

    def _set_command_specific_options(self, parser):
        pass

    @staticmethod
    def _init_logger():
        log_format_str = '%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(message)s'
        format_log = logging.Formatter(log_format_str)
        logger = logging.getLogger()
        logger.handlers[0].setFormatter(format_log)
        logger.setLevel(logging.INFO)
        logging.getLogger('kazoo').setLevel(logging.WARN)
        logging.getLogger('elasticsearch').setLevel(logging.WARN)
        logging.getLogger('nuvla').setLevel(logging.INFO)
        logging.getLogger('urllib3').setLevel(logging.WARN)

    def _publish_metric(self, name, value):
        if self.statsd:
            self.statsd.gauge(name, value)
            logging.debug(f'published: {name} {value}')

    @staticmethod
    def on_exit(signum, frame):
        print('\n\nExecution interrupted by the user!')
        Base.stop_event.set()

    def do_work(self):
        raise NotImplementedError()

    def execute(self):
        self.name = self.args.name if self.args.name is not None else names[
            int(random.uniform(1, len(names) - 1))]

        # true unless header authentication is used
        reauthenticate = self.args.api_authn_header is None
        self.api = Api(endpoint=self.args.api_url, insecure=self.args.api_insecure,
                       persist_cookie=False, reauthenticate=reauthenticate,
                       authn_header=self.args.api_authn_header)
        try:
            if self.args.api_authn_header is None:
                if self.args.api_key and self.args.api_secret:
                    response = self.api.login_apikey(self.args.api_key, self.args.api_secret)
                else:
                    response = self.api.login_password(self.args.api_user, self.args.api_pass)
                if response.status_code == 403:
                    raise ConnectionError(
                        'Login with following user/apikey {} failed!'.format(self.args.api_user))
                # Uncomment following lines for manual remote test
                # session_id = self.api.current_session()
                # self.api.operation(self.api.get(session_id), 'switch-group',
                #                    {'claim': "group/nuvla-admin"})
        except ConnectionError as e:
            logging.error('Unable to connect to Nuvla endpoint {}! {}'.format(self.api.endpoint, e))
            exit(1)

        if self.args.zk_hosts:
            from kazoo.client import KazooClient, KazooRetry
            self._kz = KazooClient(','.join(self.args.zk_hosts),
                                   connection_retry=KazooRetry(max_tries=-1),
                                   command_retry=KazooRetry(max_tries=-1), timeout=30.0)
            self._kz.start()

        if self.args.statsd:
            statsd_hp = self.args.statsd.split(':')
            statsd_port = STATSD_PORT
            statsd_host = statsd_hp[0]
            if len(statsd_hp) > 1:
                statsd_port = statsd_hp[1]
            try:
                self.statsd = StatsClient(host=statsd_host,
                                          port=statsd_port,
                                          prefix=None,
                                          ipv6=False)
            except Exception as ex:
                logging.error(f'Failed to initialise StatsD client for {self.args.statsd}: {ex}')

        self.do_work()

        while True:
            signal.pause()