Exemple #1
0
 def compile():
     self.snippetcompiler.setup_for_snippet(snippet)
     Config.set("compiler", "datatrace_enable", "true")
     (self._types, root_ns) = compiler.do_compile()
     self._namespace = root_ns.get_child("__config__")
Exemple #2
0
def server_ssl() -> "bool":
    return Config.get("compiler_rest_transport", "ssl", False)
Exemple #3
0
def server_port() -> "number":
    return Config.get("compiler_rest_transport", "port", 8888)
Exemple #4
0
def compile_project(options: argparse.Namespace):
    if options.environment is not None:
        Config.set("config", "environment", options.environment)

    if options.server is not None:
        Config.set("compiler_rest_transport", "host", options.server)

    if options.port is not None:
        Config.set("compiler_rest_transport", "port", options.port)

    if options.user is not None:
        Config.set("compiler_rest_transport", "username", options.user)

    if options.password is not None:
        Config.set("compiler_rest_transport", "password", options.password)

    if options.ssl:
        Config.set("compiler_rest_transport", "ssl", "true")

    if options.ca_cert is not None:
        Config.set("compiler_rest_transport", "ssl-ca-cert-file",
                   options.ca_cert)

    if options.export_compile_data is True:
        Config.set("compiler", "export_compile_data", "true")

    if options.export_compile_data_file is not None:
        Config.set("compiler", "export_compile_data_file",
                   options.export_compile_data_file)

    if options.datatrace is True:
        Config.set("compiler", "datatrace_enable", "true")

    if options.dataflow_graphic is True:
        Config.set("compiler", "dataflow_graphic_enable", "true")

    compiler_features.read_options_to_config(options)

    module.Project.get(options.main_file)

    if options.profile:
        import cProfile
        import pstats

        result = cProfile.runctx("do_compile()", globals(), {}, "run.profile")
        p = pstats.Stats("run.profile")
        p.strip_dirs().sort_stats("time").print_stats(20)
    else:
        t1 = time.time()
        result = do_compile()
        LOGGER.debug("Compile time: %0.03f seconds", time.time() - t1)
    return result
Exemple #5
0
def getfact(context: Context,
            resource: "any",
            fact_name: "string",
            default_value: "any" = None) -> "any":
    """
        Retrieve a fact of the given resource
    """
    global fact_cache

    resource_id = inmanta.resources.to_id(resource)
    if resource_id is None:
        raise Exception("Facts can only be retreived from resources.")

    # Special case for unit testing and mocking
    if hasattr(context.compiler, "refs") and "facts" in context.compiler.refs:
        if (resource_id in context.compiler.refs["facts"]
                and fact_name in context.compiler.refs["facts"][resource_id]):
            return context.compiler.refs["facts"][resource_id][fact_name]

        fact_value = Unknown(source=resource)
        unknown_parameters.append({
            "resource": resource_id,
            "parameter": fact_name,
            "source": "fact"
        })

        if default_value is not None:
            return default_value
        return fact_value
    # End special case

    try:
        client = context.get_client()

        env = Config.get("config", "environment", None)
        if env is None:
            raise Exception(
                "The environment of this model should be configured in config>environment"
            )

        # load cache
        if not fact_cache:

            def call():
                return client.list_params(tid=env, )

            result = context.run_sync(call)
            if result.code == 200:
                fact_values = result.result["parameters"]
                for fact_value in fact_values:
                    fact_cache.setdefault(
                        fact_value["resource_id"],
                        {})[fact_value["name"]] = fact_value["value"]

        # attempt cache hit
        if resource_id in fact_cache:
            if fact_name in fact_cache[resource_id]:
                return fact_cache[resource_id][fact_name]

        fact_value = None

        def call():
            return client.get_param(tid=env,
                                    id=fact_name,
                                    resource_id=resource_id)

        result = context.run_sync(call)

        if result.code == 200:
            fact_value = result.result["parameter"]["value"]
        else:
            logging.getLogger(__name__).info(
                "Param %s of resource %s is unknown", fact_name, resource_id)
            fact_value = Unknown(source=resource)
            unknown_parameters.append({
                "resource": resource_id,
                "parameter": fact_name,
                "source": "fact"
            })

    except ConnectionRefusedError:
        logging.getLogger(__name__).warning(
            "Param %s of resource %s is unknown because connection to server was refused",
            fact_name,
            resource_id,
        )
        fact_value = Unknown(source=resource)
        unknown_parameters.append({
            "resource": resource_id,
            "parameter": fact_name,
            "source": "fact"
        })

    if isinstance(fact_value, Unknown) and default_value is not None:
        return default_value

    return fact_value
