]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume util.system add a context manager for temporary mounting/unmounting
authorAlfredo Deza <adeza@redhat.com>
Fri, 3 Nov 2017 18:34:06 +0000 (14:34 -0400)
committerAlfredo Deza <adeza@redhat.com>
Mon, 13 Nov 2017 15:20:09 +0000 (10:20 -0500)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
(cherry picked from commit 6394cdd41943e3cda6867ecc59ab835cb2151707)

src/ceph-volume/ceph_volume/util/system.py

index f8213c6d1541598a9b1337463b67b474cb3a41cc..940b9dbdc5d371535d6bef3e26d6cd92e802b70c 100644 (file)
@@ -2,6 +2,7 @@ import errno
 import os
 import pwd
 import platform
+import tempfile
 import uuid
 from ceph_volume import process
 from . import as_string
@@ -68,6 +69,36 @@ def chown(path, recursive=True):
         os.chown(path, uid, gid)
 
 
+class tmp_mount(object):
+    """
+    Temporarily mount a device on a temporary directory,
+    and unmount it upon exit
+    """
+
+    def __init__(self, device):
+        self.device = device
+        self.path = None
+
+    def __enter__(self):
+        self.path = tempfile.mkdtemp()
+        process.run([
+            'sudo',
+            'mount',
+            '-v',
+            self.device,
+            self.path
+        ])
+        return self.path
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        process.run([
+            'sudo',
+            'umount',
+            '-v',
+            self.path
+        ])
+
+
 def path_is_mounted(path, destination=None):
     """
     Check if the given path is mounted