diff --git a/experimental/example/.gitignore b/experimental/example/.gitignore index 4eba73260..573b896b3 100644 --- a/experimental/example/.gitignore +++ b/experimental/example/.gitignore @@ -1,3 +1,4 @@ qotd/qotd -goapp/app +searchapp/searchapp shout/shout +*.marker diff --git a/experimental/example/Makefile b/experimental/example/Makefile index 2d453335e..08b4e73b6 100644 --- a/experimental/example/Makefile +++ b/experimental/example/Makefile @@ -1,26 +1,28 @@ CC=gcc CFLAGS=-g -lpthread -%.o: %.c - $(CC) -c -o $@ $< $(CFLAGS) - -all: qotd/qotd goapp/app shout/shout +all: qotd.marker app.marker client.marker searchapp.marker shout.marker qotd/qotd: qotd/qotd.o gcc -o $@ $< $(CFLAGS) -goapp/app: goapp/app.go -shout/shout: shout/shout.go +%.o: %.c + $(CC) -c -o $@ $< $(CFLAGS) -shout/shout goapp/app: +searchapp/searchapp: searchapp/app.go +shout/shout: shout/shout.go +shout/shout searchapp/searchapp: go get -tags netgo ./$(@D) go build -ldflags "-extldflags \"-static\"" -tags netgo -o $@ ./$(@D) -clean: - rm -f qotd/*.o qotd/qotd goapp/app +qotd.marker: qotd/* qotd/qotd +app.marker: app/* +client.marker: client/* +searchapp.marker: searchapp/* searchapp/searchapp +shout.marker: shout/* shout/shout +%.marker: + docker build -t $( pyapp (x2) --> goapp (x2) -> elasticsearch (x3) - | - --> qotd -> internet - | - --> redis +curl -> app --> searchapp -> elasticsearch + | + --> qotd -> internet + | + --> redis ``` diff --git a/experimental/example/pyapp/Dockerfile b/experimental/example/app/Dockerfile similarity index 95% rename from experimental/example/pyapp/Dockerfile rename to experimental/example/app/Dockerfile index 850621079..c39037cd6 100644 --- a/experimental/example/pyapp/Dockerfile +++ b/experimental/example/app/Dockerfile @@ -4,4 +4,5 @@ WORKDIR /home/weave ADD requirements.txt /home/weave/ RUN pip install -r /home/weave/requirements.txt ADD app.py /home/weave/ +EXPOSE 5000 ENTRYPOINT ["python", "/home/weave/app.py"] diff --git a/experimental/example/pyapp/app.py b/experimental/example/app/app.py similarity index 57% rename from experimental/example/pyapp/app.py rename to experimental/example/app/app.py index ae948d7c5..f7a2d643a 100644 --- a/experimental/example/pyapp/app.py +++ b/experimental/example/app/app.py @@ -1,19 +1,22 @@ import os import socket +import sys import requests import random import threading +import logging from concurrent.futures import ThreadPoolExecutor from flask import Flask from redis import Redis +from werkzeug.serving import WSGIRequestHandler app = Flask(__name__) redis = Redis(host='redis', port=6379) pool = ThreadPoolExecutor(max_workers=10) sessions = threading.local() -goapps = ['http://goapp1:8080/', 'http://goapp2:8080/'] +searchapps = ['http://searchapp:8080/'] def do_redis(): redis.incr('hits') @@ -31,18 +34,27 @@ def do_qotd(): def do_search(): if getattr(sessions, 'session', None) == None: sessions.session = requests.Session() - r = sessions.session.get(random.choice(goapps)) + r = sessions.session.get(random.choice(searchapps)) return r.text +def ignore_error(f): + try: + return str(f()) + except: + logging.error("Error executing function", exc_info=sys.exc_info()) + return "Error" + @app.route('/') def hello(): counter_future = pool.submit(do_redis) search_future = pool.submit(do_search) qotd_future = pool.submit(do_qotd) - result = 'Hello World! I have been seen %s times.' % counter_future.result() - result += search_future.result() - result += qotd_future.result() + result = 'Hello World! I have been seen %s times.' % ignore_error(counter_future.result) + result += ignore_error(search_future.result) + result += ignore_error(qotd_future.result) return result if __name__ == "__main__": + logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO) + WSGIRequestHandler.protocol_version = "HTTP/1.1" app.run(host="0.0.0.0", port=5000, debug=True) diff --git a/experimental/example/pyapp/requirements.txt b/experimental/example/app/requirements.txt similarity index 100% rename from experimental/example/pyapp/requirements.txt rename to experimental/example/app/requirements.txt diff --git a/experimental/example/client/client.py b/experimental/example/client/client.py index 9cd3b6ff8..b54cf8b1c 100644 --- a/experimental/example/client/client.py +++ b/experimental/example/client/client.py @@ -1,23 +1,26 @@ import requests import random import threading +import time import logging import sys -pyapps = ['http://pyapp1:5000/', 'http://pyapp2:5000/'] -concurrency = 5 +app = 'http://app:5000/' +concurrency = 1 def do_requests(): s = requests.Session() while True: try: - s.get(random.choice(pyapps)) + s.get(app) + logging.info("Did request") + time.sleep(1) except: logging.error("Error doing request", exc_info=sys.exc_info()) logging.info("Did request") def main(): - logging.info("Starting %d thread", concurrency) + logging.info("Starting %d threads", concurrency) threads = [threading.Thread(target=do_requests) for i in range(concurrency)] for thread in threads: thread.start() @@ -26,5 +29,5 @@ def main(): logging.info("Exiting") if __name__ == "__main__": - logging.basicConfig() + logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO) main() diff --git a/experimental/example/docker-compose.yml b/experimental/example/docker-compose.yml deleted file mode 100644 index 4ade891fd..000000000 --- a/experimental/example/docker-compose.yml +++ /dev/null @@ -1,66 +0,0 @@ -client: - build: client - links: - - qotd - - pyapp1 - - pyapp2 -pyapp1: - build: pyapp - expose: - - "5000" - links: - - qotd - - redis - - goapp1 - - goapp2 -pyapp2: - build: pyapp - expose: - - "5000" - links: - - qotd - - redis - - goapp1 - - goapp2 -qotd: - build: qotd - expose: - - "4446" -redis: - image: redis -goapp1: - build: goapp - expose: - - "8080" - links: - - elasticsearch1 - - elasticsearch2 - - elasticsearch3 -goapp2: - build: goapp - expose: - - "8080" - links: - - elasticsearch1 - - elasticsearch2 - - elasticsearch3 -elasticsearch1: - image: elasticsearch - expose: - - "9200" - - "9300" -elasticsearch2: - image: elasticsearch - expose: - - "9200" - - "9300" - links: - - elasticsearch1 -elasticsearch3: - image: elasticsearch - expose: - - "9200" - - "9300" - links: - - elasticsearch1 - - elasticsearch2 diff --git a/experimental/example/goapp/entrypoint.sh b/experimental/example/goapp/entrypoint.sh deleted file mode 100755 index 8837a8b30..000000000 --- a/experimental/example/goapp/entrypoint.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -exec /home/weave/app elasticsearch1 elasticsearch2 elasticsearch3 diff --git a/experimental/example/qotd/Dockerfile b/experimental/example/qotd/Dockerfile index 5bca6fd17..7f71ba66c 100644 --- a/experimental/example/qotd/Dockerfile +++ b/experimental/example/qotd/Dockerfile @@ -2,4 +2,5 @@ FROM ubuntu MAINTAINER Weaveworks Inc WORKDIR /home/weave ADD ./qotd /home/weave/ +EXPOSE 4446 ENTRYPOINT ["/home/weave/qotd"] diff --git a/experimental/example/run.sh b/experimental/example/run.sh new file mode 100755 index 000000000..721509aaa --- /dev/null +++ b/experimental/example/run.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +set -eux + +REPLICAS=${REPLICAS:-1} + +start_container() { + IMAGE=$1 + BASENAME=${2:-$1} + HOSTNAME=$BASENAME.weave.local + + for i in $(seq $REPLICAS); do + if docker inspect $BASENAME$i >/dev/null 2>&1; then + docker rm -f $BASENAME$i + fi + weave run --with-dns --name=$BASENAME$i --hostname=$HOSTNAME $IMAGE + done +} + +start_container elasticsearch +start_container searchapp +start_container redis +start_container qotd +start_container app +start_container client diff --git a/experimental/example/goapp/Dockerfile b/experimental/example/searchapp/Dockerfile similarity index 58% rename from experimental/example/goapp/Dockerfile rename to experimental/example/searchapp/Dockerfile index 0bfa9fe4d..b48703521 100644 --- a/experimental/example/goapp/Dockerfile +++ b/experimental/example/searchapp/Dockerfile @@ -1,5 +1,6 @@ -FROM gliderlabs/alpine +FROM progrium/busybox MAINTAINER Weaveworks Inc WORKDIR /home/weave -ADD app entrypoint.sh /home/weave/ +ADD searchapp entrypoint.sh /home/weave/ +EXPOSE 8080 ENTRYPOINT ["/home/weave/entrypoint.sh"] diff --git a/experimental/example/goapp/app.go b/experimental/example/searchapp/app.go similarity index 100% rename from experimental/example/goapp/app.go rename to experimental/example/searchapp/app.go diff --git a/experimental/example/searchapp/entrypoint.sh b/experimental/example/searchapp/entrypoint.sh new file mode 100755 index 000000000..b7862dec3 --- /dev/null +++ b/experimental/example/searchapp/entrypoint.sh @@ -0,0 +1,2 @@ +#!/bin/sh +exec /home/weave/searchapp elasticsearch.weave.local diff --git a/experimental/example/shout/Dockerfile b/experimental/example/shout/Dockerfile index 1b9be7638..95dfa6f6e 100644 --- a/experimental/example/shout/Dockerfile +++ b/experimental/example/shout/Dockerfile @@ -2,4 +2,5 @@ FROM gliderlabs/alpine MAINTAINER Weaveworks Inc WORKDIR /home/weave ADD shout /home/weave/ +EXPOSE 8090 ENTRYPOINT ["/home/weave/shout"] diff --git a/experimental/example/shout/shout.go b/experimental/example/shout/shout.go index 5014b5ebe..1c414f150 100644 --- a/experimental/example/shout/shout.go +++ b/experimental/example/shout/shout.go @@ -19,7 +19,7 @@ type requestResponse struct { func main() { var ( - addr = flag.String("addr", ":8080", "HTTP listen address") + addr = flag.String("addr", ":8090", "HTTP listen address") ) flag.Parse()