[28277] in Source-Commits

home help back first fref pref prev next nref lref last post

cupsys-config commit: Add Upstart job; tweak initscripts

daemon@ATHENA.MIT.EDU (Jonathan D Reed)
Sun Jun 22 12:28:22 2014

Date: Sun, 22 Jun 2014 12:28:13 -0400
From: Jonathan D Reed <jdreed@MIT.EDU>
Message-Id: <201406221628.s5MGSDrl005769@drugstore.mit.edu>
To: source-commits@MIT.EDU

https://github.com/mit-athena/cupsys-config/commit/ce9505455e4d8399b63510592bd291315c95b7bd
commit ce9505455e4d8399b63510592bd291315c95b7bd
Author: Jonathan Reed <jdreed@mit.edu>
Date:   Thu Jun 19 18:47:56 2014 -0400

    Add Upstart job; tweak initscripts
    
    Per (Trac: #1492), there is a weird race condition on Trusty, due
    to the avahi-cups-reload Upstart job reloading CUPS, and clashing
    with our restarting CUPS in configure-athena-printers.
    
    - Ship an upstart job "configure-athena-printers", which starts when
      CUPS is started, and tries up to 5 times to configure the printers.
      We are deliberately NOT using dh_installinit here, since this Upstart
      job can and should exist in parallel with the rc script, so we can
      easily support non-Upstart distros without adding the compatibility
      shims that dh_installinit wants.
    - Tweak configure-athena-printers to take a second argument ("init"),
      which indicates that the script was started from init, and thus it
      should expect CUPS to be running, and not try to restart it.
    - Tweak the initscript to be aware of the upstart job, and to not do
      anything if it looks like Upstart will deal.
    - Add a note for future generations in restart_cups.sh, should we
      ever decide to use that again.
    - Tell dh_installinit to start the initscript as late as possible

 debian/changelog                      |   10 ++++++
 debian/configure-athena-printers      |   23 +++++++++++---
 debian/configure-athena-printers.conf |   53 +++++++++++++++++++++++++++++++++
 debian/control                        |    2 +-
 debian/debathena-cupsys-config.init   |   30 +++++++++++++++++--
 debian/install                        |    1 +
 debian/restart-cups.sh                |    3 ++
 debian/rules                          |    3 ++
 8 files changed, 116 insertions(+), 9 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 96220ea..c3ca4fb 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,13 @@
+debathena-cupsys-config (1.19) unstable; urgency=low
+
+  * Ship an upstart job for Upstart-aware systems
+  * Make the sysv init script aware of the Upstart job, and defer to it
+  * Modify configure-athena-printers so it knows whether it's being called
+    from init or not; it will not attempt to start CUPS on its own if
+    called from init (Trac: #1492)
+
+ -- Jonathan Reed <jdreed@mit.edu>  Fri, 20 Jun 2014 08:36:26 -0400
+
 debathena-cupsys-config (1.18) unstable; urgency=low
 
   * Remove all vestiges of BrowsePolling (Trac: #1473)
diff --git a/debian/configure-athena-printers b/debian/configure-athena-printers
index 6b18384..68ee7cb 100755
--- a/debian/configure-athena-printers
+++ b/debian/configure-athena-printers
@@ -120,10 +120,23 @@ require_cups() {
     else
         invoke="/etc/init.d/$rcname"
     fi
+    # Even on Upstart-based systems, this can be used as
+    # a conditional, even though "status cups" cannot.
     if ! /etc/init.d/$rcname status; then
-	if ! $invoke start; then
-	    echo "FATAL: Couldn't start CUPS!"
-	    exit 1
+	if [ "$1" = "init" ]; then
+	    # We were started from the init script.  Init should
+	    # also have started CUPS. Oh well...
+	    if [ "$(lpstat -r)" != "scheduler is running" ]; then
+		echo "WARNING: CUPS not running; no printers added."
+		exit 2
+	    fi
+	else
+	    echo -n "Attempting to start cups..."
+	    if ! $invoke start; then
+		echo "FATAL: Couldn't start CUPS!"
+		exit 1
+	    fi
+	    echo "started."
 	fi
     fi
     if [ "$(lpstat -r)" != "scheduler is running" ]; then
@@ -134,11 +147,11 @@ require_cups() {
 
 case "$1" in
     add)
-	require_cups  
+	require_cups "$2"
 	add_printers
 	;;
     remove)
-	require_cups
+	require_cups "$2"
 	del_printers
 	;;
     *)
diff --git a/debian/configure-athena-printers.conf b/debian/configure-athena-printers.conf
new file mode 100644
index 0000000..d798156
--- /dev/null
+++ b/debian/configure-athena-printers.conf
@@ -0,0 +1,53 @@
+author "Massachusetts Institute of Technology <release-team@mit.edu>"
+description "Ensure MIT printers are configured"
+
+# This is an Upstart job to ensure Athena printers are configured.
+# It will be primarily used on Ubuntu, but could be used on any
+# Upstart system.  Its primary purpose is to avoid dumb race conditions
+# between Upstart and traditional rc scripts.
+#
+# Before doing anything to this file, please go read
+# /etc/init.d/debathena-cupsys-config and make sure you understand it
+
+respawn
+# up to 5 times in 10 seconds
+respawn limit 5 10
+
+console log
+
+start on started cups
+task
+
+script
+    if [ ! -f /var/lib/debathena-cupsys-config-reconfigure_required ]; then
+        exit 0
+    fi
+    echo "configure-athena-printers: start at $(date)" >&2
+    echo "MIT Printer reconfiguration required." >&2
+    nologin=0
+    if [ ! -f /var/run/athena-nologin ]; then
+        # Block the greeter
+        touch /var/run/athena-nologin
+        nologin=1
+    fi
+    status=0
+    if /usr/lib/debathena-cupsys-config/configure-athena-printers add init; then
+        rm -f /var/lib/debathena-cupsys-config-reconfigure_required
+        echo "MIT printer configuration successful." >&2
+    else
+        status=$?
+        if [ "$status" = "2" ]; then
+            echo "Configuration failed because CUPS was not running; will respawn" >&2
+        else
+            echo "MIT printer configuration failed." >&2
+        fi
+        # Delay before Upstart respawns us
+        sleep 1
+    fi
+    # Only remove the nologin file if we created it
+    if [ $nologin -eq 1 ]; then
+        rm /var/run/athena-nologin
+    fi
+    echo "configure-athena-printers: end at $(date) with status $status" >&2
+    exit $status
+end script
diff --git a/debian/control b/debian/control
index 90bf08a..f145e22 100644
--- a/debian/control
+++ b/debian/control
@@ -2,7 +2,7 @@ Source: debathena-cupsys-config
 Section: debathena-config/net
 Priority: extra
 Maintainer: Debathena Project <debathena@mit.edu>
-Build-Depends: debhelper (>= 7), config-package-dev (>= 5.0~)
+Build-Depends: debhelper (>= 7.0.50~), config-package-dev (>= 5.0~)
 Standards-Version: 3.9.3
 
 Package: debathena-cupsys-config
diff --git a/debian/debathena-cupsys-config.init b/debian/debathena-cupsys-config.init
index 7f91070..0411535 100755
--- a/debian/debathena-cupsys-config.init
+++ b/debian/debathena-cupsys-config.init
@@ -13,6 +13,25 @@
 
 . /lib/lsb/init-functions
 
+using_upstart() {
+    local JOB_NAME=configure-athena-printers
+    # Do our best to determine if the upstart job is likely to run
+    # If the job doesn't exist, we're not using upstart
+    [ -f "/etc/init/${JOB_NAME}.conf" ] || return 1
+    # If there's no initctl, we're not using upstart
+    hash initctl 2>/dev/null || return 1
+    # If initctl doesn't know about the job, we're not using upstart
+    initctl status "$JOB_NAME" || return 1
+    # At this point, it is highly likely we're using Upstart.  If we're not,
+    # something is horribly wrong.
+    return 0
+}
+
+if using_upstart; then
+    log_success_msg "debathena-cupsys-config: Skipping rc in favor of Upstart"
+    exit 0
+fi
+
 case "$1" in
   start)
     if [ -f /var/lib/debathena-cupsys-config-reconfigure_required ]; then
@@ -22,11 +41,16 @@ case "$1" in
 	touch /var/run/athena-nologin
 	nologin=1
       fi
-      if /usr/lib/debathena-cupsys-config/configure-athena-printers add; then
+      if /usr/lib/debathena-cupsys-config/configure-athena-printers add init; then
 	rm -f /var/lib/debathena-cupsys-config-reconfigure_required
-	log_end_msg 0
+	log_success_msg "Added MIT printers"
       else
-	log_end_msg 1
+	status=$?
+	if [ "$status" = "2" ]; then
+	  log_warning_msg "CUPS not running, could not add MIT printers"
+	else
+	  log_failure_msg "Failed to add MIT printers"
+	fi
       fi
       [ $nologin -eq 1 ] && rm /var/run/athena-nologin
     fi
diff --git a/debian/install b/debian/install
index 9e2d404..0f95054 100644
--- a/debian/install
+++ b/debian/install
@@ -1,2 +1,3 @@
 debian/restart-cups.sh usr/lib/debathena-cupsys-config
 debian/configure-athena-printers usr/lib/debathena-cupsys-config
+debian/configure-athena-printers.conf etc/init
diff --git a/debian/restart-cups.sh b/debian/restart-cups.sh
index 4f9a8bd..ff76274 100644
--- a/debian/restart-cups.sh
+++ b/debian/restart-cups.sh
@@ -5,6 +5,9 @@ restart_cups()
     # hilarity, we'll keep this hook around, but only restart
     # CUPS if it's running, and don't error out if we can't.
 
+    # Note that this will likely end badly on Trusty+, due to the
+    # avahi-cups-reload Upstart job.  Test it before making changes.
+
     # It's not 2008 anymore, there's nothing named "cupsys"
     rcname=cups
     if /etc/init.d/$rcname status; then
diff --git a/debian/rules b/debian/rules
index a089a9e..e750e84 100755
--- a/debian/rules
+++ b/debian/rules
@@ -2,3 +2,6 @@
 
 %:
 	dh $@ --with config-package
+
+override_dh_installinit:
+	dh_installinit --no-start --update-rcd-params="defaults 90 10"

home help back first fref pref prev next nref lref last post