[1629] | 1 | #!/usr/bin/python |
---|
[1626] | 2 | # Migrates the machine passed as arguments from the dev cluster. |
---|
[1429] | 3 | # To be run on the prod cluster. |
---|
| 4 | |
---|
[1626] | 5 | from invirt import remctl as r |
---|
[1611] | 6 | from lib import database |
---|
[1626] | 7 | import subprocess |
---|
| 8 | import sys |
---|
[1429] | 9 | |
---|
[1437] | 10 | dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen' |
---|
[1611] | 11 | database.connect(dev_db_uri) |
---|
| 12 | dev_sess = database.session |
---|
[1437] | 13 | |
---|
[1611] | 14 | database.connect() |
---|
| 15 | prod_sess = database.session |
---|
[1437] | 16 | |
---|
[1626] | 17 | ## dump from dev db |
---|
[1437] | 18 | def take_data(machine_name): |
---|
[1611] | 19 | dev_sess.begin() |
---|
| 20 | machine = dev_sess.query(database.Machine).filter_by(name=machine_name).one() |
---|
| 21 | |
---|
| 22 | # Clean out the ACL just so we don't have to think about it |
---|
| 23 | machine.acl = [] |
---|
| 24 | dev_sess.update(machine) |
---|
| 25 | |
---|
[1627] | 26 | print 'VM Info:' |
---|
| 27 | print ' name: %s' % machine.name |
---|
| 28 | print ' description: %s' % machine.description |
---|
| 29 | print ' cpus: %s' % machine.cpus |
---|
| 30 | print ' memory: %s' % machine.memory |
---|
| 31 | print ' owner: %s' % machine.owner |
---|
| 32 | print ' contact: %s' % machine.contact |
---|
| 33 | print ' administrator: %s' % machine.administrator |
---|
| 34 | print ' uuid: %s' % machine.uuid |
---|
| 35 | print ' type: %s' % machine.type.type_id |
---|
| 36 | print ' autorestart: %s' % machine.autorestart |
---|
| 37 | print ' adminable: %s' % machine.adminable |
---|
| 38 | print ' Disks:' |
---|
| 39 | for disk in machine.disks: |
---|
| 40 | print ' - %s (%s)' % (disk.guest_device_name, disk.size) |
---|
| 41 | print ' NICs:' |
---|
| 42 | for nic in machine.nics: |
---|
| 43 | print ' - %s, %s, %s' % (nic.mac_addr, nic.ip, nic.hostname) |
---|
| 44 | print '===============================================' |
---|
| 45 | print |
---|
| 46 | |
---|
[1611] | 47 | disks = machine.disks |
---|
| 48 | nics = machine.nics |
---|
[1437] | 49 | for r in disks + nics + [machine]: |
---|
[1626] | 50 | dev_sess.delete(r) |
---|
[1611] | 51 | |
---|
[1626] | 52 | dev_sess.commit() |
---|
[1611] | 53 | |
---|
| 54 | for r in disks + nics + [machine]: |
---|
[1620] | 55 | dev_sess.expunge(r) |
---|
| 56 | del r._instance_key |
---|
[1611] | 57 | |
---|
| 58 | return machine |
---|
[1429] | 59 | |
---|
[1626] | 60 | ## add to prod db |
---|
[1632] | 61 | def restore_data(machine, session): |
---|
[1626] | 62 | # The machine's type is still the one attached to the dev database; |
---|
| 63 | # get the right one |
---|
[1632] | 64 | machine.type = session.query(database.Type).filter_by(type_id=machine.type.type_id).one() |
---|
| 65 | session.begin() |
---|
| 66 | session.save(machine) |
---|
| 67 | session.commit() |
---|
[1626] | 68 | |
---|
| 69 | def migrate_vm(machine_name): |
---|
| 70 | # Power off the VM on dev |
---|
| 71 | # |
---|
| 72 | # This has to be done first, because once the machine is deleted |
---|
| 73 | # from the database, we can't remctl for it anymore |
---|
| 74 | out, err = r.remctl('xvm-remote.mit.edu', 'control', machine_name, 'destroy', err=True) |
---|
| 75 | print out |
---|
| 76 | |
---|
| 77 | machine = take_data(machine_name) |
---|
| 78 | |
---|
[1632] | 79 | success = True |
---|
[1626] | 80 | ## copy disk image... copy, copy... |
---|
| 81 | for disk in machine.disks: |
---|
| 82 | lvname='d_%s_%s' % (machine.name, disk.guest_device_name) |
---|
| 83 | |
---|
[1632] | 84 | if 0 != subprocess.call(['lvcreate', '-L%sM' % str(disk.size), '-n', lvname, 'xenvg']): |
---|
| 85 | success = False |
---|
[1626] | 86 | |
---|
| 87 | ssh = subprocess.Popen(['ssh', '-o', 'GSSAPIDelegateCredentials=no', |
---|
| 88 | 'torchwood-institute.mit.edu', |
---|
| 89 | 'dd', 'if=/dev/xenvg/%s' % lvname, 'bs=1M'], |
---|
| 90 | stdout=subprocess.PIPE) |
---|
| 91 | dd = subprocess.Popen(['dd', 'of=/dev/xenvg/%s' % lvname, 'bs=1M'], |
---|
| 92 | stdin=ssh.stdout) |
---|
[1632] | 93 | if 0 != dd.wait(): |
---|
| 94 | success = False |
---|
| 95 | if 0 != ssh.wait(): |
---|
| 96 | success = False |
---|
[1626] | 97 | |
---|
[1632] | 98 | if not success: |
---|
| 99 | restore_data(machine, dev_sess) |
---|
| 100 | |
---|
| 101 | print '===============================================' |
---|
| 102 | print 'ERROR: VM %s failed to migrate' % machine.name |
---|
| 103 | print '===============================================' |
---|
| 104 | else: |
---|
| 105 | restore_data(machine, prod_sess) |
---|
[1429] | 106 | |
---|
[1626] | 107 | if __name__ == '__main__': |
---|
| 108 | for vm in sys.argv[1:]: |
---|
| 109 | print '===============================================' |
---|
| 110 | print 'Migrating %s' % vm |
---|
| 111 | print '===============================================' |
---|
| 112 | migrate_vm(vm.strip()) |
---|