#!/bin/python
# Migrates the machine named $1 from the dev cluster.
# To be run on the prod cluster.

## The present version is NOT A REAL SCRIPT.
## Things may not even be tested.  Copy and paste.
not_ready_yet_do_not_run_me


from lib import database

dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen'
database.connect(dev_db_uri)
dev_sess = database.session

database.connect()
prod_sess = database.session

def take_data(machine_name):
## dump from dev db; save info well
  dev_sess.begin()
  machine = dev_sess.query(database.Machine).filter_by(name=machine_name).one()
  
  # Clean out the ACL just so we don't have to think about it
  machine.acl = []
  dev_sess.update(machine)
  
  disks = machine.disks
  nics = machine.nics
  for r in disks + nics + [machine]:
    database.session.delete(r)
  
  database.session.commit()
  
  for r in disks + nics + [machine]:
    dev_sess.expunge(i)
    del i._instance_key
  
  return machine

## shut down if up
#remctl remote control $MACHINE destroy

## copy disk image... copy, copy...
## for each disk:
lvname="d_${MACHINE}_${guest_device_name}"
lvcreate xenvg -n "$lvname" -L "${size}"M
ssh t-i dd if=/dev/xenvg/"$lvname" of=/dev/stdout bs=1M \
 | dd if=/dev/stdin of=/dev/xenvg/"$lvname" bs=1M

## add to dev db
def restore_data(machine):
  machine.type = prod_sess.query(database.Type).filter_by(type_id=machine.type.type_id).one()
  prod_sess.begin()
  ## now copy machine, disks, nics to new ORM objects (yuck, oh well)
  ## and database.session.save(those)
  prod_sess.save(machine)
  prod_sess.commit()


## power on if desired


