Make the xvm-migration-script actually do everything it needs to
[invirt/scripts/prod-migration.git] / xvm-migrate-machine
index d8c729e..0403ff5 100644 (file)
@@ -1,30 +1,83 @@
-##!/bin/bash
-# Migrates the machine named $1 from the dev cluster.
+#!/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
 
-## dump from dev db; save info well
+from invirt import remctl as r
+from lib import database
+import subprocess
+import sys
 
-#echo "\\a \\t \\\\ select * from machines where name = '$MACHINE';" \
-# | psql -h xvm -U sipb-xen sipb_xen -q
-## 581|fsck|256|price|price|2ab6638f-3f65-2b32-3fd3-c16b74a9b7fe|linux|f|1|price|test|f
-## watch out for funny characters in description; better (non-)quoting needed
-## also disks, nics
+dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen'
+database.connect(dev_db_uri)
+dev_sess = database.session
 
-## remove from dev db; ideally atomic with dump
+database.connect()
+prod_sess = database.session
 
-## shut down if up
-#remctl remote control $MACHINE destroy
+## dump from dev db
+def take_data(machine_name):
+  dev_sess.begin()
+  machine = dev_sess.query(database.Machine).filter_by(name=machine_name).one()
+  
+  # Clean out the ACL just so we don't have to think about it
+  machine.acl = []
+  dev_sess.update(machine)
+  
+  disks = machine.disks
+  nics = machine.nics
+  for r in disks + nics + [machine]:
+    dev_sess.delete(r)
+  
+  dev_sess.commit()
+  
+  for r in disks + nics + [machine]:
+    dev_sess.expunge(r)
+    del r._instance_key
+  
+  return machine
 
-## copy disk image... copy, copy...
-## for each disk:
-lvname="d_${MACHINE}_${guest_device_name}"
-lvcreate xenvg -n "$lvname" -L "${size}"M
-ssh t-i dd if=/dev/xenvg/"$lvname" of=/dev/stdout bs=1M \
- | dd if=/dev/stdin of=/dev/xenvg/"$lvname" bs=1M
+## add to prod db
+def restore_data(machine):
+  # 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()
+  
+def migrate_vm(machine_name):
+  # Power off the VM on dev
+  #
+  # This has to be done first, because once the machine is deleted
+  # from the database, we can't remctl for it anymore
+  out, err = r.remctl('xvm-remote.mit.edu', 'control', machine_name, 'destroy', err=True)
+  print out
+  
+  machine = take_data(machine_name)
+  
+  ## 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'])
+    
+    ssh = subprocess.Popen(['ssh', '-o', 'GSSAPIDelegateCredentials=no',
+                'torchwood-institute.mit.edu',
+                'dd', 'if=/dev/xenvg/%s' % lvname, 'bs=1M'],
+                 stdout=subprocess.PIPE)
+    dd = subprocess.Popen(['dd', 'of=/dev/xenvg/%s' % lvname, 'bs=1M'],
+                stdin=ssh.stdout)
+    dd.wait()
+  
+  restore_data(machine)
 
-## add to dev db
-
-## power on if desired
+if __name__ == '__main__':
+  for vm in sys.argv[1:]:
+    print '==============================================='
+    print 'Migrating %s' % vm
+    print '==============================================='
+    migrate_vm(vm.strip())