]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk: Use context manager with FileLock
authorAnirudha Bose <ani07nov@gmail.com>
Sat, 18 Jun 2016 23:40:27 +0000 (05:10 +0530)
committerAnirudha Bose <ani07nov@gmail.com>
Wed, 17 Aug 2016 07:34:32 +0000 (13:04 +0530)
acquire and release methods of FileLock are dropped

Signed-off-by: Anirudha Bose <ani07nov@gmail.com>
src/ceph-disk/ceph_disk/main.py

index e65a24a8b82c5334d93a7a259eb611cda3b85853..fbd11db09d9e2cd911f24ac5910e931e9e336748 100755 (executable)
@@ -250,12 +250,12 @@ class FileLock(object):
         self.fn = fn
         self.fd = None
 
-    def acquire(self):
+    def __enter__(self):
         assert not self.fd
         self.fd = os.open(self.fn, os.O_WRONLY | os.O_CREAT)
         fcntl.lockf(self.fd, fcntl.LOCK_EX)
 
-    def release(self):
+    def __exit__(self, exc_type, exc_val, exc_tb):
         assert self.fd
         fcntl.lockf(self.fd, fcntl.LOCK_UN)
         os.close(self.fd)
@@ -1764,9 +1764,8 @@ class Prepare(object):
         return parser
 
     def prepare(self):
-        prepare_lock.acquire()
-        self.prepare_locked()
-        prepare_lock.release()
+        with prepare_lock:
+            self.prepare_locked()
 
     @staticmethod
     def factory(args):
@@ -3308,8 +3307,7 @@ def main_activate(args):
         LOG.info('suppressed activate request on %s', args.path)
         return
 
-    activate_lock.acquire()  # noqa
-    try:
+    with activate_lock:
         mode = os.stat(args.path).st_mode
         if stat.S_ISBLK(mode):
             if (is_partition(args.path) and
@@ -3358,16 +3356,10 @@ def main_activate(args):
                 osd_id=osd_id,
             )
 
-    finally:
-        activate_lock.release()  # noqa
-
 
 def main_activate_lockbox(args):
-    activate_lock.acquire()  # noqa
-    try:
+    with activate_lock:
         main_activate_lockbox_protected(args)
-    finally:
-        activate_lock.release()  # noqa
 
 
 def main_activate_lockbox_protected(args):
@@ -3456,11 +3448,8 @@ def _remove_osd_directory_files(mounted_path, cluster):
 
 
 def main_deactivate(args):
-    activate_lock.acquire()  # noqa
-    try:
+    with activate_lock:
         main_deactivate_locked(args)
-    finally:
-        activate_lock.release()  # noqa
 
 
 def main_deactivate_locked(args):
@@ -3618,11 +3607,8 @@ def destroy_lookup_device(args, predicate, description):
 
 
 def main_destroy(args):
-    activate_lock.acquire()  # noqa
-    try:
+    with activate_lock:
         main_destroy_locked(args)
-    finally:
-        activate_lock.release()  # noqa
 
 
 def main_destroy_locked(args):
@@ -3724,8 +3710,7 @@ def main_activate_space(name, args):
     osd_id = None
     osd_uuid = None
     dev = None
-    activate_lock.acquire()  # noqa
-    try:
+    with activate_lock:
         if args.dmcrypt:
             dev = dmcrypt_map(args.dev, args.dmcrypt_key_dir)
         else:
@@ -3755,9 +3740,6 @@ def main_activate_space(name, args):
             osd_id=osd_id,
         )
 
-    finally:
-        activate_lock.release()  # noqa
-
 
 ###########################
 
@@ -3785,31 +3767,29 @@ def main_activate_all(args):
                 continue
 
             LOG.info('Activating %s', path)
-            activate_lock.acquire()  # noqa
-            try:
-                # never map dmcrypt cyphertext devices
-                (cluster, osd_id) = mount_activate(
-                    dev=path,
-                    activate_key_template=args.activate_key_template,
-                    init=args.mark_init,
-                    dmcrypt=False,
-                    dmcrypt_key_dir='',
-                )
-                start_daemon(
-                    cluster=cluster,
-                    osd_id=osd_id,
-                )
-
-            except Exception as e:
-                print(
-                    '{prog}: {msg}'.format(prog=args.prog, msg=e),
-                    file=sys.stderr
-                )
-
-                err = True
+            with activate_lock:
+                try:
+                    # never map dmcrypt cyphertext devices
+                    (cluster, osd_id) = mount_activate(
+                        dev=path,
+                        activate_key_template=args.activate_key_template,
+                        init=args.mark_init,
+                        dmcrypt=False,
+                        dmcrypt_key_dir='',
+                    )
+                    start_daemon(
+                        cluster=cluster,
+                        osd_id=osd_id,
+                    )
+
+                except Exception as e:
+                    print(
+                        '{prog}: {msg}'.format(prog=args.prog, msg=e),
+                        file=sys.stderr
+                    )
+
+                    err = True
 
-            finally:
-                activate_lock.release()  # noqa
     if err:
         raise Error('One or more partitions failed to activate')
 
@@ -4183,11 +4163,8 @@ def list_devices():
 
 
 def main_list(args):
-    activate_lock.acquire()  # noqa
-    try:
+    with activate_lock:
         main_list_protected(args)
-    finally:
-        activate_lock.release()  # noqa
 
 
 def main_list_protected(args):