Author: tmckay
Date: 2011-02-24 20:44:18 +0000 (Thu, 24 Feb 2011)
New Revision: 4557
Modified:
trunk/cumin/bin/cumin
Log:
Provide options for cumin master script to control stdin, stderr, and stdout
for operation as a daemon or operation as a standard app.
BZ680625
Modified: trunk/cumin/bin/cumin
===================================================================
--- trunk/cumin/bin/cumin 2011-02-24 18:37:10 UTC (rev 4556)
+++ trunk/cumin/bin/cumin 2011-02-24 20:44:18 UTC (rev 4557)
@@ -36,22 +36,22 @@
break
msg += t
r.close()
- if msg:
+ if len(msg) > 0:
msg = "\n"+msg
- log.error("Error in options"+msg)
+ log.error("Error in options"+msg)
+ return True
def main():
-
+
# tuple indices, for clarity
PROCESS = 0
SECTION = 1
PROGRAM = 2
- enable_logging("cumin.master", logging.INFO, os.path.join(home,
"log", "master.log"))
-
parser = OptionParser()
- parser.add_option("--init-only", dest="init_only",
action="store_true", default=False)
+ parser.add_option("--init-only", dest="init_only",
action="store_true", default=False,
+ help="Check options and initialization, then return.")
parser.add_option("--webs", dest="webs",
default="web",
help="Configuration section names for cumin-web
instances."\
@@ -61,24 +61,50 @@
help="Configuration section names for cumin-data
instances."\
"\nEach value implies a separate cumin-data
instance.")
+ parser.add_option("--console", dest="console",
action="store_true", default=False,
+ help="Do not redirect stderr or stdout, and log to
stderr.")
+
+ parser.add_option("--devel", dest="devel",
action="store_true", default=False,
+ help="Option is ignored if --console is set."\
+ "\nRedirect stderr and stdout to files in "\
+ "$CUMIN_HOME/log if set and to /dev/null if not
set.")
+
# Trap exit from parser and save standard error for logging
# Then put stderr back to original value
- err = sys.stderr
r, w = os.pipe()
sys.stderr = os.fdopen(w,"w")
try:
(options, args) = parser.parse_args()
except:
- parse_errors = True
- else:
- parse_errors = False
+ options = args = None
sys.stderr.close()
- sys.stderr = err
+ sys.stderr = sys.__stderr__
- if parse_errors:
- log_parse_errors(r)
- return 1
+ # Parse may have failed, in which case make a quick check for options ourselves
+ if options != None:
+ console = options.console
+ devel = options.devel
+ else:
+ console = "--console" in sys.argv[1:]
+ devel = "--devel" in sys.argv[1:]
+ # Set up logging and IO before we go any further
+ new_stdout = new_stderr = new_stdin = None
+ if console:
+ enable_logging("cumin.master", logging.INFO, sys.stderr)
+ else:
+ enable_logging("cumin.master", logging.INFO, os.path.join(home,
"log", "master.log"))
+ new_stdin = open(os.devnull,"r")
+ if devel:
+ new_stderr = open(os.path.join(home, "log",
"master.stderr"), "a")
+ new_stdout = open(os.path.join(home, "log",
"master.stdout"), "a")
+ else:
+ new_stderr = new_stdout = open(os.devnull,"w")
+
+ # Parser exited, either on --help or with errors
+ if options == None:
+ return log_parse_errors(r)
+
if len(args) != 0:
log.error("Extra arguments:" + "".join([" "+arg for
arg in args]))
return 1
@@ -118,9 +144,11 @@
log.warn("Restarting:" + prog_string)
else:
log.info("Starting:" + prog_string)
- app[PROCESS] = subprocess.Popen(arg)
+ app[PROCESS] = subprocess.Popen(arg,
+ stderr=new_stderr,
+ stdout=new_stdout)
sleep(30)
-
+
finally:
for app in apps:
if app[PROCESS]: