From bb0a7d21670151a0baf37d6b23ac265fa9d8d3c0 Mon Sep 17 00:00:00 2001
From: Evan Broder <broder@mit.edu>
Date: Wed, 12 Nov 2008 22:51:43 -0500
Subject: [PATCH] Fault tolerance is a good thing - add some to the migration
 script

svn path=/trunk/scripts/; revision=1632
---
 xvm-migrate-machine |   28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/xvm-migrate-machine b/xvm-migrate-machine
index 83b4e37..9b08eec 100755
--- a/xvm-migrate-machine
+++ b/xvm-migrate-machine
@@ -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:]:
-- 
1.7.9.5