예제 #1
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)
                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)
예제 #2
0
def get_constants(metadata):
    constants = {}

    constants_from_json = {}
    constants_json = os.environ.get(
        'CONSTANTS',
        json.dumps(constants_from_json)
    )
    try:
        constants_from_json = json.loads(constants_json)
    except Exception as e:
        logger.warning('Failed to parse CONSTANTS: ' + str(e))

    for constant in metadata['Constants']:
        constant_name = constant['Name']
        env_name = 'MX_%s' % constant_name.replace('.', '_')
        value = os.environ.get(
            env_name,
            constants_from_json.get(constant_name)
        )
        if value is None:
            value = constant['DefaultValue']
            logger.debug(
                'Constant not found in environment, taking default '
                'value %s' % constant_name
            )
        if constant['Type'] == 'Integer':
            value = int(value)
        constants[constant_name] = value
    return constants
예제 #3
0
def set_up_nginx_files(m2ee):
    lines = ""

    if use_instadeploy(m2ee.config.get_runtime_version()):
        mxbuild_upstream = "proxy_pass http://mendix_mxbuild"
    else:
        mxbuild_upstream = "return 501"
    with open("nginx/conf/nginx.conf") as fh:
        lines = "".join(fh.readlines())
    http_headers = parse_headers()
    lines = (
        lines.replace("CONFIG", get_path_config())
        .replace("NGINX_PORT", str(get_nginx_port()))
        .replace("RUNTIME_PORT", str(get_runtime_port()))
        .replace("ADMIN_PORT", str(get_admin_port()))
        .replace("DEPLOY_PORT", str(get_deploy_port()))
        .replace("ROOT", os.getcwd())
        .replace("HTTP_HEADERS", http_headers)
        .replace("MXBUILD_UPSTREAM", mxbuild_upstream)
    )
    for line in lines.split("\n"):
        logger.debug(line)
    with open("nginx/conf/nginx.conf", "w") as fh:
        fh.write(lines)

    gen_htpasswd({"MxAdmin": get_m2ee_password()})
    gen_htpasswd(
        {"deploy": os.getenv("DEPLOY_PASSWORD")}, file_name_suffix="-mxbuild"
    )
예제 #4
0
def activate_license():
    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="{{SERVER_ID}}"/>
  <entry key="license_key" value="{{LICENSE_KEY}}"/>
</map>"""

    license = 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)
    )
    if license is not None and server_id is not None:
        logger.debug('A license was supplied so going to activate it')
        prefs_body = prefs_template.replace(
            '{{SERVER_ID}}', server_id
            ).replace(
            '{{LICENSE_KEY}}', license
            )
        prefs_dir = os.path.expanduser('~/../.java/.userPrefs/com/mendix/core')
        if not os.path.isdir(prefs_dir):
            os.makedirs(prefs_dir)
        with open(os.path.join(prefs_dir, 'prefs.xml'), 'w') as prefs_file:
            prefs_file.write(prefs_body)
예제 #5
0
def activate_license():
    prefs_dir = os.path.expanduser('~/../.java/.userPrefs/com/mendix/core')
    buildpackutil.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="{{SERVER_ID}}"/>
  <entry key="license_key" value="{{LICENSE_KEY}}"/>
</map>"""

    license = 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)
    )
    if license is not None and server_id is not None:
        logger.debug('A license was supplied so going to activate it')
        prefs_body = prefs_template.replace(
            '{{SERVER_ID}}', server_id
            ).replace(
            '{{LICENSE_KEY}}', license
            )
        with open(os.path.join(prefs_dir, 'prefs.xml'), 'w') as prefs_file:
            prefs_file.write(prefs_body)
예제 #6
0
def _write_http_output_config(http_config):
    logger.debug('writing http output config')
    if 'url' not in http_config:
        logger.error('APPMETRICS_TARGET.url value is not defined in {}'.format(
            _get_appmetrics_target()))
        return

    http_output = {
        'url': http_config['url'],
        'method': 'POST',
        'data_format': 'influx'
    }

    username = http_config.get('username')
    password = http_config.get('password')
    if username:
        # Workaround for https://github.com/influxdata/telegraf/issues/4544
        # http_output['username'] = username
        # http_output['password'] = password
        credentials = base64.b64encode(
            ('{}:{}'.format(username, password)).encode()).decode('ascii')
        http_output['[outputs.http.headers]'] = {
            'Authorization': 'Basic {}'.format(credentials)
        }

    kpionly = http_config['kpionly'] if 'kpionly' in http_config else True
    if kpionly:
        http_output['[outputs.http.tagpass]'] = {'KPI': ['true']}

    _write_config('[[outputs.http]]', http_output)
