コード例 #1
0
def stage(buildpack_dir, build_dir):
    logging.debug("Staging database...")
    util.mkdir_p(os.path.join(build_dir, ".postgresql"))
    shutil.copy(
        os.path.join(buildpack_dir, "etc", "amazon-rds-ca.pem"),
        os.path.join(build_dir, ".postgresql", "amazon-rds-ca.pem"),
    )
コード例 #2
0
def activate_license():
    prefs_dir = os.path.expanduser("~/../.java/.userPrefs/com/mendix/core")
    util.mkdir_p(prefs_dir)

    prefs_template = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE map SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<map MAP_XML_VERSION="1.0">
  <entry key="id" value="{{LICENSE_ID}}"/>
  <entry key="license_key" value="{{LICENSE_KEY}}"/>
</map>"""

    license_key = os.environ.get(
        "FORCED_LICENSE_KEY", os.environ.get("LICENSE_KEY", None)
    )
    server_id = os.environ.get(
        "FORCED_SERVER_ID", os.environ.get("SERVER_ID", None)
    )
    license_id = os.environ.get(
        "FORCED_LICENSE_ID", os.environ.get("LICENSE_ID", None)
    )
    if server_id:
        logging.warning(
            "SERVER_ID is deprecated, please use LICENSE_ID instead"
        )

    if not license_id:
        license_id = server_id

    if license_key is not None and license_id is not None:
        logging.debug("A license was supplied so going to activate it")
        prefs_body = prefs_template.replace(
            "{{LICENSE_ID}}", license_id
        ).replace("{{LICENSE_KEY}}", license_key)
        with open(os.path.join(prefs_dir, "prefs.xml"), "w") as prefs_file:
            prefs_file.write(prefs_body)
コード例 #3
0
def stage(buildpack_path, cache_path, local_path, java_version):
    logging.debug("Staging Java...")

    # Download Java
    util.mkdir_p(os.path.join(local_path, "bin"))
    jvm_location = ensure_and_get_jvm(java_version,
                                      cache_path,
                                      local_path,
                                      package="jre")

    # Create a symlink in .local/bin/java
    os.symlink(
        # Use .. when JDK is in .local because absolute path
        # is different at staging time
        os.path.join(jvm_location.replace(local_path, ".."), "bin", "java"),
        os.path.join(local_path, "bin", "java"),
    )

    # Import Mozilla CA certificates
    # This is done by a third-party tool (keyutil),
    # using the Python certifi certificate bundle
    #
    # While recent versions of AdoptOpenJDK have these on board,
    # we still also have to deal with Oracle JREs / JDKs for now.
    # When we retire support for Mendix 6,
    # we should reconsider importing these certificates ourselves.
    util.download_and_unpack(
        util.get_blobstore_url(
            "/mx-buildpack/java-keyutil/{}".format(KEYUTIL_JAR)),
        None,
        cache_path,
    )

    _update_java_cacert(cache_path, jvm_location)
    logging.debug("Staging Java finished")
コード例 #4
0
def complete_start_procedure_safe_to_use_for_restart(m2ee):
    display_java_version()
    util.mkdir_p("model/lib/userlib")
    logs.set_up_logging_file()
    start_app(m2ee)
    security.create_admin_user(m2ee, util.is_development_mode())
    logs.update_config(m2ee)
    display_running_version(m2ee)
    configure_debugger(m2ee)
コード例 #5
0
def _set_runtime_config(m2ee, metadata, vcap_data):
    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 mode.'
        )
        app_config["DTAPMode"] = "D"

    if get_runtime_version() >= 7 and not util.is_cluster_leader():
        app_config["com.mendix.core.isClusterSlave"] = "true"
    elif (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"))
    util.upsert_custom_runtime_settings(m2ee,
                                        app_config,
                                        overwrite=True,
                                        append=True)
    util.upsert_custom_runtime_settings(
        m2ee,
        security.get_certificate_authorities(),
        overwrite=True,
        append=True,
    )
    util.upsert_custom_runtime_settings(
        m2ee,
        security.get_client_certificates(get_runtime_version()),
        overwrite=True,
        append=True,
    )
    util.upsert_custom_runtime_settings(m2ee,
                                        _get_custom_settings(metadata),
                                        overwrite=False,
                                        append=True)
    util.upsert_custom_runtime_settings(m2ee,
                                        _get_license_subscription(),
                                        overwrite=True,
                                        append=True)
    util.upsert_custom_runtime_settings(m2ee,
                                        _get_custom_runtime_settings(),
                                        overwrite=True,
                                        append=True)
コード例 #6
0
def stage(buildpack_dir, build_path, cache_path):
    logging.debug("Creating directory structure for Mendix runtime...")
    for name in ["runtimes", "log", "database", "data", "bin"]:
        util.mkdir_p(os.path.join(build_path, name))
    for name in ["files", "tmp", "database"]:
        util.mkdir_p(os.path.join(build_path, "data", name))

    logging.debug("Staging the Mendix runtime...")
    shutil.copy(
        os.path.join(buildpack_dir, "etc", "m2ee", "m2ee.yaml"),
        os.path.join(build_path, ".local", "m2ee.yaml"),
    )
    resolve_runtime_dependency(buildpack_dir, build_path, cache_path)
コード例 #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": 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())
コード例 #8
0
def run(m2ee, loglevels):
    # Shutdown handler; called on exit(0) or exit(1)
    def _terminate():
        if m2ee:
            stop(m2ee)

    atexit.register(_terminate)

    _display_java_version()
    util.mkdir_p("model/lib/userlib")
    _start_app(m2ee)
    security.create_admin_user(m2ee, util.is_development_mode())
    _display_running_model_version(m2ee)
    _configure_debugger(m2ee)
    _set_loglevels(m2ee, loglevels)
コード例 #9
0
def stage(buildpack_path, cache_path, local_path, java_version):
    logging.debug("begin download and install java")
    util.mkdir_p(os.path.join(local_path, "bin"))
    jvm_location = ensure_and_get_jvm(
        java_version, cache_path, local_path, package="jre"
    )
    # create a symlink in .local/bin/java
    os.symlink(
        # use .. when jdk is in .local because absolute path
        # is different at staging time
        os.path.join(jvm_location.replace(local_path, ".."), "bin", "java"),
        os.path.join(local_path, "bin", "java"),
    )
    # update cacert file
    update_java_cacert(buildpack_path, jvm_location)
    logging.debug("end download and install java")
コード例 #10
0
def ensure_mxbuild_in_directory(directory, mx_version, cache_dir):
    if os.path.isdir(os.path.join(directory, "modeler")):
        return
    util.mkdir_p(directory)

    url = os.environ.get("FORCED_MXBUILD_URL")
    if url:
        # don"t ever cache with a FORCED_MXBUILD_URL
        util.download_and_unpack(url, directory, cache_dir="/tmp/downloads")
    else:
        try:
            _checkout_from_git_rootfs(directory, mx_version)
        except NotFoundException as e:
            logging.debug(str(e))
            util.download_and_unpack(
                util.get_blobstore_url("/runtime/mxbuild-%s.tar.gz" %
                                       str(mx_version)),
                directory,
                cache_dir=cache_dir,
            )
コード例 #11
0
def stage(buildpack_path, cache_path, local_path, java_major_version):
    logging.debug("Staging Java...")

    # Download Java
    util.mkdir_p(os.path.join(local_path, "bin"))
    jvm_location = ensure_and_get_jvm(
        java_major_version,
        buildpack_path,
        cache_path,
        local_path,
        package="jre",
    )

    # Create a symlink in .local/bin/java
    os.symlink(
        # Use .. when JDK is in .local because absolute path
        # is different at staging time
        os.path.join(jvm_location.replace(local_path, ".."), "bin", "java"),
        os.path.join(local_path, "bin", "java"),
    )

    # Import Mozilla CA certificates
    # This is done by a third-party tool (keyutil),
    # using the Python certifi certificate bundle
    #
    # Recent versions of Adoptium have these on board,
    # we should reconsider importing these certificates ourselves.
    dependency = util.resolve_dependency(
        "java.keyutil",
        None,
        buildpack_dir=buildpack_path,
        cache_dir=cache_path,
    )
    _update_java_cacert(
        os.path.basename(dependency["artifact"]), cache_path, jvm_location
    )

    # Configure if TLSv1.0 and TLSv1.1 are allowed for outgoing connections
    _configure_outgoing_tls_10_11(jvm_location, java_major_version)

    logging.debug("Staging Java finished")
コード例 #12
0
    def test_find_file_in_directory(self):
        temp_dir = tempfile.TemporaryDirectory()

        file_names = [
            "dependency1.zip",
            "a/dependency2.zip",
            "a/b/dependency3.zip",
            "dependency4.zip/dependency4.zip",
        ]
        files = [os.path.join(temp_dir.name, f) for f in file_names]
        for f in files:
            mkdir_p(os.path.dirname(f))
            open(f, "a").close()

        for f in file_names:
            result = _find_file_in_directory(os.path.basename(f),
                                             temp_dir.name)
            assert (result and is_path_accessible(result)
                    and os.path.isfile(result))

        temp_dir.cleanup()
コード例 #13
0
def set_up_directory_structure():
    logging.debug("Creating directory structure...")
    util.mkdir_p(DOT_LOCAL_LOCATION)
    for name in ["runtimes", "log", "database", "data", "bin", ".postgresql"]:
        util.mkdir_p(os.path.join(BUILD_DIR, name))
    for name in ["files", "tmp", "database"]:
        util.mkdir_p(os.path.join(BUILD_DIR, "data", name))
コード例 #14
0
def update_project_dir():
    logging.debug("unzipping " + MPK_FILE + " to " + INCOMING_MPK_DIR)
    subprocess.check_call(("rm", "-rf", INCOMING_MPK_DIR))
    util.mkdir_p(INCOMING_MPK_DIR)
    subprocess.check_call(("unzip", "-oqq", MPK_FILE, "-d", INCOMING_MPK_DIR))
    new_mpr = os.path.basename(util.get_mpr_file_from_dir(INCOMING_MPK_DIR))
    existing_mpr_path = util.get_mpr_file_from_dir(PROJECT_DIR)
    if existing_mpr_path:
        existing_mpr = os.path.basename(existing_mpr_path)
    else:
        existing_mpr = None
    logging.debug("rsync from incoming to intermediate")
    if util.get_buildpack_loglevel() < logging.INFO:
        quiet_or_verbose = "--verbose"
    else:
        quiet_or_verbose = "--quiet"
    subprocess.call((
        "rsync",
        "--recursive",
        "--checksum",
        "--delete",
        INCOMING_MPK_DIR + "/",
        INTERMEDIATE_MPK_DIR + "/",
    ))
    logging.debug("rsync from intermediate to project")
    if new_mpr == existing_mpr:
        update_or_delete = "--update"
    else:
        update_or_delete = "--delete"

    subprocess.call((
        "rsync",
        "--recursive",
        update_or_delete,
        quiet_or_verbose,
        INTERMEDIATE_MPK_DIR + "/",
        PROJECT_DIR + "/",
    ))
コード例 #15
0
def set_up_m2ee_client(vcap_data):
    client = m2ee_class(
        yamlfiles=[".local/m2ee.yaml"],
        load_default_files=False,
        config={
            "m2ee": {
                # this is named admin_pass, but it's the verification http header
                # to communicate with the internal management port of the runtime
                "admin_pass": security.get_m2ee_password()
            }
        },
    )

    version = client.config.get_runtime_version()

    mendix_runtimes_path = "/usr/local/share/mendix-runtimes.git"
    mendix_runtime_version_path = os.path.join(os.getcwd(), "runtimes",
                                               str(version))
    if os.path.isdir(mendix_runtimes_path
                     ) and not os.path.isdir(mendix_runtime_version_path):
        util.mkdir_p(mendix_runtime_version_path)
        env = dict(os.environ)
        env["GIT_WORK_TREE"] = mendix_runtime_version_path

        # checkout the runtime version
        process = subprocess.Popen(
            ["git", "checkout", str(version), "-f"],
            cwd=mendix_runtimes_path,
            env=env,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )
        process.communicate()
        if process.returncode != 0:
            logging.info("Mendix %s is not available in the rootfs", version)
            logging.info("Fallback (1): trying to fetch Mendix %s using git",
                         version)
            process = subprocess.Popen(
                [
                    "git",
                    "fetch",
                    "origin",
                    "refs/tags/{0}:refs/tags/{0}".format(str(version)),
                    "&&",
                    "git",
                    "checkout",
                    str(version),
                    "-f",
                ],
                cwd=mendix_runtimes_path,
                env=env,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            )
            process.communicate()
            if process.returncode != 0:
                logging.info(
                    "Unable to fetch Mendix {} using git".format(version))
                url = util.get_blobstore_url("/runtime/mendix-%s.tar.gz" %
                                             str(version))
                logging.info(
                    "Fallback (2): downloading Mendix {} from {}".format(
                        version, url))
                util.download_and_unpack(url,
                                         os.path.join(os.getcwd(), "runtimes"))

        client.reload_config()
    runtime.set_runtime_config(
        client.config._model_metadata,
        client.config._conf["mxruntime"],
        vcap_data,
        client,
    )
    java_version = runtime.get_java_version(
        client.config.get_runtime_version())["version"]
    java.update_config(client.config._conf["m2ee"], vcap_data, java_version)
    runtime.set_jetty_config(client)
    newrelic.update_config(client, vcap_data["application_name"])
    appdynamics.update_config(client, vcap_data["application_name"])
    runtime.set_application_name(client, vcap_data["application_name"])
    telegraf.update_config(client, vcap_data["application_name"])
    datadog.update_config(client)
    return client
コード例 #16
0
PROJECT_DIR = ".local/project"
DEPLOYMENT_DIR = os.path.join(PROJECT_DIR, "deployment")
INCOMING_MPK_DIR = ".local/tmp_project"
INTERMEDIATE_MPK_DIR = ".local/tmp_project_2"
MPK_FILE = os.path.join(PROJECT_DIR, "app.mpk")

INSTADEPLOY_FEEDBACK_PATH = "api/1/environments/{app_id}/instadeployfeedback"

for directory in (
        MXBUILD_FOLDER,
        PROJECT_DIR,
        DEPLOYMENT_DIR,
        INCOMING_MPK_DIR,
        INTERMEDIATE_MPK_DIR,
):
    util.mkdir_p(directory)


class MxBuildFailure(Exception):
    """
    Represents any 4xx 5xx issues retrieved from MxBuild HTTP Server
    """
    def __init__(self, message, status_code, mxbuild_response):
        super().__init__(message)
        self.status_code = status_code
        self.mxbuild_response = mxbuild_response


class InstaDeployThread(threading.Thread):
    """
    The reference for this implementation can be found at