wiki:admin_interface_scripting
Last modified 9 years ago Last modified on 10/31/08 19:56:01
# ----------------------------------------------------------------------
#    Copyright (c) 2006, 2007 dCache.ORG (R)
#    (All rights reserved)
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public
#    License published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, contact Novell, Inc.
# ----------------------------------------------------------------------
# By default run as the following command
#
# jython test3.py localhost 22223 admin dickerelch
#
from org.pcells.services.connection import *
from dmg.protocols.ssh import *
from dmg.cells.nucleus import NoRouteToCellException
from java.lang import System
from java.io import File
from java.io import FileNotFoundException
import string
import sys
import re
import thread
import random


def dCacheMsgRespSecond(domain,request,responce,Context):
  print "done"
  print "domain=%s" % (domain)
  print "request=%s" % (request)
  print "responce=%s" % (responce)
  print "Context=%s" % (Context)


def dCacheMsgRespFirst(domain,request,responce,Context):
  print "domain=%s" % (domain)
  print "request=%s" % (request)
  #print "responce=%s" % (responce)
  print "Context=%s" % (Context)
  Context.addNextMessage("PoolManager","psu ls -l pgroup",dCacheMsgRespSecond,Context)




class OurDomainEventListener( DomainEventListener ) :

  def connectionOpened( self , connection ) :
    #
    # As soon as we are connected we query the PoolManager
    # for all currently active pools.
    #
    self.Listener.flushPenndingMsg()

  def connectionClosed( self , connection ) :
    print "Connection closed"

  def connectionOutOfBand( self , connection , subject ) :
    print "Connection out of band"

class OurDomainConnectionListener( DomainConnectionListener ) :

  #
  # this class only has one object in the application which
  # is used whenever data arrives from the channel.
  #
  def __init__( self ) :
    self.requestMap = {}
    self.pendingMessages = []
    
  def addNextMessage(self,Domain,Message,Callback = None, Context = None):
    # Domain is the dcache domain to message
    # Message is the admin command to send
    # Callback is the function to call
    # 
    RequestId = random.randrange(100, 10000)
    while (RequestId in self.requestMap.keys()):
      RequestId = random.randrange(100, 10000)
    if Callback == None:
      Callback = self.defaultResponce
    self.requestMap[RequestId] = [Domain,Message,Callback,Context]
    self.pendingMessages.append(RequestId)
    
  def flushPenndingMsg(self):
    # Dont resend any messages
    for i in range (0 , len(self.pendingMessages)):
      nextMessageId = self.pendingMessages[i]
      message = self.requestMap[nextMessageId]
      self.connection.sendObject( 
         message[0], 
         message[1], 
         self , 
         nextMessageId)
    self.pendingMessages = []
    
  def domainAnswerArrived( self , obj , uid ) :
    # Retrive the call details
    calldetails = self.requestMap[uid]
    del self.requestMap[uid]
    # do the call back
    calldetails[2](calldetails[0],calldetails[1],obj,calldetails[3])    
    if len(self.requestMap) > 0:
      # More messages exist so send all outstanding messages.
      self.flushPenndingMsg()
    else:
      # No more messages to send so quit
      System.exit(0)
      
  def defaultResponce(self,Domain,Request,Responce,Context):
    print "domain=%s" % (Domain)
    print "request=%s" % (Request)
    print "responce=%s" % (Responce)
    print "Context=%s" % (Context)


#----------------------------------------------------------------------
#
#   Main Routine
#
#----------------------------------------------------------------------
#


if  len(sys.argv) < 5 :
  print "Usage : ... <hostname> <portNumber> <userName> <passwd>"
  System.exit(4)
#
# Define the ListenerObject for all incoming message
#
ourMessageListener = OurDomainConnectionListener()

# Now add our first request we want to process
#
# Note: if no requests exit the OurDomainEventListener object 
# will have no messages to send so the process cant start
#
ourMessageListener.addNextMessage("PoolManager","psu ls -l pgroup",dCacheMsgRespFirst,ourMessageListener)
#
#  Define Host and portnumber
#
connection = Ssh1DomainConnection(sys.argv[1],int(sys.argv[2]))
connection.setLoginName(sys.argv[3])

#
# Establish the secret : either password or identity file
#
secret=sys.argv[4]
if secret[0] == "/" :
  connection.setPassword("")
  try :
    connection.setIdentityFile( File(secret) )
  except FileNotFoundException, e :
    print "Authentication Failed ...",e.getMessage()
    System.exit(1)
else :
   connection.setPassword(secret)

InstDomainEventListener = OurDomainEventListener()
InstDomainEventListener.Listener = ourMessageListener
connection.addDomainEventListener(InstDomainEventListener)
ourMessageListener.connection = connection
#
#  and now go!
#
try :
  connection.go()
except SshAuthenticationException, e :
  print "Authentication Failed ...",e.getMessage()
  System.exit(1)