Example #1
0
    def _command_return(self, uuid, returncode, message=None):
        self.log.debug('returning code %s (uuid=%s)', str(returncode), uuid)
        response = json.dumps(dict(returncode=returncode, message=message))
        self.xen_store.write(XenStore.join(XenStore.GUEST_DATA, uuid),
                             response)

        self.log.debug('removing command from queue (uuid=%s)', uuid)
        self.xen_store.remove(XenStore.join(XenStore.HOST_DATA, uuid))
Example #2
0
    def __iter__(self):
        try:
            uuid_list = self.xen_store.dir(XenStore.HOST_DATA)
        except XenStoreError as error:
            if error.returncode == 1:
                return
            else:
                raise

        self.log.debug('got %d commands pending', len(uuid_list))

        for uuid in uuid_list:
            if uuid in self.pending:
                self.log.debug('ignoring (uuid=%s), pending already', uuid)
                continue

            self.log.debug('reading (uuid=%s)', uuid)
            try:
                data = self.xen_store.read(
                    XenStore.join(XenStore.HOST_DATA, uuid))
            except XenStoreError as error:
                self.log.error('error reading (uuid=%s): %s', UUID, str(error))
                continue

            self.log.debug(repr(data))
            try:
                data = json.loads(''.join(s.rstrip()
                                          for s in data.splitlines()))
            except Exception as error:
                self.log.error('error decoding (uuid=%s): %s',
                               uuid,
                               str(error),
                               exc_info=True)
                continue

            try:
                class_name = data.pop('name')
            except KeyError:
                self.log.error('invalid object, name missing (uuid=%s)', uuid)
                continue

            try:
                value = data.pop('value')
            except KeyError:
                self.log.error('invalid object, value missing (uuid=%s)', uuid)
                continue

            try:
                command_class = Command._subclasses[class_name]
            except KeyError:
                self.log.warning('unsupported: %s (uuid=%s)', class_name, uuid)
                self._command_return(
                    uuid, Command.RET_UNSUPPORTED,
                    'unsupported command: {0}'.format(class_name))
                continue

            try:
                command = command_class(self, uuid, value)
            except Exception as error:
                self.log.exception()
                continue

            self.pending[command.uuid] = command
            yield command