From 3b8b8b54850f6aee870ec6a08000d08819ad4e81 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Mon, 1 Dec 2014 15:25:21 +0000 Subject: writeexts.py: convert 'mount' to context manager --- morphlib/exts/openstack.write | 6 +--- morphlib/exts/rawdisk.write | 65 ++++++++++++++++++++----------------------- morphlib/writeexts.py | 65 ++++++++++++++++++------------------------- 3 files changed, 58 insertions(+), 78 deletions(-) diff --git a/morphlib/exts/openstack.write b/morphlib/exts/openstack.write index 516fe367..b1941d3c 100755 --- a/morphlib/exts/openstack.write +++ b/morphlib/exts/openstack.write @@ -79,8 +79,7 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): def set_extlinux_root_to_virtio(self, raw_disk): '''Re-configures extlinux to use virtio disks''' self.status(msg='Updating extlinux.conf') - mp = self.mount(raw_disk) - try: + with self.mount(raw_disk) as mp: path = os.path.join(mp, 'extlinux.conf') with open(path) as f: @@ -91,9 +90,6 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): with open(path, "w") as f: f.write(extlinux_conf) - finally: - self.unmount(mp) - def get_openstack_parameters(self): '''Get the environment variables needed. diff --git a/morphlib/exts/rawdisk.write b/morphlib/exts/rawdisk.write index e1a75fe0..b17f8aa7 100755 --- a/morphlib/exts/rawdisk.write +++ b/morphlib/exts/rawdisk.write @@ -67,51 +67,46 @@ class RawDiskWriteExtension(morphlib.writeexts.WriteExtension): def upgrade_local_system(self, raw_disk, temp_root): self.complete_fstab_for_btrfs_layout(temp_root) - mp = self.mount(raw_disk) + with self.mount(raw_disk) as mp: + version_label = self.get_version_label(mp) + self.status(msg='Updating image to a new version with label %s' % + version_label) + + version_root = os.path.join(mp, 'systems', version_label) + os.mkdir(version_root) + + old_orig = os.path.join(mp, 'systems', 'default', 'orig') + new_orig = os.path.join(version_root, 'orig') + cliapp.runcmd( + ['btrfs', 'subvolume', 'snapshot', old_orig, new_orig]) + + cliapp.runcmd( + ['rsync', '-a', '--checksum', '--numeric-ids', '--delete', + temp_root + os.path.sep, new_orig]) + + self.create_run(version_root) + + default_path = os.path.join(mp, 'systems', 'default') + if os.path.exists(default_path): + os.remove(default_path) + else: + # we are upgrading and old system that does + # not have an updated extlinux config file + if self.bootloader_config_is_wanted(): + self.generate_bootloader_config(mp) + self.install_bootloader(mp) + os.symlink(version_label, default_path) - version_label = self.get_version_label(mp) - self.status(msg='Updating image to a new version with label %s' % - version_label) - - version_root = os.path.join(mp, 'systems', version_label) - os.mkdir(version_root) - - old_orig = os.path.join(mp, 'systems', 'default', 'orig') - new_orig = os.path.join(version_root, 'orig') - cliapp.runcmd( - ['btrfs', 'subvolume', 'snapshot', old_orig, new_orig]) - - cliapp.runcmd( - ['rsync', '-a', '--checksum', '--numeric-ids', '--delete', - temp_root + os.path.sep, new_orig]) - - self.create_run(version_root) - - default_path = os.path.join(mp, 'systems', 'default') - if os.path.exists(default_path): - os.remove(default_path) - else: - # we are upgrading and old system that does - # not have an updated extlinux config file if self.bootloader_config_is_wanted(): - self.generate_bootloader_config(mp) - self.install_bootloader(mp) - os.symlink(version_label, default_path) - - if self.bootloader_config_is_wanted(): - self.install_kernel(version_root, temp_root) - - self.unmount(mp) + self.install_kernel(version_root, temp_root) def get_version_label(self, mp): version_label = os.environ.get('VERSION_LABEL') if version_label is None: - self.unmount(mp) raise cliapp.AppException('VERSION_LABEL was not given') if os.path.exists(os.path.join(mp, 'systems', version_label)): - self.unmount(mp) raise cliapp.AppException('VERSION_LABEL %s already exists' % version_label) diff --git a/morphlib/writeexts.py b/morphlib/writeexts.py index 7030c0b5..91936f64 100644 --- a/morphlib/writeexts.py +++ b/morphlib/writeexts.py @@ -173,17 +173,14 @@ class WriteExtension(cliapp.Application): raise def create_system(self, temp_root, raw_disk): - try: - mp = self.mount(raw_disk) - self.create_btrfs_system_layout( - temp_root, mp, version_label='factory', - disk_uuid=self.get_uuid(raw_disk)) - except BaseException, e: - sys.stderr.write('Error creating Btrfs system layout') - self.unmount(mp) - raise - else: - self.unmount(mp) + with self.mount(raw_disk) as mp: + try: + self.create_btrfs_system_layout( + temp_root, mp, version_label='factory', + disk_uuid=self.get_uuid(raw_disk)) + except BaseException, e: + sys.stderr.write('Error creating Btrfs system layout') + raise def _parse_size(self, size): '''Parse a size from a string. @@ -249,34 +246,26 @@ class WriteExtension(cliapp.Application): # lies by exiting successfully. return cliapp.runcmd(['blkid', '-s', 'UUID', '-o', 'value', location]).strip() - - def mount(self, location): - '''Mount the filesystem so it can be tweaked. - - Return path to the mount point. - The mount point is a newly created temporary directory. - The caller must call self.unmount to unmount on the return value. - - ''' - self.status(msg='Mounting filesystem') - tempdir = tempfile.mkdtemp() - if self.is_device(location): - cliapp.runcmd(['mount', location, tempdir]) - else: - cliapp.runcmd(['mount', '-o', 'loop', location, tempdir]) - return tempdir - - def unmount(self, mount_point): - '''Unmount the filesystem mounted by self.mount. - - Also, remove the temporary directory. - - ''' - - self.status(msg='Unmounting filesystem') - cliapp.runcmd(['umount', mount_point]) - os.rmdir(mount_point) + @contextlib.contextmanager + def mount(self, location): + self.status(msg='Mounting filesystem') + try: + mount_point = tempfile.mkdtemp() + if self.is_device(location): + cliapp.runcmd(['mount', location, mount_point]) + else: + cliapp.runcmd(['mount', '-o', 'loop', location, mount_point]) + except BaseException, e: + sys.stderr.write('Error mounting filesystem') + os.rmdir(mount_point) + raise + try: + yield mount_point + finally: + self.status(msg='Unmounting filesystem') + cliapp.runcmd(['umount', mount_point]) + os.rmdir(mount_point) def create_btrfs_system_layout(self, temp_root, mountpoint, version_label, disk_uuid): -- cgit v1.2.1