From 58a55777cdbae9fe99f2ea58c6df70788c78d118 Mon Sep 17 00:00:00 2001
From: Felix Dreissig <f30@f30.me>
Date: Thu, 7 Jul 2016 09:31:18 +0200
Subject: [PATCH 1/5] Move settings irrelevant to systemd from environment file
 to init script

systemd environment files do not support variable substitutions, so
settings like `ZOOCFGDIR` have to be copied.
---
 debian/conf_example/environment | 27 +++++++++------------------
 debian/zookeeperd.init          | 10 ++++++++--
 debian/zookeeperd.upstart       |  2 +-
 3 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/debian/conf_example/environment b/debian/conf_example/environment
index be495fd..cbd8404 100644
--- a/debian/conf_example/environment
+++ b/debian/conf_example/environment
@@ -1,20 +1,11 @@
-NAME=zookeeper
-ZOOCFGDIR=/etc/$NAME/conf
-
-# TODO this is really ugly
-# How to find out, which jars are needed?
-# seems, that log4j requires the log4j.properties file to be in the classpath
-CLASSPATH="$ZOOCFGDIR:/usr/share/java/jline.jar:/usr/share/java/log4j-1.2.jar:/usr/share/java/xercesImpl.jar:/usr/share/java/xmlParserAPIs.jar:/usr/share/java/netty.jar:/usr/share/java/slf4j-api.jar:/usr/share/java/slf4j-log4j12.jar:/usr/share/java/zookeeper.jar"
-
-ZOOCFG="$ZOOCFGDIR/zoo.cfg"
-ZOO_LOG_DIR=/var/log/$NAME
-USER=$NAME
-GROUP=$NAME
-PIDDIR=/var/run/$NAME
-PIDFILE=$PIDDIR/$NAME.pid
-SCRIPTNAME=/etc/init.d/$NAME
-JAVA=/usr/bin/java
-ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
-ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
+ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
+ZOOCFG=/etc/zookeeper/conf/zoo.cfg
+ZOO_LOG_DIR=/var/log/zookeeper
+ZOO_LOG4J_PROP=INFO,ROLLINGFILE
 JMXLOCALONLY=false
 JAVA_OPTS=""
+
+# TODO: This is really ugly
+# How to find out which jars are needed?
+# Seems that log4j requires the log4j.properties file to be in the classpath
+CLASSPATH="/etc/zookeeper/conf:/usr/share/java/jline.jar:/usr/share/java/log4j-1.2.jar:/usr/share/java/xercesImpl.jar:/usr/share/java/xmlParserAPIs.jar:/usr/share/java/netty.jar:/usr/share/java/slf4j-api.jar:/usr/share/java/slf4j-log4j12.jar:/usr/share/java/zookeeper.jar"
diff --git a/debian/zookeeperd.init b/debian/zookeeperd.init
index 444789a..e76ff27 100644
--- a/debian/zookeeperd.init
+++ b/debian/zookeeperd.init
@@ -27,8 +27,14 @@
 
 # PATH should only include /usr/* if it runs after the mountnfs.sh script
 PATH=/sbin:/usr/sbin:/bin:/usr/bin
-DESC="centralized coordination service"
-SCRIPTNAME=/etc/init.d/$NAME
+DESC="Coordination service for distributed applications"
+NAME=zookeeper
+USER=$NAME
+GROUP=$NAME
+JAVA=/usr/bin/java
+PIDDIR="/var/run/$NAME"
+PIDFILE="$PIDDIR/$NAME.pid"
+SCRIPTNAME="/etc/init.d/$NAME"
 
 # Read configuration variable file if it is present
 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
diff --git a/debian/zookeeperd.upstart b/debian/zookeeperd.upstart
index 167ae02..7c7b729 100644
--- a/debian/zookeeperd.upstart
+++ b/debian/zookeeperd.upstart
@@ -1,4 +1,4 @@
-description "zookeeper centralized coordination service"
+description "ZooKeeper coordination service for distributed applications"
 
 start on runlevel [2345]
 stop on runlevel [!2345]
