Skip to content

Commit

Permalink
added support for Redhats openshift cloud hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
schmalle committed May 14, 2015
1 parent fc0ee04 commit a3745a2
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 9 deletions.
24 changes: 15 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions createopenshift.sh
Original file line number Diff line number Diff line change
@@ -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..."




39 changes: 39 additions & 0 deletions dbcore.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions openshift/openshift.sh
Original file line number Diff line number Diff line change
@@ -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/





2 changes: 2 additions & 0 deletions openshift/openshiftstart.sh
Original file line number Diff line number Diff line change
@@ -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
129 changes: 129 additions & 0 deletions servercore.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Dummy start");

0 comments on commit a3745a2

Please sign in to comment.