Fault tolerance is a good thing - add some to the migration script
[invirt/scripts/prod-migration.git] / xvm-migrate-machine
index 83b4e37..9b08eec 100755 (executable)
@@ -58,13 +58,13 @@ def take_data(machine_name):
   return machine
 
 ## add to prod db
-def restore_data(machine):
+def restore_data(machine, session):
   # The machine's type is still the one attached to the dev database;
   # get the right one
-  machine.type = prod_sess.query(database.Type).filter_by(type_id=machine.type.type_id).one()
-  prod_sess.begin()
-  prod_sess.save(machine)
-  prod_sess.commit()
+  machine.type = session.query(database.Type).filter_by(type_id=machine.type.type_id).one()
+  session.begin()
+  session.save(machine)
+  session.commit()
   
 def migrate_vm(machine_name):
   # Power off the VM on dev
@@ -76,11 +76,13 @@ def migrate_vm(machine_name):
   
   machine = take_data(machine_name)
   
+  success = True
   ## copy disk image... copy, copy...
   for disk in machine.disks:
     lvname='d_%s_%s' % (machine.name, disk.guest_device_name)
     
-    subprocess.check_call(['lvcreate', '-L%sM' % str(disk.size), '-n', lvname, 'xenvg'])
+    if 0 != subprocess.call(['lvcreate', '-L%sM' % str(disk.size), '-n', lvname, 'xenvg']):
+      success = False
     
     ssh = subprocess.Popen(['ssh', '-o', 'GSSAPIDelegateCredentials=no',
                 'torchwood-institute.mit.edu',
@@ -88,9 +90,19 @@ def migrate_vm(machine_name):
                  stdout=subprocess.PIPE)
     dd = subprocess.Popen(['dd', 'of=/dev/xenvg/%s' % lvname, 'bs=1M'],
                 stdin=ssh.stdout)
-    dd.wait()
+    if 0 != dd.wait():
+      success = False
+    if 0 != ssh.wait():
+      success = False
   
-  restore_data(machine)
+  if not success:
+    restore_data(machine, dev_sess)
+    
+    print '==============================================='
+    print 'ERROR: VM %s failed to migrate' % machine.name
+    print '==============================================='
+  else:
+    restore_data(machine, prod_sess)
 
 if __name__ == '__main__':
   for vm in sys.argv[1:]: