From cdd577b9afb31e7b7f2aa85ec7282c622bea25f3 Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Tue, 3 Jun 2014 21:33:30 +0200 Subject: [PATCH 1/9] feat(dpkg): script for building Debian package Only tested on MacOSX. --- build-deb.sh | 32 ++++++++++++++++++++++++++++++++ deb/DEBIAN/control | 7 +++++++ deb/DEBIAN/postinst | 16 ++++++++++++++++ deb/DEBIAN/postrm | 4 ++++ deb/DEBIAN/preinst | 6 ++++++ deb/DEBIAN/prerm | 6 ++++++ deb/etc/init/statsdaemon.conf | 11 +++++++++++ 7 files changed, 82 insertions(+) create mode 100755 build-deb.sh create mode 100644 deb/DEBIAN/control create mode 100755 deb/DEBIAN/postinst create mode 100755 deb/DEBIAN/postrm create mode 100755 deb/DEBIAN/preinst create mode 100755 deb/DEBIAN/prerm create mode 100644 deb/etc/init/statsdaemon.conf diff --git a/build-deb.sh b/build-deb.sh new file mode 100755 index 0000000..05b9811 --- /dev/null +++ b/build-deb.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# Inspired by http://ubuntuforums.org/showthread.php?t=910717. + +if [ "$#" -ne 2 ]; then + echo "$0 <386|amd64>" + echo + echo "where version is something like 'X.Y-Z'." + exit 1 +fi + +VERSION=$1 +BASEDIR=statsdaemon_$VERSION +ARCH=$2 + +# Fail early +set -e errexit + +GOOS=linux GOARCH=$ARCH go build statsdaemon.go + +if [ -d $BASEDIR ];then + rm -frv $BASEDIR +fi +cp -r deb $BASEDIR +mkdir -pv $BASEDIR/usr/local/bin +cp -v statsdaemon $BASEDIR/usr/local/bin + +sed "s/VERSION/$VERSION/g" deb/DEBIAN/control | sed "s/ARCH/$ARCH/g" > $BASEDIR/DEBIAN/control + +if [ -e ${BASEDIR}.deb ];then + rm -v ${BASEDIR}.deb +fi +dpkg-deb --build $BASEDIR diff --git a/deb/DEBIAN/control b/deb/DEBIAN/control new file mode 100644 index 0000000..2fbb7f9 --- /dev/null +++ b/deb/DEBIAN/control @@ -0,0 +1,7 @@ +Package: statsdaemon +Version: VERSION +Section: base +Priority: optional +Architecture: ARCH +Maintainer: Jens Rantil +Description: A binary statsd server implemented in Go. diff --git a/deb/DEBIAN/postinst b/deb/DEBIAN/postinst new file mode 100755 index 0000000..3487a4c --- /dev/null +++ b/deb/DEBIAN/postinst @@ -0,0 +1,16 @@ +#!/bin/bash +set -o errexit + +USER=statsdaemon +USER_HOME=/tmp + +if [ "$1" = configure ]; then + adduser --system \ + --quiet \ + --home "$USER_HOME" \ + --no-create-home \ + --disabled-password \ + --group "$USER" +fi + +service statsdaemon start || echo "" diff --git a/deb/DEBIAN/postrm b/deb/DEBIAN/postrm new file mode 100755 index 0000000..c4a848c --- /dev/null +++ b/deb/DEBIAN/postrm @@ -0,0 +1,4 @@ +#!/bin/bash +set -e errexit + +deluser statsdaemon || echo "" diff --git a/deb/DEBIAN/preinst b/deb/DEBIAN/preinst new file mode 100755 index 0000000..d856b69 --- /dev/null +++ b/deb/DEBIAN/preinst @@ -0,0 +1,6 @@ +#!/bin/bash +set -o errexit + +if [ -e /etc/init/statsdaemon.conf ]; then + service statsdaemon stop || echo "" +fi diff --git a/deb/DEBIAN/prerm b/deb/DEBIAN/prerm new file mode 100755 index 0000000..d856b69 --- /dev/null +++ b/deb/DEBIAN/prerm @@ -0,0 +1,6 @@ +#!/bin/bash +set -o errexit + +if [ -e /etc/init/statsdaemon.conf ]; then + service statsdaemon stop || echo "" +fi diff --git a/deb/etc/init/statsdaemon.conf b/deb/etc/init/statsdaemon.conf new file mode 100644 index 0000000..e19a37d --- /dev/null +++ b/deb/etc/init/statsdaemon.conf @@ -0,0 +1,11 @@ +# statsdaemon - binary statsd server. + +description "statsdaemon server" + +start on runlevel [2345] +stop on runlevel [!2345] +respawn +respawn limit 360 180 +setuid statsdaemon +setuid statsdaemon +exec /usr/local/bin/statsdaemon From 4ad6402359d2669396e1c3600ae6823e1b844206 Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Tue, 3 Jun 2014 21:34:50 +0200 Subject: [PATCH 2/9] doc(dpkg/readme): How to create Deb package --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 46643ce..14acbe8 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,13 @@ Usage of ./statsdaemon: -receive-counter="": Metric name for total metrics recevied per interval -version=false: print version string ``` + +Building a Debian package +========================= +In your terminal, execute: + + ./build-deb.sh <386|amd64> + +where ```` is something like `0.5.0-5`. To compile this on mac you +need to install ``dpkg`` using either Homebrew or Ports. You also need support +for cross compiling Go applications. From 14c8e45fd89fd82c11d5fd44c7b6dd098200a261 Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Tue, 3 Jun 2014 21:51:36 +0200 Subject: [PATCH 3/9] feat(dpkg): support custom server flags --- .gitignore | 2 +- deb/etc/default/statsdaemon | 3 +++ deb/etc/init/statsdaemon.conf | 9 ++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 deb/etc/default/statsdaemon diff --git a/.gitignore b/.gitignore index 16ff669..0b0e183 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -statsdaemon +/statsdaemon diff --git a/deb/etc/default/statsdaemon b/deb/etc/default/statsdaemon new file mode 100644 index 0000000..c73e896 --- /dev/null +++ b/deb/etc/default/statsdaemon @@ -0,0 +1,3 @@ +# Additional settings for statsdaemon + +STATSDAEMON_OPTS= diff --git a/deb/etc/init/statsdaemon.conf b/deb/etc/init/statsdaemon.conf index e19a37d..c05d01b 100644 --- a/deb/etc/init/statsdaemon.conf +++ b/deb/etc/init/statsdaemon.conf @@ -8,4 +8,11 @@ respawn respawn limit 360 180 setuid statsdaemon setuid statsdaemon -exec /usr/local/bin/statsdaemon + +script + STATSDAEMON_OPTS= + if [ -e /etc/default/statsdaemon ];then + . /etc/default/statsdaemon + fi + exec /usr/local/bin/statsdaemon $STATSDAEMON_OPTS +end script From fca2419ed168efc0f15b16a0b0a93bd70a64198c Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Sun, 15 Jun 2014 17:17:37 +0200 Subject: [PATCH 4/9] Fix: correct fail-early option Cred to @ploxiln for pointing this out. --- build-deb.sh | 3 +-- deb/DEBIAN/postrm | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/build-deb.sh b/build-deb.sh index 05b9811..b323557 100755 --- a/build-deb.sh +++ b/build-deb.sh @@ -12,8 +12,7 @@ VERSION=$1 BASEDIR=statsdaemon_$VERSION ARCH=$2 -# Fail early -set -e errexit +set -o errexit GOOS=linux GOARCH=$ARCH go build statsdaemon.go diff --git a/deb/DEBIAN/postrm b/deb/DEBIAN/postrm index c4a848c..f684f25 100755 --- a/deb/DEBIAN/postrm +++ b/deb/DEBIAN/postrm @@ -1,4 +1,4 @@ #!/bin/bash -set -e errexit +set -o errexit deluser statsdaemon || echo "" From 55eb8a7a2882908a85433cc639de9834504baa3d Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Sun, 15 Jun 2014 17:18:37 +0200 Subject: [PATCH 5/9] put fail-early option in file header Better to enable it for the whole file. --- build-deb.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/build-deb.sh b/build-deb.sh index b323557..7880271 100755 --- a/build-deb.sh +++ b/build-deb.sh @@ -1,5 +1,6 @@ #!/bin/bash # Inspired by http://ubuntuforums.org/showthread.php?t=910717. +set -o errexit if [ "$#" -ne 2 ]; then echo "$0 <386|amd64>" @@ -12,8 +13,6 @@ VERSION=$1 BASEDIR=statsdaemon_$VERSION ARCH=$2 -set -o errexit - GOOS=linux GOARCH=$ARCH go build statsdaemon.go if [ -d $BASEDIR ];then From c07d630f792c0df768346497843331c5f1f29ab5 Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Sun, 15 Jun 2014 17:29:23 +0200 Subject: [PATCH 6/9] deb: add `Homepage` to control file @ploxiln requested this. --- deb/DEBIAN/control | 1 + 1 file changed, 1 insertion(+) diff --git a/deb/DEBIAN/control b/deb/DEBIAN/control index 2fbb7f9..4fc600c 100644 --- a/deb/DEBIAN/control +++ b/deb/DEBIAN/control @@ -5,3 +5,4 @@ Priority: optional Architecture: ARCH Maintainer: Jens Rantil Description: A binary statsd server implemented in Go. +Homepage: https://github.com/bitly/statsdaemon From 7d6adbf66713e33c01f10ab8bf26c0a72a78ebce Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Sun, 15 Jun 2014 17:32:21 +0200 Subject: [PATCH 7/9] fix(deb/init): avoid duplicate `setuid` Cred to @ploxlin for spotting this. --- deb/etc/init/statsdaemon.conf | 1 - 1 file changed, 1 deletion(-) diff --git a/deb/etc/init/statsdaemon.conf b/deb/etc/init/statsdaemon.conf index c05d01b..247e612 100644 --- a/deb/etc/init/statsdaemon.conf +++ b/deb/etc/init/statsdaemon.conf @@ -7,7 +7,6 @@ stop on runlevel [!2345] respawn respawn limit 360 180 setuid statsdaemon -setuid statsdaemon script STATSDAEMON_OPTS= From 1215e83e9b9f4d193d6a8f21e1bf716f74956ee4 Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Sun, 15 Jun 2014 17:32:50 +0200 Subject: [PATCH 8/9] deb: set `setgid` in init config Missed this previously. --- deb/etc/init/statsdaemon.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/deb/etc/init/statsdaemon.conf b/deb/etc/init/statsdaemon.conf index 247e612..9bb3f16 100644 --- a/deb/etc/init/statsdaemon.conf +++ b/deb/etc/init/statsdaemon.conf @@ -7,6 +7,7 @@ stop on runlevel [!2345] respawn respawn limit 360 180 setuid statsdaemon +setgid statsdaemon script STATSDAEMON_OPTS= From 2adc14bb50857f10d4820b70fec8697e2f5706a5 Mon Sep 17 00:00:00 2001 From: Jens Rantil Date: Sun, 15 Jun 2014 17:34:13 +0200 Subject: [PATCH 9/9] fix: avoid unnecessary `|| echo ""` --- deb/DEBIAN/postinst | 2 +- deb/DEBIAN/postrm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deb/DEBIAN/postinst b/deb/DEBIAN/postinst index 3487a4c..bd6a38d 100755 --- a/deb/DEBIAN/postinst +++ b/deb/DEBIAN/postinst @@ -13,4 +13,4 @@ if [ "$1" = configure ]; then --group "$USER" fi -service statsdaemon start || echo "" +service statsdaemon start || true diff --git a/deb/DEBIAN/postrm b/deb/DEBIAN/postrm index f684f25..5cca173 100755 --- a/deb/DEBIAN/postrm +++ b/deb/DEBIAN/postrm @@ -1,4 +1,4 @@ #!/bin/bash set -o errexit -deluser statsdaemon || echo "" +deluser statsdaemon || true