Exemple #6
0
 def __init__(self):
     self.libs = tempfile.mkdtemp()
     self.env = tempfile.mkdtemp()
     Config.load_config()
     Config.set("config", "environment", str(uuid.uuid4()))
Exemple #7
0
def test_configfile_hierarchy(monkeypatch, tmpdir):
    etc_inmanta_dir = os.path.join(tmpdir, "etc", "inmanta")
    os.makedirs(etc_inmanta_dir, exist_ok=False)

    main_inmanta_cfg_file = os.path.join(etc_inmanta_dir, "inmanta.cfg")

    inmanta_d_dir = os.path.join(etc_inmanta_dir, "inmanta.d")
    os.mkdir(inmanta_d_dir)

    inmanta_d_cfg_file01 = os.path.join(inmanta_d_dir, "01-dbconfig.cfg")
    inmanta_d_cfg_file02 = os.path.join(inmanta_d_dir, "02-dbconfig.cfg")
    inmanta_d_cfg_file_no_cfg_extension = os.path.join(inmanta_d_dir,
                                                       "03-config")

    dot_inmanta_file = os.path.join(tmpdir, ".inmanta")
    dot_inmanta_cfg_file = os.path.join(tmpdir, ".inmanta.cfg")

    min_c_file = os.path.join(tmpdir, "custom.cfg")

    monkeypatch.setenv("INMANTA_SERVER_AUTH", "true")
    monkeypatch.setenv("INMANTA_SERVER_AGENT_TIMEOUT", "60")

    with open(main_inmanta_cfg_file, "w", encoding="utf-8") as f:
        f.write("""
[server]
auth=false
[config]
log-dir=/log
[database]
host=host1
name=db1
port=1234
[influxdb]
host=host1
interval=10
tags=tag1=value1
[dashboard]
path=/some/directory
client-id=test
        """)

    with open(inmanta_d_cfg_file01, "w", encoding="utf-8") as f:
        f.write("""
[database]
host=host2
name=db2
[influxdb]
host=host2
        """)

    with open(inmanta_d_cfg_file02, "w", encoding="utf-8") as f:
        f.write("""
[database]
port=5678
[influxdb]
host=host3
interval=20
        """)

    with open(inmanta_d_cfg_file_no_cfg_extension, "w", encoding="utf-8") as f:
        f.write("""
[database]
port=9999
        """)

    with open(dot_inmanta_file, "w", encoding="utf-8") as f:
        f.write("""
[database]
host=host3
[influxdb]
tags=tag2=value2
[dashboard]
path=/some/other/directory
        """)

    with open(dot_inmanta_cfg_file, "w", encoding="utf-8") as f:
        f.write("""
[dashboard]
path=/directory
client-id=test123
        """)

    with open(min_c_file, "w", encoding="utf-8") as f:
        f.write("""
[dashboard]
client-id=test456
        """)

    os.chdir(tmpdir)
    Config.load_config(min_c_config_file=min_c_file,
                       config_dir=inmanta_d_dir,
                       main_cfg_file=main_inmanta_cfg_file)

    assert Config.get("config", "log-dir") == "/log"
    assert Config.get("database", "host") == "host3"
    assert Config.get("database", "name") == "db2"
    assert Config.get("database", "port") == 5678
    assert Config.get("influxdb", "host") == "host3"
    assert Config.get("influxdb", "interval") == 20
    assert Config.get("influxdb", "tags")["tag2"] == "value2"
    assert Config.get("dashboard", "path") == "/directory"
    assert Config.get("dashboard", "client-id") == "test456"
    assert Config.get("server", "auth")
    assert Config.get("server", "agent-timeout") == 60
Exemple #8
0
def app():
    """
        Run the compiler
    """

    normalformatter = logging.Formatter(fmt="%(levelname)-8s%(message)s")
    # set logging to sensible defaults
    formatter = colorlog.ColoredFormatter(
        "%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s",
        datefmt=None,
        reset=True,
        log_colors={
            'DEBUG': 'cyan',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red',
        }
    )

    stream = logging.StreamHandler()
    stream.setLevel(logging.INFO)

    if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
        stream.setFormatter(formatter)
    else:
        stream.setFormatter(normalformatter)

    logging.root.handlers = []
    logging.root.addHandler(stream)
    logging.root.setLevel(0)

    # do an initial load of known config files to build the libdir path
    Config.load_config()

    parser = cmd_parser()

    options, other = parser.parse_known_args()
    options.other = other

    if options.timed:
        if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
            formatter = colorlog.ColoredFormatter(
                "%(asctime)s %(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s",
                datefmt=None,
                reset=True,
                log_colors={
                    'DEBUG': 'cyan',
                    'INFO': 'green',
                    'WARNING': 'yellow',
                    'ERROR': 'red',
                    'CRITICAL': 'red',
                }
            )
        else:
            formatter = logging.Formatter(fmt="%(asctime)s %(levelname)-8s%(message)s")
        stream.setFormatter(formatter)

    # set the log level
    level = options.verbose
    if level >= len(log_levels):
        level = 3
    stream.setLevel(log_levels[level])

    # set the logfile
    if options.log_file:
        level = options.log_file_level
        if level >= len(log_levels):
            level = 3

        formatter = logging.Formatter(fmt="%(asctime)s %(levelname)-8s %(name)-10s %(message)s")

        file_handler = logging.FileHandler(filename=options.log_file, mode="w")
        file_handler.setFormatter(formatter)

        file_handler.setLevel(log_levels[level])
        logging.root.addHandler(file_handler)

    # Load the configuration
    Config.load_config(options.config_file)

    # start the command
    if not hasattr(options, "func"):
        # show help
        parser.print_usage()
        return

    options.func(options)
Exemple #9
0
def compile_project(options):
    if options.environment is not None:
        Config.set("config", "environment", options.environment)

    if options.server is not None:
        Config.set("compiler_rest_transport", "host", options.server)

    if options.port is not None:
        Config.set("compiler_rest_transport", "port", options.port)

    if options.user is not None:
        Config.set("compiler_rest_transport", "username", options.user)

    if options.password is not None:
        Config.set("compiler_rest_transport", "password", options.password)

    if options.ssl:
        Config.set("compiler_rest_transport", "ssl", "true")

    if options.ca_cert is not None:
        Config.set("compiler_rest_transport", "ssl-ca-cert-file", options.ca_cert)

    module.Project.get(options.main_file)
    try:
        if options.profile:
            import cProfile
            import pstats
            result = cProfile.runctx('do_compile()', globals(), {}, "run.profile")
            p = pstats.Stats('run.profile')
            p.strip_dirs().sort_stats("time").print_stats(20)
        else:
            t1 = time.time()
            result = do_compile()
            LOGGER.debug("Compile time: %0.03f seconds", time.time() - t1)
        return result
    except CompilerException as e:
        if not options.errors:
            print(str(e), file=sys.stderr)
            sys.exit(1)
        else:
            raise e
