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