Ejemplo n.º 1
0
def main():
    _log.debug("Reading config from stdin")
    conf_raw = ''.join(sys.stdin.readlines()).replace('\n', '')
    config = json.loads(conf_raw)

    # Get the log level from the config file, default to INFO.
    log_level = config.get(LOG_LEVEL_KEY, "INFO").upper()

    # Setup logger. We log to file and to stderr based on the
    # log level provided in the network configuration file.
    configure_logging(_log, LOG_FILENAME,
                      log_level=log_level,
                      stderr_level=logging.INFO)
    configure_logging(logging.getLogger("pycalico"), LOG_FILENAME,
                      log_level=log_level,
                      stderr_level=logging.INFO)

    # Get copy of environment.
    env = os.environ.copy()

    try:
        # Execute IPAM.
        output = IpamPlugin(env, config["ipam"]).execute()
    except CniError as e:
        # We caught a CNI error - print the result to stdout and
        # exit.
        _exit_on_error(e.code, e.msg, e.details)
    except Exception as e:
        _log.exception("Unhandled exception")
        _exit_on_error(ERR_CODE_GENERIC,
                       message="Unhandled Exception",
                       details=e.message)
    else:
        if output:
            print output
Ejemplo n.º 2
0
def main():
    """
    Main function - configures and runs the plugin.
    """
    # Read the network config file from stdin. Replace newline characters
    # so that we can properly load it as json.
    # The python json library loads strings as as the unicode type. This causes
    # problems when loading values into os.environ.
    # Rather than try to encode them as strings, just use the yaml library.
    # For more details see http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python
    config_raw = ''.join(sys.stdin.readlines()).replace('\n', '')
    network_config = yaml.safe_load(config_raw)

    # Get the log level from the config file, default to INFO.
    log_level = network_config.get(LOG_LEVEL_KEY, "INFO").upper()

    # Configure logging for CNI
    configure_logging(_log, LOG_FILENAME, log_level=log_level)

    # Configure logging for libcalico (pycalico)
    configure_logging(logging.getLogger("pycalico"), LOG_FILENAME,
                      log_level=log_level)

    _log.debug("Loaded network config:\n%s",
               json.dumps(network_config, indent=2))

    # Get the etcd configuration from the config file. Set the
    # environment variables.
    etcd_authority = network_config.get(ETCD_AUTHORITY_KEY)
    etcd_endpoints = network_config.get(ETCD_ENDPOINTS_KEY)
    if etcd_authority:
        os.environ[ETCD_AUTHORITY_ENV] = etcd_authority
        _log.debug("Using %s=%s", ETCD_AUTHORITY_ENV, etcd_authority)
    if etcd_endpoints:
        os.environ[ETCD_ENDPOINTS_ENV] = etcd_endpoints
        _log.debug("Using %s=%s", ETCD_ENDPOINTS_ENV, etcd_endpoints)

    # Get the CNI environment.
    env = os.environ.copy()
    _log.debug("Loaded environment:\n%s", json.dumps(env, indent=2))

    # Call the CNI plugin and handle any errors.
    rc = 0
    try:
        _log.info("Starting Calico CNI plugin execution")
        CniPlugin(network_config, env).execute()
    except SystemExit as e:
        # SystemExit indicates an error that was handled earlier
        # in the stack.  Just set the return code.
        rc = e.code
    except Exception:
        # An unexpected Exception has bubbled up - catch it and
        # log it out.
        _log.exception("Unhandled Exception killed plugin")
        rc = ERR_CODE_GENERIC
        print_cni_error(rc, "Unhandled Exception killed plugin")
    finally:
        _log.info("Calico CNI execution complete, rc=%s", rc)
        sys.exit(rc)
Ejemplo n.º 3
0
def main():
    """
    Main function - configures and runs the plugin.
    """
    # Read the network config file from stdin. Replace newline characters
    # so that we can properly load it as json.
    config_raw = ''.join(sys.stdin.readlines()).replace('\n', '')
    network_config = json.loads(config_raw)

    # Get the log level from the config file, default to INFO.
    log_level = network_config.get(LOG_LEVEL_KEY, "INFO").upper()

    # Configure logging.
    configure_logging(_log, LOG_FILENAME, log_level=log_level)
    _log.debug("Loaded network config:\n%s", 
               json.dumps(network_config, indent=2))

    # Get the etcd authority from the config file. Set the 
    # environment variable.
    etcd_authority = network_config.get(ETCD_AUTHORITY_KEY, 
                                        ETCD_AUTHORITY_DEFAULT)
    os.environ[ETCD_AUTHORITY_ENV] = etcd_authority
    _log.debug("Using ETCD_AUTHORITY=%s", etcd_authority)

    # Get the CNI environment. 
    env = os.environ.copy()
    _log.debug("Loaded environment:\n%s", json.dumps(env, indent=2))

    # Call the CNI plugin and handle any errors.
    rc = 0
    try:
        _log.info("Starting Calico CNI plugin execution")
        CniPlugin(network_config, env).execute()
    except SystemExit as e:
        # SystemExit indicates an error that was handled earlier
        # in the stack.  Just set the return code.
        rc = e.code
    except Exception:
        # An unexpected Exception has bubbled up - catch it and
        # log it out.
        _log.exception("Unhandled Exception killed plugin")
        rc = ERR_CODE_GENERIC
        print_cni_error(rc, "Unhandled Exception killed plugin")
    finally:
        _log.info("Calico CNI execution complete, rc=%s", rc)
        sys.exit(rc)
Ejemplo n.º 4
0
def main():
    _log.debug("Reading config from stdin")
    conf_raw = ''.join(sys.stdin.readlines()).replace('\n', '')
    config = json.loads(conf_raw)

    # Get the log level from the config file, default to INFO.
    log_level_file = config.get(LOG_LEVEL_FILE_KEY, "NONE").upper()
    log_level_stderr = config.get(LOG_LEVEL_STDERR_KEY, "INFO").upper()

    log_filename = "ipam.log"

    # Configure logging for CNI
    configure_logging(_log, log_level_file, log_level_stderr, log_filename)

    # Configure logging for libcalico (pycalico)
    configure_logging(logging.getLogger("pycalico"), "ERROR", "ERROR",
                      log_filename)

    # Get copy of environment.
    env = os.environ.copy()

    try:
        # Execute IPAM.
        output = IpamPlugin(env, config["ipam"]).execute()
    except CniError as e:
        # We caught a CNI error - print the result to stdout and
        # exit.
        _exit_on_error(e.code, e.msg, e.details)
    except Exception as e:
        _log.exception("Unhandled exception")
        _exit_on_error(ERR_CODE_GENERIC,
                       message="Unhandled Exception",
                       details=e.message)
    else:
        if output:
            print output
Ejemplo n.º 5
0
def main():
    _log.debug("Reading config from stdin")
    conf_raw = ''.join(sys.stdin.readlines()).replace('\n', '')
    config = json.loads(conf_raw)

    # Get the log level from the config file, default to INFO.
    log_level_file = config.get(LOG_LEVEL_FILE_KEY, "NONE").upper()
    log_level_stderr = config.get(LOG_LEVEL_STDERR_KEY, "INFO").upper()

    log_filename = "ipam.log"

    # Configure logging for CNI
    configure_logging(_log, log_level_file, log_level_stderr, log_filename)

    # Configure logging for libcalico (pycalico)
    configure_logging(logging.getLogger("pycalico"), "ERROR", "ERROR",
                      log_filename)

    # Get copy of environment.
    env = os.environ.copy()

    try:
        # Execute IPAM.
        output = IpamPlugin(env, config["ipam"]).execute()
    except CniError as e:
        # We caught a CNI error - print the result to stdout and
        # exit.
        _exit_on_error(e.code, e.msg, e.details)
    except Exception as e:
        _log.exception("Unhandled exception")
        _exit_on_error(ERR_CODE_GENERIC,
                       message="Unhandled Exception",
                       details=e.message)
    else:
        if output:
            print output
Ejemplo n.º 6
0
def main():
    """
    Main function - configures and runs the plugin.
    """
    # Read the network config file from stdin. Replace newline characters
    # so that we can properly load it as json.
    # The python json library loads strings as as the unicode type. This causes
    # problems when loading values into os.environ.
    # Rather than try to encode them as strings, just use the yaml library.
    # For more details see http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python
    config_raw = ''.join(sys.stdin.readlines()).replace('\n',
                                                        '').replace('\t', '')
    network_config = yaml.safe_load(config_raw)

    # Get the log level from the config file, default to INFO.
    log_level_file = network_config.get(LOG_LEVEL_FILE_KEY, "INFO").upper()
    log_level_stderr = network_config.get(LOG_LEVEL_STDERR_KEY,
                                          "ERROR").upper()

    log_filename = "cni.log"
    # Configure logging for CNI
    configure_logging(_log, log_level_file, log_level_stderr, log_filename)

    # Configure logging for libcalico (pycalico)
    configure_logging(logging.getLogger("pycalico"), "WARNING", "WARNING",
                      log_filename)

    _log.debug("Loaded network config:\n%s",
               json.dumps(network_config, indent=2))

    # Get the etcd configuration from the config file. Set the
    # environment variables.
    etcd_authority = network_config.get(ETCD_AUTHORITY_KEY)
    etcd_endpoints = network_config.get(ETCD_ENDPOINTS_KEY)
    if etcd_authority:
        os.environ[ETCD_AUTHORITY_ENV] = etcd_authority
        _log.debug("Using %s=%s", ETCD_AUTHORITY_ENV, etcd_authority)
    if etcd_endpoints:
        os.environ[ETCD_ENDPOINTS_ENV] = etcd_endpoints
        _log.debug("Using %s=%s", ETCD_ENDPOINTS_ENV, etcd_endpoints)

    # Get the CNI environment.
    env = os.environ.copy()
    _log.debug("Loaded environment:\n%s", json.dumps(env, indent=2))

    # Call the CNI plugin and handle any errors.
    rc = 0
    try:
        _log.info("Starting Calico CNI plugin execution")
        CniPlugin(network_config, env).execute()
    except SystemExit as e:
        # SystemExit indicates an error that was handled earlier
        # in the stack.  Just set the return code.
        rc = e.code
    except Exception:
        # An unexpected Exception has bubbled up - catch it and
        # log it out.
        _log.exception("Unhandled Exception killed plugin")
        rc = ERR_CODE_GENERIC
        print_cni_error(rc, "Unhandled Exception killed plugin")
    finally:
        _log.info("Calico CNI execution complete, rc=%s", rc)
        sys.exit(rc)