source: trunk/scripts/xvm-migrate-machine @ 1839

Last change on this file since 1839 was 1662, checked in by broder, 16 years ago

The script we finally used to do the actual migration

  • Property svn:executable set to *
File size: 4.9 KB
RevLine 
[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]5from invirt import remctl as r
[1611]6from lib import database
[1626]7import subprocess
8import sys
[1662]9import time
10import os
[1429]11
[1662]12sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
13
14kinit = dict(principal='host/aperture-science.mit.edu', keytab='/etc/krb5.keytab')
15
[1437]16dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen'
[1611]17database.connect(dev_db_uri)
18dev_sess = database.session
[1437]19
[1611]20database.connect()
21prod_sess = database.session
[1437]22
[1626]23## dump from dev db
[1437]24def take_data(machine_name):
[1611]25  dev_sess.begin()
26  machine = dev_sess.query(database.Machine).filter_by(name=machine_name).one()
27 
28  # Clean out the ACL just so we don't have to think about it
29  machine.acl = []
30  dev_sess.update(machine)
31 
[1627]32  print 'VM Info:'
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
44  print '  Disks:'
45  for disk in machine.disks:
46    print '  - %s (%s)' % (disk.guest_device_name, disk.size)
47  print '  NICs:'
48  for nic in machine.nics:
49    print '  - %s, %s, %s' % (nic.mac_addr, nic.ip, nic.hostname)
50  print '==============================================='
51  print
52 
[1611]53  disks = machine.disks
54  nics = machine.nics
[1437]55  for r in disks + nics + [machine]:
[1626]56    dev_sess.delete(r)
[1611]57 
[1662]58  dev_sess.flush()
[1611]59 
60  for r in disks + nics + [machine]:
[1620]61    dev_sess.expunge(r)
62    del r._instance_key
[1611]63 
64  return machine
[1429]65
[1626]66## add to prod db
[1632]67def restore_data(machine, session):
[1626]68  # The machine's type is still the one attached to the dev database;
69  # get the right one
[1632]70  machine.type = session.query(database.Type).filter_by(type_id=machine.type.type_id).one()
71  session.begin()
72  session.save(machine)
73  session.commit()
[1626]74 
75def migrate_vm(machine_name):
76  # Power off the VM on dev
77  #
78  # This has to be done first, because once the machine is deleted
79  # from the database, we can't remctl for it anymore
[1662]80  out, err = r.remctl('xvm-remote.mit.edu', 'control', machine_name, 'destroy', err=True, **kinit)
[1626]81  print out
82 
83  machine = take_data(machine_name)
[1662]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)])
[1626]86 
[1632]87  success = True
[1626]88  ## copy disk image... copy, copy...
89  for disk in machine.disks:
90    lvname='d_%s_%s' % (machine.name, disk.guest_device_name)
91   
[1632]92    if 0 != subprocess.call(['lvcreate', '-L%sM' % str(disk.size), '-n', lvname, 'xenvg']):
93      success = False
[1633]94      break
[1626]95   
[1662]96    ssh = subprocess.Popen(['rsh',
97                '10.6.0.165',
[1626]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'],
101                stdin=ssh.stdout)
[1632]102    if 0 != dd.wait():
103      success = False
[1633]104      break
[1632]105    if 0 != ssh.wait():
106      success = False
[1633]107      break
[1626]108 
[1632]109  if not success:
[1662]110    dev_sess.rollback()
[1632]111   
112    print '==============================================='
113    print 'ERROR: VM %s failed to migrate' % machine.name
114    print '==============================================='
115  else:
116    restore_data(machine, prod_sess)
[1662]117    dev_sess.commit()
118 
119  return success
[1429]120
[1626]121if __name__ == '__main__':
[1662]122  while True:
123    r.checkKinit(**kinit)
124    p = subprocess.Popen(['curl', '-s', '-k', '--negotiate', '-u', ':', 'https://xvm.mit.edu:442/offlist'], stdout=subprocess.PIPE)
125    if 0 != p.wait():
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'])
128        time.sleep(15)
129        continue
130   
131    next_line = p.stdout.read().split('\n')[0]
132    if next_line == '':
133      subprocess.call(['zwrite', '-d', '-c', 'xvm', '-i', 'migration', '-s', 'XVM Migration Script', '-m',
134                       'XVM migration complete'])
135      break
136   
137    next, uptime = next_line.split('\t')[:2]
138   
[1626]139    print '==============================================='
[1662]140    print 'Migrating %s' % next
[1626]141    print '==============================================='
[1662]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])
145      sys.exit(1)
146    if uptime.strip() != '':
147      r.remctl('xvm-remote-dev.mit.edu', 'control', next, 'create', **kinit)
148   
149    subprocess.call(['zwrite', '-d', '-c', 'xvm-auto', '-i', 'migration', '-s', 'XVM Migration Script', '-m',
150                     'done'])
Note: See TracBrowser for help on using the repository browser.