-- 
2.8.1


From 5ed3c9f7ec92cb9ad6eea7851786722b50acb420 Mon Sep 17 00:00:00 2001
From: Felix Dreissig <f30@f30.me>
Date: Thu, 7 Jul 2016 09:52:08 +0200
Subject: [PATCH 2/5] Remove support for JMXDISABLE

It wasn't really documented for the Debian package (not used in any
default configuration file for service start-up) and is hard to
replicate with a systemd unit file.
---
 debian/zookeeperd.init | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/debian/zookeeperd.init b/debian/zookeeperd.init
index e76ff27..b596cd4 100644
--- a/debian/zookeeperd.init
+++ b/debian/zookeeperd.init
@@ -112,16 +112,10 @@ do_stop()
 
 case "$1" in
   start)
-        if [ "x$JMXDISABLE" = "x" ]
-        then
-            [ "$VERBOSE" != no ] && log_action_msg "$NAME: JMX enabled by default"
-            # for some reason these two options are necessary on jdk6 on Ubuntu
-            #   accord to the docs they are not necessary, but otw jconsole cannot
-            #   do a local attach
-            JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY"
-        else
-            [ "$VERBOSE" != no ] && log_action_msg "$NAME: JMX disabled by user request"
-        fi
+	# For some reason, these two options are necessary on JDK6 on Ubuntu
+	# According to the docs they are not necessary, but otherwise jconsole
+	# cannot do a local attach
+	JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY"
 
 	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
 	do_start
-- 
2.8.1


From 66cee8251277d3176e56e2fb2f3ea19ae1b6d253 Mon Sep 17 00:00:00 2001
From: Felix Dreissig <f30@f30.me>
Date: Thu, 7 Jul 2016 10:07:33 +0200
Subject: [PATCH 3/5] Remove misleading example from default file

This will not work when used as systemd environment file, as variable
substitutions are not supported in that case.
---
 debian/zookeeperd.default | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/debian/zookeeperd.default b/debian/zookeeperd.default
index 9eacc73..4843899 100644
--- a/debian/zookeeperd.default
+++ b/debian/zookeeperd.default
@@ -5,7 +5,3 @@
 #JMXLOCALONLY=false
 
 #JAVA_OPTS=""
-
-# Make sure to include the original classpath since it contains the zookeeper
-# jars
-#CLASSPATH="$CLASSPATH:YOURCLASSPATH"
-- 
2.8.1


From 5ee7d96c4a96fea6aa45f56f58cec77fe51e3cc5 Mon Sep 17 00:00:00 2001
From: Felix Dreissig <f30@f30.me>
Date: Thu, 7 Jul 2016 10:57:46 +0200
Subject: [PATCH 4/5] Add systemd unit file for zookeeperd

---
 debian/control            |  1 +
 debian/rules              |  2 +-
 debian/zookeeperd.service | 18 ++++++++++++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 debian/zookeeperd.service

diff --git a/debian/control b/debian/control
index 7488951..2df6d0e 100644
--- a/debian/control
+++ b/debian/control
@@ -11,6 +11,7 @@ Build-Depends:
  debhelper (>= 9~),
  default-jdk (>= 2:1.6),
  default-jdk-doc,
+ dh-systemd,
  help2man,
  ivy-debian-helper,
  javacc,
diff --git a/debian/rules b/debian/rules
index d69ce5e..b6a0f0a 100755
--- a/debian/rules
+++ b/debian/rules
@@ -16,7 +16,7 @@ ANT_ARGS = -Dversion=$(DEB_UPSTREAM_VERSION) \
            -Dant.build.javac.target=1.5
 
 %:
-	dh $@ --buildsystem=ivy --with javahelper --with python2
+	dh $@ --buildsystem=ivy --with javahelper --with python2 --with systemd
 
 override_dh_auto_build:
 	# TODO exclude the fatjar contrib from upstream tarball and include the rest contrib
