Exemple #1
0
    def delete(self, ignore_not_found=True, cmd_args=None):
        """
        :param ignore_not_found: If True, named resources which are not present will not raise an error.
        :param base_args: Additional delete arguments
        :param wait_for: Include an `oc wait --for=delete ...` for each resource deleted
        :param cmd_args: An optional list of additional arguments to pass on the command line
        :return: Returns a list of qualified object names which were deleted.
        """
        names = self.qnames()

        r = Result("delete")

        if len(names) == 0:
            return []

        base_args = list()
        if ignore_not_found:
            base_args.append("--ignore-not-found")
        base_args.append("-o=name")

        r.add_action(
            oc_action(self.context,
                      "delete",
                      all_namespaces=self.all_namespaces,
                      cmd_args=[
                          self._selection_args(needs_all=True), base_args,
                          cmd_args
                      ]))

        r.fail_if("Error deleting objects")
        return split_names(r.out())
Exemple #2
0
    def annotate(self, annotations, overwrite=True, cmd_args=None):
        """
        Applies a set of annotations to selected objects.
        :param annotations: A dictionary of annotations to apply.
        :param overwrite: If true, any existing annotations will be overwritten. If false, existing annotations will cause
        a failure.
        :param cmd_args: An optional list of additional arguments to pass on the command line
        """

        r = Result("annotate")
        base_args = list()

        if overwrite:
            base_args.append("--overwrite")

        for l, v in six.iteritems(annotations):
            if not v:
                if not l.endswith("-"):
                    l += "-"  # Indicate removal on command line if caller has not applied "-" suffix
                base_args.append(l)
            else:
                base_args.append('{}={}'.format(l, v))

        r.add_action(
            oc_action(self.context,
                      "annotate",
                      all_namespaces=self.all_namespaces,
                      cmd_args=[
                          self._selection_args(needs_all=True), base_args,
                          cmd_args
                      ]))

        r.fail_if("Error running annotate")
        return self
Exemple #3
0
    def scale(self, replicas, cmd_args=None):
        r = Result("scale")
        base_args = list()
        base_args.append('--scale={}'.format(replicas))
        r.add_action(
            oc_action(self.context,
                      "scale",
                      all_namespaces=self.all_namespaces,
                      cmd_args=[
                          self._selection_args(needs_all=False), base_args,
                          cmd_args
                      ]))

        r.fail_if("Error running scale")
        return self
Exemple #4
0
    def start_build(self, cmd_args=None):
        r = Selector()

        # Have start-build output a list of objects it creates
        base_args = list()
        base_args.append("-o=name")

        for name in self.qnames():
            r.add_action(
                oc_action(self.context,
                          "start-build",
                          cmd_args=[name, base_args, cmd_args]))

        r.fail_if("Error running start-build on at least one item: " +
                  str(self.qnames()))
        r.object_list = split_names(r.out())
        return r
Exemple #5
0
    def describe(self, auto_raise=True, cmd_args=None):
        """
        Runs oc describe against the selected objects and returns the string which results.
        :param auto_raise: If True, an exception will be raised if an error occurs. If False,
        the returned string will contain stderr.
        :param cmd_args: An optional list of additional arguments to pass on the command line
        :return: A string containing the oc describe output.
        """
        r = Result("describe")
        r.add_action(
            oc_action(self.context,
                      "describe",
                      all_namespaces=self.all_namespaces,
                      cmd_args=[self._selection_args(), cmd_args]))
        if auto_raise:
            r.fail_if('Error during describe')

        return (r.out() + "\n" + r.err()).strip()
Exemple #6
0
    def object_json(self, exportable=False, ignore_not_found=False):
        """
        Returns all objects selected by the receiver as a JSON string. If multiple objects are
        selected, an OpenShift List kind will be returned.
        :param exportable: Set to True if the export verb should be used.
        :param ignore_not_found: If True, no error will result if receiver tries to select objects which are not present
        :return: Returns all selected objects marshalled as an OpenShift JSON representation.
        """

        # If the selector is static and empty return an empty list object
        if self.object_list is not None and len(self.object_list) == 0:
            return json.dumps({
                "apiVersion": "v1",
                "kind": "List",
                "metadata": {},
                "items": []
            })

        verb = "export" if exportable else "get"

        cmd_args = ["-o=json", self._selection_args()]

        if ignore_not_found:
            cmd_args.append("--ignore-not-found")

        r = Result(verb)
        r.add_action(
            oc_action(self.context,
                      verb,
                      all_namespaces=self.all_namespaces,
                      cmd_args=cmd_args))
        r.fail_if("Unable to read object")

        # --ignore-not-found returns an empty string instead of an error if nothing is found
        if not r.out().strip():
            return json.dumps({
                "apiVersion": "v1",
                "kind": "List",
                "metadata": {},
                "items": []
            })

        return r.out()
Exemple #7
0
    def _query_names(self):
        """
        Invokes oc to query for current objects selected by this selector.
        :return: Returns a list of qualified names (list may be empty).
        """

        result = Result("query_names")
        result.add_action(
            oc_action(self.context,
                      'get',
                      all_namespaces=self.all_namespaces,
                      cmd_args=['-o=name', self._selection_args()]))

        # TODO: This check is necessary until --ignore-not-found is implemented and prevalent
        if result.status() != 0 and "(NotFound)" in result.err():
            return []

        # Otherwise, errors are fatal
        result.fail_if("Unable to retrieve object names")
        return split_names(result.out())
Exemple #8
0
 def raw_action(self, verb, *args, **kwargs):
     return oc_action(self.context,
                      verb,
                      all_namespaces=self.all_namespaces,
                      cmd_args=[self._selection_args(), args],
                      **kwargs)