예제 #7
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)
예제 #8
0
def set_up_nginx_files(m2ee):
    lines = ""
    x_frame_options = os.environ.get("X_FRAME_OPTIONS", "ALLOW")
    if x_frame_options == "ALLOW":
        x_frame_options = ""
    else:
        x_frame_options = "add_header X-Frame-Options '%s';" % x_frame_options
    if use_instadeploy(m2ee.config.get_runtime_version()):
        mxbuild_upstream = "proxy_pass http://mendix_mxbuild"
    else:
        mxbuild_upstream = "return 501"
    with open("nginx/conf/nginx.conf") as fh:
        lines = "".join(fh.readlines())
    lines = (
        lines.replace("CONFIG", get_path_config())
        .replace("NGINX_PORT", str(get_nginx_port()))
        .replace("RUNTIME_PORT", str(get_runtime_port()))
        .replace("ADMIN_PORT", str(get_admin_port()))
        .replace("DEPLOY_PORT", str(get_deploy_port()))
        .replace("ROOT", os.getcwd())
        .replace("XFRAMEOPTIONS", x_frame_options)
        .replace("MXBUILD_UPSTREAM", mxbuild_upstream)
    )
    for line in lines.split("\n"):
        logger.debug(line)
    with open("nginx/conf/nginx.conf", "w") as fh:
        fh.write(lines)

    gen_htpasswd({"MxAdmin": get_m2ee_password()})
    gen_htpasswd(
        {"deploy": os.getenv("DEPLOY_PASSWORD")}, file_name_suffix="-mxbuild"
    )
예제 #9
0
def _write_http_output_config(http_config):
    logger.debug("writing http output config")
    if "url" not in http_config:
        logger.error("APPMETRICS_TARGET.url value is not defined in {}".format(
            _get_appmetrics_target()))
        return

    http_output = {
        "url": http_config["url"],
        "method": "POST",
        "data_format": "influx",
    }

    username = http_config.get("username")
    password = http_config.get("password")
    if username:
        # Workaround for https://github.com/influxdata/telegraf/issues/4544
        # http_output['username'] = username
        # http_output['password'] = password
        credentials = base64.b64encode(
            ("{}:{}".format(username, password)).encode()).decode("ascii")
        http_output["[outputs.http.headers]"] = {
            "Authorization": "Basic {}".format(credentials)
        }

    kpionly = http_config["kpionly"] if "kpionly" in http_config else True
    if kpionly:
        http_output["[outputs.http.tagpass]"] = {"KPI": ["true"]}

    _write_config("[[outputs.http]]", http_output)
예제 #10
0
def _write_aai_output_config():
    logger.debug("writing aai output config")
    aai_output = {
        "instrumentation_key": _get_appmetrics_aai(),
    }

    _write_config("[[outputs.application_insights]]", aai_output)
예제 #11
0
def parse_headers():
    header_config = ""
    headers_from_json = {}

    # this is kept for X-Frame-Options backward compatibility
    x_frame_options = os.environ.get("X_FRAME_OPTIONS", "ALLOW")
    if x_frame_options != "ALLOW":
        headers_from_json["X-Frame-Options"] = x_frame_options

    headers_json = os.environ.get("HTTP_RESPONSE_HEADERS", "{}")

    try:
        headers_from_json.update(json.loads(headers_json))
    except Exception as e:
        logger.error(
            "Failed to parse HTTP_RESPONSE_HEADERS, due to invalid JSON string: '{}'"
            .format(headers_json),
            exc_info=True,
        )
        raise

    for header_key, header_value in headers_from_json.items():
        regEx = DEFAULT_HEADERS[header_key]
        if regEx and re.match(regEx, header_value):
            escaped_value = header_value.replace('"',
                                                 '\\"').replace("'", "\\'")
            header_config += "add_header {} '{}';\n".format(
                header_key, escaped_value)
            logger.debug("Added header {} to nginx config".format(header_key))
        else:
            logger.warning(
                "Skipping {} config, value '{}' is not valid".format(
                    header_key, header_value))

    return header_config
