Example #1
0
    def __init__(self, statefile):
        """Open, lock, read and parse the file.

    @type statefile: file
    @param statefile: State file object

    """
        self.statefile = statefile

        try:
            state_data = self.statefile.read()
            if not state_data:
                self._data = {}
            else:
                self._data = serializer.Load(state_data)
        except Exception as msg:  # pylint: disable=W0703
            # Ignore errors while loading the file and treat it as empty
            self._data = {}
            logging.warning(("Invalid state file. Using defaults."
                             " Error message: %s"), msg)

        if "instance" not in self._data:
            self._data["instance"] = {}
        if "node" not in self._data:
            self._data["node"] = {}

        self._orig_data = serializer.Dump(self._data)
Example #2
0
    def _ValidateResult(self):
        """Process the allocator results.

    This will process and if successful save the result in
    self.out_data and the other parameters.

    """
        try:
            rdict = serializer.Load(self.out_text)
        except Exception as err:
            raise errors.OpExecError("Can't parse iallocator results: %s" %
                                     str(err))

        if not isinstance(rdict, dict):
            raise errors.OpExecError(
                "Can't parse iallocator results: not a dict")

        # TODO: remove backwards compatiblity in later versions
        if "nodes" in rdict and "result" not in rdict:
            rdict["result"] = rdict["nodes"]
            del rdict["nodes"]

        for key in "success", "info", "result":
            if key not in rdict:
                raise errors.OpExecError("Can't parse iallocator results:"
                                         " missing key '%s'" % key)
            setattr(self, key, rdict[key])

        self.req.ValidateResult(self, self.result)
        self.out_data = rdict
  def _ValidateResult(self):
    """Process the allocator results.

    This will process and if successful save the result in
    self.out_data and the other parameters.

    """
    try:
      rdict = serializer.Load(self.out_text)
    except Exception, err:
      raise errors.OpExecError("Can't parse iallocator results: %s" % str(err))
Example #4
0
    def _LoadInstanceStash(self, instance_name):
        """Load information stashed in file which was created by
    L{_SaveInstanceStash}.

    """
        stash_file = self._InstanceStashFilePath(instance_name)
        try:
            return serializer.Load(utils.ReadFile(stash_file))
        except (EnvironmentError, ValueError) as err:
            raise HypervisorError(
                "Failed to load instance stash file %s : %s" %
                (stash_file, err))