Esempio n. 1
0
    def get_servers_for_playbook(self, playbook_id, suggested_servers,
                                 cluster_model):
        plugs = plugins.get_public_playbook_plugins()
        if playbook_id not in plugs:
            raise http_exceptions.UnknownPlaybookError(playbook_id)

        plug = plugs[playbook_id]()
        if plug.REQUIRED_SERVER_LIST:
            if not suggested_servers:
                raise http_exceptions.ServerListIsRequiredForPlaybookError(
                    playbook_id
                )

            servers = server.ServerModel.find_by_model_id(*suggested_servers)
            if not isinstance(servers, list):
                servers = [servers]
            if len(servers) != len(set(suggested_servers)):
                raise ValueError(
                    "All suggested servers were not found. "
                    "Suggested servers were {0}".format(suggested_servers))

            deleted_servers = [srv for srv in servers if srv.time_deleted]
            if deleted_servers:
                raise ValueError(
                    "Some servers were deleted: {0}".format(
                        ", ".join(srv.model_id for srv in deleted_servers)))
            return servers

        return cluster_model.server_list
Esempio n. 2
0
    def make_configuration(self, cluster, servers, hints):
        plug = plugins.get_public_playbook_plugins()
        plug = plug[self.playbook_id]()
        configuration = plug.build_playbook_configuration(
            cluster, servers, hints)

        return configuration
Esempio n. 3
0
    def make_configuration(self, cluster, servers, hints):
        plug = plugins.get_public_playbook_plugins()
        plug = plug[self.playbook_id]()
        configuration = plug.build_playbook_configuration(
            cluster, servers, hints
        )

        return configuration
Esempio n. 4
0
    def post(self):
        cluster_model = self.get_cluster_model(self.request_json["cluster_id"])

        plugs = plugins.get_public_playbook_plugins()
        if self.request_json["playbook_id"] not in plugs:
            raise http_exceptions.UnknownPlaybookError(
                self.request_json["playbook_id"])
        plug = plugs[self.request_json["playbook_id"]]()

        servers_for_playbook = self.get_servers_for_playbook(
            plug,
            self.request_json["playbook_id"],
            self.request_json["server_ids"],
            cluster_model
        )
        try:
            plug.SERVER_LIST_POLICY.check(cluster_model, servers_for_playbook)
        except ValueError as exc:
            raise http_exceptions.ServerListPolicyViolation(
                self.request_json["playbook_id"],
                plug.SERVER_LIST_POLICY,
                exc
            )

        try:
            pcmodel = playbook_configuration.PlaybookConfigurationModel.create(
                name=self.request_json["name"],
                playbook_id=self.request_json["playbook_id"],
                cluster=cluster_model,
                servers=servers_for_playbook,
                initiator_id=self.initiator_id,
                hints=self.request_json["hints"]
            )
        except base_exceptions.UniqueConstraintViolationError as exc:
            LOG.warning(
                "Cannot create cluster %s (unique constraint "
                "violation)",
                self.request_json["name"]
            )
            raise http_exceptions.ImpossibleToCreateSuchModel() from exc
        except base_exceptions.ClusterMustBeDeployedError as exc:
            mid = cluster_model.model_id
            LOG.warning(
                "Attempt to create playbook configuration for not "
                "deployed cluster %s", mid)
            raise http_exceptions.ClusterMustBeDeployedError(mid) from exc

        LOG.info("Playbook configuration %s (%s) created by %s",
                 self.request_json["name"], pcmodel.model_id,
                 self.initiator_id)

        return pcmodel
Esempio n. 5
0
    def post(self):
        cluster_model = self.get_cluster_model(self.request_json["cluster_id"])

        plugs = plugins.get_public_playbook_plugins()
        if self.request_json["playbook_id"] not in plugs:
            raise http_exceptions.UnknownPlaybookError(
                self.request_json["playbook_id"])
        plug = plugs[self.request_json["playbook_id"]]()

        servers_for_playbook = self.get_servers_for_playbook(
            plug,
            self.request_json["playbook_id"],
            self.request_json["server_ids"],
            cluster_model
        )
        try:
            plug.SERVER_LIST_POLICY.check(cluster_model, servers_for_playbook)
        except ValueError as exc:
            raise http_exceptions.ServerListPolicyViolation(
                self.request_json["playbook_id"],
                plug.SERVER_LIST_POLICY,
                exc
            )

        run_after = self.request_json.get("run", False)
        parameters = {
            "name": self.request_json["name"],
            "playbook_id": self.request_json["playbook_id"],
            "cluster_model": cluster_model,
            "servers": servers_for_playbook,
            "initiator_id": self.initiator_id,
            "hints": self.request_json["hints"]
        }
        with misc.created_playbook_configuration_model(
                **parameters, delete_on_fail=run_after) as model:
            response = model.make_api_structure()
            response["data"]["created_execution_id"] = None

            if run_after:
                with misc.created_execution_model(
                        model, self.initiator_id) as exc_model:
                    response["data"]["created_execution_id"] = \
                        exc_model.model_id

            return response
Esempio n. 6
0
    def get(self):
        data = []

        for plugin in plugins.get_public_playbook_plugins().values():
            plug = plugin()

            hints = []
            if hasattr(plug.HINTS, "make_api_structure"):
                hints = plug.HINTS

            plugin_data = {
                "name": plug.name,
                "id": plug.entry_point,
                "description": plug.DESCRIPTION,
                "required_server_list": bool(plug.REQUIRED_SERVER_LIST),
                "hints": hints
            }
            data.append(plugin_data)

        return {"items": sorted(data, key=lambda elem: elem["name"])}
Esempio n. 7
0
    def get(self):
        data = []

        for plugin in plugins.get_public_playbook_plugins().values():
            plug = plugin()

            hints = []
            if hasattr(plug.HINTS, "make_api_structure"):
                hints = plug.HINTS

            plugin_data = {
                "name": plug.name,
                "id": plug.entry_point,
                "description": plug.DESCRIPTION,
                "required_server_list": bool(plug.REQUIRED_SERVER_LIST),
                "server_list_policy": plug.SERVER_LIST_POLICY.name,
                "hints": hints
            }
            data.append(plugin_data)

        return {"items": sorted(data, key=lambda elem: elem["name"])}
Esempio n. 8
0
    def post(self):
        cluster_model = self.get_cluster_model(self.request_json["cluster_id"])

        plugs = plugins.get_public_playbook_plugins()
        if self.request_json["playbook_id"] not in plugs:
            raise http_exceptions.UnknownPlaybookError(
                self.request_json["playbook_id"])
        plug = plugs[self.request_json["playbook_id"]]()

        servers_for_playbook = self.get_servers_for_playbook(
            plug, self.request_json["playbook_id"],
            self.request_json["server_ids"], cluster_model)
        try:
            plug.SERVER_LIST_POLICY.check(cluster_model, servers_for_playbook)
        except ValueError as exc:
            raise http_exceptions.ServerListPolicyViolation(
                self.request_json["playbook_id"], plug.SERVER_LIST_POLICY, exc)

        run_after = self.request_json.get("run", False)
        parameters = {
            "name": self.request_json["name"],
            "playbook_id": self.request_json["playbook_id"],
            "cluster_model": cluster_model,
            "servers": servers_for_playbook,
            "initiator_id": self.initiator_id,
            "hints": self.request_json["hints"]
        }
        with misc.created_playbook_configuration_model(
                **parameters, delete_on_fail=run_after) as model:
            response = model.make_api_structure()
            response["data"]["created_execution_id"] = None

            if run_after:
                with misc.created_execution_model(
                        model, self.initiator_id) as exc_model:
                    response["data"]["created_execution_id"] = \
                        exc_model.model_id

            return response