예제 #12
0
def activate_license():
    prefs_dir = os.path.expanduser("~/../.java/.userPrefs/com/mendix/core")
    buildpackutil.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:
        logger.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:
        logger.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)
예제 #13
0
 def run(self):
     logger.debug(
         "Starting metrics emitter with interval %d", self.interval
     )
     for line in itertools.cycle(HEARTBEAT_STRING_LIST):
         logger.info("MENDIX-LOGGING-HEARTBEAT: %s", line)
         time.sleep(self.interval)
예제 #14
0
def set_up_nginx_files(m2ee):
    lines = ''
    x_frame_options = os.environ.get('X_FRAME_OPTIONS', 'ALLOW')
    if x_frame_options == 'ALLOW':
        x_frame_options = ''
    else:
        x_frame_options = "add_header X-Frame-Options '%s';" % x_frame_options
    if use_instadeploy(m2ee.config.get_runtime_version()):
        mxbuild_upstream = 'proxy_pass http://mendix_mxbuild'
    else:
        mxbuild_upstream = 'return 501'
    with open('nginx/conf/nginx.conf') as fh:
        lines = ''.join(fh.readlines())
    lines = lines.replace('CONFIG', get_path_config()).replace(
        'NGINX_PORT',
        str(get_nginx_port())).replace('RUNTIME_PORT', str(
            get_runtime_port())).replace('ADMIN_PORT', str(
                get_admin_port())).replace('DEPLOY_PORT', str(
                    get_deploy_port())).replace('ROOT', os.getcwd()).replace(
                        'XFRAMEOPTIONS',
                        x_frame_options).replace('MXBUILD_UPSTREAM',
                                                 mxbuild_upstream)
    for line in lines.split('\n'):
        logger.debug(line)
    with open('nginx/conf/nginx.conf', 'w') as fh:
        fh.write(lines)

    gen_htpasswd({'MxAdmin': get_m2ee_password()})
    gen_htpasswd({'deploy': os.getenv('DEPLOY_PASSWORD')},
                 file_name_suffix='-mxbuild')
예제 #15
0
def get_constants(metadata):
    constants = {}

    constants_from_json = {}
    constants_json = os.environ.get(
        "CONSTANTS", json.dumps(constants_from_json)
    )
    try:
        constants_from_json = json.loads(constants_json)
    except Exception as e:
        logger.warning(
            "Failed to parse model constant values, due to invalid JSON. "
            "Application terminating.",
            exc_info=True,
        )
        raise

    for constant in metadata["Constants"]:
        constant_name = constant["Name"]
        env_name = "MX_%s" % constant_name.replace(".", "_")
        value = os.environ.get(
            env_name, constants_from_json.get(constant_name)
        )
        if value is None:
            value = constant["DefaultValue"]
            logger.debug(
                "Constant not found in environment, taking default "
                "value %s" % constant_name
            )
        if constant["Type"] == "Integer":
            value = int(value)
        constants[constant_name] = value
    return constants
예제 #16
0
def set_jvm_memory(javaopts, vcap_max_mem, java_version):
    max_memory = os.environ.get('MEMORY_LIMIT')
    env_heap_size = os.environ.get('HEAP_SIZE')

    if max_memory:
        match = re.search('([0-9]+)M', max_memory.upper())
        limit = int(match.group(1))
    else:
        limit = int(vcap_max_mem)

    if limit >= 8192:
        heap_size = limit - 2048
    elif limit >= 4096:
        heap_size = limit - 1536
    elif limit >= 2048:
        heap_size = limit - 1024
    else:
        heap_size = int(limit / 2)

    heap_size = str(heap_size) + 'M'

    if env_heap_size:
        max_memory = max_memory[:-1] if max_memory else vcap_max_mem
        heap_size = env_heap_size if int(
            env_heap_size[:-1]) < int(max_memory) else heap_size

    javaopts.append('-Xmx%s' % heap_size)
    javaopts.append('-Xms%s' % heap_size)

    if java_version.startswith('7'):
        javaopts.append('-XX:MaxPermSize=256M')
    elif java_version.startswith('8'):
        javaopts.append('-XX:MaxMetaspaceSize=256M')

    logger.debug('Java heap size set to %s' % heap_size)
