#!/usr/bin/python
from optparse import OptionParser
import os
import sys
import string
import time
import syslog

import imagerdb
import daemon
import mounter


#Unfortunately this tool is now running as root.
#Any suggestions to make interaction with 
# * nbd-client
# * nbd-server --proxy
# 
# safer is welcomed

def pollChange():
    ImgDB.expirestate(60*5)
    change=ImgDB.getchange_request()
    if (change is None):
        return 0
    print change
    (name, ip, currentstate, reqstate)=change
    newcurrentstate=currentstate
    if (reqstate=="dead"):
	if (currentstate=='active'):
            newcurrentstate='dead'
        if (currentstate=='blockconnected'):
            mounter.dettach_blockdevice(0)
            newcurrentstate='active'
    if (reqstate=='active'):
	if (currentstate=='dead'):
	    ImgDB.requeststate(name,"blockconnected")
	    newcurrentstate='active'
        if (currentstate=='blockconnected'):
            mounter.dettach_blockdevice(0)
            newcurrentstate='active'
    if (reqstate=='blockconnected'):
	if (currentstate=='dead'):
		newcurrentstate='active'
	if (currentstate=='active'):
		print "Mounting"
		mounter.attach_blockdevice(0,ip,7000,"/tmp/"+name)
		newcurrentstate='blockconnected'

    ImgDB.setcurrentstate(name,newcurrentstate)
#nothin to do
    return 1

def pollChanges():
    while (pollChange()): pass
	


if __name__=='__main__':
  parser = OptionParser()
  parser.add_option("-p", "--pidfile", dest="pidfile",default="/var/run/mount_daemon.pid",
                  help="file to store the daemon processid", metavar="FILE")
  parser.add_option("-d", "--no_daemon",
                  action="store_false", dest="daemon", default=True,
                  help="start in foreground")

  (options, args) = parser.parse_args()

  if (options.daemon):
      retCode = daemon.createDaemon(options.pidfile)

  ImgDB=imagerdb.ImagerDb()

  try :
    while(1):
	time.sleep(1)
	pollChanges()

  except KeyboardInterrupt:
    del ImgDB
    print '%s' % sys.exc_type
    print 'shutting down'
  


a4d2a4aa3d86b079c7476b59872cef79b0eaba30
#!/usr/bin/python
from optparse import OptionParser
import os
import sys
import string
import time
import syslog

import imagerdb
import daemon
import mounter


#Unfortunately this tool is now running as root.
#Any suggestions to make interaction with 
# * nbd-client
# * nbd-server --proxy
# 
# safer is welcomed

def pollChange():
    ImgDB.expirestate(60*5)
    change=ImgDB.getchange_request()
    if (change is None):
        return 0
    print change
    (name, ip, currentstate, reqstate)=change
    newcurrentstate=currentstate
    if (reqstate=="dead"):
	if (currentstate=='active'):
            newcurrentstate='dead'
        if (currentstate=='blockconnected'):
            mounter.dettach_blockdevice(0)
            newcurrentstate='active'
    if (reqstate=='active'):
	if (currentstate=='dead'):
	    ImgDB.requeststate(name,"blockconnected")
	    newcurrentstate='active'
        if (currentstate=='blockconnected'):
            mounter.dettach_blockdevice(0)
            newcurrentstate='active'
    if (reqstate=='blockconnected'):
	if (currentstate=='dead'):
		newcurrentstate='active'
	if (currentstate=='active'):
		print "Mounting"
		mounter.attach_blockdevice(0,ip,7000,"/tmp/"+name)
		newcurrentstate='blockconnected'

    ImgDB.setcurrentstate(name,newcurrentstate)
#nothin to do
    return 1

def pollChanges():
    while (pollChange()): pass
	


if __name__=='__main__':
  parser = OptionParser()
  parser.add_option("-p", "--pidfile", dest="pidfile",default="/var/run/mount_daemon.pid",
                  help="file to store the daemon processid", metavar="FILE")
  parser.add_option("-d", "--no_daemon",
                  action="store_false", dest="daemon", default=True,
                  help="start in foreground")

  (options, args) = parser.parse_args()

  if (options.daemon):
      retCode = daemon.createDaemon(options.pidfile)

  ImgDB=imagerdb.ImagerDb()

  try :
    while(1):
	time.sleep(1)
	pollChanges()

  except KeyboardInterrupt:
    del ImgDB
    print '%s' % sys.exc_type
    print 'shutting down'
  

