示例#1
0
    def __init__(self, cmp_client=None):
        """Construct a new FlexerContext object."""
        self.response_headers = {}
        self.config = {}
        self.secrets = {}
        self.state = None
        self.customer_id = None
        self.user_id = None
        self.user_name = ""
        self.region = Config.CMP_REGION
        self.platform = Config.CMP_PLATFORM
        self.version = Config.FLEXER_VERSION
        self.module_id = Config.MODULE_ID

        if cmp_client is None:
            auth = (Config.CMP_USERNAME, Config.CMP_PASSWORD)
            self.api = CmpClient(url=Config.CMP_URL,
                                 auth=auth,
                                 access_token=Config.CMP_ACCESS_TOKEN)
        else:
            self.api = cmp_client

        self.api_url = self.api.api_url
        self.api_auth = self.api.api_auth
        self.api_token = self.api.api_token
        self._db_regex = re.compile(r'mongodb://[^:]+:[^@]+@[^/]+/[^ ]+')
示例#2
0
 def __init__(self, cmp_client=None):
     """Construct a new FlexerContext object."""
     self.response_headers = {}
     if cmp_client is None:
         self.api_url = Config.CMP_URL
         self.api_auth = (Config.CMP_USERNAME, Config.CMP_PASSWORD)
         self.api = CmpClient(self.api_url, self.api_auth)
     else:
         self.api_url = cmp_client._url
         self.api_auth = cmp_client._auth
         self.api = cmp_client
示例#3
0
    def setUpClass(cls):
        # The rule to evaluate number as boolean is inspired by C++11 implementation
        # All numbers but 0 and True or true will be evaluated as True
        cls.logging = re.search(r'^-?[1-9][0-9]*$|^[Tt]rue$',
                                os.getenv("LOG_TO_STDOUT", "False")) != None
        path = os.getenv("CONFIG_YAML", DEFAULT_CONFIG_YAML)
        cls.account = read_yaml_file(path)
        cls.account["credentials"] = (lookup_values(
            cls.account.get("credentials_keys")))
        if "resources" in cls.account:
            cls.resource_data = cls.account.get("resources")
        else:
            cls.resource_data = [{
                "resource":
                cls.account.get("resource"),
                "expected_metrics":
                cls.account.get("expected_metrics")
            }]

        cls.runner = Flexer()
        cfg = load_config(cfg_file=CONFIG_FILE)["regions"]["default"]
        client = CmpClient(url=cfg["cmp_url"],
                           auth=(cfg['cmp_api_key'], cfg['cmp_api_secret']))
        cls.context = FlexerContext(cmp_client=client)
        secrets = (lookup_values(cls.account.get("secrets_keys")))
        cls.context.secrets = secrets
示例#4
0
    def __init__(self, cmp_client=None):
        """Construct a new FlexerContext object."""
        self.response_headers = {}
        self.config = {}
        self.secrets = {}
        self.state = None
        self.module_id = Config.MODULE_ID

        if cmp_client is None:
            auth = (Config.CMP_USERNAME, Config.CMP_PASSWORD)
            self.api = CmpClient(url=Config.CMP_URL,
                                 auth=auth,
                                 access_token=Config.CMP_ACCESS_TOKEN)
        else:
            self.api = cmp_client

        self.api_url = self.api.api_url
        self.api_auth = self.api.api_auth
        self.api_token = self.api.api_token
示例#5
0
def cli(ctx, auth):
    """flexer manages your nFlex scripts from the terminal."""
    cfg = ctx.config["regions"][auth]
    verify_ssl = ctx.config.get("verify_ssl", False)
    if "verify_ssl" in cfg:
        verify_ssl = cfg["verify_ssl"]

    ctx.cmp = CmpClient(url=cfg['cmp_url'],
                        auth=(cfg['cmp_api_key'], cfg['cmp_api_secret']),
                        verify_ssl=verify_ssl)
    ctx.nflex = NflexClient(ctx.cmp)
示例#6
0
    def setUpClass(cls):
        path = os.getenv("CONFIG_YAML", DEFAULT_CONFIG_YAML)
        cls.account = read_yaml_file(path)
        cls.account["credentials"] = (lookup_credentials(
            cls.account.get("credentials_keys")))

        cls.runner = Flexer()
        cfg = load_config(cfg_file=CONFIG_FILE)["regions"]["default"]
        client = CmpClient(url=cfg["cmp_url"],
                           auth=(cfg['cmp_api_key'], cfg['cmp_api_secret']))
        cls.context = FlexerContext(cmp_client=client)
示例#7
0
    def test_tasks(self, mock):
        auth = ('username', 'password')
        cmp_client = CmpClient(url='http://localhost',
                               auth=auth,
                               access_token='foo')

        task_id = 'task-1'
        context = FlexerContext(cmp_client=cmp_client)

        # Should do nothing
        set_task_percentage(context, None, 100)

        mock.post('http://localhost/tasks/task-1/update', text='null')
        set_task_percentage(context, task_id, 100)

        mock.post('http://localhost/tasks/task-1/update', text='"Oops"',
                  status_code=500)
        with pytest.raises(Exception) as e:
            set_task_percentage(context, task_id, 100)
        assert 'Got bad response from tasks API' in str(e)
