def _set_up_postgres():
    # TODO: set up a way to disable this, on shared database (mxapps.io) we
    # don't want to allow this.
    if not buildpackutil.i_am_primary_instance():
        return
    dbconfig = database_config.get_database_config()
    for k in (
        "DatabaseType",
        "DatabaseUserName",
        "DatabasePassword",
        "DatabaseHost",
    ):
        if k not in dbconfig:
            logger.warn(
                "Skipping database configuration for DataDog because "
                "configuration is not found. See database_config.py "
                "for details"
            )
            return
    if dbconfig["DatabaseType"] != "PostgreSQL":
        return
    with open(".local/datadog/conf.d/postgres.yaml", "w") as fh:
        config = {
            "init_config": {},
            "instances": [
                {
                    "host": dbconfig["DatabaseHost"].split(":")[0],
                    "port": int(dbconfig["DatabaseHost"].split(":")[1]),
                    "username": dbconfig["DatabaseUserName"],
                    "password": dbconfig["DatabasePassword"],
                    "dbname": dbconfig["DatabaseName"],
                }
            ],
        }
        fh.write(yaml.safe_dump(config))
def get_scheduled_events(metadata):
    scheduled_events = os.getenv("SCHEDULED_EVENTS", None)
    if not i_am_primary_instance():
        logger.debug(
            "Disabling all scheduled events because I am not the primary "
            "instance"
        )
        return ("NONE", None)
    elif scheduled_events is None or scheduled_events == "ALL":
        logger.debug("Enabling all scheduled events")
        return ("ALL", None)
    elif scheduled_events == "NONE":
        logger.debug("Disabling all scheduled events")
        return ("NONE", None)
    else:
        parsed_scheduled_events = scheduled_events.split(",")
        metadata_scheduled_events = [
            scheduled_event["Name"]
            for scheduled_event in metadata["ScheduledEvents"]
        ]
        result = []
        for scheduled_event in parsed_scheduled_events:
            if scheduled_event not in metadata_scheduled_events:
                logger.warning(
                    'Scheduled event defined but not detected in model: "%s"'
                    % scheduled_event
                )
            else:
                result.append(scheduled_events)
        logger.debug("Enabling scheduled events %s" % ",".join(result))
        return ("SPECIFIED", result)
Example #3
0
    def run(self):
        logger.debug("Starting metrics emitter with interval %d" %
                     self.interval)
        while True:
            stats = {}
            try:
                if buildpackutil.i_am_primary_instance():
                    stats = self._inject_database_stats(stats)
                    stats = self._inject_storage_stats(stats)
                    stats = self._inject_health(stats)
                try:
                    stats = self._inject_m2ee_stats(stats)
                except Exception:
                    logger.debug("Unable to get metrics from runtime")

                self.emit(stats)
            except psycopg2.OperationalError as up:
                logger.exception("METRICS: error while gathering metrics")
                self.emit({
                    "health": {
                        "health": 0,
                        "diagnosis": "Database error: %s" % str(up),
                    }
                })
            except Exception as e:
                logger.exception("METRICS: error while gathering metrics")
                self.emit({
                    "health": {
                        "health": 4,
                        "diagnosis": "Unable to retrieve metrics",
                    }
                })

            time.sleep(self.interval)
Example #4
0
def get_scheduled_events(metadata):
    scheduled_events = os.getenv('SCHEDULED_EVENTS', None)
    if not i_am_primary_instance():
        logger.debug(
            'Disabling all scheduled events because I am not the primary '
            'instance')
        return ('NONE', None)
    elif scheduled_events is None or scheduled_events == 'ALL':
        logger.debug('Enabling all scheduled events')
        return ('ALL', None)
    elif scheduled_events == 'NONE':
        logger.debug('Disabling all scheduled events')
        return ('NONE', None)
    else:
        parsed_scheduled_events = scheduled_events.split(',')
        metadata_scheduled_events = [
            scheduled_event['Name']
            for scheduled_event in metadata['ScheduledEvents']
        ]
        result = []
        for scheduled_event in parsed_scheduled_events:
            if scheduled_event not in metadata_scheduled_events:
                logger.warning(
                    'Scheduled event defined but not detected in model: "%s"' %
                    scheduled_event)
            else:
                result.append(scheduled_events)
        logger.debug('Enabling scheduled events %s' % ','.join(result))
        return ('SPECIFIED', result)
Example #5
0
def _set_up_postgres():
    # TODO: set up a way to disable this, on shared database (mxapps.io) we
    # don't want to allow this.
    if not buildpackutil.i_am_primary_instance():
        return
    dbconfig = buildpackutil.get_database_config()
    for k in (
            'DatabaseType',
            'DatabaseUserName',
            'DatabasePassword',
            'DatabaseHost',
    ):
        if k not in dbconfig:
            return
    if dbconfig['DatabaseType'] != 'PostgreSQL':
        return
    with open('.local/datadog/conf.d/postgres.yaml', 'w') as fh:
        config = {
            'init_config': {},
            'instances': [{
                'host': dbconfig['DatabaseHost'].split(':')[0],
                'port': int(dbconfig['DatabaseHost'].split(':')[1]),
                'username': dbconfig['DatabaseUserName'],
                'password': dbconfig['DatabasePassword'],
                'dbname': dbconfig['DatabaseName'],
            }],
        }
        fh.write(yaml.safe_dump(config))
Example #6
0
def set_runtime_config(metadata, mxruntime_config, vcap_data, m2ee):
    scheduled_event_execution, my_scheduled_events = get_scheduled_events(
        metadata)
    app_config = {
        "ApplicationRootUrl": "https://%s" % vcap_data["application_uris"][0],
        "MicroflowConstants": get_constants(metadata),
        "ScheduledEventExecution": scheduled_event_execution,
    }

    if my_scheduled_events is not None:
        app_config["MyScheduledEvents"] = my_scheduled_events

    if is_development_mode():
        logger.warning("Runtime is being started in Development Mode. Set "
                       'DEVELOPMENT_MODE to "false" (currently "true") to '
                       "set it to production.")
        app_config["DTAPMode"] = "D"

    if m2ee.config.get_runtime_version() >= 7 and not i_am_primary_instance():
        app_config["com.mendix.core.isClusterSlave"] = "true"
    elif (m2ee.config.get_runtime_version() >= 5.15
          and os.getenv("ENABLE_STICKY_SESSIONS", "false").lower() == "true"):
        logger.info("Enabling sticky sessions")
        app_config["com.mendix.core.SessionIdCookieName"] = "JSESSIONID"

    buildpackutil.mkdir_p(os.path.join(os.getcwd(), "model", "resources"))
    mxruntime_config.update(app_config)
    mxruntime_config.update(
        buildpackutil.get_database_config(
            development_mode=is_development_mode()))
    mxruntime_config.update(get_filestore_config(m2ee))
    mxruntime_config.update(get_certificate_authorities())
    mxruntime_config.update(get_client_certificates())
    mxruntime_config.update(get_custom_settings(metadata, mxruntime_config))
    mxruntime_config.update(get_custom_runtime_settings())
Example #7
0
def set_runtime_config(metadata, mxruntime_config, vcap_data, m2ee):
    scheduled_event_execution, my_scheduled_events = (
        get_scheduled_events(metadata))
    app_config = {
        'ApplicationRootUrl': 'https://%s' % vcap_data['application_uris'][0],
        'MicroflowConstants': get_constants(metadata),
        'ScheduledEventExecution': scheduled_event_execution,
    }

    if my_scheduled_events is not None:
        app_config['MyScheduledEvents'] = my_scheduled_events

    if is_development_mode():
        logger.warning('Runtime is being started in Development Mode. Set '
                       'DEVELOPMENT_MODE to "false" (currently "true") to '
                       'set it to production.')
        app_config['DTAPMode'] = 'D'

    if (m2ee.config.get_runtime_version() >= 7
            and not i_am_primary_instance()):
        app_config['com.mendix.core.isClusterSlave'] = 'true'
    elif (m2ee.config.get_runtime_version() >= 5.15
          and os.getenv('ENABLE_STICKY_SESSIONS', 'false').lower() == 'true'):
        logger.info('Enabling sticky sessions')
        app_config['com.mendix.core.SessionIdCookieName'] = 'JSESSIONID'

    mxruntime_config.update(app_config)
    mxruntime_config.update(
        buildpackutil.get_database_config(
            development_mode=is_development_mode(), ))
    mxruntime_config.update(get_filestore_config(m2ee))
    mxruntime_config.update(get_certificate_authorities())
    mxruntime_config.update(get_client_certificates())
    mxruntime_config.update(get_custom_settings(metadata, mxruntime_config))
    mxruntime_config.update(get_custom_runtime_settings())
    def run(self):
        logger.debug('Starting metrics emitter with interval %d' %
                     self.interval)
        while True:
            stats = {}
            try:
                if buildpackutil.i_am_primary_instance():
                    stats = self._inject_database_stats(stats)
                    stats = self._inject_storage_stats(stats)
                    stats = self._inject_health(stats)
                stats = self._inject_m2ee_stats(stats)
                self.emit(stats)
            except psycopg2.OperationalError as up:
                logger.exception('METRICS: error while gathering metrics')
                self.emit({
                    'health': {
                        'health': 0,
                        'diagnosis': "Database error: %s" % str(up)
                    }
                })
            except Exception as e:
                logger.exception('METRICS: error while gathering metrics')
                self.emit({
                    'health': {
                        'health': 4,
                        'diagnosis': "Unable to retrieve metrics"
                    }
                })

            time.sleep(self.interval)
