Esempio n. 1
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 util.i_am_primary_instance():
        return
    dbconfig = database.get_config()
    for k in (
            "DatabaseType",
            "DatabaseUserName",
            "DatabasePassword",
            "DatabaseHost",
    ):
        if k not in dbconfig:
            logging.warning(
                "Skipping database configuration for Datadog because "
                "configuration is not found. See database_config.py "
                "for details")
            return
    if dbconfig["DatabaseType"] != "PostgreSQL":
        return

    os.makedirs(DD_AGENT_CHECKS_DIR + "/postgres.d", exist_ok=True)
    with open(DD_AGENT_CHECKS_DIR + "/postgres.d/conf.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 util.i_am_primary_instance():
        logging.debug(
            "Disabling all scheduled events because I am not the primary "
            "instance"
        )
        return ("NONE", None)
    elif scheduled_events is None or scheduled_events == "ALL":
        logging.debug("Enabling all scheduled events")
        return ("ALL", None)
    elif scheduled_events == "NONE":
        logging.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:
                logging.warning(
                    'Scheduled event defined but not detected in model: "%s"',
                    scheduled_event,
                )
            else:
                result.append(scheduled_events)
        logging.debug("Enabling scheduled events %s", ",".join(result))
        return ("SPECIFIED", result)
def _get_db_config():
    if (include_db_metrics()
            or datadog.get_api_key()) and util.i_am_primary_instance():
        db_config = database.get_config()
        if db_config and db_config["DatabaseType"] == "PostgreSQL":
            return db_config
    return None
Esempio n. 4
0
def start_app(m2ee):
    m2ee.start_appcontainer()
    if not m2ee.send_runtime_config():
        sys.exit(1)

    logging.debug("Appcontainer has been started")

    abort = False
    success = False
    while not (success or abort):
        startresponse = m2ee.client.start({"autocreatedb": True})
        logging.debug("startresponse received")
        result = startresponse.get_result()
        if result == 0:
            success = True
            logging.info("The MxRuntime is fully started now.")
        else:
            startresponse.display_error()
            if result == 2:
                logging.warning("DB does not exists")
                abort = True
            elif result == 3:
                if util.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"]:
                            logging.info(line)
                    m2eeresponse = m2ee.client.execute_ddl_commands()
                    if m2eeresponse.has_error():
                        m2eeresponse.display_error()
                        abort = True
                else:
                    logging.info("waiting 10 seconds before primary instance "
                                 "synchronizes database")
                    time.sleep(10)
            elif result == 4:
                logging.warning("Not enough constants!")
                abort = True
            elif result == 5:
                logging.warning("Unsafe password!")
                abort = True
            elif result == 6:
                logging.warning("Invalid state!")
                abort = True
            elif result == 7 or result == 8 or result == 9:
                logging.warning(
                    "You'll have to fix the configuration and run start "
                    "again... (or ask for help..)")
                abort = True
            else:
                abort = True
    if abort:
        logging.warning("start failed, stopping")
        sys.exit(1)
Esempio n. 5
0
 def _select_stats_to_emit(self):
     selected_stats = []
     if util.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 set_runtime_config(metadata, mxruntime_config, vcap_data, m2ee):
    scheduled_event_execution, my_scheduled_events = get_scheduled_events(
        metadata
    )

    app_config = {
        "ApplicationRootUrl": get_application_root_url(vcap_data),
        "MicroflowConstants": get_constants(metadata),
        "ScheduledEventExecution": scheduled_event_execution,
    }

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

    if util.is_development_mode():
        logging.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 util.i_am_primary_instance()
    ):
        app_config["com.mendix.core.isClusterSlave"] = "true"
    elif (
        m2ee.config.get_runtime_version() >= 6
        and os.getenv("ENABLE_STICKY_SESSIONS", "false").lower() == "true"
    ):
        logging.info("Enabling sticky sessions")
        app_config["com.mendix.core.SessionIdCookieName"] = "JSESSIONID"

    util.mkdir_p(os.path.join(os.getcwd(), "model", "resources"))
    mxruntime_config.update(app_config)

    # db configuration might be None, database should then be set up with
    # MXRUNTIME_Database... custom runtime settings.
    runtime_db_config = database.get_config()
    if runtime_db_config:
        mxruntime_config.update(runtime_db_config)

    mxruntime_config.update(storage.get_config(m2ee))
    mxruntime_config.update(security.get_certificate_authorities())
    mxruntime_config.update(
        security.get_client_certificates(m2ee.config.get_runtime_version())
    )
    mxruntime_config.update(get_custom_settings(metadata, mxruntime_config))
    mxruntime_config.update(get_license_subscription())
    mxruntime_config.update(get_custom_runtime_settings())