Exemple #10
0
async def test_export_with_specific_export_plugin(tmpdir, client):
    server_port = Config.get("client_rest_transport", "port")
    server_host = Config.get("client_rest_transport", "host", "localhost")
    result = await client.create_project("test")
    assert result.code == 200
    proj_id = result.result["project"]["id"]
    result = await client.create_environment(proj_id, "test", None, None)
    assert result.code == 200
    env_id = result.result["environment"]["id"]
    workspace = tmpdir.mkdir("tmp")
    libs_dir = workspace.join("libs")

    # project.yml
    path_project_yml_file = workspace.join("project.yml")
    path_project_yml_file.write(f"""
name: testproject
modulepath: {libs_dir}
downloadpath: {libs_dir}
repo: https://github.com/inmanta/
""")

    # main.cf
    path_main_file = workspace.join("main.cf")
    path_main_file.write("import test")

    # test module
    module_dir = libs_dir.join("test")
    os.makedirs(module_dir)

    # Module.yml
    module_yml_file = module_dir.join("module.yml")
    module_yml_file.write("""
name: test
license: test
version: 1.0.0
    """)

    # .inmanta
    dot_inmanta_cfg_file = workspace.join(".inmanta")
    dot_inmanta_cfg_file.write("""
[config]
export=other_exporter
    """)

    # plugin/__init__.py
    plugins_dir = module_dir.join("plugins")
    os.makedirs(plugins_dir)
    init_file = plugins_dir.join("__init__.py")
    init_file.write("""
from inmanta.export import export, Exporter

@export("test_exporter")
def test_exporter(exporter: Exporter) -> None:
    print("test_exporter ran")

@export("other_exporter")
def other_exporter(exporter: Exporter) -> None:
    print("other_exporter ran")
    """)

    # model/_init.cf
    model_dir = module_dir.join("model")
    os.makedirs(model_dir)
    init_cf_file = model_dir.join("_init.cf")
    init_cf_file.write("")

    os.chdir(workspace)

    args = [
        sys.executable,
        "-m",
        "inmanta.app",
        "export",
        "--export-plugin",
        "test_exporter",
        "-e",
        str(env_id),
        "--server_port",
        str(server_port),
        "--server_address",
        str(server_host),
    ]

    process = await subprocess.create_subprocess_exec(*args,
                                                      stdout=subprocess.PIPE,
                                                      stderr=subprocess.PIPE)
    try:
        (stdout, stderr) = await asyncio.wait_for(process.communicate(),
                                                  timeout=30)
    except asyncio.TimeoutError as e:
        process.kill()
        await process.communicate()
        raise e

    # Make sure exitcode is zero
    assert process.returncode == 0

    assert "test_exporter ran" in stdout.decode("utf-8")
    assert "other_exporter" not in stdout.decode("utf-8")

    # ## Failure

    path_main_file.write("""import test

vm1=std::Host(name="non-existing-machine", os=std::linux)

vm1.name = "other"
""")

    process = await subprocess.create_subprocess_exec(*args,
                                                      stdout=subprocess.PIPE,
                                                      stderr=subprocess.PIPE)
    try:
        (stdout, stderr) = await asyncio.wait_for(process.communicate(),
                                                  timeout=30)
    except asyncio.TimeoutError as e:
        process.kill()
        await process.communicate()
        raise e

    # Make sure exitcode is one
    assert process.returncode == 1

    assert "test_exporter ran" not in stdout.decode("utf-8")
    assert "other_exporter" not in stdout.decode("utf-8")

    shutil.rmtree(workspace)
Exemple #11
0
def export(options):
    if options.environment is not None:
        Config.set("config", "environment", options.environment)

    if options.server is not None:
        Config.set("compiler_rest_transport", "host", options.server)

    if options.server is not None:
        Config.set("compiler_rest_transport", "port", options.port)

    if options.token is not None:
        Config.set("compiler_rest_transport", "token", options.token)

    if options.ssl:
        Config.set("compiler_rest_transport", "ssl", "true")

    if options.ca_cert is not None:
        Config.set("compiler_rest_transport", "ssl-ca-cert-file", options.ca_cert)

    module.Project.get(options.main_file)

    from inmanta.export import Exporter  # noqa: H307

    exp = None
    try:
        (types, scopes) = do_compile()
    except Exception as e:
        exp = e
        types, scopes = (None, None)

    export = Exporter(options)
    version, _ = export.run(types, scopes)

    if exp is not None:
        if not options.errors:
            print(exp, file=sys.stderr)
            sys.exit(1)
        else:
            raise exp

    if options.deploy:
        conn = protocol.Client("compiler")
        LOGGER.info("Triggering deploy for version %d" % version)
        tid = cfg_env.get()
        IOLoop.current().run_sync(lambda: conn.release_version(tid, version, True), 60)
