From 98b4ff490e517036fd21631e558c7e48ff5b53e0 Mon Sep 17 00:00:00 2001 From: Don Schenck Date: Wed, 28 Oct 2015 11:37:54 -0400 Subject: [PATCH 1/7] Refactored; now using Environment Variables. --- ci-cd-product/Dockerfile | 9 +++- ci-cd-product/productws.js | 90 ++++++++++++++++++++++++++++---------- ci-cd-watcher/README.md | 6 +-- ci-cd-watcher/whwatch.py | 7 +-- 4 files changed, 79 insertions(+), 33 deletions(-) diff --git a/ci-cd-product/Dockerfile b/ci-cd-product/Dockerfile index 2d6c79a..43dd36b 100644 --- a/ci-cd-product/Dockerfile +++ b/ci-cd-product/Dockerfile @@ -1,14 +1,19 @@ FROM node:0.10-onbuild +MAINTAINER Don Schenck "don.schenck@gmail.com" + RUN mkdir -p /usr/local/src WORKDIR /usr/local/src -RUN apt-get update && apt-get install -y npm +RUN apt-get update +RUN apt-get install -y npm RUN npm install restify RUN npm install request +RUN npm install mongodb +COPY package.json package.json COPY productws.js productws.js EXPOSE 8080 -ENTRYPOINT node productws.js +CMD node productws.js diff --git a/ci-cd-product/productws.js b/ci-cd-product/productws.js index 8c54d07..b9fefe1 100644 --- a/ci-cd-product/productws.js +++ b/ci-cd-product/productws.js @@ -1,37 +1,81 @@ var restify = require('restify'); var request = require('request'); -function listProducts(req, res, next) { - request('http://10.176.11.153:5984/kixx/_all_docs', function(error, response, body){ - if (!error && response.statusCode == 200) { - var rows = JSON.parse(body); - var tablerows = rows.rows; - var output; - res.send(rows); +var mongo = require('mongodb'), + Server = mongo.Server, + Db = mongo.Db; +var server = new Server(process.env.DB_IP, process.env.DB_PORT, { + auto_reconnect: true +}); +var db = new Db('guestbook', server); + +var onErr = function(err, callback) { + db.close(); + callback(err); +}; + +var findGuests = function(db, callback) { + db.open(function(err, db) { + if (!err) { + var cursor = db.collection('guests').find(); + cursor.toArray(function(err, data) { + callback(err, data); + }); + } else { + onErr(err, callback); + }; + }); +}; + +var findGuestById = function(db, guestId, callback) { + db.open(function(err, db) { + if (!err) { + var o_id = new mongo.ObjectID(guestId); + var cursor = db.collection('guests').find({"_id":o_id}); + cursor.toArray(function(err, data) { + callback(err, data); + }); + } else { + onErr(err, callback); + }; + }); +}; + +function listGuests(req, res, next) { + findGuests(db, function(err, data) { + if (!err) { + res.send(data); + } else { + res.send(err); + } next(); + db.close(); + }); + }; + +function getGuestById(req, res, next) { + findGuestById(db, req.params.id, function(err, data) { + if (!err) { + res.send(data); + } else { + res.send(err); } - }) -} -function getProduct(req, res, next) { - request('http://10.176.11.153:5984/kixx/' + req.params.id, function(error, response, body){ - if (!error && response.statusCode == 200) { - var rows = JSON.parse(body); - var tablerows = rows.rows; - var output; - res.send(rows); - next(); - } - }) -} + next(); + db.close(); + }); +}; + function getVersion(req, res, next) { - res.send("1.0.2"); + res.send("2.1.0"); } + var server = restify.createServer({ name: 'productws', }); + server.use(restify.bodyParser()); server.get('/version', getVersion); -server.get('/product', listProducts); -server.get('/product/:id', getProduct); +server.get('/guest', listGuests); +server.get('/guest/:id', getGuestById); server.listen(8080, function() { console.log('%s listening at %s', server.name, server.url); }); diff --git a/ci-cd-watcher/README.md b/ci-cd-watcher/README.md index cd6b48b..5f16d89 100644 --- a/ci-cd-watcher/README.md +++ b/ci-cd-watcher/README.md @@ -1,7 +1,3 @@ ``` -docker run -d \ - --volumes-from swarm-data \ - -p 5000:5000 \ - --name watcher \ - +docker run -d -e WHWATCH_IMAGE_NAME="donschenck/productws:latest" --volumes-from swarm-data -p 5000:5000 --name watcher donschenck/whwwatch:latest ``` diff --git a/ci-cd-watcher/whwatch.py b/ci-cd-watcher/whwatch.py index 8065529..0632362 100644 --- a/ci-cd-watcher/whwatch.py +++ b/ci-cd-watcher/whwatch.py @@ -1,24 +1,25 @@ from flask import Flask, session, redirect, url_for, escape, request import docker import sys +import os app = Flask(__name__) app.debug = True @app.route("/version", methods=['GET']) def hello(): - return "1.0.0" + return "1.0.1" @app.route('/webhook', methods=['POST']) def webhook(): try: tls_config = docker.tls.TLSConfig( - client_cert=('/etc/docker/server-cert.pem', '/etc/docker/server-key.pem'), + client_cert=('/etc/docker/server-cert.pem', '/etc/docker/server-key.pem'), verify='/etc/docker/ca.pem') cli = docker.Client(base_url='https://swarm-manager:2376', tls=tls_config) host_config = cli.create_host_config(port_bindings={8080: 8080}) - container = cli.create_container(image="donschenck/productws", detach=True, host_config=host_config) + container = cli.create_container(image=os.environ["WHWATCH_IMAGE_NAME"], detach=True, host_config=host_config) response = cli.start(container=container.get('Id')) except Exception: From 9cd2b6db4d2c2e39dd3d392f07eea1b13354ed69 Mon Sep 17 00:00:00 2001 From: Don Schenck Date: Wed, 28 Oct 2015 11:53:08 -0400 Subject: [PATCH 2/7] Changed whwatch.py version to 2.0.1 --- ci-cd-watcher/whwatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci-cd-watcher/whwatch.py b/ci-cd-watcher/whwatch.py index 0632362..6e32b87 100644 --- a/ci-cd-watcher/whwatch.py +++ b/ci-cd-watcher/whwatch.py @@ -8,7 +8,7 @@ @app.route("/version", methods=['GET']) def hello(): - return "1.0.1" + return "2.0.1" @app.route('/webhook', methods=['POST']) def webhook(): From 3584867c7b5dd98bc94be1f731a9efce04ac2f8a Mon Sep 17 00:00:00 2001 From: Don Schenck Date: Wed, 28 Oct 2015 11:57:05 -0400 Subject: [PATCH 3/7] Fixed typos in README.md --- ci-cd-watcher/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci-cd-watcher/README.md b/ci-cd-watcher/README.md index 5f16d89..ed0261e 100644 --- a/ci-cd-watcher/README.md +++ b/ci-cd-watcher/README.md @@ -1,3 +1,3 @@ ``` -docker run -d -e WHWATCH_IMAGE_NAME="donschenck/productws:latest" --volumes-from swarm-data -p 5000:5000 --name watcher donschenck/whwwatch:latest +docker run -d -e WHWATCH_IMAGE_NAME="donschenck/productws:latest" --volumes-from swarm-data -p 5050:5000 --name watcher donschenck/whwatch:latest ``` From e3fae86ca5b890a8f8da502b9bc791ce0f4216a9 Mon Sep 17 00:00:00 2001 From: Don Schenck Date: Wed, 28 Oct 2015 13:03:31 -0400 Subject: [PATCH 4/7] Added package.json --- ci-cd-product/package.json | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ci-cd-product/package.json diff --git a/ci-cd-product/package.json b/ci-cd-product/package.json new file mode 100644 index 0000000..1b98572 --- /dev/null +++ b/ci-cd-product/package.json @@ -0,0 +1,11 @@ +{ + "name" : "getter", + "version" : "2.0.1", + "description" : "Microservice example -- retrieves products", + "dependencies" : { + "restify" : ">=0.0.0", + "request" : ">=0.0.0", + "mongodb" : ">=3.0.6" + }, + "main" : "productws.js" +} From db7612d28c5a7c860a7c62132d7ceb80788fd603 Mon Sep 17 00:00:00 2001 From: Don Schenck Date: Thu, 29 Oct 2015 10:44:49 -0400 Subject: [PATCH 5/7] Remove npm install; should be included with node. --- ci-cd-product/Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ci-cd-product/Dockerfile b/ci-cd-product/Dockerfile index 43dd36b..9c9fc3e 100644 --- a/ci-cd-product/Dockerfile +++ b/ci-cd-product/Dockerfile @@ -1,4 +1,4 @@ -FROM node:0.10-onbuild +FROM node:latest MAINTAINER Don Schenck "don.schenck@gmail.com" @@ -6,7 +6,8 @@ RUN mkdir -p /usr/local/src WORKDIR /usr/local/src RUN apt-get update -RUN apt-get install -y npm +#RUN apt-get install -y npm +#RUN yum install npm RUN npm install restify RUN npm install request RUN npm install mongodb From 78a73cbf03b4888c03737bcc664a09706f6b804b Mon Sep 17 00:00:00 2001 From: Don Schenck Date: Thu, 29 Oct 2015 14:31:43 -0400 Subject: [PATCH 6/7] Added environment variables --- ci-cd-watcher/README.md | 2 +- ci-cd-watcher/whwatch.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ci-cd-watcher/README.md b/ci-cd-watcher/README.md index ed0261e..a961958 100644 --- a/ci-cd-watcher/README.md +++ b/ci-cd-watcher/README.md @@ -1,3 +1,3 @@ ``` -docker run -d -e WHWATCH_IMAGE_NAME="donschenck/productws:latest" --volumes-from swarm-data -p 5050:5000 --name watcher donschenck/whwatch:latest +docker run -d -e WHWATCH_IMAGE_NAME="donschenck/productws:latest" -e DB_IP="104.130.0.7" -e DB_PORT="32769" --volumes-from swarm-data -p 5050:5000 --name watcher donschenck/whwatch:latest ``` diff --git a/ci-cd-watcher/whwatch.py b/ci-cd-watcher/whwatch.py index 6e32b87..36a1c6e 100644 --- a/ci-cd-watcher/whwatch.py +++ b/ci-cd-watcher/whwatch.py @@ -17,9 +17,10 @@ def webhook(): client_cert=('/etc/docker/server-cert.pem', '/etc/docker/server-key.pem'), verify='/etc/docker/ca.pem') cli = docker.Client(base_url='https://swarm-manager:2376', tls=tls_config) - + container_env="DB_IP=" + os.environ["DB_IP"] + container_env=",DB_POST=" + os.environ["DB_PORT"] host_config = cli.create_host_config(port_bindings={8080: 8080}) - container = cli.create_container(image=os.environ["WHWATCH_IMAGE_NAME"], detach=True, host_config=host_config) + container = cli.create_container(image=os.environ["WHWATCH_IMAGE_NAME"], detach=True, host_config=host_config, environment=container_env) response = cli.start(container=container.get('Id')) except Exception: From 30df6f2e826d078f42eb34265b801dcd9f4b1b11 Mon Sep 17 00:00:00 2001 From: Don Schenck Date: Thu, 29 Oct 2015 15:01:11 -0400 Subject: [PATCH 7/7] hard-coded ip and port for testing only --- ci-cd-watcher/whwatch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci-cd-watcher/whwatch.py b/ci-cd-watcher/whwatch.py index 36a1c6e..bf811f5 100644 --- a/ci-cd-watcher/whwatch.py +++ b/ci-cd-watcher/whwatch.py @@ -8,7 +8,7 @@ @app.route("/version", methods=['GET']) def hello(): - return "2.0.1" + return "2.0.2" @app.route('/webhook', methods=['POST']) def webhook(): @@ -20,7 +20,7 @@ def webhook(): container_env="DB_IP=" + os.environ["DB_IP"] container_env=",DB_POST=" + os.environ["DB_PORT"] host_config = cli.create_host_config(port_bindings={8080: 8080}) - container = cli.create_container(image=os.environ["WHWATCH_IMAGE_NAME"], detach=True, host_config=host_config, environment=container_env) + container = cli.create_container(image=os.environ["WHWATCH_IMAGE_NAME"], detach=True, host_config=host_config, environment="DB_IP=104.130.0.7,DB_PORT=32769") response = cli.start(container=container.get('Id')) except Exception: