[Checkins] SVN: zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/ If using chkconfig, then set stdin /dev/null to avoid

Jim Fulton jim at zope.com
Tue Dec 5 15:48:09 EST 2006


Log message for revision 71421:
  If using chkconfig, then set stdin /dev/null to avoid
  a bug on some versions of centos.  If the script is run automatically,
  then there's no one to specify any input anyway.
  
  If no user is specified, then ommit the root check.  This is useful
  for development.
  
  Allow arbitrary commands.  The ability to get to stuff like fg and
  rotate seems to outweigh the benefit of error checking.
  

Changed:
  U   zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/README.txt
  U   zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/__init__.py

-=-
Modified: zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/README.txt
===================================================================
--- zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/README.txt	2006-12-05 20:48:04 UTC (rev 71420)
+++ zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/README.txt	2006-12-05 20:48:08 UTC (rev 71421)
@@ -51,17 +51,7 @@
     # NOT be installed as a system startup script!
     <BLANKLINE>
     <BLANKLINE>
-    if [ $(whoami) != "root" ]; then
-      echo "You must be root."
-      exit 1
-    fi
-    <BLANKLINE>
     case $1 in 
-      start|status)
-    <BLANKLINE>
-        /opt/zope/bin/zopectl -C /etc/zope.conf $*
-    <BLANKLINE>
-        ;;
       stop)
     <BLANKLINE>
         /opt/zope/bin/zopectl -C /etc/zope.conf $*
@@ -75,8 +65,9 @@
     <BLANKLINE>
         ;;
       *) 
-        echo "Usage: ${0} [ start | stop | status | restart ]"
-        exit 1
+    <BLANKLINE>
+        /opt/zope/bin/zopectl -C /etc/zope.conf $*
+    <BLANKLINE>
         ;;
     esac
     <BLANKLINE>
@@ -131,20 +122,11 @@
     # description: please, please work
     <BLANKLINE>
     <BLANKLINE>
-    if [ $(whoami) != "root" ]; then
-      echo "You must be root."
-      exit 1
-    fi
-    <BLANKLINE>
     case $1 in 
-      start|status)
-    <BLANKLINE>
-        /opt/zope/bin/zopectl -C /etc/zope.conf $*
-    <BLANKLINE>
-        ;;
       stop)
     <BLANKLINE>
-        /opt/zope/bin/zopectl -C /etc/zope.conf $*
+        /opt/zope/bin/zopectl -C /etc/zope.conf $* \
+          </dev/null
     <BLANKLINE>
         ;;
       restart)
@@ -155,8 +137,10 @@
     <BLANKLINE>
         ;;
       *) 
-        echo "Usage: ${0} [ start | stop | status | restart ]"
-        exit 1
+    <BLANKLINE>
+        /opt/zope/bin/zopectl -C /etc/zope.conf $* \
+          </dev/null
+    <BLANKLINE>
         ;;
     esac
     <BLANKLINE>
@@ -199,16 +183,11 @@
     fi
     <BLANKLINE>
     case $1 in 
-      start|status)
-    <BLANKLINE>
-        su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/zope.conf $*"
-    <BLANKLINE>
-        ;;
       stop)
     <BLANKLINE>
         su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/zope.conf $*"
+          "/opt/zope/bin/zopectl -C /etc/zope.conf $*" \
+          </dev/null
     <BLANKLINE>
         ;;
       restart)
@@ -219,8 +198,11 @@
     <BLANKLINE>
         ;;
       *) 
-        echo "Usage: ${0} [ start | stop | status | restart ]"
-        exit 1
+    <BLANKLINE>
+        su zope -c \
+          "/opt/zope/bin/zopectl -C /etc/zope.conf $*" \
+          </dev/null
+    <BLANKLINE>
         ;;
     esac
     <BLANKLINE>
@@ -229,6 +211,74 @@
 script is included in double quotes, it can't contain double
 quotes. (The recipe makes no attempt to escape double quotes.)
 