def start_app(m2ee):
    m2ee.start_appcontainer()
    if not m2ee.send_runtime_config():
        sys.exit(1)

    logger.debug("Appcontainer has been started")

    abort = False
    success = False
    while not (success or abort):
        startresponse = m2ee.client.start({"autocreatedb": True})
        logger.debug("startresponse received")
        result = startresponse.get_result()
        if result == 0:
            success = True
            logger.info("The MxRuntime is fully started now.")
        else:
            startresponse.display_error()
            if result == 2:
                logger.warning("DB does not exists")
                abort = True
            elif result == 3:
                if i_am_primary_instance():
                    if os.getenv("SHOW_DDL_COMMANDS", "").lower() == "true":
                        for line in m2ee.client.get_ddl_commands(
                            {"verbose": True}
                        ).get_feedback()["ddl_commands"]:
                            logger.info(line)
                    m2eeresponse = m2ee.client.execute_ddl_commands()
                    if m2eeresponse.has_error():
                        m2eeresponse.display_error()
                        abort = True
                else:
                    logger.info(
                        "waiting 10 seconds before primary instance "
                        "synchronizes database"
                    )
                    time.sleep(10)
            elif result == 4:
                logger.warning("Not enough constants!")
                abort = True
            elif result == 5:
                logger.warning("Unsafe password!")
                abort = True
            elif result == 6:
                logger.warning("Invalid state!")
                abort = True
            elif result == 7 or result == 8 or result == 9:
                logger.warning(
                    "You'll have to fix the configuration and run start "
                    "again... (or ask for help..)"
                )
                abort = True
            else:
                abort = True
    if abort:
        logger.warning("start failed, stopping")
        sys.exit(1)
Example #10
0
 def _select_stats_to_emit(self):
     selected_stats = []
     if buildpackutil.i_am_primary_instance():
         selected_stats = [
             self._inject_database_stats,
             self._inject_storage_stats,
             self._inject_health,
         ]
     selected_stats.append(self._inject_m2ee_stats)
     return selected_stats
    def run(self):
        logger.debug('Starting metrics emitter with interval %d' %
                     self.interval)
        while True:

            stats = {
                'version': '1.0',
                'timestamp': datetime.datetime.now().isoformat(),
            }
            stats = self._inject_m2ee_stats(stats)
            if buildpackutil.i_am_primary_instance():
                stats = self._inject_storage_stats(stats)
                stats = self._inject_database_stats(stats)

            logger.info('MENDIX-METRICS: ' + json.dumps(stats))

            time.sleep(self.interval)
Example #12
0
    def run(self):
        logger.debug('Starting metrics emitter with interval %d' %
                     self.interval)
        while True:

            try:
                stats = {
                    'version': '1.0',
                    'timestamp': datetime.datetime.now().isoformat(),
                    'instance_index': os.getenv('CF_INSTANCE_INDEX', 0)
                }
                stats = self._inject_m2ee_stats(stats)
                if buildpackutil.i_am_primary_instance():
                    stats = self._inject_storage_stats(stats)
                    stats = self._inject_database_stats(stats)
                    stats = self._inject_health(stats)
                self.emit(stats)
            except Exception as e:
                logger.exception('METRICS: error while gathering metrics')

            time.sleep(self.interval)