Fixed lv deactivation for rename
[invirt/packages/invirt-images.git] / invirt-images
index 2175ead..e6f987d 100755 (executable)
@@ -41,19 +41,39 @@ def lvcreate(name, size):
         return 6
 
 def lvrename(dest, src):
-    lvr = subprocess.Popen(['lvrename', 'xenvg', src, dest],
+    lvr = subprocess.Popen(['lvchange', '-an', "xenvg/%s" % (src,)],
                            stderr=subprocess.PIPE,
                            stdout=getOutput()['stdout'])
     ret = lvr.wait()
-    if not ret:
-        return 0
-    stderr = lvr.stderr.read()
-    if 'not found in volume group' in stderr:
-        return 0
-    else:
+
+    if ret:
+       if verbosity > 0:
+           print lvr.stderr.read()
+       return ret
+
+    lvr = subprocess.Popen(['lvrename', "xenvg/%s" % (src,), "xenvg/%s" % (dest,)],
+                           stderr=subprocess.PIPE,
+                           stdout=getOutput()['stdout'])
+    ret = lvr.wait()
+
+    if ret:
+        stderr = lvr.stderr.read()
+        if not ('not found in volume group' in stderr):
+            if verbosity > 0:
+                print stderr
+            return ret
+
+    subprocess.Popen(['lvchange', '-ay', "xenvg/%s" % (dest,)],
+                     stderr=subprocess.PIPE,
+                     stdout=getOutput()['stdout'])
+    ret = lvr.wait()
+
+    if ret:
         if verbosity > 0:
-            print stderr
-        return ret
+           print lvr.stderr.read()
+
+    return ret
+
 
 def lv_random(func, pattern, *args):
     """
@@ -191,7 +211,8 @@ def main():
     global verbosity
     
     database.connect()
-    
+    database.session.begin()
+
     usage = """%prog [options] --add [--cdrom] cdrom_id description mirror_id uri_suffix
        %prog [options] --add --mirror mirror_id uri_prefix
 
@@ -243,8 +264,8 @@ def main():
             attrs = dict(zip(('cdrom_id', 'description', 'mirror_id', 'uri_suffix'),
                              args))
             cdrom = database.CDROM(**attrs)
-            database.session.save(cdrom)
-            database.session.flush()
+            database.session.add(cdrom)
+            database.session.commit()
             
             load_image(cdrom)
         
@@ -252,8 +273,8 @@ def main():
             attrs = dict(zip(('mirror_id', 'uri_prefix'),
                              args))
             mirror = database.Mirror(**attrs)
-            database.session.save(mirror)
-            database.session.flush()
+            database.session.add(mirror)
+            database.session.commit()
     elif options.action == 'update':
         if len(args) > 0:
             images = [database.CDROM.query().get(arg) for arg in args]