Example #1
0
 def _refresh_impl_locked(self):
     """Same as _refresh_impl, but assumes relevant files are locked."""
     try:
         with open(self._store_file, 'r') as fp:
             return host_info.json_deserialize(fp)
     except IOError as e:
         if e.errno == errno.ENOENT:
             raise host_info.StoreError(
                     'No backing file. You must commit to the store before '
                     'trying to read a value from it.')
         raise host_info.StoreError('Failed to read backing file (%s) : %r'
                                    % (self._store_file, e))
     except host_info.DeserializationError as e:
         raise host_info.StoreError(
                 'Failed to desrialize backing file %s: %r' %
                 (self._store_file, e))
Example #2
0
    def _refresh_impl(self):
        """Obtains HostInfo directly from the AFE."""
        try:
            hosts = self._afe.get_hosts(hostname=self._hostname)
        except rpc_proxy.JSONRPCException as e:
            raise host_info.StoreError(e)

        if not hosts:
            raise host_info.StoreError('No hosts founds with hostname: %s' %
                                       self._hostname)

        if len(hosts) > 1:
            logging.warning(
                'Found %d hosts with the name %s. Picking the first one.',
                len(hosts), self._hostname)
        host = hosts[0]
        return host_info.HostInfo(host.labels, host.attributes)
Example #3
0
 def _commit_impl_locked(self, info):
     """Same as _commit_impl, but assumes relevant files are locked."""
     try:
         with open(self._store_file, 'w') as fp:
             host_info.json_serialize(info, fp)
     except IOError as e:
         raise host_info.StoreError('Failed to write backing file (%s) : %r'
                                    % (self._store_file, e))
Example #4
0
    def _add_labels_on_afe(self, labels):
        """Requests the AFE to add the given labels.

        @param labels: Add these.
        """
        if not labels:
            return

        logging.info('adding labels: %s', labels)
        try:
            self._afe.run('host_add_labels', id=self._hostname, labels=labels)
        except rpc_proxy.JSONRPCException as e:
            raise host_info.StoreError(e)
Example #5
0
    def _lock_backing_file(self):
        """Context to lock the backing store file.

        @raises StoreError if the backing file can not be locked.
        """
        def _retry_locking_failures(exc):
            return isinstance(exc, locking.LockNotAcquiredError)

        try:
            retry_util.GenericRetry(
                    handler=_retry_locking_failures,
                    functor=self._lock.write_lock,
                    max_retry=self._lock_max_retry,
                    sleep=self._lock_sleep)
        # If self._lock fails to write the locking file, it'll leak an OSError
        except (locking.LockNotAcquiredError, OSError) as e:
            raise host_info.StoreError(e)

        with self._lock:
            yield
 def _commit_impl(self, _):
     if self.commit_raises:
         raise host_info.StoreError('wont wont wont')
 def _refresh_impl(self):
     if self.refresh_raises:
         raise host_info.StoreError('no can do')
     return host_info.HostInfo()
Example #8
0
 def _commit_impl(self, info):
     print('commit_impl: %s' % info)
     if self._raise_on_commit:
         raise host_info.StoreError('Test commit error')
     super(_FakeRaisingStore, self)._commit_impl(info)
Example #9
0
 def _refresh_impl(self):
     print('refresh_impl')
     if self._raise_on_refresh:
         raise host_info.StoreError('Test refresh error')
     return super(_FakeRaisingStore, self)._refresh_impl()