Skip to content

Commit 4907148

Browse files
committed
riak works on joyent with clustering
1 parent b9ff17e commit 4907148

15 files changed

+249
-116
lines changed

app/lib/middleware.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var express = require('express'),
22
connect = require('connect'),
33
stylus = require('stylus'),
4-
//RedisStore = require('connect-redis')(express),
54
connect_timeout = require('connect-timeout'),
5+
MongoServer = require('mongodb').Server,
66
MongoStore = require('connect-mongodb');
77

88
// Middleware
@@ -23,15 +23,15 @@ module.exports = function(app) {
2323
});
2424

2525
// Sessions
26+
var server_config = new MongoServer(app.config.session.host, app.config.session.port, {auto_reconnect: true, native_parser: true});
2627
var mongoStore = new MongoStore({
27-
url: app.config.session.url,
28-
maxAge: app.constants.session_length
28+
server_config: server_config,
29+
collection: app.constants.name + '_sessions'
2930
});
3031

3132
var session_middleware = express.session({
3233
key: app.config.session_key,
3334
cookie: { secure: true },
34-
//store: new RedisStore(),
3535
store: mongoStore,
3636
maxAge: app.config.session_length
3737
});

config/default.env.json

+6
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@
1414
},
1515
"session": {
1616
"url": "mongodb://localhost:27017/base12_session"
17+
},
18+
"deployment": {
19+
"type": "joyent",
20+
"node": {
21+
"version": "0.6.15"
22+
}
1723
}
1824
}

config/prod.db.env.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"deployment": {
3+
"type": "joyent",
4+
"mongodb": {
5+
"private_ip": true
6+
}
7+
}
8+
}

config/prod.web.env.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"view_engine": "jade",
3+
"view_options": { "layout": false },
4+
"cookie_secret": "mysecret",
5+
"session": {
6+
"key": "mykey"
7+
},
8+
"http": {
9+
"port":4000
10+
},
11+
"redis": {
12+
"host": "localhost",
13+
"port": 6379
14+
},
15+
"session": {
16+
"host": "localhost",
17+
"port": 27017
18+
},
19+
"deployment": {
20+
"type": "joyent",
21+
"nodejs": {
22+
"version": "0.6.15"
23+
}
24+
}
25+
}

config/prod.worker.env.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"deployment": {
3+
"type": "joyent",
4+
"riak": {
5+
"cookie": "somecookie",
6+
"master": "10.112.1.127"
7+
}
8+
}
9+
}

config/web.stack.json

-4
This file was deleted.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"connect": "2.0.3",
1414
"connect-timeout": "latest",
1515
"stylus": "latest",
16-
"connect-redis": "latest",
16+
"mongodb": "latest",
1717
"connect-mongodb": "latest",
1818
"express-resource": "latest",
1919
"express-messages": "git://github.com/hunterloftis/express-messages.git#master",

scripts/lib/joyent/cycle.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
echo "Copying environment file into ~/node-service/current"
22
cp /home/node/.env.json /home/node/node-service/current/.env.json
3+
chown -R node:node /home/node
34
echo "Restarting node-service"
4-
node-service-restart
5+
/opt/nodejs/bin/node-service-restart

scripts/lib/joyent/mongo.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
PRIVATE_IP=`ifconfig -a | grep 'inet 10.' | grep -v '127.0.0.1' | awk '{ print $2}'`
2+
3+
echo "Creating /mongodb/mongodb.conf with private IP $PRIVATE_IP"
4+
5+
cat <<'EOF' > /mongodb/mongodb.conf
6+
# This is an example config file for MongoDB.
7+
dbpath = /data/db
8+
bind_ip = $PRIVATE_IP
9+
port = 27017
10+
pidfilepath = /data/db/mongodb.pid
11+
logpath = /var/log/mongodb/mongodb.log
12+
logappend = true
13+
journal = true
14+
nohttpinterface = true
15+
directoryperdb = true
16+
auth = true
17+
EOF

scripts/lib/joyent/mongodb.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
PRIVATE_IP=`ifconfig -a | grep 'inet 10.' | grep -v '127.0.0.1' | awk '{ print $2}'`
2+
3+
echo "Creating /mongodb/mongodb.conf with private IP $PRIVATE_IP"
4+
5+
cat <<'EOF' > /mongodb/mongodb.conf
6+
# This is an example config file for MongoDB.
7+
dbpath = /data/db
8+
bind_ip = $PRIVATE_IP
9+
port = 27017
10+
pidfilepath = /data/db/mongodb.pid
11+
logpath = /var/log/mongodb/mongodb.log
12+
logappend = true
13+
journal = true
14+
nohttpinterface = true
15+
directoryperdb = true
16+
auth = true
17+
EOF
18+
19+
echo "Restarting mongodb service"
20+
svcadm restart mongodb

scripts/lib/joyent/nodejs.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if [[ $EUID -ne 0 ]]; then
1111
exit 1
1212
fi
1313

14-
VERSION="0.6.15"
14+
VERSION="{{config.deployment.nodejs.version}}"
1515
if [ -z "$VERSION" ]; then
1616
echo "Usage: $0 <version> (e.g. 0.6.0)"
1717
exit 127

scripts/lib/joyent/provision.js

+61-32
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,99 @@
11
var async = require('async');
2-
32
var exec = require('child_process').exec;
43

54
var Shell = require('../shell');
65

76
var shell = new Shell();
87

