Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from Nextdoor/docker
Browse files Browse the repository at this point in the history
Dockerize ZkWatcher
  • Loading branch information
diranged committed Dec 17, 2015
2 parents 9333a79 + 134dd6f commit 625a68d
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 158 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv
bin
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
.build
.coverage
.venv
bin
zookeeper.out
build
dist
*.egg-info
docs/_build
.vagrant
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: required
language: python
python:
- "2.7"
- "pypy"
- 2.7
install:
- make build
script: make test
sudo: false
script:
- make test
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM alpine:3.2
MAINTAINER Matt Wise <matt@nextdoor.com>

RUN apk add --update bash python py-pip py-yaml ca-certificates curl wget

RUN mkdir /app /app/zk_watcher

ADD requirements*.txt /app/
RUN pip install -r /app/requirements.test.txt -r /app/requirements.txt

ADD setup.py /app/
ADD README.rst /app/
ADD zk_watcher /app/zk_watcher/
RUN cd /app; python setup.py install

ADD entrypoint.sh /
RUN chmod +x /entrypoint.sh

ENTRYPOINT "/entrypoint.sh"
43 changes: 41 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ BIN = $(HERE)/bin

BUILD_DIRS = bin .build build include lib lib64 man share package *.egg

.PHONY: all build clean test docs
ZOOKEEPER = $(BIN)/zookeeper
ZOOKEEPER_VERSION ?= 3.4.7
ZOOKEEPER_PATH ?= $(ZOOKEEPER)
ZOOKEEPER_URL = http://apache.osuosl.org/zookeeper/zookeeper-$(ZOOKEEPER_VERSION)/zookeeper-$(ZOOKEEPER_VERSION).tar.gz

.PHONY: all build clean test docs zookeeper clean-zookeeper

all: build

Expand All @@ -14,10 +19,30 @@ build: .build
pip install -r requirements.test.txt
touch .build

image:
docker build -t zkwatcher .

hello-world:
docker pull tutum/hello-world
docker rm -f hello-world || echo "No pre-existing container found"
docker run -d --name hello-world tutum/hello-world

run: hello-world zookeeper
docker rm -f zkwatcher || echo "No pre-existing container found"
docker run \
--name "zkwatcher" \
--sig-proxy=false \
--env "CMD=curl --silent --fail http://\$$APACHE_PORT_80_TCP_ADDR:\$$APACHE_PORT_80_TCP_PORT" \
--env "SVC_PORT=80" \
--env "SVC_HOST=$(shell hostname -f)" \
--env "ZK_PATH=/hello-world" \
--link "hello-world:apache" \
zkwatcher

clean:
find . -type f -name '*.pyc' -exec rm "{}" \;
rm -rf $(BUILD_DIRS)
$(MAKE) -C docs clean
$(MAKE) -C docs clean docs

test: build docs
python setup.py test pep8 pyflakes
Expand All @@ -27,3 +52,17 @@ integration: build

docs:
$(MAKE) -C docs html

$(ZOOKEEPER):
@echo "Installing Zookeeper"
mkdir -p $(BIN) && cd $(BIN) && curl -C - $(ZOOKEEPER_URL) | tar -zx
mv $(BIN)/zookeeper-$(ZOOKEEPER_VERSION) $(ZOOKEEPER_PATH)
chmod a+x $(ZOOKEEPER_PATH)/bin/zkServer.sh
cp $(ZOOKEEPER_PATH)/conf/zoo_sample.cfg $(ZOOKEEPER_PATH)/conf/zoo.cfg
@echo "Finished installing Zookeeper"

zookeeper: $(ZOOKEEPER)
$(ZOOKEEPER_PATH)/bin/zkServer.sh start

clean-zookeeper:
rm -rf zookeeper $(ZOOKEEPER_PATH)
32 changes: 32 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require './vagrant.rb'

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'ubuntu-14.04'
config.vm.box_url = 'https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box'

# Forward ports from the HOST machine to the GUEST VM
config.vm.network :forwarded_port, guest: 8154, host: 8154, auto_correct: true
config.vm.network :forwarded_port, guest: 8155, host: 8153, auto_correct: true
config.vm.network :private_network, type: "dhcp"

# VirtualBox Specific Customization
config.vm.provider :virtualbox do |vb|
vb.gui = false
vb.customize ['modifyvm', :id, '--memory', '512']
end
config.nfs.map_uid = Process.uid
config.nfs.map_gid = Process.gid
config.vm.synced_folder './', '/home/vagrant/src', type: "nfs"