示例#8
0
class FlexerContext(object):
    """The FlexerContext object provides context to the nflex module
    during it's execution.
    """
    def __init__(self, cmp_client=None):
        """Construct a new FlexerContext object."""
        self.response_headers = {}
        self.config = {}
        self.secrets = {}
        self.state = None
        self.customer_id = None
        self.user_id = None
        self.user_name = ""
        self.region = Config.CMP_REGION
        self.platform = Config.CMP_PLATFORM
        self.version = Config.FLEXER_VERSION
        self.module_id = Config.MODULE_ID

        if cmp_client is None:
            auth = (Config.CMP_USERNAME, Config.CMP_PASSWORD)
            self.api = CmpClient(url=Config.CMP_URL,
                                 auth=auth,
                                 access_token=Config.CMP_ACCESS_TOKEN)
        else:
            self.api = cmp_client

        self.api_url = self.api.api_url
        self.api_auth = self.api.api_auth
        self.api_token = self.api.api_token
        self._db_regex = re.compile(r'mongodb://[^:]+:[^@]+@[^/]+/[^ ]+')

    def database(self, name):
        conn_string = self.secrets.get(Config.DB_KEY_PREFIX + name)
        if not conn_string:
            return None

        if not self._db_regex.match(conn_string):
            raise Exception(
                "The %s secret is not a valid MongoDB connection string" %
                name)

        client = MongoClient(conn_string,
                             ssl=True,
                             ssl_ca_certs='/nflex-root.pem',
                             connectTimeoutMS=3000)

        return client.get_database()

    def log(self, message, severity="info"):
        """Log a message to CMP."""
        try:
            payload = {
                "message": message,
                "severity": severity.upper(),
                "service": "nflex.flexer",
                "timestamp":
                datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
            }
            if self.module_id:
                payload["resource_id"] = "nflex-module-%s" % self.module_id

            r = self.api.post("/logs", [payload])

            if r.status_code != 200:
                print("Error sending logs to CMP: %s" % r.text)

        except Exception as err:
            print("Error sending logs to CMP: %s" % err)

    def mail(self,
             user=None,
             group=None,
             matter='nflex-email-default',
             medium='email',
             subject=None,
             message=None,
             params=None):
        """Send an email through CMP."""
        if params is None:
            params = {}

        url = ''
        if user:
            url = '/notifications/send/%s' % user
        elif group:
            url = '/notifications/groups/%s/send' % group
        else:
            url = '/notifications/send'

        # function keyword helpers for subject and message
        # (used in default template)
        if subject:
            params['subject'] = subject

        if message:
            params['message'] = message

        try:
            r = self.api.post(url, {
                'matter': matter,
                'medium': medium,
                'params': params,
            })

            if r.status_code != 200:
                logger.error("Error sending logs to CMP: %s", r.text)

        except Exception as err:
            logger.error("Error sending logs to CMP: %s", err)

    def set_response_header(self, key, value):
        """Set a response header"""
        self.response_headers[key] = value
示例#9
0
class FlexerContext(object):
    """The FlexerContext object provides context to the nflex module
    during it's execution.
    """
    def __init__(self, cmp_client=None):
        """Construct a new FlexerContext object."""
        self.response_headers = {}
        if cmp_client is None:
            self.api_url = Config.CMP_URL
            self.api_auth = (Config.CMP_USERNAME, Config.CMP_PASSWORD)
            self.api = CmpClient(self.api_url, self.api_auth)
        else:
            self.api_url = cmp_client._url
            self.api_auth = cmp_client._auth
            self.api = cmp_client

    def log(self, message, severity="info"):
        """Log a message to CMP."""
        try:
            r = self.api.post("/logs", [
                {
                    "message": message,
                    "severity": severity.upper(),
                    "service": "nflex.runner",
                    "timestamp": datetime.utcnow().strftime(
                        '%Y-%m-%dT%H:%M:%S.%fZ'
                    ),
                }
            ])

        except Exception as err:
            logger.error("Error sending logs to CMP: %s", err)

        if r.status_code != 200:
            logger.error("Error sending logs to CMP: %s", r.text)

    def mail(self,
             user=None,
             group=None,
             matter='nflex-email-default',
             medium='email',
             subject=None,
             message=None,
             params=None):
        """Send an email through CMP."""
        if params is None:
            params = {}

        url = ''
        if user:
            url = '/notifications/send/%s' % user
        elif group:
            url = '/notifications/groups/%s/send' % group
        else:
            url = '/notifications/send'

        # function keyword helpers for subject and message
        # (used in default template)
        if subject:
            params['subject'] = subject

        if message:
            params['message'] = message

        try:
            r = self.api.post(url, {
                'matter': matter,
                'medium': medium,
                'params': params,
            })

        except Exception as err:
            logger.error("Error sending logs to CMP: %s", err)

        if r.status_code != 200:
            logger.error("Error sending logs to CMP: %s", r.text)

    def set_response_header(self, key, value):
        """Set a response header"""
        self.response_headers[key] = value
示例#10
0
 def __init__(self):
     self.credentials = load_config(CONFIG_FILE)
     self.cmp = CmpClient(url=self.credentials['cmp_url'],
                          auth=(self.credentials['cmp_username'],
                                self.credentials['cmp_password']))
     self.nflex = NflexClient(self.cmp)