Package omero :: Package plugins :: Module node
[hide private]
[frames] | no frames]

Source Code for Module omero.plugins.node

  1  #!/usr/bin/env python 
  2  """ 
  3   :author: Josh Moore, josh at glencoesoftware.com 
  4   
  5   OMERO Grid node controller 
  6   
  7   This is a python wrapper around icegridnode. 
  8   
  9   Copyright 2008 Glencoe Software, Inc.  All Rights Reserved. 
 10   Use is subject to license terms supplied in LICENSE.txt 
 11   
 12  """ 
 13   
 14  from omero.cli import Arguments, BaseControl 
 15  from omero_ext.strings import shlex 
 16  import re, os, sys, signal 
 17  from exceptions import Exception as Exc 
 18  from path import path 
 19   
 20  RE=re.compile("^\s*(\S*)\s*(start|stop|restart|status)\s*(\S*)\s*$") 
 21   
 22  #From: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157035 
23 -def tail_lines(filename,linesback=10,returnlist=0):
24 """Does what "tail -10 filename" would have done 25 Parameters:: 26 filename file to read 27 linesback Number of lines to read from end of file 28 returnlist Return a list containing the lines instead of a string 29 30 """ 31 avgcharsperline=75 32 33 file = open(filename,'r') 34 while 1: 35 try: file.seek(-1 * avgcharsperline * linesback,2) 36 except IOError: file.seek(0) 37 if file.tell() == 0: atstart=1 38 else: atstart=0 39 40 lines=file.read().split("\n") 41 if (len(lines) > (linesback+1)) or atstart: break 42 #The lines are bigger than we thought 43 avgcharsperline=avgcharsperline * 1.3 #Inc avg for retry 44 file.close() 45 46 if len(lines) > linesback: start=len(lines)-linesback -1 47 else: start=0 48 if returnlist: return lines[start:len(lines)-1] 49 50 out="" 51 for l in lines[start:len(lines)-1]: out=out + l + "\n" 52 return out
53 54
55 -class NodeControl(BaseControl):
56
57 - def help(self, args = None):
58 self.ctx.out( """ 59 Syntax: %(program_name)s node [node-name ] [sync] [ start | stop | status | restart ] 60 start -- Start the node via icegridnode. With sync doesn't return until reachable. 61 stop -- Stop the node via icegridadmin. With sync doesn't return until stopped. 62 status -- Prints a status message. Return code is non-zero if there is a problem. 63 restart -- Calls "sync start" then "stop" ("sync stop" if sync is specified) 64 65 node-name cannot be "start", "stop", "restart", "status", or "sync". 66 """ )
67
68 - def _likes(self, args):
69 args = Arguments(args) 70 first, other = args.firstOther() 71 return hasattr(self,first) or RE.match(args.join(" ")) and True or False
72
73 - def _noargs(self):
74 self.help()
75
76 - def __call__(self, *args):
77 args = Arguments(*args) 78 first, other = args.firstOther() 79 try: 80 name = self._node() 81 sync = False 82 acts = [] 83 84 if first == "sync": 85 # No master specified 86 sync = True 87 name = self._node() 88 acts.extend(other) 89 elif first == "start" or first == "stop" or first =="stop" or first == "kill" or first == "restart": 90 # Neither master nor sync specified. Defaults in effect 91 acts.append(first) 92 acts.extend(other) 93 else: 94 # Otherwise, command is name of master 95 name = first 96 # Check for sync 97 if len(other) > 0 and other[0] == "sync": 98 sync = True 99 other.pop(0) 100 acts.extend(other) 101 102 self._node(name) 103 if len(acts) == 0: 104 self.help() 105 else: 106 for act in acts: 107 c = getattr(self, act) 108 c(name, sync) 109 finally: 110 pass
111 112 #self.ctx.dbg(str(ex)) 113 #self.ctx.die(100, "Bad argument: "+ str(first) + ", " + ", ".join(other)) 114
115 - def _handleNZRC(self, nzrc):
116 """ 117 Set the return value from nzrc on the context, and print 118 out the last two lines of any error messages if present. 119 """ 120 props = self._properties() 121 self.ctx.rv = nzrc.rv 122 myoutput = self.dir / path(props["Ice.StdErr"]) 123 if not myoutput.exists(): 124 pass 125 else: 126 print "from %s:" % str(myoutput) 127 print tail_lines(str(myoutput),2)
128 129 130 ############################################## 131 # 132 # Commands : Since node plugin implements its own 133 # __call__() method, the pattern for the following 134 # commands is somewhat different. 135 # 136
137 - def start(self, name = None, sync = False):
138 139 self._initDir() 140 141 if name == None: 142 name = self._node() 143 144 try: 145 command = ["icegridnode", self._icecfg()] 146 if self._isWindows(): 147 command = command + ["--install","OMERO."+self._node()] 148 self.ctx.call(command) 149 self.ctx.call(["icegridnode","--start","OMERO."+self._node()]) 150 else: 151 command = command + ["--daemon", "--pidfile", str(self._pid()),"--nochdir"] 152 self.ctx.call(command) 153 except OSError, o: 154 msg = """%s\nPossibly an error finding "icegridnode". Try "icegridnode -h" from the command line.""" % o 155 raise Exc(msg) 156 except NonZeroReturnCode, nzrc: 157 self._handleNZRC(nzrc)
158
159 - def status(self, name = None):
160 161 if name == None: 162 name = self._node() 163 164 self.ctx.pub(["admin","status",name])
165
166 - def stop(self, name = None, sync = False):
167 if name == None: 168 name = self._node() 169 if self._isWindows(): 170 try: 171 command = ["icegridnode", "--stop", "OMERO."+self._node()] 172 self.ctx.call(command) 173 command = ["icegridnode", "--uninstall", "OMERO."+self._node()] 174 self.ctx.call(command) 175 except NonZeroReturnCode, nzrc: 176 self._handleNZRC(nzrc) 177 else: 178 pid = open(self._pid(),"r").readline() 179 os.kill(int(pid), signal.SIGQUIT)
180
181 - def kill(self, name = None, sync = False):
182 if name == None: 183 name = self._node() 184 pid = open(self._pid(),"r").readline() 185 os.kill(int(pid), signal.SIGKILL)
186 187 try: 188 register("node", NodeControl) 189 except NameError: 190 NodeControl()._main() 191