9-
module.exports = function(options) {
8+
module.exports = {
9+
provision: provision
10+
};
11+
12+
function section(name) {
13+
console.log('\n===== ' + name + ' =====');
14+
}
1015

16+
function provision(options, callback) {
17+
// Weird quirk of Joyent servers
1118
console.log("Be sure to ssh to root@" + options.host + " at least once to enable remote access.");
1219

20+
// Go with Joyent's grain
1321
options.constants.name = 'node';
14-
//console.log("OPTIONS:", options);
15-
return {
22+
23+
console.log("OPTIONS:", options);
24+
25+
var actions = {
26+
27+
// Stack: nodejs
1628

1729
keys: function(callback) {
30+
if (!options.config.deployment.nodejs) return callback("not a node stack");
1831
console.log("Use the Joyent Web Admin tool to add user keys!");
1932
return callback();
2033
},
2134

2235
user: function(callback) {
36+
if (!options.config.deployment.nodejs) return callback("not a node stack");
2337
console.log("The Joyent default node.js user is 'node'");
2438
return callback();
2539
},
2640

27-
install: function(callback) {
28-
var map = {
29-
'system': {
30-
user: 'root',
31-
script: '/system.sh'
32-
},
33-
'nodejs': {
34-
user: 'root',
35-
script: '/nodejs.sh'
36-
}
37-
};
38-
var to_install = ['system'].concat(options.stack.install);
39-
console.log("Installing stack software:", to_install);
40-
console.log("(This could take several minutes)");
41-
function installItem(item, callback) {
42-
console.log("Installing " + item + "...");
43-
var installer = map[item];
44-
if (installer) {
45-
return shell.remote(installer.user, options.host, __dirname, installer.script, options, callback);
46-
}
47-
return callback();
48-
}
49-
return async.forEachSeries(to_install, installItem, callback);
41+
nodejs: function(callback) {
42+
if (!options.config.deployment.nodejs) return callback("not a node stack");
43+
console.log("Installing node.js v" + options.config.deployment.nodejs.version);
44+
return shell.remote('root', options.host, __dirname, '/nodejs.sh', options, callback);
5045
},
5146

5247
environment: function(callback) {
53-
shell.local(__dirname, '/environment.sh', options, callback);
48+
if (!options.config.deployment.nodejs) return callback("not a node stack");
49+
return shell.local(__dirname, '/environment.sh', options, callback);
5450
},
5551

5652
service: function(callback) {
53+
if (!options.config.deployment.nodejs) return callback("not a node stack");
5754
console.log("The Joyent node.js stack already configures a node.js service!");
5855
return callback();
5956
},
6057

6158
remote: function(callback) {
62-
shell.local(__dirname, '/remote.sh', options, callback);
59+
if (!options.config.deployment.nodejs) return callback("not a node stack");
60+
if (!options.remote) {
61+
console.warn("no remote specified");
62+
return callback();
63+
}
64+
return shell.local(__dirname, '/remote.sh', options, callback);
6365
},
6466

6567
cycle: function(callback) {
66-
shell.remote('node', options.host, __dirname, '/cycle.sh', options, callback);
67-
}
68+
if (!options.config.deployment.nodejs) return callback("not a node stack");
69+
return shell.remote('root', options.host, __dirname, '/cycle.sh', options, callback);
70+
},
71+
72+
// Stack: mongodb
73+
74+
mongodb: function(callback) {
75+
if (!options.config.deployment.mongodb) return callback("not a mongo stack");
76+
return shell.remote('root', options.host, __dirname, '/mongodb.sh', options, callback);
77+
},
78+
79+
// Stack: riak
6880

81+
riak: function(callback) {
82+
var riak = options.config.deployment.riak;
83+
if (!riak) return callback("not a riak stack");
84+
return shell.remote('root', options.host, __dirname, '/riak.sh', options, callback);
85+
}
6986
};
70-
};
87+
88+
// Execute all these actions
89+
console.log("actions:", actions);
90+
async.forEachSeries(Object.keys(actions), action, callback);
91+
92+
function action(item, callback) {
93+
section(item);
94+
actions[item](function(err, result) {
95+
if (err) console.log(err);
96+
return callback();
97+
});
98+
}
99+
}

scripts/lib/joyent/riak.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
PRIVATE_IP=`ifconfig -a | grep 'inet 10.' | grep -v '127.0.0.1' | awk '{ print $2}'`
2+
3+
echo "Master IP: {{{config.deployment.riak.master}}}"
4+
echo "Private IP: $PRIVATE_IP"
5+
6+
echo "Disabling riak service"
7+
svcadm disable riak
8+
sleep 5
9+
10+
echo "Setting cookie"
11+
sed -i "s/-setcookie.*/-setcookie {{{config.deployment.riak.cookie}}}/" /opt/local/etc/riak/vm.args
12+
sleep 5
13+
14+
echo "Enabling riak service"
15+
svcadm enable riak
16+
sleep 10
17+
18+
# Abort if this IP is already in our ring
19+
(riak-admin status | grep ring_members | grep {{{config.deployment.riak.master}}}) || (
20+
21+
echo "Joining to cluster via {{{config.deployment.riak.master}}}"
22+
/opt/local/sbin/riak-admin join riak@{{{config.deployment.riak.master}}}
23+
24+
sleep 10
25+
riak-admin status | grep ring_members
26+
)
27+
28+
sleep 10
29+
riak-admin status | grep ring_members

scripts/lib/shell.js

-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ Shell.prototype = {
2222
},
2323
remote: function(user, host, dir, script, data, callback) {
2424
this.template(dir, script, data);
25-
var cmd = 'ssh ' + user + '@' + host + " 'bash -s' < " + this.tmpfile;
2625
var self = this;
27-
console.log("EXECUTING:", cmd);
2826

2927
// Create SSH process
3028
var ssh = spawn('ssh', [user + '@' + host, "bash -s"]);

0 commit comments

Comments
 (0)