Exemplo n.º 1
0
    def test_try_acquire_exclusive(self):
        lock_path = self.test_dir_path / 'lock'
        lock_path.touch()
        lock = locks.try_acquire_exclusive(lock_path)
        try:
            self.assertIsNotNone(lock)
        finally:
            lock.release()
            lock.close()

        with self.using_shared(lock_path):
            self.assertIsNone(locks.try_acquire_exclusive(lock_path))
Exemplo n.º 2
0
def _cleanup_active(expiration):
    LOG.info('remove pods before: %s', expiration)
    with locks.acquiring_exclusive(_get_active_path()):
        for pod_dir_path, config in _iter_configs():
            pod_id = _get_id(pod_dir_path)
            if _get_ref_count(pod_dir_path) > 1:
                LOG.debug('pod is still referenced: %s', pod_id)
                continue
            pod_dir_lock = locks.try_acquire_exclusive(pod_dir_path)
            if not pod_dir_lock:
                LOG.debug('pod is still active: %s', pod_id)
                continue
            try:
                pod_status = _get_pod_status(pod_dir_path, config)
                last_updated = _get_last_updated(pod_status)
                if last_updated is None:
                    # Prevent cleaning up just-prepared pod directory.
                    last_updated = datetimes.utcfromtimestamp(
                        _get_config_path(pod_dir_path).stat().st_mtime
                    )
                if last_updated < expiration:
                    with locks.acquiring_exclusive(_get_graveyard_path()):
                        LOG.info('clean up pod: %s', pod_id)
                        _move_pod_dir_to_graveyard(pod_dir_path)
                    journals.remove_journal_dir(pod_id)
            finally:
                pod_dir_lock.release()
                pod_dir_lock.close()
Exemplo n.º 3
0
def cmd_export_overlay(pod_id, output_path, filter_patterns, *, debug=False):
    oses.assert_root_privilege()
    ASSERT.not_predicate(output_path, g1.files.lexists)
    # Exclude pod-generated files.
    # TODO: Right now we hard-code the list, but this is fragile.
    filter_args = [
        '--exclude=/etc/machine-id',
        '--exclude=/var/lib/dbus/machine-id',
        '--exclude=/etc/hostname',
        '--exclude=/etc/hosts',
        '--exclude=/etc/systemd',
        '--exclude=/etc/.pwd.lock',
        '--exclude=/etc/mtab',
        # Remove distro unit files.
        '--exclude=/etc/systemd/system',
        '--exclude=/lib/systemd/system',
        '--exclude=/usr/lib/systemd/system',
    ]
    filter_args.extend('--%s=%s' % pair for pair in filter_patterns)
    if debug:
        # Log which files are included/excluded due to filter rules.
        filter_args.append('--debug=FILTER2')
    with locks.acquiring_exclusive(_get_active_path()):
        pod_dir_path = ASSERT.predicate(_get_pod_dir_path(pod_id), Path.is_dir)
        pod_dir_lock = ASSERT.true(locks.try_acquire_exclusive(pod_dir_path))
    try:
        upper_path = _get_upper_path(pod_dir_path)
        bases.rsync_copy(upper_path, output_path, filter_args)
    finally:
        pod_dir_lock.release()
        pod_dir_lock.close()
Exemplo n.º 4
0
def _cleanup_top_dir(top_dir_path):
    for path in top_dir_path.iterdir():
        if not path.is_dir():
            LOG.info('remove unknown file: %s', path)
            path.unlink()
            continue
        lock = locks.try_acquire_exclusive(path)
        if not lock:
            continue
        try:
            _remove_pod_dir(path)
        finally:
            lock.release()
            lock.close()
Exemplo n.º 5
0
def _cleanup_tmp():
    for tmp_path in _get_tmp_path().iterdir():
        if not tmp_path.is_dir():
            LOG.info('remove unknown temporary file: %s', tmp_path)
            tmp_path.unlink()
            continue
        tmp_lock = locks.try_acquire_exclusive(tmp_path)
        if not tmp_lock:
            continue
        try:
            LOG.info('remove temporary directory: %s', tmp_path)
            shutil.rmtree(tmp_path)
        finally:
            tmp_lock.release()
            tmp_lock.close()
Exemplo n.º 6
0
 def _cleanup_top_dir(self, top_dir_path):
     for ops_dir_path in top_dir_path.iterdir():
         log_args = (self.kind, ops_dir_path)
         if not ops_dir_path.is_dir():
             LOG.warning('%s cleanup: %s; reason: unknown file', *log_args)
             ops_dir_path.unlink()
             continue
         ops_dir_lock = locks.try_acquire_exclusive(ops_dir_path)
         if not ops_dir_lock:
             LOG.info('skip: %s cleanup: %s', *log_args)
             continue
         try:
             LOG.info('%s cleanup: %s', *log_args)
             self._remove_ops_dir(ops_dir_path)
         finally:
             ops_dir_lock.release()
             ops_dir_lock.close()