Exemple #12
0
    def __init__(self, server, auth=False, ssl=False):
        self.server = server
        TransportConfig("autotest")
        Config.set("autotest_rest_transport", "host", self.server)
        Config.set("autotest_rest_transport", "port", "8888")
        Config.set("compiler_rest_transport", "host", self.server)
        Config.set("compiler_rest_transport", "port", "8888")

        if auth:
            Config.set("autotest_rest_transport", "username", "jos")
            Config.set("autotest_rest_transport", "password",
                       "raienvnWAVbaerMSZ")
            Config.set("compiler_rest_transport", "username", "jos")
            Config.set("compiler_rest_transport", "password",
                       "raienvnWAVbaerMSZ")

        if ssl:
            Config.set("autotest_rest_transport", "ssl", "true")
            Config.set("compiler_rest_transport", "ssl", "true")
            Config.set("autotest_rest_transport", "ssl_ca_cert_file",
                       "/etc/pki/tls/certs/server.crt")
            Config.set("compiler_rest_transport", "ssl_ca_cert_file",
                       "/etc/pki/tls/certs/server.crt")

        self._client = inmanta.protocol.Client("autotest")
        self.auth = auth
        self.ssl = ssl
Exemple #13
0
def app() -> None:
    """
    Run the compiler
    """
    # Send logs to stdout
    stream_handler = _get_default_stream_handler()
    logging.root.handlers = []
    logging.root.addHandler(stream_handler)
    logging.root.setLevel(0)

    # do an initial load of known config files to build the libdir path
    Config.load_config()
    parser = cmd_parser()
    options, other = parser.parse_known_args()
    options.other = other

    # Log everything to a log_file if logfile is provided
    if options.log_file:
        watched_file_handler = _get_watched_file_handler(options)
        logging.root.addHandler(watched_file_handler)
        logging.root.removeHandler(stream_handler)
    else:
        if options.timed:
            formatter = _get_log_formatter_for_stream_handler(timed=True)
            stream_handler.setFormatter(formatter)
        log_level = _convert_to_log_level(options.verbose)
        stream_handler.setLevel(log_level)

    logging.captureWarnings(True)

    if options.inmanta_version:
        print_versions_installed_components_and_exit()

    if options.config_file and not os.path.exists(options.config_file):
        LOGGER.warning("Config file %s doesn't exist", options.config_file)

    # Load the configuration
    Config.load_config(options.config_file, options.config_dir)

    if options.warnings is not None:
        Config.set("warnings", "default", options.warnings)

    config = Config.get()
    assert isinstance(config, ConfigParser)
    WarningsManager.apply_config(config["warnings"] if "warnings" in
                                 config else None)

    # start the command
    if not hasattr(options, "func"):
        # show help
        parser.print_usage()
        return

    def report(e: Exception) -> None:
        minus_x_set_top_level_command = options.errors
        minus_x_set_subcommand = hasattr(
            options, "errors_subcommand") and options.errors_subcommand
        if not minus_x_set_top_level_command and not minus_x_set_subcommand:
            if isinstance(e, CompilerException):
                print(e.format_trace(indent="  "), file=sys.stderr)
            else:
                print(str(e), file=sys.stderr)
        else:
            sys.excepthook(*sys.exc_info())

        if isinstance(e, CompilerException):
            from inmanta.compiler.help.explainer import ExplainerFactory

            helpmsg = ExplainerFactory().explain_and_format(
                e, plain=not _is_on_tty())
            if helpmsg is not None:
                print(helpmsg)

    try:
        options.func(options)
    except ShowUsageException as e:
        print(e.args[0], file=sys.stderr)
        parser.print_usage()
    except CLIException as e:
        report(e)
        sys.exit(e.exitcode)
    except Exception as e:
        report(e)
        sys.exit(1)
    except KeyboardInterrupt as e:
        report(e)
        sys.exit(1)
    sys.exit(0)