# Required to go and get our dependencies using `godep` below
config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
config.vm.provision "file", source: "~/.ssh/id_rsa", destination: ".ssh/id_rsa"
config.vm.provision 'shell', inline: 'apt-get update; apt-get install -yf git python-virtualenv default-jre'

# Before running Puppet, we need to install some gems.
config.vm.provision 'shell', inline: 'curl -sSL https://get.docker.com/ | sh'
config.vm.provision 'shell', inline: 'usermod -aG docker vagrant'
end
13 changes: 13 additions & 0 deletions docs/auth.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Authentication
--------------

If you wish to create a Digset authentication token and use that for your
client session with Zookeeper, you can add the settings to the config file
like this ::

[auth]
user: username
password: 123456

If you do this, please look at the `nd_service_registry` docs to understand how
the auth token is used, and what permissions are setup by default.
75 changes: 13 additions & 62 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ Zookeeper Watcher Daemon Docs
About
=====

`zk_watcher` is a Python script that registers
`zk_watcher` is a simple service that registers
`Ephemeral Nodes <http://zookeeper.apache.org/doc/current/zookeeperProgrammers.html#Ephemeral+Nodes>`__
in Apache Zookeeper based on the result of a healthcheck.
in Apache Zookeeper based on the result of a healthcheck. The service is
available both as a Python script that you can run on your own, or as a Docker
image (`nextdoor/zkwatcher <https://hub.docker.com/r/nextdoor/zkwatcher/>`__)
that you can pull down.

The goal of `zk_watcher` is to monitor a particular service on a host machine
and register that machine as a `provider` of that service at a given path
on the Zookeeper service.
The goal of `zk_watcher` is to monitor a particular service and register that
machine as a `provider` of that service at a given path on the Zookeeper
service.

A simple example is having `zk_watcher` monitor Apache httpd by running `service
apache2 status` at a regular interval and registers with ZooKeeper at a given
Expand All @@ -24,64 +27,12 @@ is `web1.mydomain.com`, the registration path would look like this ::
In the event that the service check fails, the host will be immediately de-
registered from that path.

Installation
------------
.. toctree::
:maxdepth: 3

To install, run ::

python setup.py install

or ::

pip install zk_watcher

Service Configs
---------------

To configure, edit the '/etc/zk/config.cfg' file. The file consists of sections
that each point to a particular service you want to monitor and register with
ZooKeeper. An example file is provided, but could look like this ::

[ssh]
cmd: /etc/init.d/sshd status
refresh: 60
service_port: 22
service_hostname: 123.234.123.123
zookeeper_path: /services/ssh
zookeeper_data: { "foo": "bar", "bah": "humbug" }

[apache]
cmd: /etc/init.d/apache status
refresh: 60
service_port: 22
zookeeper_path: /services/web
zookeeper_data: foo=bar, bah=humbug

Authentication
--------------

If you wish to create a Digset authentication token and use that for your
client session with Zookeeper, you can add the settings to the config file
like this ::

[auth]
user: username
password: 123456

If you do this, please look at the `nd_service_registry` docs to understand how
the auth token is used, and what permissions are setup by default.

Running it
----------
See the 'zk_watcher.rst' file for configuration and run-time options.

Caveats
-------
Right now you must install this package as `root`, or you must create the
`/etc/zk` directory ahead of time and change its ownership to your installation
user name. The `setup.py` uses a hard-coded path (`/etc/zk/config.cfg`) for the
config file, and will fail if it cannot create the file at that path. This will
be fixed in the next version.
install
running
auth

Indices and tables
==================
Expand Down
31 changes: 31 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Installation
------------

Local Python Install
~~~~~~~~~~~~~~~~~~~~

To install the application locally, run :

.. code-block:: bash
$ python setup.py install
or

.. code-block:: bash
$ pip install zk_watcher
Docker Container Installation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can pull down the latest builds of `zk_watcher` built into a fully
self-sufficient Docker image like this:

.. code-block:: bash
$ docker pull nextdoor/zkwatcher
Using default tag: latest
latest: Pulling from nextdoor/zkwatcher
Digest: sha256:47eee56494a190e35c5d25d2285056d0e1e347ee276d7792973fb803511da00a
Status: Image is up to date for nextdoor/zkwatcher:latest

0 comments on commit 625a68d

Please sign in to comment.