Example #1
0
    def _remove_old_enough_file(self, base_file, maxage, remove_lock=True):
        """Remove a single swap or base file if it is old enough."""
        exists, age = self._get_age_of_file(base_file)
        if not exists:
            return

        lock_file = os.path.split(base_file)[-1]

        @utils.synchronized(lock_file, external=True,
                            lock_path=self.lock_path)
        def _inner_remove_old_enough_file():
            # NOTE(mikal): recheck that the file is old enough, as a new
            # user of the file might have come along while we were waiting
            # for the lock
            exists, age = self._get_age_of_file(base_file)
            if not exists or age < maxage:
                return

            LOG.info(_LI('Removing base or swap file: %s'), base_file)
            try:
                os.remove(base_file)

                # TODO(mdbooth): We have removed all uses of info files in
                # Newton and we no longer create them, but they may still
                # exist from before we upgraded, and they may still be
                # created by older compute hosts if we're on shared storage.
                # While there may still be pre-Newton computes writing here,
                # the only safe place to delete info files is here,
                # when deleting the cache entry. Once we can be sure that
                # all computes are running at least Newton (i.e. in Ocata),
                # we can delete these files unconditionally during the
                # periodic task, which will make this code obsolete.
                signature = get_info_filename(base_file)
                if os.path.exists(signature):
                    os.remove(signature)
            except OSError as e:
                LOG.error(_LE('Failed to remove %(base_file)s, '
                              'error was %(error)s'),
                          {'base_file': base_file,
                           'error': e})

        if age < maxage:
            LOG.info(_LI('Base or swap file too young to remove: %s'),
                         base_file)
        else:
            _inner_remove_old_enough_file()
            if remove_lock:
                try:
                    # NOTE(jichenjc) The lock file will be constructed first
                    # time the image file was accessed. the lock file looks
                    # like nova-9e881789030568a317fad9daae82c5b1c65e0d4a
                    # or nova-03d8e206-6500-4d91-b47d-ee74897f9b4e
                    # according to the original file name
                    lockutils.remove_external_lock_file(lock_file,
                        lock_file_prefix='nova-', lock_path=self.lock_path)
                except OSError as e:
                    LOG.debug('Failed to remove %(lock_file)s, '
                              'error was %(error)s',
                              {'lock_file': lock_file,
                               'error': e})
Example #2
0
    def _remove_old_enough_file(self, base_file, maxage, remove_lock=True):
        """Remove a single swap or base file if it is old enough."""
        exists, age = self._get_age_of_file(base_file)
        if not exists:
            return

        lock_file = os.path.split(base_file)[-1]

        @utils.synchronized(lock_file, external=True,
                            lock_path=self.lock_path)
        def _inner_remove_old_enough_file():
            # NOTE(mikal): recheck that the file is old enough, as a new
            # user of the file might have come along while we were waiting
            # for the lock
            exists, age = self._get_age_of_file(base_file)
            if not exists or age < maxage:
                return

            LOG.info(_LI('Removing base or swap file: %s'), base_file)
            try:
                os.remove(base_file)

                # TODO(mdbooth): We have removed all uses of info files in
                # Newton and we no longer create them, but they may still
                # exist from before we upgraded, and they may still be
                # created by older compute hosts if we're on shared storage.
                # While there may still be pre-Newton computes writing here,
                # the only safe place to delete info files is here,
                # when deleting the cache entry. Once we can be sure that
                # all computes are running at least Newton (i.e. in Ocata),
                # we can delete these files unconditionally during the
                # periodic task, which will make this code obsolete.
                signature = get_info_filename(base_file)
                if os.path.exists(signature):
                    os.remove(signature)
            except OSError as e:
                LOG.error(_LE('Failed to remove %(base_file)s, '
                              'error was %(error)s'),
                          {'base_file': base_file,
                           'error': e})

        if age < maxage:
            LOG.info(_LI('Base or swap file too young to remove: %s'),
                         base_file)
        else:
            _inner_remove_old_enough_file()
            if remove_lock:
                try:
                    # NOTE(jichenjc) The lock file will be constructed first
                    # time the image file was accessed. the lock file looks
                    # like nova-9e881789030568a317fad9daae82c5b1c65e0d4a
                    # or nova-03d8e206-6500-4d91-b47d-ee74897f9b4e
                    # according to the original file name
                    lockutils.remove_external_lock_file(lock_file,
                        lock_file_prefix='nova-', lock_path=self.lock_path)
                except OSError as e:
                    LOG.debug('Failed to remove %(lock_file)s, '
                              'error was %(error)s',
                              {'lock_file': lock_file,
                               'error': e})
Example #3
0
    def _remove_old_enough_file(self,
                                base_file,
                                maxage,
                                remove_sig=True,
                                remove_lock=True):
        """Remove a single swap or base file if it is old enough."""
        exists, age = self._get_age_of_file(base_file)
        if not exists:
            return

        lock_file = os.path.split(base_file)[-1]

        @utils.synchronized(lock_file, external=True, lock_path=self.lock_path)
        def _inner_remove_old_enough_file():
            # NOTE(mikal): recheck that the file is old enough, as a new
            # user of the file might have come along while we were waiting
            # for the lock
            exists, age = self._get_age_of_file(base_file)
            if not exists or age < maxage:
                return

            LOG.info(_LI('Removing base or swap file: %s'), base_file)
            try:
                os.remove(base_file)
                if remove_sig:
                    signature = get_info_filename(base_file)
                    if os.path.exists(signature):
                        os.remove(signature)
            except OSError as e:
                LOG.error(
                    _LE('Failed to remove %(base_file)s, '
                        'error was %(error)s'), {
                            'base_file': base_file,
                            'error': e
                        })

        if age < maxage:
            LOG.info(_LI('Base or swap file too young to remove: %s'),
                     base_file)
        else:
            _inner_remove_old_enough_file()
            if remove_lock:
                try:
                    # NOTE(jichenjc) The lock file will be constructed first
                    # time the image file was accessed. the lock file looks
                    # like nova-9e881789030568a317fad9daae82c5b1c65e0d4a
                    # or nova-03d8e206-6500-4d91-b47d-ee74897f9b4e
                    # according to the original file name
                    lockutils.remove_external_lock_file(
                        lock_file,
                        lock_file_prefix='nova-',
                        lock_path=self.lock_path)
                except OSError as e:
                    LOG.debug(
                        'Failed to remove %(lock_file)s, '
                        'error was %(error)s', {
                            'lock_file': lock_file,
                            'error': e
                        })
 def test_remove_lock_external_file_permission_error(
         self, path_mock, remove_mock, log_mock):
     lockutils.remove_external_lock_file(mock.sentinel.name,
                                         mock.sentinel.prefix,
                                         mock.sentinel.lock_path)
     path_mock.assert_called_once_with(mock.sentinel.name,
                                       mock.sentinel.prefix,
                                       mock.sentinel.lock_path)
     remove_mock.assert_called_once_with(path_mock.return_value)
     log_mock.assert_called()
    def _test_remove_lock_external_file(self, lock_dir, use_external=False):
        lock_name = 'mylock'
        lock_pfix = 'mypfix-remove-lock-test-'

        if use_external:
            lock_path = lock_dir
        else:
            lock_path = None

        lockutils.remove_external_lock_file(lock_name, lock_pfix, lock_path)

        for ent in os.listdir(lock_dir):
            self.assertRaises(OSError, ent.startswith, lock_pfix)
Example #6
0
    def _remove_old_enough_file(self, base_file, maxage, remove_sig=True,
                                remove_lock=True):
        """Remove a single swap or base file if it is old enough."""
        exists, age = self._get_age_of_file(base_file)
        if not exists:
            return

        lock_file = os.path.split(base_file)[-1]

        @utils.synchronized(lock_file, external=True,
                            lock_path=self.lock_path)
        def _inner_remove_old_enough_file():
            # NOTE(mikal): recheck that the file is old enough, as a new
            # user of the file might have come along while we were waiting
            # for the lock
            exists, age = self._get_age_of_file(base_file)
            if not exists or age < maxage:
                return

            LOG.info(_LI('Removing base or swap file: %s'), base_file)
            try:
                os.remove(base_file)
                if remove_sig:
                    signature = get_info_filename(base_file)
                    if os.path.exists(signature):
                        os.remove(signature)
            except OSError as e:
                LOG.error(_LE('Failed to remove %(base_file)s, '
                              'error was %(error)s'),
                          {'base_file': base_file,
                           'error': e})

        if age < maxage:
            LOG.info(_LI('Base or swap file too young to remove: %s'),
                         base_file)
        else:
            _inner_remove_old_enough_file()
            if remove_lock:
                try:
                    # NOTE(jichenjc) The lock file will be constructed first
                    # time the image file was accessed. the lock file looks
                    # like nova-9e881789030568a317fad9daae82c5b1c65e0d4a
                    # or nova-03d8e206-6500-4d91-b47d-ee74897f9b4e
                    # according to the original file name
                    lockutils.remove_external_lock_file(lock_file,
                        lock_file_prefix='nova-', lock_path=self.lock_path)
                except OSError as e:
                    LOG.debug('Failed to remove %(lock_file)s, '
                              'error was %(error)s',
                              {'lock_file': lock_file,
                               'error': e})
Example #7
0
    def _remove_old_enough_file(self,
                                base_file,
                                maxage,
                                remove_sig=True,
                                remove_lock=True):
        """Remove a single swap or base file if it is old enough."""
        exists, age = self._get_age_of_file(base_file)
        if not exists:
            return

        if age < maxage:
            LOG.info(_LI('Base or swap file too young to remove: %s'),
                     base_file)
        else:
            LOG.info(_LI('Removing base or swap file: %s'), base_file)
            try:
                os.remove(base_file)
                if remove_sig:
                    signature = get_info_filename(base_file)
                    if os.path.exists(signature):
                        os.remove(signature)
            except OSError as e:
                LOG.error(
                    _LE('Failed to remove %(base_file)s, '
                        'error was %(error)s'), {
                            'base_file': base_file,
                            'error': e
                        })

            if remove_lock:
                try:
                    # NOTE(jichenjc) The lock file will be constructed first
                    # time the image file was accessed. the lock file looks
                    # like patron-9e881789030568a317fad9daae82c5b1c65e0d4a
                    # or patron-03d8e206-6500-4d91-b47d-ee74897f9b4e
                    # according to the original file name
                    lock_file = os.path.split(base_file)[-1]
                    lockutils.remove_external_lock_file(
                        lock_file,
                        lock_file_prefix='patron-',
                        lock_path=self.lock_path)
                except OSError as e:
                    LOG.debug(
                        'Failed to remove %(lock_file)s, '
                        'error was %(error)s', {
                            'lock_file': lock_file,
                            'error': e
                        })
    def _test_remove_lock_external_file(self, lock_dir, use_external=False):
        lock_name = 'mylock'
        lock_pfix = 'mypfix-remove-lock-test-'

        if use_external:
            lock_path = lock_dir
        else:
            lock_path = None

        lockutils.remove_external_lock_file(lock_name, lock_pfix, lock_path)

        for ent in os.listdir(lock_dir):
            self.assertRaises(OSError, ent.startswith, lock_pfix)

        if os.path.exists(lock_dir):
            shutil.rmtree(lock_dir, ignore_errors=True)
Example #9
0
    def _remove_old_enough_file(self, base_file, maxage, remove_sig=True,
                                remove_lock=True):
        """Remove a single swap or base file if it is old enough."""
        exists, age = self._get_age_of_file(base_file)
        if not exists:
            return

        if age < maxage:
            LOG.info(_LI('Base or swap file too young to remove: %s'),
                         base_file)
        else:
            LOG.info(_LI('Removing base or swap file: %s'), base_file)
            try:
                os.remove(base_file)
                if remove_sig:
                    signature = get_info_filename(base_file)
                    if os.path.exists(signature):
                        os.remove(signature)
            except OSError as e:
                LOG.error(_LE('Failed to remove %(base_file)s, '
                              'error was %(error)s'),
                          {'base_file': base_file,
                           'error': e})

            if remove_lock:
                try:
                    # NOTE(jichenjc) The lock file will be constructed first
                    # time the image file was accessed. the lock file looks
                    # like patron-9e881789030568a317fad9daae82c5b1c65e0d4a
                    # or patron-03d8e206-6500-4d91-b47d-ee74897f9b4e
                    # according to the original file name
                    lock_file = os.path.split(base_file)[-1]
                    lockutils.remove_external_lock_file(lock_file,
                        lock_file_prefix='patron-', lock_path=self.lock_path)
                except OSError as e:
                    LOG.debug('Failed to remove %(lock_file)s, '
                              'error was %(error)s',
                              {'lock_file': lock_file,
                               'error': e})