예제 #17
0
def set_heap_size(javaopts):
    max_memory = os.environ.get('MEMORY_LIMIT', '512m').upper()
    match = re.search('([0-9]+)([A-Z])', max_memory)
    max_memory = '%d%s' % (int(match.group(1)) / 2, match.group(2))
    heap_size = os.environ.get('HEAP_SIZE', max_memory)
    javaopts.append('-Xmx%s' % heap_size)
    javaopts.append('-Xms%s' % heap_size)
    logger.debug('Java heap size set to %s' % max_memory)
예제 #18
0
def set_heap_size(javaopts):
    max_memory = os.environ.get('MEMORY_LIMIT', '512m').upper()
    match = re.search('([0-9]+)([A-Z])', max_memory)
    max_memory = '%d%s' % (int(match.group(1)) / 2, match.group(2))
    heap_size = os.environ.get('HEAP_SIZE', max_memory)
    javaopts.append('-Xmx%s' % heap_size)
    javaopts.append('-Xms%s' % heap_size)
    logger.debug('Java heap size set to %s' % max_memory)
예제 #19
0
def get_m2ee_password():
    m2ee_password = os.getenv("M2EE_PASSWORD", get_admin_password())
    if not m2ee_password:
        logger.debug(
            "No ADMIN_PASSWORD or M2EE_PASSWORD configured, using a random password for the m2ee admin api"
        )
        m2ee_password = default_m2ee_password
    return m2ee_password
예제 #20
0
def get_current_buildpack_commit():
    try:
        with open(".buildpack_commit", "r") as commit_file:
            short_commit = commit_file.readline().strip()
            return short_commit
    except Exception:
        logger.debug("Failed to read file", exc_info=True)
        return "unknown_commit"
예제 #21
0
 def run(self):
     logger.debug("Starting metrics emitter with interval %d" %
                  self.interval)
     while True:
         stats = self._gather_metrics()
         stats = self._set_stats_info(stats)
         self.emit(stats)
         time.sleep(self.interval)
예제 #22
0
    def process_request(self):
        try:
            form = MyFieldStorage(
                fp=self.rfile,
                headers=self.headers,
                environ={
                    "REQUEST_METHOD": "POST",
                    "CONTENT_TYPE": self.headers["Content-Type"],
                },
            )
            if "file" in form:
                with open(MPK_FILE, "wb") as output:
                    shutil.copyfileobj(form["file"].file, output)
                update_project_dir()
                mxbuild_response = build()
                logger.debug(mxbuild_response)
                if mxbuild_response["status"] == "Busy":
                    return (200, {"state": "BUSY"}, mxbuild_response)
                if mxbuild_response["status"] != "Success":
                    # possible 'status': Success, Failure, Busy
                    logger.warning("Failed to build project, "
                                   "keeping previous model running")
                    state = "FAILED"
                elif mxbuild_response["restartRequired"] is True:
                    logger.info("Restarting app after MPK push")
                    self.server.restart_callback()
                    state = "STARTED"
                else:
                    logger.info("Reloading model after MPK push")
                    self.server.reload_callback()
                    state = "STARTED"
                return (200, {"state": state}, mxbuild_response)
            else:
                return (
                    401,
                    {
                        "state": "FAILED",
                        "errordetails": "No MPK found"
                    },
                    None,
                )

        except MxBuildFailure as mbf:
            logger.warning(
                "InstaDeploy terminating with MxBuildFailure: {}".format(
                    mbf.message))
            return (200, {"state": "FAILED"}, mbf.mxbuild_response)

        except Exception:
            logger.warning("Instadeploy failed", exc_info=True)
            return (
                500,
                {
                    "state": "FAILED",
                    "errordetails": traceback.format_exc()
                },
                None,
            )
