From a3745a283804ee365fe63631e27c51cc13ddbf29 Mon Sep 17 00:00:00 2001 From: Markus Schmall Date: Thu, 14 May 2015 17:08:32 +0200 Subject: [PATCH] added support for Redhats openshift cloud hosting --- .idea/workspace.xml | 24 ++++--- createopenshift.sh | 12 ++++ dbcore.js | 39 +++++++++++ openshift/openshift.sh | 14 ++++ openshift/openshiftstart.sh | 2 + servercore.js | 129 ++++++++++++++++++++++++++++++++++++ test.js | 1 + 7 files changed, 212 insertions(+), 9 deletions(-) create mode 100755 createopenshift.sh create mode 100644 dbcore.js create mode 100755 openshift/openshift.sh create mode 100755 openshift/openshiftstart.sh create mode 100644 servercore.js create mode 100644 test.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 33cbea4..8b1e053 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,12 @@ + + + + + + @@ -866,13 +872,7 @@ - - - - 1429472189835 - 1430061016117 @@ -1162,11 +1162,17 @@ - - diff --git a/createopenshift.sh b/createopenshift.sh new file mode 100755 index 0000000..b3a5889 --- /dev/null +++ b/createopenshift.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +echo "Starting creation of a new Nodepot app named $1" + +rhc app create $1 nodejs-0.10 --from-code=http://github.com/schmalle/nodepot.git +rhc add-cartridge http://cartreflect-claytondev.rhcloud.com/reflect?github=smarterclayton/openshift-redis-cart --app $1 + +echo "Finished..." + + + + diff --git a/dbcore.js b/dbcore.js new file mode 100644 index 0000000..061e87a --- /dev/null +++ b/dbcore.js @@ -0,0 +1,39 @@ +var redis = require("redis"); + +var port = process.env.OPENSHIFT_REDIS_PORT; + +function initClient() +{ + var openShiftIP = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1"; + if (openShiftIP != undefined) + { + console.log("Starting DB client with ip/port server " + port + " : " + process.env.OPENSHIFT_REDIS_HOST + " on openshift"); + + var client = redis.createClient(port, process.env.OPENSHIFT_REDIS_HOST); + client.auth(process.env.REDIS_PASSWORD); + return client; + + } + else + { + console.log("Starting DB without special parameters outside of OpenShift environment ..."); + var client = redis.createClient(); + return client; + } + +} + +var client = initClient(); +console.log("Starting after initclient..."); + +if (client != undefined) +{ + console.log("client after initClient is defined"); +} +else +{ + console.log("client after initClient is not defined"); +} + +module.exports = client; +exports.initClient = initClient; \ No newline at end of file diff --git a/openshift/openshift.sh b/openshift/openshift.sh new file mode 100755 index 0000000..f04d136 --- /dev/null +++ b/openshift/openshift.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +cd $HOME/app-root/data +mkdir html +mkdir dl +mkdir download +mkdir logs +mkdir log +cp ./../repo/template/config.js . +cp ./../repo/html/* ./html/ + + + + + diff --git a/openshift/openshiftstart.sh b/openshift/openshiftstart.sh new file mode 100755 index 0000000..81e009d --- /dev/null +++ b/openshift/openshiftstart.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +/bin/sh $HOME/app-root/repo/openshift/openshift.sh; cd $HOME/app-root/repo; node ./app.js \ No newline at end of file diff --git a/servercore.js b/servercore.js new file mode 100644 index 0000000..62b4f57 --- /dev/null +++ b/servercore.js @@ -0,0 +1,129 @@ +var http = require("http"); +var url = require("url"); +var fs = require("fs"); +var path = require('path'); +var ana = require("./analyzer"); +var S = require('string'); +var twitter = require("./twitter"); +var moment = require("moment"); +var config = "test"; + + + +var mimeTypes = { + "html": "text/html", + "jpeg": "image/jpeg", + "jpg": "image/jpeg", + "png": "image/png", + "js": "text/javascript", + "css": "text/css"}; + + +/** + * retrive the correct path for the stored html data + * @param config + * @param openShiftDataDir + * @returns {string} + */ +function getHtmlPath(config, openShiftDataDir) { + + var openShiftDataDir = process.env.OPENSHIFT_DATA_DIR; + + + var configuredHtmlPath = config.html; + if (openShiftDataDir != undefined) + { + configuredHtmlPath = openShiftDataDir + '/html/'; + } + else + { + configuredHtmlPath = config.html; + } + + return configuredHtmlPath; +} + + +function start(configName) { + + config = require(configName); + + function onRequest(request, response) { + + + /* set the correct content type */ + response.writeHead(200, {"Content-Type": "text/html"}); + + var query = request.url; + var ip = request.connection.remoteAddress; + + // get data directory for OpenShift (easy check, if we run in Openshift) + var openShiftDataDir = process.env.OPENSHIFT_DATA_DIR; + var logPath = "/var/log/nodepot.log"; + + var configuredHtmlPath = getHtmlPath(config, openShiftDataDir); + + if (openShiftDataDir != undefined) + { + logPath = openShiftDataDir + "/log/nodepot.log" + } + + + // admin check (query and IP range) + if ((query == "/admin" || query == "admin") && (S(ip).contains("127.0.0.1") || S(ip).contains(config.home_ip))) + { + // show UI + + var defaultTemplateStart = fs.readFileSync(configuredHtmlPath + '/adminstart.html', 'utf8'); + var learnedStuff = fs.readFileSync(logPath, 'utf8'); + var defaultTemplateEnd = fs.readFileSync(configuredHtmlPath + '/adminend.html', 'utf8'); + response.write(defaultTemplateStart); + response.write(learnedStuff); + response.write(defaultTemplateEnd); + + } + else + { /* default analyze case */ + + /* check if the request contains an .. code */ + statusAnalyze = ana.analyze(request, response, config); + + + + + var defaultTemplate = fs.readFileSync(configuredHtmlPath + '/demo.html', 'utf8'); + var learnedStuff = fs.readFileSync(configuredHtmlPath + '/dork.html', 'utf8'); + + response.write(defaultTemplate); + response.write(learnedStuff); + + if (config.twitter.verbose == "yes" && statusAnalyze != null) + { + twitter.tweet(moment().format('MMMM Do YYYY, h:mm:ss a') + "(Nodepot): Found request from ip " + ip, config); + } + + + } + response.end(); + } + + var openShiftIP = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1"; + var port = process.env.OPENSHIFT_NODEJS_PORT; + + if (port != undefined) { + console.log(moment().format('MMMM Do YYYY, h:mm:ss a') + " Server tries to start on port " + port); + } + + if (openShiftIP != undefined) + { + http.createServer(onRequest).listen(process.env.OPENSHIFT_NODEJS_PORT, openShiftIP); + } + else + { + http.createServer(onRequest).listen(config.port); + } + + console.log(moment().format('MMMM Do YYYY, h:mm:ss a') + " Server has started."); +} + +exports.start = start; \ No newline at end of file diff --git a/test.js b/test.js new file mode 100644 index 0000000..961149f --- /dev/null +++ b/test.js @@ -0,0 +1 @@ +console.log("Dummy start");