diff --git a/ci-cd-product/Dockerfile b/ci-cd-product/Dockerfile index 2d6c79a..9c9fc3e 100644 --- a/ci-cd-product/Dockerfile +++ b/ci-cd-product/Dockerfile @@ -1,14 +1,20 @@ -FROM node:0.10-onbuild +FROM node:latest +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 yum install 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/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" +} 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..a961958 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" -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 8065529..bf811f5 100644 --- a/ci-cd-watcher/whwatch.py +++ b/ci-cd-watcher/whwatch.py @@ -1,24 +1,26 @@ 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 "2.0.2" @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) - + 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="donschenck/productws", detach=True, host_config=host_config) + 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: