2 # Migrates the machine passed as arguments from the dev cluster.
3 # To be run on the prod cluster.
5 from invirt import remctl as r
6 from lib import database
12 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
14 kinit = dict(principal='host/aperture-science.mit.edu', keytab='/etc/krb5.keytab')
16 dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen'
17 database.connect(dev_db_uri)
18 dev_sess = database.session
21 prod_sess = database.session
24 def take_data(machine_name):
26 machine = dev_sess.query(database.Machine).filter_by(name=machine_name).one()
28 # Clean out the ACL just so we don't have to think about it
30 dev_sess.update(machine)
33 print ' name: %s' % machine.name
34 print ' description: %s' % machine.description
35 print ' cpus: %s' % machine.cpus
36 print ' memory: %s' % machine.memory
37 print ' owner: %s' % machine.owner
38 print ' contact: %s' % machine.contact
39 print ' administrator: %s' % machine.administrator
40 print ' uuid: %s' % machine.uuid
41 print ' type: %s' % machine.type.type_id
42 print ' autorestart: %s' % machine.autorestart
43 print ' adminable: %s' % machine.adminable
45 for disk in machine.disks:
46 print ' - %s (%s)' % (disk.guest_device_name, disk.size)
48 for nic in machine.nics:
49 print ' - %s, %s, %s' % (nic.mac_addr, nic.ip, nic.hostname)
50 print '==============================================='
55 for r in disks + nics + [machine]:
60 for r in disks + nics + [machine]:
67 def restore_data(machine, session):
68 # The machine's type is still the one attached to the dev database;
70 machine.type = session.query(database.Type).filter_by(type_id=machine.type.type_id).one()
75 def migrate_vm(machine_name):
76 # Power off the VM on dev
78 # This has to be done first, because once the machine is deleted
79 # from the database, we can't remctl for it anymore
80 out, err = r.remctl('xvm-remote.mit.edu', 'control', machine_name, 'destroy', err=True, **kinit)
83 machine = take_data(machine_name)
84 subprocess.call(['zwrite', '-d', '-c', 'xvm-auto', '-i', 'migration', '-s', 'XVM Migration Script', '-m',
85 'Migrating %s, disk size %0.2fG...' % (machine.name, sum(disk.size for disk in machine.disks) / 1024.0)])
88 ## copy disk image... copy, copy...
89 for disk in machine.disks:
90 lvname='d_%s_%s' % (machine.name, disk.guest_device_name)
92 if 0 != subprocess.call(['lvcreate', '-L%sM' % str(disk.size), '-n', lvname, 'xenvg']):
96 ssh = subprocess.Popen(['rsh',
98 'dd', 'if=/dev/xenvg/%s' % lvname, 'bs=1M'],
99 stdout=subprocess.PIPE)
100 dd = subprocess.Popen(['dd', 'of=/dev/xenvg/%s' % lvname, 'bs=1M'],
112 print '==============================================='
113 print 'ERROR: VM %s failed to migrate' % machine.name
114 print '==============================================='
116 restore_data(machine, prod_sess)
121 if __name__ == '__main__':
123 r.checkKinit(**kinit)
124 p = subprocess.Popen(['curl', '-s', '-k', '--negotiate', '-u', ':', 'https://xvm.mit.edu:442/offlist'], stdout=subprocess.PIPE)
126 subprocess.call(['zwrite', '-d', '-c', 'xvm', '-i', 'migration', '-s', 'XVM Migration Script', '-m',
127 'Failed to get list of remaining VMs. Will try again in 15 seconds'])
131 next_line = p.stdout.read().split('\n')[0]
133 subprocess.call(['zwrite', '-d', '-c', 'xvm', '-i', 'migration', '-s', 'XVM Migration Script', '-m',
134 'XVM migration complete'])
137 next, uptime = next_line.split('\t')[:2]
139 print '==============================================='
140 print 'Migrating %s' % next
141 print '==============================================='
142 if not migrate_vm(next):
143 subprocess.call(['zwrite', '-d', '-c', 'xvm', '-i', 'migration', '-s', 'XVM Migration Script', '-m',
144 'Error in migrating %s' % next])
146 if uptime.strip() != '':
147 r.remctl('xvm-remote-dev.mit.edu', 'control', next, 'create', **kinit)
149 subprocess.call(['zwrite', '-d', '-c', 'xvm-auto', '-i', 'migration', '-s', 'XVM Migration Script', '-m',