Beispiel #1
0
    def load_from_path(cls, username, project_path):
        """
        Load user session from path.

        :param username:        Username (str)
        :param project_path:    Project path (str)
        """
        session = cls(username, project_path)

        # Ensure config paths exists
        project_root = session.get_paths().get_project_root()
        if not os.path.exists(project_root):
            os.makedirs(project_root)
        user_root = session.get_paths().get_user_root()
        if not os.path.exists(user_root):
            os.makedirs(user_root)

        session_file = session.get_paths().get_user_session_file_path()
        if not os.path.exists(session_file):
            session.session_data = {"current": None}
        else:
            with io_open(session_file, mode="r") as handle:
                session.session_data = yaml_ordered_load(handle.read())

        return session
Beispiel #2
0
def test_schema_error():
    """Schema."""
    schema_def = """\
standard:
    volumes:
        - portainer
    services:
        - portainer
    networks:
        - net

standard2:
    includes:
        - standard3
    services:
        - pouet
    networks:
        - net

hello:
    services:
        - hello-world\
    """

    with pytest.raises(MalformedSchema):
        # Should error
        SchemaCollection.load_from_data(yaml_ordered_load(schema_def))
Beispiel #3
0
def test_old_database():
    """Test old database."""
    with using_temporary_directory() as tempdir:
        project_path = copy_sample("sample03", tempdir)

        # Values key should be present
        db_file = os.path.join(project_path, ".docknv", ".docknv.yml")
        with io_open(db_file) as handle:
            data = yaml_ordered_load(handle.read())
            assert "values" in data

        project = Project.load_from_path(project_path)
        assert len(project.database) == 2

        with io_open(db_file) as handle:
            data = yaml_ordered_load(handle.read())
            assert "values" not in data
Beispiel #4
0
    def load_from_path(cls, path):
        """
        Load from path.

        :param path:    Path (str)
        """
        if not os.path.isfile(path):
            raise MissingComposefile(path)

        with io_open(path, mode="r") as handle:
            content = yaml_ordered_load(handle.read())

        return cls(content)
Beispiel #5
0
def renderer_render_compose_template(compose_content, environment_data=None):
    """
    Resolve compose content.

    :param compose_content:      Compose content (dict)
    :param environment_data:     Environment data (dict?) (default: None)
    :rtype: Template data (dict)
    """
    output_content = copy.deepcopy(compose_content)

    template_result = renderer_render_template_inplace(output_content,
                                                       environment_data)

    return yaml_ordered_load(template_result)
Beispiel #6
0
def test_schema():
    """Schema."""
    schema_def = """\
standard:
    volumes:
        - portainer
    services:
        - portainer
    networks:
        - net

standard2:
    includes:
        - standard
    services:
        - pouet
    networks:
        - net

hello:
    services:
        - hello-world\
    """

    # Load schemas
    schemas = SchemaCollection.load_from_data(yaml_ordered_load(schema_def))
    assert len(schemas) == 3

    # First schema
    first_schema = schemas.get_schema("standard2")
    assert first_schema.name == "standard2"
    assert len(first_schema.services) == 2
    assert len(first_schema.networks) == 1
    assert len(first_schema.volumes) == 1
    assert "portainer" in first_schema.services
    assert "pouet" in first_schema.services
    assert "net" in first_schema.networks
    assert "portainer" in first_schema.volumes

    # Should error
    with pytest.raises(MissingSchema):
        schemas.get_schema("standard3")

    assert schemas.includes_schema("standard")
    assert not schemas.includes_schema("standard3")

    # Show schemas
    schemas.show()
Beispiel #7
0
    def load_from_project(cls, project_path):
        """
        Load from project.

        :param project_path:    Project path (str)
        """
        paths = composefile_get_composefile_paths(project_path)
        composefile_content = {}

        for path in paths:
            with io_open(path, mode="r") as handle:
                content = yaml_ordered_load(handle.read())
                composefile_content = yaml_merge(
                    [composefile_content, content])

        return cls(composefile_content)
Beispiel #8
0
    def load_from_project(cls, project):
        """
        Load from project.

        :param project:    Project
        """
        project_path = project.project_path
        project_file_path = database_get_database_path(project_path)

        if os.path.isfile(project_file_path):
            with io_open(project_file_path, encoding="utf-8",
                         mode="r") as handle:
                config_data = yaml_ordered_load(handle.read())
            return cls(project, config_data)

        return cls(project, {})
Beispiel #9
0
    def load_from_path(cls, project_path):
        """
        Load from project path.

        :param project_path:    Project path
        """
        project_file_path = project_get_config_path(project_path)

        if os.path.isfile(project_file_path):
            with io_open(project_file_path, encoding="utf-8",
                         mode="r") as handle:
                config_data = yaml_ordered_load(handle.read())
        else:
            raise MissingProject(project_path)

        return cls(project_path, config_data)
Beispiel #10
0
def env_set_env_value(env_path, key, value):
    """
    Set environment value from file at `env_path`.

    :param env_path:    Environment path (str)
    :param key:         Key (str)
    :param value:       Value (any)
    """
    with io_open(env_path, mode="r") as handle:
        content = yaml_ordered_load(handle)

    if content["environment"] is None:
        content["environment"] = {}
    content["environment"][key] = value

    with io_open(env_path, mode="w") as handle:
        handle.write(yaml_ordered_dump(content))
Beispiel #11
0
def test_load():
    """Test ordered_load function."""
    content = """
one:
    two: [a, b, c]
    three: 1
    four:
        - a
        - b
        - c
two:
    - pouet
aaa:
"""

    loaded = yaml_ordered_load(content)
    keys = list(loaded.keys())

    assert keys[0] == "one"
    assert keys[1] == "two"
Beispiel #12
0
        def _load(project_path, name):
            path = env_get_yaml_path(project_path, name)
            if not os.path.isfile(path):
                raise MissingEnvironment(name)

            loaded_env = OrderedDict()
            with io_open(path, mode="r") as handle:
                env_content = yaml_ordered_load(handle.read())

            # Detect imports
            imports = []
            if "imports" in env_content:
                for entry in env_content["imports"]:
                    imports.append(entry)

            for imported_env in imports:
                # Ignore self-import
                if imported_env == name:
                    continue
                # Ignore already imported envs
                if imported_env in imported:
                    continue

                # Save imported
                imported.add(imported_env)

                # Load imported environments
                result = _load(project_path, imported_env)
                for key, value in result.data.items():
                    loaded_env[key] = value

            # Load environment
            if env_content.get("environment", None):
                for key in env_content["environment"]:
                    loaded_env[key] = env_content["environment"][key]

            return Environment(name, loaded_env)