Changeset 1626


Ignore:
Timestamp:
Nov 12, 2008, 10:38:38 AM (15 years ago)
Author:
broder
Message:

Make the xvm-migration-script actually do everything it needs to

Location:
trunk/scripts
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scripts/xvm-migrate-machine

    r1620 r1626  
    11#!/bin/python
    2 # Migrates the machine named $1 from the dev cluster.
     2# Migrates the machine passed as arguments from the dev cluster.
    33# To be run on the prod cluster.
    44
     
    77not_ready_yet_do_not_run_me
    88
    9 
     9from invirt import remctl as r
    1010from lib import database
     11import subprocess
     12import sys
    1113
    1214dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen'
     
    1719prod_sess = database.session
    1820
     21## dump from dev db
    1922def take_data(machine_name):
    20 ## dump from dev db; save info well
    2123  dev_sess.begin()
    2224  machine = dev_sess.query(database.Machine).filter_by(name=machine_name).one()
     
    2931  nics = machine.nics
    3032  for r in disks + nics + [machine]:
    31     database.session.delete(r)
     33    dev_sess.delete(r)
    3234 
    33   database.session.commit()
     35  dev_sess.commit()
    3436 
    3537  for r in disks + nics + [machine]:
     
    3941  return machine
    4042
    41 ## shut down if up
    42 #remctl remote control $MACHINE destroy
    43 
    44 ## copy disk image... copy, copy...
    45 ## for each disk:
    46 lvname="d_${MACHINE}_${guest_device_name}"
    47 lvcreate xenvg -n "$lvname" -L "${size}"M
    48 ssh t-i dd if=/dev/xenvg/"$lvname" of=/dev/stdout bs=1M \
    49  | dd if=/dev/stdin of=/dev/xenvg/"$lvname" bs=1M
    50 
    51 ## add to dev db
     43## add to prod db
    5244def restore_data(machine):
     45  # The machine's type is still the one attached to the dev database;
     46  # get the right one
    5347  machine.type = prod_sess.query(database.Type).filter_by(type_id=machine.type.type_id).one()
    5448  prod_sess.begin()
    55   ## now copy machine, disks, nics to new ORM objects (yuck, oh well)
    56   ## and database.session.save(those)
    5749  prod_sess.save(machine)
    5850  prod_sess.commit()
     51 
     52def 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)
    5977
    60 
    61 ## power on if desired
    62 
    63 
     78if __name__ == '__main__':
     79  for vm in sys.argv[1:]:
     80    print '==============================================='
     81    print 'Migrating %s' % vm
     82    print '==============================================='
     83    migrate_vm(vm.strip())
Note: See TracChangeset for help on using the changeset viewer.