Exemple #14
0
def export(options: argparse.Namespace) -> None:
    if options.environment is not None:
        Config.set("config", "environment", options.environment)

    if options.server is not None:
        Config.set("compiler_rest_transport", "host", options.server)

    if options.port is not None:
        Config.set("compiler_rest_transport", "port", options.port)

    if options.token is not None:
        Config.set("compiler_rest_transport", "token", options.token)

    if options.ssl:
        Config.set("compiler_rest_transport", "ssl", "true")

    if options.ca_cert is not None:
        Config.set("compiler_rest_transport", "ssl-ca-cert-file",
                   options.ca_cert)

    if options.export_compile_data is True:
        Config.set("compiler", "export_compile_data", "true")

    if options.export_compile_data_file is not None:
        Config.set("compiler", "export_compile_data_file",
                   options.export_compile_data_file)

    compiler_features.read_options_to_config(options)

    # try to parse the metadata as json. If a normal string, create json for it.
    if options.metadata is not None and len(options.metadata) > 0:
        try:
            metadata = json.loads(options.metadata)
        except json.decoder.JSONDecodeError:
            metadata = {"message": options.metadata}
    else:
        metadata = {"message": "Manual compile on the CLI by user"}

    if "cli-user" not in metadata and "USERNAME" in os.environ:
        metadata["cli-user"] = os.environ["USERNAME"]

    if "hostname" not in metadata:
        metadata["hostname"] = socket.gethostname()

    if "type" not in metadata:
        metadata["type"] = "manual"

    module.Project.get(options.main_file)

    from inmanta.export import Exporter  # noqa: H307

    exp = None
    try:
        (types, scopes) = do_compile()
    except Exception as e:
        exp = e
        types, scopes = (None, None)

    # Even if the compile failed we might have collected additional data such as unknowns. So
    # continue the export

    export = Exporter(options)
    version, _ = export.run(types,
                            scopes,
                            metadata=metadata,
                            model_export=options.model_export,
                            export_plugin=options.export_plugin)

    if exp is not None:
        raise exp

    if options.model:
        modelexporter = ModelExporter(types)
        with open("testdump.json", "w", encoding="utf-8") as fh:
            print(yaml.dump(modelexporter.export_all()))
            json.dump(modelexporter.export_all(), fh)

    if options.deploy:
        conn = protocol.SyncClient("compiler")
        LOGGER.info("Triggering deploy for version %d" % version)
        tid = cfg_env.get()
        agent_trigger_method = const.AgentTriggerMethod.get_agent_trigger_method(
            options.full_deploy)
        conn.release_version(tid, version, True, agent_trigger_method)
def test_timeout(free_port):

    from inmanta.config import Config
    import inmanta.agent.config  # nopep8
    import inmanta.server.config  # nopep8

    io_loop = IOLoop.current()

    # start server
    Config.load_config()
    Config.set("server_rest_transport", "port", free_port)
    Config.set("agent_rest_transport", "port", free_port)
    Config.set("compiler_rest_transport", "port", free_port)
    Config.set("client_rest_transport", "port", free_port)
    Config.set("cmdline_rest_transport", "port", free_port)
    server = Server("server", io_loop, interval=2)
    server.start()

    env = uuid.uuid4()

    # agent 1
    agent = Agent("agent", io_loop)
    agent.add_end_point_name("agent")
    agent.set_environment(env)
    agent.start()

    @gen.coroutine
    def do_call():
        # wait till up
        yield retry_limited(lambda: len(server._sessions) == 1, 0.1)
        assert len(server._sessions) == 1

        # agent 2
        agent2 = Agent("agent", io_loop)
        agent2.add_end_point_name("agent")
        agent2.set_environment(env)
        agent2.start()

        # wait till up
        yield retry_limited(lambda: len(server._sessions) == 2, 0.1)
        assert len(server._sessions) == 2

        # see if it stays up
        yield(check_sessions(server._sessions.values()))
        yield sleep(2)
        assert len(server._sessions) == 2
        yield(check_sessions(server._sessions.values()))

        # take it down
        agent2.stop()

        # timout
        yield sleep(2)
        # check if down
        assert len(server._sessions) == 1
        print(server._sessions)
        yield(check_sessions(server._sessions.values()))
        assert server.expires == 1
        agent.stop()
        server.stop()

        io_loop.stop()

    io_loop.add_callback(do_call)
    io_loop.add_timeout(io_loop.time() + 2, lambda: io_loop.stop())
    try:
        io_loop.start()
    except KeyboardInterrupt:
        io_loop.stop()
    server.stop()
    agent.stop()
Exemple #16
0
def main():
    Config.load_config()
    cmd()
def test_2way_protocol(free_port, logs=False):

    from inmanta.config import Config

    import inmanta.agent.config  # nopep8
    import inmanta.server.config  # nopep8

    if logs:
        # set logging to sensible defaults
        formatter = colorlog.ColoredFormatter(
            "%(log_color)s%(levelname)-8s%(reset)s %(green)s%(name)s %(blue)s%(message)s",
            datefmt=None,
            reset=True,
            log_colors={
                'DEBUG': 'cyan',
                'INFO': 'green',
                'WARNING': 'yellow',
                'ERROR': 'red',
                'CRITICAL': 'red',
            }
        )

        stream = logging.StreamHandler()
        stream.setLevel(logging.DEBUG)

        if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
            stream.setFormatter(formatter)

        logging.root.handlers = []
        logging.root.addHandler(stream)
        logging.root.setLevel(logging.DEBUG)

    Config.load_config()
    Config.set("server_rest_transport", "port", free_port)
    Config.set("agent_rest_transport", "port", free_port)
    Config.set("compiler_rest_transport", "port", free_port)
    Config.set("client_rest_transport", "port", free_port)
    Config.set("cmdline_rest_transport", "port", free_port)

    io_loop = IOLoop.current()
    server = Server("server", io_loop)
    server.start()

    agent = Agent("agent", io_loop)
    agent.add_end_point_name("agent")
    agent.set_environment(uuid.uuid4())
    agent.start()

    @gen.coroutine
    def do_call():
        client = protocol.Client("client")
        status = yield client.get_status_x(str(agent.environment))
        assert status.code == 200
        assert "agents" in status.result
        assert len(status.result["agents"]) == 1
        assert status.result["agents"][0]["status"], "ok"
        server.stop()
        io_loop.stop()

    io_loop.add_callback(do_call)
    io_loop.add_timeout(io_loop.time() + 2, lambda: io_loop.stop())
    try:
        io_loop.start()
    except KeyboardInterrupt:
        io_loop.stop()
    server.stop()
    agent.stop()
Exemple #18
0
def main() -> None:
    Config.load_config()
    cmd()
Exemple #19
0
async def test_bind_port(unused_tcp_port, async_finalizer, client, caplog):
    @protocol.method(path="/test", operation="POST", client_types=["api"])
    async def test_endpoint():
        pass

    class TestSlice(ServerSlice):
        @protocol.handle(test_endpoint)
        async def test_endpoint_handle(self):
            return 200

    async def assert_port_bound():
        # Start server
        rs = Server()
        rs.add_slice(TestSlice("test"))
        await rs.start()
        async_finalizer(rs.stop)

        # Check if server is reachable on loopback interface
        result = await client.test_endpoint()
        assert result.code == 200
        await rs.stop()

    deprecation_line_log_line = (
        "The server_rest_transport.port config option is deprecated in favour of the "
        "server.bind-port option.")
    ignoring_log_line = (
        "Ignoring the server_rest_transport.port config option since the new config options "
        "server.bind-port/server.bind-address are used.")

    # Old config option server_rest_transport.port is set
    Config.load_config()
    Config.set("server_rest_transport", "port", str(unused_tcp_port))
    Config.set("client_rest_transport", "port", str(unused_tcp_port))
    caplog.clear()
    await assert_port_bound()
    log_sequence = LogSequence(caplog, allow_errors=False)
    log_sequence.contains("py.warnings", logging.WARNING,
                          deprecation_line_log_line)
    log_sequence.assert_not("py.warnings", logging.WARNING, ignoring_log_line)

    # Old config option server_rest_transport.port and new config option server.bind-port are set together
    Config.load_config()
    Config.set("server_rest_transport", "port", str(unused_tcp_port))
    Config.set("server", "bind-port", str(unused_tcp_port))
    Config.set("client_rest_transport", "port", str(unused_tcp_port))
    caplog.clear()
    await assert_port_bound()
    log_sequence = LogSequence(caplog, allow_errors=False)
    log_sequence.assert_not("py.warnings", logging.WARNING,
                            deprecation_line_log_line)
    log_sequence.contains("py.warnings", logging.WARNING, ignoring_log_line)

    # The new config option server.bind-port is set
    Config.load_config()
    Config.set("server", "bind-port", str(unused_tcp_port))
    Config.set("client_rest_transport", "port", str(unused_tcp_port))
    caplog.clear()
    await assert_port_bound()
    log_sequence = LogSequence(caplog, allow_errors=False)
    log_sequence.assert_not("py.warnings", logging.WARNING,
                            deprecation_line_log_line)
    log_sequence.assert_not("py.warnings", logging.WARNING, ignoring_log_line)
