1 | #!/bin/python |
---|
2 | # Migrates the machine passed as arguments from the dev cluster. |
---|
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. |
---|
7 | not_ready_yet_do_not_run_me |
---|
8 | |
---|
9 | from invirt import remctl as r |
---|
10 | from lib import database |
---|
11 | import subprocess |
---|
12 | import sys |
---|
13 | |
---|
14 | dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen' |
---|
15 | database.connect(dev_db_uri) |
---|
16 | dev_sess = database.session |
---|
17 | |
---|
18 | database.connect() |
---|
19 | prod_sess = database.session |
---|
20 | |
---|
21 | ## dump from dev db |
---|
22 | def take_data(machine_name): |
---|
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 | |
---|
30 | disks = machine.disks |
---|
31 | nics = machine.nics |
---|
32 | for r in disks + nics + [machine]: |
---|
33 | dev_sess.delete(r) |
---|
34 | |
---|
35 | dev_sess.commit() |
---|
36 | |
---|
37 | for r in disks + nics + [machine]: |
---|
38 | dev_sess.expunge(r) |
---|
39 | del r._instance_key |
---|
40 | |
---|
41 | return machine |
---|
42 | |
---|
43 | ## add to prod db |
---|
44 | def restore_data(machine): |
---|
45 | # The machine's type is still the one attached to the dev database; |
---|
46 | # get the right one |
---|
47 | machine.type = prod_sess.query(database.Type).filter_by(type_id=machine.type.type_id).one() |
---|
48 | prod_sess.begin() |
---|
49 | prod_sess.save(machine) |
---|
50 | prod_sess.commit() |
---|
51 | |
---|
52 | def migrate_vm(machine_name): |
---|
53 | # Power off the VM on dev |
---|
54 | # |
---|
55 | # This has to be done first, because once the machine is deleted |
---|
56 | # from the database, we can't remctl for it anymore |
---|
57 | out, err = r.remctl('xvm-remote.mit.edu', 'control', machine_name, 'destroy', err=True) |
---|
58 | print out |
---|
59 | |
---|
60 | machine = take_data(machine_name) |
---|
61 | |
---|
62 | ## copy disk image... copy, copy... |
---|
63 | for disk in machine.disks: |
---|
64 | lvname='d_%s_%s' % (machine.name, disk.guest_device_name) |
---|
65 | |
---|
66 | subprocess.check_call(['lvcreate', '-L%sM' % str(disk.size), '-n', lvname, 'xenvg']) |
---|
67 | |
---|
68 | ssh = subprocess.Popen(['ssh', '-o', 'GSSAPIDelegateCredentials=no', |
---|
69 | 'torchwood-institute.mit.edu', |
---|
70 | 'dd', 'if=/dev/xenvg/%s' % lvname, 'bs=1M'], |
---|
71 | stdout=subprocess.PIPE) |
---|
72 | dd = subprocess.Popen(['dd', 'of=/dev/xenvg/%s' % lvname, 'bs=1M'], |
---|
73 | stdin=ssh.stdout) |
---|
74 | dd.wait() |
---|
75 | |
---|
76 | restore_data(machine) |
---|
77 | |
---|
78 | if __name__ == '__main__': |
---|
79 | for vm in sys.argv[1:]: |
---|
80 | print '===============================================' |
---|
81 | print 'Migrating %s' % vm |
---|
82 | print '===============================================' |
---|
83 | migrate_vm(vm.strip()) |
---|