def _remove_pod_dir(pod_dir_path): LOG.info('remove pod directory: %s', pod_dir_path) _umount_overlay(pod_dir_path) with locks.acquiring_shared(images.get_trees_path()): for ref_image_id in _iter_ref_image_ids(pod_dir_path): images.touch(ref_image_id) shutil.rmtree(pod_dir_path)
def cmd_list(): # Don't need root privilege here. with locks.acquiring_shared(_get_tags_path()), \ locks.acquiring_shared(get_trees_path()): for image_dir_path, metadata in _iter_metadatas(): image_id = _get_id(image_dir_path) last_updated = _get_last_updated(image_dir_path) yield { 'id': image_id, 'name': metadata.name, 'version': metadata.version, 'tags': _find_tags(image_id), 'ref-count': _get_ref_count(image_dir_path), 'last-updated': last_updated, 'rootfs': get_rootfs_path(image_dir_path), }
def cmd_add_ref(pod_id, target_path): oses.assert_root_privilege() with locks.acquiring_shared(_get_active_path()): _add_ref( ASSERT.predicate(_get_pod_dir_path(pod_id), Path.is_dir), ASSERT.not_predicate(target_path, g1.files.lexists), ) return 0
def listing_ops_dirs(self): """Return a list of ops dir objects. NOTE: This only locks the active dir, and does NOT locks each ops dir. """ with locks.acquiring_shared(self.active_dir_path): yield self._list_ops_dirs()
def _locking_top_dirs(*, read_only=False): if read_only: acquiring = locks.acquiring_shared else: acquiring = locks.acquiring_exclusive with acquiring(_get_xars_repo_path()): with locks.acquiring_shared(images.get_trees_path()): yield
def cmd_tag(*, image_id=None, name=None, version=None, tag=None, new_tag): oses.assert_root_privilege() with locks.acquiring_exclusive(_get_tags_path()): with locks.acquiring_shared(get_trees_path()): image_dir_path = ASSERT.not_none( _find_image_dir_path(image_id, name, version, tag) ) _tag_image(new_tag, image_dir_path)
def using_ops_dir(self, label, version): ops_dir_path = self._get_ops_dir_path(label, version) with locks.acquiring_shared(self.active_dir_path): ASSERT.predicate(ops_dir_path, Path.is_dir) ops_dir_lock = locks.acquire_exclusive(ops_dir_path) try: yield self.ops_dir_type(ops_dir_path) finally: ops_dir_lock.release() ops_dir_lock.close()
def test_acquiring_shared(self): lock_path = self.test_dir_path / 'lock' lock_path.touch() self.assertTrue(self.check_shared(lock_path)) self.assertTrue(self.check_exclusive(lock_path)) with locks.acquiring_shared(lock_path): self.assertTrue(self.check_shared(lock_path)) self.assertFalse(self.check_exclusive(lock_path)) self.assertTrue(self.check_shared(lock_path)) self.assertTrue(self.check_exclusive(lock_path))
def _add_ref_image_ids(pod_dir_path, config): deps_path = _get_deps_path(pod_dir_path) with locks.acquiring_shared(images.get_trees_path()): # Replace pod config with resolved image IDs because tags may # change over time. new_images = [] for image_id in _iter_image_ids(config): images.add_ref(image_id, deps_path / image_id) new_images.append(models.PodConfig.Image(id=image_id)) new_config = dataclasses.replace(config, images=new_images) _pod_dir_create_config(pod_dir_path, new_config) return new_config
def cmd_show(pod_id): # Don't need root privilege here. with locks.acquiring_shared(_get_active_path()): pod_dir_path = ASSERT.predicate(_get_pod_dir_path(pod_id), Path.is_dir) config = _read_config(pod_dir_path) pod_status = _get_pod_status(pod_dir_path, config) return [{ 'name': app.name, 'status': pod_status.get(app.name, (None, None))[0], 'last-updated': pod_status.get(app.name, (None, None))[1], 'ref-count': _get_ref_count(pod_dir_path), } for app in config.apps]
def cmd_list(): # Don't need root privilege here. with locks.acquiring_shared(_get_active_path()): for pod_dir_path, config in _iter_configs(): pod_status = _get_pod_status(pod_dir_path, config) yield { 'id': _get_id(pod_dir_path), 'name': config.name, 'version': config.version, # Use _iter_image_ids rather than _iter_ref_image_ids # for ordered results. 'images': list(_iter_image_ids(config)), 'active': locks.is_locked_by_other(pod_dir_path), 'last-updated': _get_last_updated(pod_status), 'ref-count': _get_ref_count(pod_dir_path), }
def cmd_exec(xar_name, xar_args): # Don't need root privilege here. with locks.acquiring_shared(_get_xars_repo_path()): xar_dir_path = ASSERT.predicate(_get_xar_dir_path(xar_name), Path.is_dir) exec_abspath = ASSERT.predicate(_get_exec_path(xar_dir_path), Path.exists).resolve() lock = locks.FileLock( _get_ref_path(xar_dir_path, _get_image_id(exec_abspath)), close_on_exec=False, ) lock.acquire_shared() # TODO: Or should argv[0] be exec_abspath.name? argv = [xar_name] argv.extend(xar_args) LOG.debug('exec: path=%s, argv=%s', exec_abspath, argv) os.execv(str(exec_abspath), argv)
def get(self): with locks.acquiring_shared(self.path): return Tokens.load(self.path)