Exemple #20
0
def configure(unused_tcp_port, database_name, database_port):
    import inmanta.agent.config  # noqa: F401
    import inmanta.server.config  # noqa: F401
    from inmanta.config import Config

    free_port = str(unused_tcp_port)
    Config.load_config()
    Config.set("server", "bind-port", free_port)
    Config.set("agent_rest_transport", "port", free_port)
    Config.set("compiler_rest_transport", "port", free_port)
    Config.set("client_rest_transport", "port", free_port)
    Config.set("cmdline_rest_transport", "port", free_port)
    Config.set("database", "name", database_name)
    Config.set("database", "host", "localhost")
    Config.set("database", "port", str(database_port))
Exemple #21
0
async def test_export(tmpvenv_active_inherit: env.VirtualEnv, tmpdir, server, client, push_method, set_server, set_port):
    server_port = Config.get("client_rest_transport", "port")
    server_host = Config.get("client_rest_transport", "host", "localhost")

    result = await client.create_project("test")
    assert result.code == 200
    proj_id = result.result["project"]["id"]
    result = await client.create_environment(proj_id, "test", None, None)
    assert result.code == 200
    env_id = result.result["environment"]["id"]

    workspace = tmpdir.mkdir("tmp")
    path_main_file = workspace.join("main.cf")
    path_project_yml_file = workspace.join("project.yml")
    path_config_file = workspace.join(".inmanta")
    libs_dir = workspace.join("libs")

    path_project_yml_file.write(
        f"""
name: testproject
modulepath: {libs_dir}
downloadpath: {libs_dir}
repo: https://github.com/inmanta/
"""
    )

    path_main_file.write(
        """
vm1=std::Host(name="non-existing-machine", os=std::linux)
std::ConfigFile(host=vm1, path="/test", content="")
"""
    )

    path_config_file.write(
        f"""
[compiler_rest_transport]
{'host=' + server_host if not set_server else ''}
{'port=' + str(server_port) if not set_port else ''}

[cmdline_rest_transport]
{'host=' + server_host if not set_server else ''}
{'port=' + str(server_port) if not set_port else ''}
"""
    )

    await install_project(tmpvenv_active_inherit, workspace)

    os.chdir(workspace)

    args = [
        sys.executable,
        "-m",
        "inmanta.app",
        "export",
        "-e",
        str(env_id),
    ]
    if set_port:
        args.extend(["--server_port", str(server_port)])
    if set_server:
        args.extend(["--server_address", str(server_host)])
    args += push_method

    process = await subprocess.create_subprocess_exec(*args, stdout=sys.stdout, stderr=sys.stderr)
    try:
        await asyncio.wait_for(process.communicate(), timeout=30)
    except asyncio.TimeoutError as e:
        process.kill()
        await process.communicate()
        raise e

    # Make sure exitcode is zero
    assert process.returncode == 0, f"Process ended with bad return code, got {process.returncode} (expected 0)"

    result = await client.list_versions(env_id)
    assert result.code == 200
    assert len(result.result["versions"]) == 1

    details_exported_version = result.result["versions"][0]

    assert details_exported_version["result"] == VersionState.deploying.name

    shutil.rmtree(workspace)
Exemple #22
0
from tornado import httpclient
import logging
from subprocess import CalledProcessError
from time import sleep
from tornado.escape import json_decode
from tornado.httpclient import HTTPRequest

import os

LOGGER = logging.getLogger(__name__)

##################
# Pre setup
##################

Config.load_config()

stream = logging.StreamHandler()
stream.setLevel(logging.INFO)

logging.root.handlers = []
logging.root.addHandler(stream)
logging.root.setLevel(logging.DEBUG)


def get_os_credentials():
    keytoenv = {
        "connection_url": "OS_AUTH_URL",
        "password": "******",
        "username": "******",
        "tenant": "OS_TENANT_NAME"