Esempio n. 9
0
def compose_commandline(path, playbook_config):
    destpath = path.joinpath("execute.sh")
    faketask = task.PlaybookPluginTask(
        playbook_config.playbook_id, playbook_config._id, None)

    plugin = plugins.get_public_playbook_plugins()[playbook_config.playbook_id]
    plugin = plugin()
    plugin.compose_command(faketask)
    proc = plugin.proc

    proc.env = {}
    proc.options["--inventory-file"] = "inventory.yaml"
    extras = json.loads(proc.options["--extra-vars"])
    extras["decapod_common_playbooks"] = "../common_playbooks"
    extras["fetch_directory"] = "fetch_directory"
    extras = patch_plugin_paths(extras, plugin)
    proc.options["--extra-vars"] = process.jsonify(extras)
    proc.command = "ansible-playbook"
    proc.args = [
        path.joinpath("plugin", plugin.playbook_filename)
        .relative_to(path).as_posix()
    ]

    shell_script = """\
    #!/bin/bash
    set +e
    cd "$(dirname "$0")"

    {0}

    cd - >/dev/null 2>&1
    """.format(proc.printable_commandline)
    shell_script = textwrap.dedent(shell_script)

    destpath.write_text(shell_script)
    destpath.chmod(0o755)
Esempio n. 10
0
def compose_commandline(path, playbook_config):
    destpath = path.joinpath("execute.sh")
    faketask = task.PlaybookPluginTask(playbook_config.playbook_id,
                                       playbook_config._id, None)

    plugin = plugins.get_public_playbook_plugins()[playbook_config.playbook_id]
    plugin = plugin()
    plugin.compose_command(faketask)
    proc = plugin.proc

    proc.env = {}
    proc.options["--inventory-file"] = "inventory.yaml"
    extras = json.loads(proc.options["--extra-vars"])
    extras["decapod_common_playbooks"] = "../common_playbooks"
    extras["fetch_directory"] = "fetch_directory"
    extras = patch_plugin_paths(extras, plugin)
    proc.options["--extra-vars"] = process.jsonify(extras)
    proc.command = "ansible-playbook"
    proc.args = [
        path.joinpath("plugin",
                      plugin.playbook_filename).relative_to(path).as_posix()
    ]

    shell_script = """\
    #!/bin/bash
    set +e
    cd "$(dirname "$0")"

    {0}

    cd - >/dev/null 2>&1
    """.format(proc.printable_commandline)
    shell_script = textwrap.dedent(shell_script)

    destpath.write_text(shell_script)
    destpath.chmod(0o755)
def get_playbook_plug(public_playbook_name):
    plug = plugins.get_public_playbook_plugins()
    plug = plug[public_playbook_name]

    return plug
Esempio n. 12
0
                    "Unknown permission value {0} for class {1}".format(
                        v, key))

        self.permissions[key] = set(value)

    def __getitem__(self, key):
        return self.permissions[key]

    def make_api_structure(self, *args, **kwargs):
        return [{
            "name": k,
            "permissions": sorted(v)
        } for k, v in self.permissions.items()]


for plugin in plugins.get_public_playbook_plugins():
    PermissionSet.add_permission("playbook", plugin)


class RoleModel(generic.Model):
    """This is a model for the role.

    Role is a model which has a list of permissions for
    user in Decapod.
    """

    MODEL_NAME = "role"
    COLLECTION_NAME = "role"
    DEFAULT_SORT_BY = [("name", generic.SORT_ASC)]

    def __init__(self):
Esempio n. 13
0
            else:
                valid_values.append(v)

        self.permissions[key] = set(valid_values)

    def __getitem__(self, key):
        return self.permissions[key]

    def make_api_structure(self, *args, **kwargs):
        return [
            {"name": k, "permissions": sorted(v)}
            for k, v in self.permissions.items()
        ]


for plugin in plugins.get_public_playbook_plugins():
    PermissionSet.add_permission("playbook", plugin)


class RoleModel(generic.Model):
    """This is a model for the role.

    Role is a model which has a list of permissions for
    user in Decapod.
    """

    MODEL_NAME = "role"
    COLLECTION_NAME = "role"
    DEFAULT_SORT_BY = [("name", generic.SORT_ASC)]

    def __init__(self):