def set_up_m2ee_client(vcap_data):
    m2ee = M2EE(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': get_m2ee_password(),
        }
    })
    version = m2ee.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):
        buildpackutil.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:
            logger.info('Mendix {} is not available in the rootfs'.format(version))
            logger.info('Fallback (1): trying to fetch Mendix {} using git'.format(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:
                logger.info('Unable to fetch Mendix {} using git'.format(version))
                url = buildpackutil.get_blobstore_url('/runtime/mendix-%s.tar.gz' % str(version))
                logger.info('Fallback (2): downloading Mendix {} from {}'.format(version, url))
                buildpackutil.download_and_unpack(url, os.path.join(os.getcwd(), 'runtimes'))

        m2ee.reload_config()
    set_runtime_config(
        m2ee.config._model_metadata,
        m2ee.config._conf['mxruntime'],
        vcap_data,
        m2ee,
    )
    java_version = buildpackutil.get_java_version(
        m2ee.config.get_runtime_version()
    )
    set_jvm_memory(
        m2ee.config._conf['m2ee'],
        vcap_data,
        java_version,
    )
    set_jetty_config(m2ee)
    activate_new_relic(m2ee, vcap_data['application_name'])
    activate_appdynamics(m2ee, vcap_data['application_name'])
    set_application_name(m2ee, vcap_data['application_name'])
    telegraf.update_config(m2ee, vcap_data['application_name'])
    datadog.update_config(m2ee, vcap_data['application_name'])
    return m2ee
def set_up_m2ee_client(vcap_data):
    m2ee = M2EE(
        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": get_m2ee_password()
            }
        },
    )
    version = m2ee.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
    ):
        buildpackutil.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:
            logger.info(
                "Mendix {} is not available in the rootfs".format(version)
            )
            logger.info(
                "Fallback (1): trying to fetch Mendix {} using git".format(
                    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:
                logger.info(
                    "Unable to fetch Mendix {} using git".format(version)
                )
                url = buildpackutil.get_blobstore_url(
                    "/runtime/mendix-%s.tar.gz" % str(version)
                )
                logger.info(
                    "Fallback (2): downloading Mendix {} from {}".format(
                        version, url
                    )
                )
                buildpackutil.download_and_unpack(
                    url, os.path.join(os.getcwd(), "runtimes")
                )

        m2ee.reload_config()
    set_runtime_config(
        m2ee.config._model_metadata,
        m2ee.config._conf["mxruntime"],
        vcap_data,
        m2ee,
    )
    java_version = buildpackutil.get_java_version(
        m2ee.config.get_runtime_version()
    )
    set_jvm_memory(m2ee.config._conf["m2ee"], vcap_data, java_version)
    set_jvm_locale(m2ee.config._conf["m2ee"], java_version)
    set_jetty_config(m2ee)
    activate_new_relic(m2ee, vcap_data["application_name"])
    activate_appdynamics(m2ee, vcap_data["application_name"])
    set_application_name(m2ee, vcap_data["application_name"])
    telegraf.update_config(m2ee, vcap_data["application_name"])
    datadog.update_config(m2ee, vcap_data["application_name"])
    return m2ee