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
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',
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:]: