Example #1
0
    def delete(self, name):
        """Deactivate and removes the workspace."""

        if not self.has_workspace(name):
            raise exceptions.IRWorkspaceMissing(workspace=name)
        else:
            if self.is_active(name):
                os.remove(self.active_file)
            shutil.rmtree(os.path.join(self.workspace_dir, name))
Example #2
0
    def _get_inventory(self, workspace_name=None):
        """Returns Inventory object for the provided workspace.
           Use active workspace as default

           :param workspace_name: workspace name to list nodes from.
        """
        workspace = self.get(
            workspace_name) if workspace_name else self.get_active_workspace()

        if workspace is None:
            if workspace_name is None:
                raise exceptions.IRNoActiveWorkspaceFound()
            else:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)

        return InventoryManager(DataLoader(), sources=workspace.inventory)
Example #3
0
    def activate(self, name):
        """Activates the workspace."""

        if os.environ.get(ACTIVE_WORKSPACE_ENV_NAME):
            raise exceptions.IRException(
                message="'workspace checkout' command is disabled while "
                "{} environment variable is set.".format(
                    ACTIVE_WORKSPACE_ENV_NAME))

        if self.has_workspace(name):
            with open(self.active_file, 'w') as prf_file:
                prf_file.write(name)
            LOG.debug("Activating workspace %s in %s", name,
                      os.path.join(self.workspace_dir, name))
        else:
            raise exceptions.IRWorkspaceMissing(workspace=name)
Example #4
0
    def delete(self, name, keep_active_workspace_file=False):
        """Deactivates and removes the workspace.

        :param name: Name of the workspace to delete
        :param keep_active_workspace_file: Whether to keep the active
        workspace file or not (used in workspace cleanup)
        """

        if not self.has_workspace(name):
            raise exceptions.IRWorkspaceMissing(workspace=name)

        if os.path.islink(self.active_path) and not \
                keep_active_workspace_file:
            if self.get_active_workspace().name == name:
                os.remove(self.active_path)

        shutil.rmtree(os.path.join(self.workspace_dir, name))
Example #5
0
    def delete(self, name, keep_active_workspace_file=False):
        """Deactivates and removes the workspace.

        :param name: Name of the workspace to delete
        :param keep_active_workspace_file: Whether to keep the active
        workspace file or not (used in workspace cleanup)
        """

        if not self.has_workspace(name):
            raise exceptions.IRWorkspaceMissing(workspace=name)

        if os.path.isfile(self.active_file) and not keep_active_workspace_file:
            with open(self.active_file) as fp:
                f_active_workspace = fp.read().strip()
            if f_active_workspace == name:
                os.remove(self.active_file)

        shutil.rmtree(os.path.join(self.workspace_dir, name))
Example #6
0
    def _get_inventory(self, workspace_name=None):
        """Returns Inventory object for the provided workspace.
           Use active workspace as default

           :param workspace_name: workspace name to list nodes from.
        """
        workspace = self.get(
            workspace_name) if workspace_name else self.get_active_workspace()

        if workspace is None:
            if workspace_name is None:
                raise exceptions.IRNoActiveWorkspaceFound()
            else:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)

        # need to have import here to avoid ansible patching
        from ansible.inventory.manager import InventoryManager
        from ansible.parsing.dataloader import DataLoader

        return InventoryManager(DataLoader(), sources=workspace.inventory)
Example #7
0
    def activate(self, name):
        """Activates the workspace.

        :param name: Name of the workspace to activate
        """

        if os.environ.get(ACTIVE_WORKSPACE_ENV_NAME):
            raise exceptions.IRException(
                message="'workspace checkout' command is disabled while "
                "{} environment variable is set.".format(
                    ACTIVE_WORKSPACE_ENV_NAME))

        if name == self.active_basename:
            raise exceptions.IRWorkspaceInvalidName(workspace=name)

        if self.has_workspace(name):
            self._create_active_path(name)
            LOG.debug("Activating workspace %s in %s", name,
                      os.path.join(self.workspace_dir, name))
        else:
            raise exceptions.IRWorkspaceMissing(workspace=name)
Example #8
0
    def export_workspace(self, workspace_name, file_name=None):
        """Export content of workspace folder as gzipped tar file

           Replaces existing .tgz file
        """

        if workspace_name:
            workspace = self.get(workspace_name)
            if workspace is None:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)
        else:
            workspace = self.get_active_workspace()
            if workspace is None:
                raise exceptions.IRNoActiveWorkspaceFound()

        fname = file_name or workspace.name

        with tarfile.open(fname + '.tgz', "w:gz") as tar:
            tar.add(workspace.path, arcname="./")

        print("Workspace {} is exported to file {}.tgz".format(
            workspace.name, fname))
Example #9
0
    def export_workspace(self, workspace_name, file_name=None, copykeys=False):
        """Export content of workspace folder as gzipped tar file

        Replaces existing .tgz file
        """

        if workspace_name:
            workspace = self.get(workspace_name)
            if workspace is None:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)
        else:
            workspace = self.get_active_workspace()
            if workspace is None:
                raise exceptions.IRNoActiveWorkspaceFound()

        fname = file_name or workspace.name

        # Copy workspace to not damage original,
        tmpdir = tempfile.mkdtemp()
        tmp_workspace_path = os.path.join(tmpdir,
                                          os.path.basename(workspace.path))

        try:
            shutil.copytree(workspace.path, tmp_workspace_path,
                            symlinks=True)
            tmp_workspace = Workspace(name=workspace.name,
                                      path=tmp_workspace_path)
            tmp_workspace._update_paths(workspace.path,
                                        workspace.path_placeholder)
            if copykeys:
                tmp_workspace._copy_outside_keys()
            with tarfile.open(fname + '.tgz', "w:gz") as tar:
                tar.add(tmp_workspace.path, arcname="./")
        finally:
            shutil.rmtree(tmpdir)

        print("Workspace {} is exported to file {}.tgz".format(workspace.name,
                                                               fname))
Example #10
0
    def node_list(self, workspace_name=None):
        """Lists nodes and connection types from workspace's inventory

           nodes with connection type 'local' are skipped
           :param workspace_name: workspace name to list nodes from.
                                Use active workspace as default
        """

        workspace = self.get(
            workspace_name) if workspace_name else self.get_active_workspace()

        if workspace is None:
            if workspace_name is None:
                raise exceptions.IRNoActiveWorkspaceFound()
            else:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)

        invent = inventory.Inventory(DataLoader(),
                                     VariableManager(),
                                     host_list=workspace.inventory)
        hosts = invent.get_hosts()
        return [(host.name, host.address) for host in hosts
                if host.vars.get("ansible_connection") != "local"]