Fault tolerance is a good thing - add some to the migration script
[invirt/scripts/prod-migration.git] / xvm-migrate-machine
old mode 100644 (file)
new mode 100755 (executable)
index 0403ff5..9b08eec
@@ -1,11 +1,7 @@
-#!/bin/python
+#!/usr/bin/python
 # Migrates the machine passed as arguments from the dev cluster.
 # To be run on the prod cluster.
 
-## The present version is NOT A REAL SCRIPT.
-## Things may not even be tested.  Copy and paste.
-not_ready_yet_do_not_run_me
-
 from invirt import remctl as r
 from lib import database
 import subprocess
@@ -27,6 +23,27 @@ def take_data(machine_name):
   machine.acl = []
   dev_sess.update(machine)
   
+  print 'VM Info:'
+  print '  name: %s' % machine.name
+  print '  description: %s' % machine.description
+  print '  cpus: %s' % machine.cpus
+  print '  memory: %s' % machine.memory
+  print '  owner: %s' % machine.owner
+  print '  contact: %s' % machine.contact
+  print '  administrator: %s' % machine.administrator
+  print '  uuid: %s' % machine.uuid
+  print '  type: %s' % machine.type.type_id
+  print '  autorestart: %s' % machine.autorestart
+  print '  adminable: %s' % machine.adminable
+  print '  Disks:'
+  for disk in machine.disks:
+    print '  - %s (%s)' % (disk.guest_device_name, disk.size)
+  print '  NICs:'
+  for nic in machine.nics:
+    print '  - %s, %s, %s' % (nic.mac_addr, nic.ip, nic.hostname)
+  print '==============================================='
+  print
+  
   disks = machine.disks
   nics = machine.nics
   for r in disks + nics + [machine]:
@@ -41,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
@@ -59,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',
@@ -71,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:]: