CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
o2oMgr.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 '''
4 '''
5 
6 __author__ = 'Giacomo Govi'
7 
8 import CondCore.Utilities.o2o as o2olib
9 import sys
10 import optparse
11 import argparse
12 from sets import Set
13 
14 class CommandTool(object):
15  def __init__(self):
17  self.options = Set()
18  self.commands = {}
19  self.args = None
20 
21  def addOption( self, *keys, **params ):
22  nkeys = []
23  if len(keys)>1:
24  nkeys.append( "-"+keys[1] )
25  nkeys.append( "--"+keys[0] )
26  action = "store"
27  if 'type' not in params.keys():
28  params['action'] = "store_true"
29  self.parser.add_argument(*nkeys,**params )
30  self.options.add( keys[0] )
31 
32  def addCommand( self, command_name, help_entry, *requiredOptions ):
33  self.parser.add_argument("--"+command_name, action="store_true", help=help_entry )
34  for opt in requiredOptions:
35  if opt not in self.options:
36  raise Exception("Option '%s' has not been registered." %opt )
37  self.commands[command_name] = requiredOptions
38 
39  def setup():
40  return
41 
42  def execute(self):
43  self.args = self.parser.parse_args()
44  executed = False
45  self.setup()
46  for k in self.commands.keys():
47  if getattr(self.args,k):
48  if executed:
49  print 'Ignoring command %s...' %k
50  else:
51  required_options = self.commands[k]
52  for o in required_options:
53  val = getattr(self.args,o)
54  if val is None:
55  raise Exception( "Required option '%s' has not been specified." %o )
56  func = getattr(self,k)
57  func()
58  executed = True
59  return executed
60 
62  def __init__(self):
63  CommandTool.__init__(self)
64  self.mgr = None
65  CommandTool.addOption(self,"name", "n", type=str, help="the o2o job name")
66  CommandTool.addOption(self,"configFile", "c", type=str, help="the JSON configuration file path")
67  CommandTool.addOption(self,"interval", "i", type=int, help="the chron job interval")
68  CommandTool.addOption(self,"db", type=str, help="the target database: pro ( for prod ) or dev ( for prep ). default=pro")
69  CommandTool.addOption(self,"auth","a", type=str, help="path of the authentication file")
70  CommandTool.addCommand( self,"create", "create a new O2O job", "name","configFile","interval")
71  CommandTool.addCommand(self,"setConfig","set a new configuration for the specified job","name","configFile" )
72  CommandTool.addCommand(self,"setInterval","set a new execution interval for the specified job","name","interval" )
73  CommandTool.addCommand(self,"enable","enable the O2O job","name" )
74  CommandTool.addCommand(self,"disable", "disable the O2O job" , "name")
75  CommandTool.addCommand(self,"migrate", "migrate the tag info for the jobs in configuration entries" )
76  CommandTool.addCommand(self,"listJobs", "list the registered jobs" )
77  CommandTool.addCommand(self,"listConf", "shows the configurations for the specified job", "name")
78 
79  def setup(self):
80  db_service = o2olib.prod_db_service
81  if self.args.db is not None:
82  if self.args.db == 'dev' or self.args.db == 'oradev' :
83  db_service = o2olib.dev_db_service
84  elif self.args.db != 'orapro' and self.args.db != 'onlineorapro' and self.args.db != 'pro':
85  raise Exception("Database '%s' is not known." %self.args.db )
86 
87  self.mgr = o2olib.O2OJobMgr()
88  return self.mgr.connect( db_service, self.args.auth )
89 
90  def create(self):
91  self.mgr.add( self.args.name, self.args.configFile, self.args.interval, True )
92 
93  def setConfig(self):
94  self.mgr.setConfig( self.args.name, self.args.configFile )
95 
96  def setInterval(self):
97  self.mgr.setConfig( self.args.name, self.args.interval )
98 
99  def enable(self):
100  self.mgr.setConfig( self.args.name, True )
101 
102  def disable(self):
103  self.mgr.setConfig( self.args.name, False )
104 
105  def migrate(self):
106  self.mgr.migrateConfig()
107 
108  def listJobs(self):
109  self.mgr.listJobs()
110 
111  def listConf(self):
112  self.mgr.listConfig( self.args.name )
113 
114 def main( argv ):
115 
116  tool = O2OMgrTool()
117  ret = False
118  try:
119  ret = tool.execute()
120  except Exception as e:
121  print e
122  return ret
123 
124 if __name__ == '__main__':
125 
126  sys.exit(main(sys.argv))
def main
Definition: o2oMgr.py:114
Definition: main.py:1