+Also note that now the script must be run as root, so the generated
+script checks that root is running it.
+
+If we say the user is root:
+
+    >>> write('buildout.cfg',
+    ... """
+    ... [buildout]
+    ... parts = zoperc
+    ...
+    ... [zoperc]
+    ... recipe = zc.recipe.rhrc
+    ... parts = zope
+    ... dest = %(dest)s
+    ... chkconfig = 345 90 10
+    ... chkconfigcommand = echo
+    ... user = root
+    ...
+    ... [zope]
+    ... run-script = /opt/zope/bin/zopectl -C /etc/zope.conf
+    ... """ % dict(dest=demo))
+
+
+Then the generated script won't su, but it will still check that root
+is running it:
+
+    >>> print system('bin/buildout'),
+    buildout: Uninstalling zoperc
+    buildout: Installing zoperc
+    --add zoperc
+
+    >>> cat(demo, 'zoperc')
+    #!/bin/sh 
+    <BLANKLINE>
+    # the next line is for chkconfig
+    # chkconfig: 345 90 10
+    # description: please, please work
+    <BLANKLINE>
+    <BLANKLINE>
+    if [ $(whoami) != "root" ]; then
+      echo "You must be root."
+      exit 1
+    fi
+    <BLANKLINE>
+    case $1 in 
+      stop)
+    <BLANKLINE>
+        /opt/zope/bin/zopectl -C /etc/zope.conf $* \
+          </dev/null
+    <BLANKLINE>
+        ;;
+      restart)
+    <BLANKLINE>
+        ${0} stop
+        sleep 1
+        ${0} start
+    <BLANKLINE>
+        ;;
+      *) 
+    <BLANKLINE>
+        /opt/zope/bin/zopectl -C /etc/zope.conf $* \
+          </dev/null
+    <BLANKLINE>
+        ;;
+    esac
+    <BLANKLINE>
+
+
 A part that defines a run script can also define environment-variable
 settings to be used by the rc script by supplying an env option:
 
@@ -269,18 +319,12 @@
     fi
     <BLANKLINE>
     case $1 in 
-      start|status)
-    <BLANKLINE>
-        LD_LIBRARY_PATH=/opt/foolib \
-          su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/zope.conf $*"
-    <BLANKLINE>
-        ;;
       stop)
     <BLANKLINE>
         LD_LIBRARY_PATH=/opt/foolib \
           su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/zope.conf $*"
+          "/opt/zope/bin/zopectl -C /etc/zope.conf $*" \
+          </dev/null
     <BLANKLINE>
         ;;
       restart)
@@ -291,8 +335,12 @@
     <BLANKLINE>
         ;;
       *) 
-        echo "Usage: ${0} [ start | stop | status | restart ]"
-        exit 1
+    <BLANKLINE>
+        LD_LIBRARY_PATH=/opt/foolib \
+          su zope -c \
+          "/opt/zope/bin/zopectl -C /etc/zope.conf $*" \
+          </dev/null
+    <BLANKLINE>
         ;;
     esac
     <BLANKLINE>
@@ -342,26 +390,17 @@
     fi
     <BLANKLINE>
     case $1 in 
-      start|status)
-    <BLANKLINE>
-        LD_LIBRARY_PATH=/opt/foolib \
-          su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*"
-    <BLANKLINE>
-        LD_LIBRARY_PATH=/opt/foolib \
-          su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*"
-    <BLANKLINE>
-        ;;
       stop)
     <BLANKLINE>
         LD_LIBRARY_PATH=/opt/foolib \
           su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*"
+          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*" \
+          </dev/null
     <BLANKLINE>
         LD_LIBRARY_PATH=/opt/foolib \
           su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*"
+          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*" \
+          </dev/null
     <BLANKLINE>
         ;;
       restart)
@@ -372,8 +411,17 @@
     <BLANKLINE>
         ;;
       *) 
-        echo "Usage: ${0} [ start | stop | status | restart ]"
-        exit 1
+    <BLANKLINE>
+        LD_LIBRARY_PATH=/opt/foolib \
+          su zope -c \
+          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*" \
+          </dev/null
+    <BLANKLINE>
+        LD_LIBRARY_PATH=/opt/foolib \
+          su zope -c \
+          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*" \
+          </dev/null
+    <BLANKLINE>
         ;;
     esac
     <BLANKLINE>
@@ -402,13 +450,6 @@
     fi
     <BLANKLINE>
     case $1 in 
-      start|status)
-    <BLANKLINE>
-        LD_LIBRARY_PATH=/opt/foolib \
-          su zope -c \
-          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*"
-    <BLANKLINE>
-        ;;
       stop)
     <BLANKLINE>
         LD_LIBRARY_PATH=/opt/foolib \
@@ -424,8 +465,11 @@
     <BLANKLINE>
         ;;
       *) 
-        echo "Usage: ${0} [ start | stop | status | restart ]"
-        exit 1
+    <BLANKLINE>
+        LD_LIBRARY_PATH=/opt/foolib \
+          su zope -c \
+          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*"
+    <BLANKLINE>
         ;;
     esac
     <BLANKLINE>
@@ -491,26 +535,17 @@
     fi
     <BLANKLINE>
     case $1 in 
-      start|status)
-    <BLANKLINE>
-        LD_LIBRARY_PATH=/opt/foolib \
-          su acme -c \
-          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*"
-    <BLANKLINE>
-        LD_LIBRARY_PATH=/opt/foolib \
-          su acme -c \
-          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*"
-    <BLANKLINE>
-        ;;
       stop)
     <BLANKLINE>
         LD_LIBRARY_PATH=/opt/foolib \
           su acme -c \
-          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*"
+          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*" \
+          </dev/null
     <BLANKLINE>
         LD_LIBRARY_PATH=/opt/foolib \
           su acme -c \
-          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*"
+          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*" \
+          </dev/null
     <BLANKLINE>
         ;;
       restart)
@@ -521,8 +556,17 @@
     <BLANKLINE>
         ;;
       *) 
-        echo "Usage: ${0} [ start | stop | status | restart ]"
-        exit 1
+    <BLANKLINE>
+        LD_LIBRARY_PATH=/opt/foolib \
+          su acme -c \
+          "/opt/zope/bin/zopectl -C /etc/instance1.conf $*" \
+          </dev/null
+    <BLANKLINE>
+        LD_LIBRARY_PATH=/opt/foolib \
+          su acme -c \
+          "/opt/zope/bin/zopectl -C /etc/instance2.conf $*" \
+          </dev/null
+    <BLANKLINE>
         ;;
     esac
     <BLANKLINE>

Modified: zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/__init__.py
===================================================================
--- zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/__init__.py	2006-12-05 20:48:04 UTC (rev 71420)
+++ zc.recipe.rhrc/trunk/src/zc/recipe/rhrc/__init__.py	2006-12-05 20:48:08 UTC (rev 71421)
@@ -43,6 +43,8 @@
         scripts = options['scripts'].split('\n')
         chkconfig = self.options.get('chkconfig')
         user = options.get('user', '')
+        if user == 'root':
+            user = '' # no need to su to root
         envs = options['envs'].split('\n')
         created = []
         try:
@@ -57,6 +59,8 @@
                 env = envs[0]
                 if env:
                     script = env + ' \\\n      ' + script
+                if chkconfig:
+                    script += ' \\\n      </dev/null'
                 self.output(chkconfig, script, self.name, created)
             else:
                 cooked = []
@@ -73,6 +77,11 @@
                     
                 for part, script in zip(parts, cooked):
                     self.output('', script, self.name+'-'+part, created)
+
+                if chkconfig:
+                    cooked = [s + ' \\\n      </dev/null'
+                              for s in cooked]
+                    
                 script = '\n\n    '.join(cooked)
                 cooked.reverse()
                 rscript = '\n\n    '.join(cooked)
@@ -86,6 +95,7 @@
         if rscript is None:
             rscript = script
         rc = rc_template % dict(
+            rootcheck = self.options.get('user') and rootcheck or '',
             CHKCONFIG = (chkconfig
                          and (chkconfig_template % chkconfig)
                          or non_chkconfig_template),
@@ -117,21 +127,18 @@
 # NOT be installed as a system startup script!
 '''
 
-rc_template = """#!/bin/sh 
-
-%(CHKCONFIG)s
-
+rootcheck = """
 if [ $(whoami) != "root" ]; then
   echo "You must be root."
   exit 1
 fi
+"""
 
+rc_template = """#!/bin/sh 
+
+%(CHKCONFIG)s
+%(rootcheck)s
 case $1 in 
-  start|status)
-  
-    %(CTL_SCRIPT)s
-
-    ;;
   stop)
   
     %(CTL_SCRIPT_R)s
@@ -145,8 +152,9 @@
 
     ;;
   *) 
-    echo "Usage: ${0} [ start | stop | status | restart ]"
-    exit 1
+  
+    %(CTL_SCRIPT)s
+
     ;;
 esac
 



More information about the Checkins mailing list