예제 #23
0
def service_backups():
    vcap_services = buildpackutil.get_vcap_services_data()
    if not vcap_services or 'schnapps' not in vcap_services:
        logger.debug("No backup service detected")
        return

    backup_service = {}
    if 'amazon-s3' in vcap_services:
        s3_credentials = vcap_services['amazon-s3'][0]['credentials']
        backup_service['filesCredentials'] = {
            'accessKey': s3_credentials['access_key_id'],
            'secretKey': s3_credentials['secret_access_key'],
            'bucketName': s3_credentials['bucket'],
        }
        if 'key_suffix' in s3_credentials:  # Not all s3 plans have this field
            backup_service['filesCredentials']['keySuffix'] = s3_credentials['key_suffix']

    try:
        db_config = buildpackutil.get_database_config()
        if db_config['DatabaseType'] != 'PostgreSQL':
            raise Exception(
                'Schnapps only supports postgresql, not %s'
                % db_config['DatabaseType']
            )
        host_and_port = db_config['DatabaseHost'].split(':')
        backup_service['databaseCredentials'] = {
            'host': host_and_port[0],
            'username': db_config['DatabaseUserName'],
            'password': db_config['DatabasePassword'],
            'dbname': db_config['DatabaseName'],
            'port': int(host_and_port[1]) if len(host_and_port) > 1 else 5432,
        }
    except Exception as e:
        logger.exception(
            'Schnapps will not be activated because error occurred with '
            'parsing the database credentials'
        )
        return
    schnapps_url = vcap_services['schnapps'][0]['credentials']['url']
    schnapps_api_key = vcap_services['schnapps'][0]['credentials']['apiKey']

    try:
        result = requests.put(
            schnapps_url,
            headers={
                'Content-Type': 'application/json',
                'apiKey': schnapps_api_key
            },
            data=json.dumps(backup_service),
        )
    except Exception as e:
        logger.warning('Failed to contact backup service: ' + e)
        return

    if result.status_code == 200:
        logger.info("Successfully updated backup service")
    else:
        logger.warning("Failed to update backup service: " + result.text)
예제 #24
0
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)
예제 #25
0
 def run(self):
     logger.debug("Starting metrics emitter with interval %d",
                  self.interval)
     counter = 1
     while True:
         logger.info("MENDIX-LOGGING-HEARTBEAT: Heartbeat number %s",
                     counter)
         time.sleep(self.interval)
         counter += 1
예제 #26
0
def _create_config_file(agent_config):
    logger.debug('writing config file')
    with open('.local/telegraf/etc/telegraf/telegraf.conf', 'w') as tc:
        print('[agent]', file=tc)
        for item in agent_config:
            value = agent_config[item]
            print('  {} = {}'.format(item, _config_value_str(value)), file=tc)

        print('', file=tc)
예제 #27
0
def _create_config_file(agent_config):
    logger.debug("writing config file")
    with open(".local/telegraf/etc/telegraf/telegraf.conf", "w") as tc:
        print("[agent]", file=tc)
        for item in agent_config:
            value = agent_config[item]
            print("  {} = {}".format(item, _config_value_str(value)), file=tc)

        print("", file=tc)
예제 #28
0
 def _cleanup_logging(self):
     # atexit
     if self.m2ee._logproc:
         logger.debug("Stopping log output...")
         self.prompt = self._default_prompt
         if not self.m2ee._logproc.poll():
             os.kill(self.m2ee._logproc.pid, signal.SIGTERM)
         self.m2ee._logproc = None
         return True
     return False
예제 #29
0
 def _cleanup_logging(self):
     # atexit
     if self.m2ee._logproc:
         logger.debug("Stopping log output...")
         self.prompt = self._default_prompt
         if not self.m2ee._logproc.poll():
             os.kill(self.m2ee._logproc.pid, signal.SIGTERM)
         self.m2ee._logproc = None
         return True
     return False
예제 #30
0
 def run(self):
     logger.debug("Going to start mxbuild in serve mode")
     mxbuild.start_mxbuild_server(os.path.join(os.getcwd(), ".local"),
                                  self.mx_version)
     time.sleep(10)
     logger.debug("Listening on port %d for MPK uploads" % int(self.port))
     server = HTTPServer(("", self.port), MPKUploadHandler)
     server.restart_callback = self.restart_callback
     server.reload_callback = self.reload_callback
     server.serve_forever()
예제 #31
0
def update_config(m2ee, app_name):
    if not is_enabled() or not _is_installed():
        return

    # Telegraf config, taking over defaults from telegraf.conf from the distro
    logger.debug("creating telegraf config")
    _create_config_file({
        "interval": "10s",
        "round_interval": True,
        "metric_batch_size": 1000,
        "metric_buffer_limit": 10000,
        "collection_jitter": "0s",
        "flush_interval": "10s",
        "flush_jitter": "5s",
        "precision": "",
        "debug": False,
        "logfile": "",
        "hostname": buildpackutil.get_hostname(),
        "omit_hostname": False,
    })

    _write_config("[global_tags]", _get_tags())
    _write_config(
        "[[inputs.statsd]]",
        {
            "protocol": "udp",
            "max_tcp_connections": 250,
            "tcp_keep_alive": False,
            "service_address": ":8125",
            "delete_gauges": True,
            "delete_counters": True,
            "delete_sets": True,
            "delete_timings": True,
            "percentiles": [90],
            "metric_separator": ".",
            "parse_data_dog_tags": True,
            "allowed_pending_messages": 10000,
            "percentile_limit": 1000,
        },
    )

    # Forward metrics also to DataDog when enabled
    if datadog.is_enabled():
        _write_config("[[outputs.datadog]]", {"apikey": datadog.get_api_key()})

    # # Write http_oputs (one or array)
    http_configs = json.loads(_get_appmetrics_target())
    if type(http_configs) is list:
        for http_config in http_configs:
            _write_http_output_config(http_config)
    else:
        _write_http_output_config(http_configs)

    # Enable Java Agent on MxRuntime to
    datadog.enable_runtime_agent(m2ee)
예제 #32
0
def set_jetty_config(m2ee):
    jetty_config_json = os.environ.get("JETTY_CONFIG")
    if not jetty_config_json:
        return None
    try:
        jetty_config = json.loads(jetty_config_json)
        jetty = m2ee.config._conf["m2ee"]["jetty"]
        jetty.update(jetty_config)
        logger.debug("Jetty configured: %s", json.dumps(jetty))
    except Exception as e:
        logger.warning("Failed to configure jetty", exc_info=True)
예제 #33
0
def set_jetty_config(m2ee):
    jetty_config_json = os.environ.get('JETTY_CONFIG')
    if not jetty_config_json:
        return None
    try:
        jetty_config = json.loads(jetty_config_json)
        jetty = m2ee.config._conf['m2ee']['jetty']
        jetty.update(jetty_config)
        logger.debug('Jetty configured: %s', json.dumps(jetty))
    except Exception as e:
        logger.warning('Failed to configure jetty', exc_info=True)
예제 #34
0
파일: m2ee.py 프로젝트: pommi/m2ee-tools
 def __init__(self, yaml_files=None):
     logger.debug('Using m2ee-tools version %s' % m2ee.__version__)
     cmd.Cmd.__init__(self)
     if yaml_files:
         self.m2ee = M2EE(yamlfiles=yaml_files, load_default_files=False)
     else:
         self.m2ee = M2EE()
     self.do_status(None)
     username = pwd.getpwuid(os.getuid())[0]
     self._default_prompt = "m2ee(%s): " % username
     self.prompt = self._default_prompt
     logger.info("Application Name: %s" % self.m2ee.config.get_app_name())
예제 #35
0
def get_scheduled_events(metadata):
    scheduled_events = os.getenv('SCHEDULED_EVENTS', None)
    if not am_i_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)
예제 #36
0
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})
        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 am_i_primary_instance():
                    m2eeresponse = m2ee.client.execute_ddl_commands()
                    m2eeresponse.display_error()
                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)
예제 #37
0
def get_constants(metadata):
    constants = {}
    for constant in metadata['Constants']:
        env = 'MX_%s' % constant['Name'].replace('.', '_')
        value = os.environ.get(env)
        if value is None:
            value = constant['DefaultValue']
            logger.debug(
                'constant not found in environment, taking default '
                'value %s' % constant['Name']
            )
        if constant['Type'] == 'Integer':
            value = int(value)
        constants[constant['Name']] = value
    return constants