diff --git a/debian/zookeeperd.service b/debian/zookeeperd.service
new file mode 100644
index 0000000..6137014
--- /dev/null
+++ b/debian/zookeeperd.service
@@ -0,0 +1,18 @@
+[Unit]
+Description=Coordination service for distributed applications
+After=network.target
+ConditionPathExists=/var/lib/zookeeper/myid
+
+[Service]
+Type=simple
+User=zookeeper
+SyslogIdentifier=zookeeper
+EnvironmentFile=/etc/zookeeper/conf/environment
+EnvironmentFile=-/etc/default/zookeeper
+ExecStart=/usr/bin/java -cp ${CLASSPATH} $JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=${JMXLOCALONLY} -Dzookeeper.log.dir=${ZOO_LOG_DIR} -Dzookeeper.root.logger=${ZOO_LOG4J_PROP} $ZOOMAIN $ZOOCFG
+SuccessExitStatus=143
+# ZooKeeper is "fail-fast", see https://zookeeper.apache.org/doc/r3.4.8/zookeeperAdmin.html#sc_supervision
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
-- 
2.8.1


From 2ba87fc47d17f2cbe61337774e8e584b987adbd0 Mon Sep 17 00:00:00 2001
From: Felix Dreissig <f30@f30.me>
Date: Thu, 7 Jul 2016 13:53:05 +0200
Subject: [PATCH 5/5] Use `--name` option for dh_installinit and
 dh_systemd_enable

systemd unit files are currently installed by both programs (see Debian
bug #830208).
dh_systemd_enable does not support dh_installinit's `-d` option, so that
"zookeeper.service" and "zookeeperd.service" would both get installed.
Using `--name` fixes that, but of course also requires the source file
names to match the right format.
---
 debian/rules                                                | 5 ++++-
 debian/{zookeeperd.default => zookeeperd.zookeeper.default} | 0
 debian/{zookeeperd.init => zookeeperd.zookeeper.init}       | 0
 debian/{zookeeperd.service => zookeeperd.zookeeper.service} | 0
 debian/{zookeeperd.upstart => zookeeperd.zookeeper.upstart} | 0
 5 files changed, 4 insertions(+), 1 deletion(-)
 rename debian/{zookeeperd.default => zookeeperd.zookeeper.default} (100%)
 rename debian/{zookeeperd.init => zookeeperd.zookeeper.init} (100%)
 rename debian/{zookeeperd.service => zookeeperd.zookeeper.service} (100%)
 rename debian/{zookeeperd.upstart => zookeeperd.zookeeper.upstart} (100%)

diff --git a/debian/rules b/debian/rules
index b6a0f0a..f58e45c 100755
--- a/debian/rules
+++ b/debian/rules
@@ -139,4 +139,7 @@ override_dh_auto_clean:
 	rm -rf src/c/TEST-*
 
 override_dh_installinit:
-	dh_installinit -d
+	dh_installinit --name=zookeeper
+
+override_dh_systemd_enable:
+	dh_systemd_enable --name=zookeeper
diff --git a/debian/zookeeperd.default b/debian/zookeeperd.zookeeper.default
similarity index 100%
rename from debian/zookeeperd.default
rename to debian/zookeeperd.zookeeper.default
diff --git a/debian/zookeeperd.init b/debian/zookeeperd.zookeeper.init
similarity index 100%
rename from debian/zookeeperd.init
rename to debian/zookeeperd.zookeeper.init
diff --git a/debian/zookeeperd.service b/debian/zookeeperd.zookeeper.service
similarity index 100%
rename from debian/zookeeperd.service
rename to debian/zookeeperd.zookeeper.service
diff --git a/debian/zookeeperd.upstart b/debian/zookeeperd.zookeeper.upstart
similarity index 100%
rename from debian/zookeeperd.upstart
rename to debian/zookeeperd.zookeeper.upstart
-- 
2.8.1

