Browse files

* In addition to data/ports, a data/hosts file is read if present. Th…

…at file should contain hostnames separated by whitespace. sc-proxy will forward traffic intended for those hostnames to the site in question. I'm about to test this in ubuntuland, bear with me a moment.

* install-node-and-mongo-on-ubuntu.bash is a nicely tested script for installing Node and MongoDB correctly on Ubuntu via the recommended repositories for each.
  • Loading branch information...
1 parent 8b31afa commit 39c99bf0935b9e61b7a23505d7857a4cffa7b28b @boutell boutell committed Apr 27, 2012
Showing with 57 additions and 4 deletions.
  1. +5 −0 README.md
  2. +28 −0 sc-proxy/install-node-and-mongo-on-ubuntu.bash
  3. +24 −4 sc-proxy/server.js
View
5 README.md
@@ -99,6 +99,11 @@ Then cd to the `sc-proxy` folder and run:
The `sc-proxy` folder also contains an `upstart` script that can start and stop the proxy and the associated apps on an Ubuntu system. By copying this script to `/etc/init` on your Ubuntu system you can arrange for your proxy and web apps to be running at all times. You can also `start stagecoach` and `stop stagecoach` at any time (as root).
+install-node-and-mongo-on-ubuntu.bash
+=====================================
+
+This shell script is provided in the `sc-proxy` folder. It does what it says: it installs Node and MongoDB correctly on Ubuntu, using the recommended repositories for the latest stable releases, not the older stuff in Ubuntu's official repositories. It also configures MongoDB to run safely, accepting connections only on localhost. You can change that if you like, just please consider the security implications. MongoDB's default configuration has no security of any kind.
+
TODO
====
View
28 sc-proxy/install-node-and-mongo-on-ubuntu.bash
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# Install the latest official stable versions of Node and MongoDB from
+# their own repositories, not the older stuff in the Ubuntu repositories.
+
+# Configures MongoDB to accept connections on localhost only - a safe default
+# configuration. If you don't like this, you should explicitly configure MongoDB
+# to allow connections from some safe set of IPs or to require authentication.
+# MongoDB's default configuration is insecure.
+
+# Assumes you have at least Ubuntu 10.04 (I haven't tested further back)
+
+echo "Installing Node" &&
+apt-get install python-software-properties &&
+add-apt-repository ppa:chris-lea/node.js &&
+apt-get -y update &&
+apt-get -y install nodejs npm &&
+echo "Node installed" &&
+echo "Installing MongoDB" &&
+apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10 &&
+echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" >> /etc/apt/sources.list &&
+apt-get -y update &&
+apt-get -y install mongodb-10gen &&
+echo "Installed MongoDB" &&
+echo "Configuring MongoDB to listen only on localhost" &&
+echo "bind_ip = 127.0.0.1" >> /etc/mongodb.conf &&
+/usr/sbin/service mongodb restart &&
+echo "Success!"
View
28 sc-proxy/server.js
@@ -2,6 +2,7 @@
var config = require('./config');
var fs = require('fs');
+var path = require('path');
var http = require('http');
var httpProxy = require('http-proxy');
@@ -43,34 +44,53 @@ function resetRouter()
// If failQuickly is true, return false as soon as we hit a port file
// we can't read or parse, so we can retry. If failQuickly is false,
// just keep going and return the best result we can (more appropriate
-// at startup).
+// at startup). We also read an optional hosts file containing hostnames
+// that should be routed to this site, which allows production hosting
function rebuildRouter(failQuickly)
{
- console.log("Scanning " + config.appsDir);
var router = {};
// Grab the names of the web apps, then read their data/port files to discover the sites we are proxying and to what ports
var servers = fs.readdirSync(config.appsDir);
var i;
for (i = 0; (i < servers.length); i++)
{
var site = servers[i];
+ var hosts = [];
+ var port;
+ // If there is a data/hosts file, read whitespace-separated hostnames from it, and
+ // respond on those for the site. This overrides the default "listen on a subdomain for
+ // each staging site" behavior for that site. Enables production hosting with stagecoach
+ var hostsPath = config.appsDir + '/' + site + '/data/hosts';
try
{
- var port = fs.readFileSync(config.appsDir + '/' + site + '/data/port', 'UTF-8').replace(/\s+$/, '');
+ if (path.existsSync(hostsPath))
+ {
+ hosts = fs.readFileSync(hostsPath, 'UTF-8').replace(/\s+$/, '');
+ hosts = hosts.split(/\s+/);
+ }
+ port = fs.readFileSync(config.appsDir + '/' + site + '/data/port', 'UTF-8').replace(/\s+$/, '');
if (port < 1000)
{
throw "Bad port number, probably not a complete write";
}
} catch (err)
{
+ // Fail on an unexpected filesystem error or a file we suspect is incomplete
if (failQuickly)
{
return false;
}
continue;
}
- router[site + '.' + config.domain] = '127.0.0.1:' + port;
+ var local = '127.0.0.1:' + port;
+ if (!hosts.length)
+ {
+ router[site + '.' + config.domain] = local;
+ }
+ hosts.forEach(function(host) {
+ router[host] = local;
+ }
}
return router;
}

0 comments on commit 39c99bf

Please sign in to comment.