예제 #38
0
    def _stop(self):
        (pid_alive, m2ee_alive) = self.m2ee.check_alive()
        if not pid_alive and not m2ee_alive:
            logger.info("Nothing to stop, the application is not running.")
            return True

        logger.debug("Trying to stop the application.")
        stopped = False

        stopped = self.m2ee.stop()
        if stopped:
            return True

        answer = None
        while answer not in ('y', 'n'):
            answer = ('y' if self.yolo_mode
                      else raw_input("Do you want to try to signal the JVM "
                                     "process to stop immediately? (y)es, (n)o? "))
            if answer == 'y':
                stopped = self.m2ee.terminate()
                if stopped:
                    return True
            elif answer == 'n':
                logger.info("Doing nothing, use stop again to check if the "
                            "process finally disappeared...")
                return False
            else:
                print("Unknown option %s" % answer)

        answer = None
        while answer not in ('y', 'n'):
            answer = ('y' if self.yolo_mode
                      else raw_input("Do you want to kill the JVM process? "
                                     "(y)es, (n)o? "))
            if answer == 'y':
                stopped = self.m2ee.kill()
                if stopped:
                    return True
            elif answer == 'n':
                logger.info("Doing nothing, use stop again to check if the "
                            "process finally disappeared...")
                return False
            else:
                print("Unknown option %s" % answer)

        return False
예제 #39
0
 def do_show_license_information(self, args):
     if self._report_not_implemented(3) or self._report_not_running():
         return
     m2eeresp = self.m2ee.client.get_license_information()
     m2eeresp.display_error()
     if not m2eeresp.has_error():
         feedback = m2eeresp.get_feedback()
         if 'license' in feedback:
             logger.debug(yaml.safe_dump(feedback['license'],
                          allow_unicode=True))
             import copy
             licensecopy = copy.deepcopy(feedback['license'])
             self._print_license(licensecopy)
         elif 'license_id' in feedback:
             print("Unlicensed environment.")
             print("Server ID: %s" % feedback['license_id'])
         else:
             print("Unlicensed environment.")
예제 #40
0
def create_admin_user(m2ee):
    logger.info('Ensuring admin user credentials')
    logger.debug('Creating admin user')
    m2eeresponse = m2ee.client.create_admin_user({
        'password': os.environ.get('ADMIN_PASSWORD'),
    })
    if m2eeresponse.has_error():
        m2eeresponse.display_error()
        sys.exit(1)

    logger.debug('Setting admin user password')
    m2eeresponse = m2ee.client.create_admin_user({
        'username': m2ee.config._model_metadata['AdminUser'],
        'password': os.environ.get('ADMIN_PASSWORD'),
    })
    if m2eeresponse.has_error():
        m2eeresponse.display_error()
        sys.exit(1)
예제 #41
0
def activate_new_relic(m2ee, app_name):
    if buildpackutil.get_new_relic_license_key() is None:
        logger.debug(
            'Skipping New Relic setup, no license key found in environment'
        )
        return
    logger.info('Adding new relic')
    m2ee_section = m2ee.config._conf['m2ee']
    if 'custom_environment' not in m2ee_section:
        m2ee_section['custom_environment'] = {}
    m2ee_section['custom_environment']['NEW_RELIC_LICENSE_KEY'] = (
        buildpackutil.get_new_relic_license_key()
    )
    m2ee_section['custom_environment']['NEW_RELIC_APP_NAME'] = app_name
    m2ee_section['custom_environment']['NEW_RELIC_LOG'] = (
        os.path.abspath('newrelic/agent.log')
    )

    m2ee.config._conf['m2ee']['javaopts'].append(
        '-javaagent:{path}'.format(
            path=os.path.abspath('newrelic/newrelic.jar')
        )
    )
예제 #42
0
def configure_debugger(m2ee):
    debugger_password = os.environ.get('DEBUGGER_PASSWORD')

    if debugger_password is None:
        logger.debug('Not configuring debugger, as environment variable '
                     'was not found')
        return

    response = m2ee.client.enable_debugger({
        'password': debugger_password
    })
    response.display_error()
    if not response.has_error():
        logger.info(
            'The remote debugger is now enabled, the password to '
            'use is %s' % debugger_password
        )
        logger.info(
            'You can use the remote debugger option in the Mendix '
            'Business Modeler to connect to the /debugger/ sub '
            'url on your application (e.g. '
            'https://app.example.com/debugger/). '
        )
예제 #43
0
 def do_reload(self, args):
     logger.debug("Reloading configuration...")
     self.m2ee.reload_config()
예제 #44
0
def set_application_name(m2ee, name):
    logger.debug('Application name is %s' % name)
    m2ee.config._conf['m2ee']['app_name'] = name