#!/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 invirt import database

dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen'


def take_data(machine_name):
## dump from dev db; save info well
  database.connect(dev_db_uri)
  database.session.begin()
  machine = database.Machine.query().get_by(name=machine_name)
  disks = database.Disk.query().filter_by(machine=machine).all()
  nics = database.NIC.query().filter_by(machine=machine).all()
## remove from dev db; ideally atomic with dump
  for r in disks + nics + [machine]:
    database.session.delete(r)
  database.session.commit()
  return machine, disks, nics


## 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, disks, nics):
  database.connect() # to prod DB
  database.session.begin()
  ## now copy machine, disks, nics to new ORM objects (yuck, oh well)
  ## and database.session.save(those)
  database.session.commit()


## power on if desired


