Skip to content
This repository
Browse code

[docs] pushed to gh-pages

  • Loading branch information...
commit 64809e32e68a01b06b28707e0eaa833b6e700d4c 1 parent 36c425c
nlco authored

Showing 90 changed files with 54 additions and 5,497 deletions. Show diff stats Hide diff stats

  1. +0 19 LICENSE
  2. +0 194 README.md
  3. +0 78 bin/carapace
  4. 0  bin/haibu
  5. +0 52 bin/haibu-balancer
  6. +0 57 bin/haibu-server
  7. +0 18 bin/mkdirs
  8. +0 6 config/auth.json.example
  9. 0  {docs → }/docco.css
  10. BIN  examples/haibu.png
  11. +0 35 examples/hello-spawn.js
  12. 0  {docs → }/haibu.html
  13. 0  {docs → }/haibu/balancer/balancer.html
  14. 0  {docs → }/haibu/balancer/index.html
  15. 0  {docs → }/haibu/core/config.html
  16. 0  {docs → }/haibu/core/process-store.html
  17. 0  {docs → }/haibu/core/spawner.html
  18. 0  {docs → }/haibu/drone/client.html
  19. 0  {docs → }/haibu/drone/drone.html
  20. 0  {docs → }/haibu/drone/index.html
  21. 0  {docs → }/haibu/drone/service.html
  22. 0  {docs → }/haibu/plugins/changelog.html
  23. 0  {docs → }/haibu/plugins/chroot.html
  24. 0  {docs → }/haibu/plugins/logger.html
  25. 0  {docs → }/haibu/repositories/git.html
  26. 0  {docs → }/haibu/repositories/index.html
  27. 0  {docs → }/haibu/repositories/local-file.html
  28. 0  {docs → }/haibu/repositories/npm.html
  29. 0  {docs → }/haibu/repositories/remote-file.html
  30. 0  {docs → }/haibu/repositories/repository.html
  31. 0  {docs → }/haibu/repositories/tar.html
  32. 0  {docs → }/haibu/repositories/zip.html
  33. 0  {docs → }/haibu/utils/base64.html
  34. 0  {docs → }/haibu/utils/bin.html
  35. 0  {docs → }/haibu/utils/index.html
  36. +54 0 index.html
  37. +0 65 lib/haibu.js
  38. +0 301 lib/haibu/balancer/balancer.js
  39. +0 63 lib/haibu/balancer/index.js
  40. +0 230 lib/haibu/core/config.js
  41. +0 109 lib/haibu/core/process-store.js
  42. +0 218 lib/haibu/core/spawner.js
  43. +0 164 lib/haibu/drone/client.js
  44. +0 231 lib/haibu/drone/drone.js
  45. +0 47 lib/haibu/drone/index.js
  46. +0 137 lib/haibu/drone/service.js
  47. +0 2  lib/haibu/plugins/changelog.js
  48. +0 58 lib/haibu/plugins/chroot.js
  49. +0 163 lib/haibu/plugins/logger.js
  50. +0 158 lib/haibu/repositories/git.js
  51. +0 101 lib/haibu/repositories/index.js
  52. +0 87 lib/haibu/repositories/local-file.js
  53. +0 240 lib/haibu/repositories/npm.js
  54. +0 114 lib/haibu/repositories/remote-file.js
  55. +0 210 lib/haibu/repositories/repository.js
  56. +0 63 lib/haibu/repositories/tar.js
  57. +0 65 lib/haibu/repositories/zip.js
  58. +0 44 lib/haibu/utils/base64.js
  59. +0 71 lib/haibu/utils/bin.js
  60. +0 310 lib/haibu/utils/index.js
  61. +0 39 package.json
  62. +0 66 test/core/config-api-test.js
  63. +0 38 test/core/config-dirs-test.js
  64. +0 64 test/core/config-test.js
  65. +0 27 test/core/package-test.js
  66. +0 117 test/core/spawner-test.js
  67. +0 139 test/drone/client-test.js
  68. +0 230 test/drone/drone-api-test.js
  69. +0 210 test/drone/drone-test.js
  70. +0 48 test/drone/process-store-test.js
  71. +0 47 test/fixtures/apps.js
  72. +0 4 test/fixtures/repositories/bad-app/index.js
  73. +0 13 test/fixtures/repositories/bad-app/package.json
  74. BIN  test/fixtures/repositories/chat.tgz
  75. +0 7 test/fixtures/repositories/local-file/package.json
  76. +0 9 test/fixtures/repositories/local-file/run.js
  77. BIN  test/fixtures/repositories/npm-deps.tgz
  78. +0 9 test/fixtures/repositories/npm-deps/index.js
  79. +0 16 test/fixtures/repositories/npm-deps/package.json
  80. BIN  test/fixtures/repositories/test.tgz
  81. +0 78 test/helpers.js
  82. +0 4 test/mocks/mailer.js
  83. +0 133 test/plugins/chroot-test.js
  84. +0 25 test/plugins/logger-test.js
  85. +0 51 test/repositories/git-test.js
  86. +0 64 test/repositories/local-file-test.js
  87. +0 69 test/repositories/npm-test.js
  88. +0 95 test/repositories/remote-file-test.js
  89. +0 93 test/repositories/tar-test.js
  90. +0 92 test/repositories/zip-test.js
19 LICENSE
... ... @@ -1,19 +0,0 @@
1   -Copyright (c) 2010 Nodejitsu Inc.
2   -
3   -Permission is hereby granted, free of charge, to any person obtaining a copy
4   -of this software and associated documentation files (the "Software"), to deal
5   -in the Software without restriction, including without limitation the rights
6   -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7   -copies of the Software, and to permit persons to whom the Software is
8   -furnished to do so, subject to the following conditions:
9   -
10   -The above copyright notice and this permission notice shall be included in
11   -all copies or substantial portions of the Software.
12   -
13   -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14   -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15   -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16   -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17   -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18   -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19   -THE SOFTWARE.
194 README.md
Source Rendered
... ... @@ -1,194 +0,0 @@
1   -# haibu
2   -
3   -<img src="https://github.com/nodejitsu/haibu/raw/master/examples/haibu.png"/></img>
4   -
5   -*spawn your own node.js clouds, on your own hardware*
6   -
7   -# What is haibu?
8   -
9   -haibu is the open-source [node.js](http://nodejs.org) project for spawning and managing several node.js applications on a single server. It's an integral part of [Nodejitsu's](http://nodejitsu.com) production stack and is fully supported by a dedicated team of core node.js developers.
10   -
11   -# How does it work?
12   -
13   -haibu (which is Japanese for "hive") transforms node.js applications (using a [Carapace](https://github.com/nodejitsu/haibu/blob/master/bin/carapace)) into "drones". This approach allows haibu to directly interact with node.js applications and add all sorts of additional functionality. haibu also contains a plugin system, so you can easily add even more functionality without needing to dive too far into the codebase.
14   -
15   -`haibu` builds on this concept of "drones" and exposes a robust and granular API for interacting with your node.js applications. At a low level, haibu's API is exposed as a RESTFul HTTP webservice. Any system that supports basic HTTP requests can communicate with a haibu server. If you are working in Node.js, haibu comes with a high-level Node.js API client.
16   -
17   -## Where can I run haibu?
18   -
19   -`haibu` doesn't discriminate. If your environment supports node.js, you can install `haibu` and start up your own node.js cloud. This makes `haibu` an ideal tool for both development purposes and production usage since you can seamlessly setup haibu on your local machine, on utility computing providers (such as Amazon EC2 or Rackspace), on dedicated servers, or even on a mobile phone!
20   -
21   -# Installation
22   -
23   - [sudo] npm install haibu -g
24   -
25   -# Documentation
26   -
27   -haibu's documentation is still very much a work in progress. We'll be actively updating the documentation in the upcoming weeks to make it easier to get acclimated with `haibu`. Aside from the overview provided in this `README.md` file, `haibu` uses docco and literate style programming to provide comprehensive source code documentation. Check out `/docs/haibu.html` for more information.
28   -
29   -# An overview of using haibu
30   -
31   -## Starting up a haibu-server
32   -
33   -```
34   -[sudo] node bin/haibu-server
35   -(...)
36   -haibu started @ 127.0.0.1 on port 9002 as api-server
37   -```
38   -
39   -**Now that there is a haibu server running, we can begin to interact with it's API.**
40   -
41   -##Starting an application using the haibu Client
42   -*(From: /examples/hello-spawn.js)*
43   -
44   -Allows you to call haibu programmatically from inside your node.js scripts.
45   -
46   -```javascript
47   -var eyes = require('eyes'),
48   - haibu = require('haibu');
49   -
50   -// Create a new client for communicating with the haibu server
51   -var client = new haibu.drone.Client({
52   - host: 'localhost',
53   - port: 9002
54   -});
55   -
56   -// A basic package.json for a node.js application on haibu
57   -var app = {
58   - "user": "marak",
59   - "name": "test",
60   - "domain": "devjitsu.com",
61   - "repository": {
62   - "type": "git",
63   - "url": "https://github.com/Marak/hellonode.git",
64   - },
65   - "scripts": {
66   - "start": "server.js"
67   - }
68   -};
69   -
70   -// Attempt to start up a new application
71   -client.start(app, function (err, result) {
72   - if (err) {
73   - console.log('Error spawning app: ' + app.name);
74   - return eyes.inspect(err);
75   - }
76   -
77   - console.log('Successfully spawned app:');
78   - eyes.inspect(result);
79   -});
80   -
81   -
82   -client.start(app, function (err, result) {
83   - eyes.inspect(err);
84   - eyes.inspect(result);
85   -});
86   -```
87   -
88   -## Using haibu-balancer
89   -
90   -Once your node.js application has been started on `haibu` you're going to want to access it. `haibu-balancer` will load balance multiple instances of your application using [node-http-proxy][1] based on the `domain` property supplied in the package.json sent to each `start` request. Starting `haibu-balancer` is very simple:
91   -
92   -```
93   -sudo haibu-balancer
94   -(...)
95   -haibu started @ 127.0.0.1 on port 80 as balancer
96   -```
97   -
98   -Lets look at the sample data we sent to the `haibu-server` in the above example
99   -
100   -```javascript
101   -{
102   - "user": "marak",
103   - "name": "test",
104   - "domain": "devjitsu.com",
105   - "repository": {
106   - "type": "git",
107   - "url": "https://github.com/Marak/hellonode.git",
108   - },
109   - "scripts": {
110   - "start": "server.js"
111   - }
112   -}
113   -```
114   -
115   -As you can see, the `domain` property is set to `devjitsu.com`. This means that incoming HTTP requests which have their `HOST` header set to `devjitsu.com` will be round-robin load-balanced across all instances of your application managed by `haibu`. If you're testing locally you can modify your `/etc/hosts` file for `devjitsu.com` and see your applications running in your local development environment.
116   -
117   -## RESTful Webservice
118   -
119   -If you need to integrate non-node.js systems with haibu, you can use haibu's RESTful JSON API. We recommend using haibu's native Node.js Client, but if you need to integrate with none-node.js systems this is not always a viable option.
120   -
121   -**Starting an application through the webservice**
122   -
123   - POST http://127.0.0.1:9002/drones/test/start
124   - ...
125   - {
126   - "start": {
127   - "user": "marak",
128   - "name": "test",
129   - "domain": "devjitsu.com",
130   - "repository": {
131   - "type": "git",
132   - "url": "https://github.com/Marak/hellonode.git"
133   - },
134   - "scripts": {
135   - "start": "server.js"
136   - }
137   - }
138   - }
139   -
140   - **Response**
141   -
142   - HTTP/1.1 200 OK
143   - Date: Thu, 05 May 2011 18:15:36 GMT
144   - Server: journey/0.4.0
145   - Content-Type: application/json
146   - Content-Length: 353
147   - Connection: close
148   - ...
149   - {
150   - drone: {
151   - uid: 'gbE3',
152   - ctime: 1304619335818,
153   - pid: 7903,
154   - foreverPid: 7195,
155   - logFile: '/Users/Charlie/.forever/gbE3.log',
156   - options: [ '/Users/Charlie/Nodejitsu/haibu/local/marak/test/hellonode/server.js', '127.0.0.1', 8001 ],
157   - file: '/Users/Charlie/Nodejitsu/haibu/bin/carapace',
158   - pidFile: '/Users/Charlie/.forever/pids/gbE3.pid',
159   - port: 8001,
160   - host: '127.0.0.1'
161   - }
162   - }
163   -
164   -**Stopping an application through the webservice**
165   -
166   -
167   - POST http://127.0.0.1:9002/drones/test/stop
168   - ...
169   - {
170   - "stop": {
171   - "name": "test"
172   - }
173   - }
174   -
175   - **response**
176   -
177   - HTTP/1.1 200 OK
178   - Date: Thu, 05 May 2011 18:16:22 GMT
179   - Server: journey/0.4.0
180   - Connection: close
181   - Transfer-Encoding: chunked
182   -
183   -## Run Tests
184   -All of the `haibu` tests are written in [vows][0], and cover all of the use cases described above.
185   -<pre>
186   - sudo vows test/**/*-test.js --spec
187   -</pre>
188   -
189   -*If you copy and paste the above link, the test suite will attempt to connect to Rackspace for some of the remote file tests. You don't need to run these tests or use Rackspace to get started. We'll be improving our test runner soon to help make this process a bit more intuitive.*
190   -
191   -#### Author: [Nodejitsu Inc.](http://www.nodejitsu.com)
192   -
193   -[0]: http://vowsjs.org
194   -[1]: http://github.com/nodejitsu/node-http-proxy
78 bin/carapace
... ... @@ -1,78 +0,0 @@
1   -/*
2   - * carapace.js: Light-weight wrapper thats envelopes a drone, allowing us to override
3   - * several aspects of the drone, such as the HTTP server. We also have the
4   - * benefit of getting returned process informations about the drone.
5   - *
6   - * (C) 2010, Nodejitsu Inc.
7   - *
8   - */
9   -
10   -var util = require('util'),
11   - daemon = require('daemon'),
12   - net = require('net'),
13   - http = require('http');
14   -
15   -//
16   -// TODO (indexzero): Make this more robust when forever is included.
17   -//
18   -var carapace = {};
19   -carapace.config = {
20   - script : process.argv[2],
21   - server : process.argv[3],
22   - port : process.argv[4],
23   - root : process.argv[5],
24   - appPath : process.argv[6],
25   - pid : process.pid
26   -};
27   -
28   -var netListen = net.Server.prototype.listen;
29   -
30   -//
31   -// Helper function from Node code to parse port arguments
32   -// passed to net.prototype.Server.listen
33   -//
34   -function toPort(x) {
35   - return (x = Number(x)) >= 0 ? x : false;
36   -}
37   -
38   -//
39   -// Donkey punch the listen() function.
40   -//
41   -// 1. Listen on a UNIX socket: server.listen('/tmp/socket');
42   -// Ignore these cases, pass through to netListen
43   -//
44   -// 2. Listen on port 8000, accept connections from INADDR_ANY: server.listen(8000);
45   -// Change the port to the port we have been passed by the config
46   -//
47   -// 3. Listen on port 8000, accept connections to '192.168.1.2': server.listen(8000, '192.168.1.2');
48   -// Change the port to the port we have been passed by the config
49   -//
50   -net.Server.prototype.listen = function () {
51   - var args = Array.prototype.slice.call(arguments),
52   - port = toPort(args[0]);
53   -
54   - //
55   - // If arguments[0] is a port, assume (2, 3) from the documentation
56   - // above and change the value of the arguments to what we've been passed.
57   - //
58   - if (port) {
59   - args[0] = carapace.config.port;
60   - }
61   -
62   - return netListen.apply(this, args);
63   -};
64   -
65   -// Update the require path of the target drone start script
66   -var p = carapace.config.script.replace('.js', ''),
67   - tmp = util.puts, drone;
68   -
69   -// Set the root of the child process if requested
70   -if (carapace.config.root) {
71   - daemon.chroot(carapace.config.root);
72   - process.chdir(carapace.config.appPath);
73   -}
74   -
75   -require.paths.unshift(process.cwd());
76   -
77   -// Now just require the drone to get things moving.
78   -drone = require(p);
0  bin/haibu
No changes.
52 bin/haibu-balancer
... ... @@ -1,52 +0,0 @@
1   -#!/usr/bin/env node
2   -
3   -require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
4   -
5   -var util = require('util'),
6   - colors = require('colors'),
7   - argv = require('optimist').argv,
8   - haibu = require('haibu');
9   -
10   -var help = [
11   - 'usage: haibu-balancer [options]',
12   - '',
13   - 'Starts the haibu balancer server responsible for handling all incoming HTTP requests.',
14   - '',
15   - 'options:',
16   - ' -a IP Address that you want the server to bind to [dynamic]',
17   - ' -p Port that you want the server to run on [80]',
18   - ' -e [env] The environment to the specified command in [development]',
19   - ' --logger Use the haibu logger plugin [true]',
20   - ' -s --silent Suppress the log messages from the output',
21   - ' -h, --help You\'re staring at it',
22   -].join('\n');
23   -
24   -if (argv.h || argv.help) {
25   - return util.puts(help);
26   -}
27   -
28   -var env = argv.env || 'development',
29   - logger = typeof argv.logger !== 'undefined' ? argv.logger : true,
30   - port = argv.p || 80;
31   -
32   -haibu.utils.bin.getAddress(argv.a, function (err, address) {
33   - if (err) {
34   - log('Error getting IP Address: ' + err.message);
35   - }
36   -
37   - haibu.utils.bin.tryLoadCache(function (err, config) {
38   - var options = {
39   - env: env,
40   - port: port,
41   - config: config
42   - };
43   -
44   - haibu.balancer.start(options, function (err, server, proxy, active) {
45   - if (logger) {
46   - haibu.use(haibu.plugins.logger);
47   - }
48   -
49   - haibu.utils.showWelcome('balancer', address, port);
50   - });
51   - });
52   -});
57 bin/haibu-server
... ... @@ -1,57 +0,0 @@
1   -#!/usr/bin/env node
2   -
3   -require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
4   -
5   -var util = require('util'),
6   - colors = require('colors'),
7   - argv = require('optimist').argv,
8   - haibu = require('haibu');
9   -
10   -var help = [
11   - 'usage: haibu-server [options]',
12   - '',
13   - 'Starts the haibu API server responsible for spawning node.js applications.',
14   - '',
15   - 'options:',
16   - ' -a IP Address that you want the server to bind to [dynamic]',
17   - ' -p Port that you want the server to run on [9002]',
18   - ' -e [env] The environment to the specified command in [development]',
19   - ' --logger Use the haibu logger plugin [true]',
20   - ' --chroot Deploy drones using chroot',
21   - ' -s --silent Suppress the log messages from the output',
22   - ' -h, --help You\'re staring at it',
23   -].join('\n');
24   -
25   -if (argv.h || argv.help) {
26   - return util.puts(help);
27   -}
28   -
29   -if (argv.chroot) {
30   - haibu.use(haibu.plugins.chroot);
31   -}
32   -
33   -var env = argv.env || 'development',
34   - logger = typeof argv.logger !== 'undefined' ? argv.logger : true,
35   - port = argv.p || 9002;
36   -
37   -haibu.utils.bin.getAddress(argv.a, function (err, address) {
38   - if (err) {
39   - log('Error getting IP Address: ' + err.message);
40   - }
41   -
42   - haibu.utils.bin.tryLoadCache(function (err, config) {
43   - var options = {
44   - env: env,
45   - port: port,
46   - config: config
47   - };
48   -
49   - haibu.drone.start(options, function () {
50   - if (logger) {
51   - haibu.use(haibu.plugins.logger);
52   - }
53   -
54   - haibu.utils.showWelcome('api-server', address, port);
55   - });
56   - });
57   -});
18 bin/mkdirs
... ... @@ -1,18 +0,0 @@
1   -#!/usr/bin/env node
2   -
3   -require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
4   -
5   -var sys = require('sys'),
6   - haibu = require('haibu');
7   -
8   -haibu.utils.initDirectories(function (err, paths) {
9   - if (err) {
10   - sys.puts('Failed to create directories for haibu: ' + err.message);
11   - return;
12   - }
13   -
14   - sys.puts('Successfully created directories for haibu: ');
15   - paths.forEach(function (path) {
16   - sys.puts(' ' + path);
17   - });
18   -});
6 config/auth.json.example
... ... @@ -1,6 +0,0 @@
1   -{
2   - "auth": {
3   - "username": "test-username",
4   - "apiKey": "test-apiKey"
5   - }
6   -}
0  docs/docco.css → docco.css
File renamed without changes
BIN  examples/haibu.png
35 examples/hello-spawn.js
... ... @@ -1,35 +0,0 @@
1   -require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
2   -
3   -var eyes = require('eyes'),
4   - haibu = require('haibu');
5   -
6   -// Create a new client for communicating with the haibu server
7   -var client = new haibu.drone.Client({
8   - host: 'localhost',
9   - port: 9002
10   -});
11   -
12   -// A basic package.json for a node.js application on Haibu
13   -var app = {
14   - "user": "marak",
15   - "name": "test",
16   - "domain": "devjitsu.com",
17   - "repository": {
18   - "type": "git",
19   - "url": "https://github.com/Marak/hellonode.git",
20   - },
21   - "scripts": {
22   - "start": "server.js"
23   - }
24   -};
25   -
26   -// Attempt to start up a new application
27   -client.start(app, function (err, result) {
28   - if (err) {
29   - console.log('Error spawning app: ' + app.name);
30   - return eyes.inspect(err);
31   - }
32   -
33   - console.log('Successfully spawned app:');
34   - eyes.inspect(result);
35   -});
0  docs/haibu.html → haibu.html
File renamed without changes
0  docs/haibu/balancer/balancer.html → haibu/balancer/balancer.html
File renamed without changes
0  docs/haibu/balancer/index.html → haibu/balancer/index.html
File renamed without changes
0  docs/haibu/core/config.html → haibu/core/config.html
File renamed without changes
0  docs/haibu/core/process-store.html → haibu/core/process-store.html
File renamed without changes
0  docs/haibu/core/spawner.html → haibu/core/spawner.html
File renamed without changes
0  docs/haibu/drone/client.html → haibu/drone/client.html
File renamed without changes
0  docs/haibu/drone/drone.html → haibu/drone/drone.html
File renamed without changes
0  docs/haibu/drone/index.html → haibu/drone/index.html
File renamed without changes
0  docs/haibu/drone/service.html → haibu/drone/service.html
File renamed without changes
0  docs/haibu/plugins/changelog.html → haibu/plugins/changelog.html
File renamed without changes
0  docs/haibu/plugins/chroot.html → haibu/plugins/chroot.html
File renamed without changes
0  docs/haibu/plugins/logger.html → haibu/plugins/logger.html
File renamed without changes
0  docs/haibu/repositories/git.html → haibu/repositories/git.html
File renamed without changes
0  docs/haibu/repositories/index.html → haibu/repositories/index.html
File renamed without changes
0  docs/haibu/repositories/local-file.html → haibu/repositories/local-file.html
File renamed without changes
0  docs/haibu/repositories/npm.html → haibu/repositories/npm.html
File renamed without changes
0  docs/haibu/repositories/remote-file.html → haibu/repositories/remote-file.html
File renamed without changes
0  docs/haibu/repositories/repository.html → haibu/repositories/repository.html
File renamed without changes
0  docs/haibu/repositories/tar.html → haibu/repositories/tar.html
File renamed without changes
0  docs/haibu/repositories/zip.html → haibu/repositories/zip.html
File renamed without changes
0  docs/haibu/utils/base64.html → haibu/utils/base64.html
File renamed without changes
0  docs/haibu/utils/bin.html → haibu/utils/bin.html
File renamed without changes
0  docs/haibu/utils/index.html → haibu/utils/index.html
File renamed without changes
54 index.html
... ... @@ -0,0 +1,54 @@
  1 +<!DOCTYPE html> <html> <head> <title>haibu.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="haibu/balancer/balancer.html"> haibu/balancer/balancer.html </a> <a class="source" href="haibu/balancer/index.html"> haibu/balancer/index.html </a> <a class="source" href="haibu/core/config.html"> haibu/core/config.html </a> <a class="source" href="haibu/core/process-store.html"> haibu/core/process-store.html </a> <a class="source" href="haibu/core/spawner.html"> haibu/core/spawner.html </a> <a class="source" href="haibu/drone/client.html"> haibu/drone/client.html </a> <a class="source" href="haibu/drone/drone.html"> haibu/drone/drone.html </a> <a class="source" href="haibu/drone/index.html"> haibu/drone/index.html </a> <a class="source" href="haibu/drone/service.html"> haibu/drone/service.html </a> <a class="source" href="haibu/plugins/changelog.html"> haibu/plugins/changelog.html </a> <a class="source" href="haibu/plugins/chroot.html"> haibu/plugins/chroot.html </a> <a class="source" href="haibu/plugins/logger.html"> haibu/plugins/logger.html </a> <a class="source" href="haibu/repositories/git.html"> haibu/repositories/git.html </a> <a class="source" href="haibu/repositories/index.html"> haibu/repositories/index.html </a> <a class="source" href="haibu/repositories/local-file.html"> haibu/repositories/local-file.html </a> <a class="source" href="haibu/repositories/npm.html"> haibu/repositories/npm.html </a> <a class="source" href="haibu/repositories/remote-file.html"> haibu/repositories/remote-file.html </a> <a class="source" href="haibu/repositories/repository.html"> haibu/repositories/repository.html </a> <a class="source" href="haibu/repositories/tar.html"> haibu/repositories/tar.html </a> <a class="source" href="haibu/repositories/zip.html"> haibu/repositories/zip.html </a> <a class="source" href="haibu/utils/base64.html"> haibu/utils/base64.html </a> <a class="source" href="haibu/utils/bin.html"> haibu/utils/bin.html </a> <a class="source" href="haibu/utils/index.html"> haibu/utils/index.html </a> <a class="source" href="haibu.html"> haibu.html </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> haibu.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*</span>
  2 +<span class="cm"> * haibu.js: Top level include for the haibu module</span>
  3 +<span class="cm"> *</span>
  4 +<span class="cm"> * (C) 2010, Nodejitsu Inc.</span>
  5 +<span class="cm"> *</span>
  6 +<span class="cm"> */</span>
  7 +
  8 +<span class="kd">var</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;fs&#39;</span><span class="p">),</span>
  9 + <span class="nx">events</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;events&#39;</span><span class="p">);</span>
  10 +
  11 +<span class="nx">require</span><span class="p">.</span><span class="nx">paths</span><span class="p">.</span><span class="nx">unshift</span><span class="p">(</span><span class="nx">__dirname</span><span class="p">);</span>
  12 +
  13 +<span class="kd">var</span> <span class="nx">haibu</span> <span class="o">=</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">events</span><span class="p">.</span><span class="nx">EventEmitter</span><span class="p">();</span>
  14 +
  15 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">config</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;haibu/core/config&#39;</span><span class="p">);</span>
  16 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">utils</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;haibu/utils&#39;</span><span class="p">);</span>
  17 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">Spawner</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;haibu/core/spawner&#39;</span><span class="p">).</span><span class="nx">Spawner</span><span class="p">;</span>
  18 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">ProcessStore</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;haibu/core/process-store&#39;</span><span class="p">).</span><span class="nx">ProcessStore</span><span class="p">;</span>
  19 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">repository</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;haibu/repositories&#39;</span><span class="p">);</span>
  20 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">drone</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;haibu/drone&#39;</span><span class="p">);</span>
  21 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">balancer</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;haibu/balancer&#39;</span><span class="p">);</span>
  22 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">initialized</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
  23 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">plugins</span> <span class="o">=</span> <span class="p">{};</span>
  24 +<span class="nx">haibu</span><span class="p">.</span><span class="nx">_plugins</span> <span class="o">=</span> <span class="p">{};</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>function init (options, callback)
  25 +Initializes haibu directories and models</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">haibu</span><span class="p">.</span><span class="nx">init</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
  26 + <span class="k">if</span> <span class="p">(</span><span class="nx">haibu</span><span class="p">.</span><span class="nx">initialized</span><span class="p">)</span> <span class="p">{</span>
  27 + <span class="k">return</span> <span class="nx">callback</span><span class="p">();</span>
  28 + <span class="p">}</span>
  29 +
  30 + <span class="nx">haibu</span><span class="p">.</span><span class="nx">config</span><span class="p">.</span><span class="nx">load</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
  31 + <span class="nx">haibu</span><span class="p">.</span><span class="nx">utils</span><span class="p">.</span><span class="nx">initDirectories</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  32 + <span class="nx">haibu</span><span class="p">.</span><span class="nx">initialized</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
  33 + <span class="nx">callback</span><span class="p">();</span>
  34 + <span class="p">});</span>
  35 + <span class="p">});</span>
  36 +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h3>function use (plugin)</h3>
  37 +
  38 +<h4>@plugin {Object} Instance of a plugin from <code>haibu.plugins</code></h4>
  39 +
  40 +<p>Adds the specified <code>plugin</code> to the set of active plugins used by haibu.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">haibu</span><span class="p">.</span><span class="nx">use</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">plugin</span><span class="p">)</span> <span class="p">{</span>
  41 + <span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
  42 + <span class="nx">callback</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">pop</span><span class="p">(),</span>
  43 + <span class="nx">options</span> <span class="o">=</span> <span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
  44 +
  45 + <span class="nx">haibu</span><span class="p">.</span><span class="nx">_plugins</span><span class="p">[</span><span class="nx">plugin</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">plugin</span><span class="p">;</span>
  46 + <span class="nx">plugin</span><span class="p">.</span><span class="nx">init</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span>
  47 +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Define each of our plugins as a lazy loaded <code>require</code> statement</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">fs</span><span class="p">.</span><span class="nx">readdirSync</span><span class="p">(</span><span class="nx">__dirname</span> <span class="o">+</span> <span class="s1">&#39;/haibu/plugins&#39;</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">plugin</span><span class="p">)</span> <span class="p">{</span>
  48 + <span class="nx">plugin</span> <span class="o">=</span> <span class="nx">plugin</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="s1">&#39;.js&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">);</span>
  49 + <span class="nx">haibu</span><span class="p">.</span><span class="nx">plugins</span><span class="p">.</span><span class="nx">__defineGetter__</span><span class="p">(</span><span class="nx">plugin</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  50 + <span class="k">return</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./haibu/plugins/&#39;</span> <span class="o">+</span> <span class="nx">plugin</span><span class="p">);</span>
  51 + <span class="p">});</span>
  52 +<span class="p">});</span>
  53 +
  54 +</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
65 lib/haibu.js
... ... @@ -1,65 +0,0 @@
1   -/*
2   - * haibu.js: Top level include for the haibu module
3   - *
4   - * (C) 2010, Nodejitsu Inc.
5   - *
6   - */
7   -
8   -var fs = require('fs'),
9   - events = require('events');
10   -
11   -require.paths.unshift(__dirname);
12   -
13   -var haibu = module.exports = new events.EventEmitter();
14   -
15   -haibu.config = require('haibu/core/config');
16   -haibu.utils = require('haibu/utils');
17   -haibu.Spawner = require('haibu/core/spawner').Spawner;
18   -haibu.ProcessStore = require('haibu/core/process-store').ProcessStore;
19   -haibu.repository = require('haibu/repositories');
20   -haibu.drone = require('haibu/drone');
21   -haibu.balancer = require('haibu/balancer');
22   -haibu.initialized = false;
23   -haibu.plugins = {};
24   -haibu._plugins = {};
25   -
26   -//
27   -// function init (options, callback)
28   -// Initializes haibu directories and models
29   -//
30   -haibu.init = function (options, callback) {
31   - if (haibu.initialized) {
32   - return callback();
33   - }
34   -
35   - haibu.config.load(options, function (err) {
36   - haibu.utils.initDirectories(function () {
37   - haibu.initialized = true;
38   - callback();
39   - });
40   - });
41   -};
42   -
43   -//
44   -// ### function use (plugin)
45   -// #### @plugin {Object} Instance of a plugin from `haibu.plugins`
46   -// Adds the specified `plugin` to the set of active plugins used by haibu.
47   -//
48   -haibu.use = function (plugin) {
49   - var args = Array.prototype.slice.call(arguments),
50   - callback = typeof args[args.length - 1] === 'function' && args.pop(),
51   - options = args.length > 1 && args.pop();
52   -
53   - haibu._plugins[plugin.name] = plugin;
54   - plugin.init(options, callback);
55   -};
56   -
57   -//
58   -// Define each of our plugins as a lazy loaded `require` statement
59   -//
60   -fs.readdirSync(__dirname + '/haibu/plugins').forEach(function (plugin) {
61   - plugin = plugin.replace('.js', '');
62   - haibu.plugins.__defineGetter__(plugin, function () {
63   - return require('./haibu/plugins/' + plugin);
64   - });
65   -});
301 lib/haibu/balancer/balancer.js
... ... @@ -1,301 +0,0 @@
1   -/*
2   - * proxy.js: Responsible for proxying across all applications available to haibu.
3   - *
4   - * (C) 2010, Nodejitsu Inc.
5   - *
6   - */
7   -
8   -var util = require('util'),
9   - path = require('path'),
10   - fs = require('fs'),
11   - events = require('events'),
12   - async = require('async'),
13   - colors = require('colors'),
14   - qs = require('querystring'),
15   - httpProxy = require('http-proxy'),
16   - haibu = require('haibu');
17   -
18   -//
19   -// ### @matchers
20   -// Regular expression parsers for handling different
21   -// persistent storage files used by `haibu`.
22   -//
23   -var matchers = {
24   - app: /^([\w|\-]+)\.package\.json/,
25   - pid: /^([\w|\-]+)\.(\d+)\.json/
26   -};
27   -
28   -//
29   -// ### function Balancer (options)
30   -// #### @options {Object} Options for this instance.
31   -// Constructor function for the `haibu` Balancer responsible
32   -// for load balancing across all applications know to haibu on
33   -// this machine.
34   -//
35   -var Balancer = exports.Balancer = function (options) {
36   - events.EventEmitter.call(this);
37   -
38   - var self = this;
39   -
40   - //
41   - // Setup shared state for the `haibu.ProcessStore`.
42   - //
43   - this.active = {};
44   - this.processes = new haibu.ProcessStore(options);
45   -
46   - //
47   - // Load files into Memory when they are found
48   - // in the ProcessStore
49   - //
50   - this.processes.on('created', function (file) {
51   - self._add(file, function () {
52   - self.emit('created', file);
53   - haibu.emit('balancer:created', 'info', { file: file });
54   - });
55   - });
56   -
57   - //
58   - // Remove files from the Balancer when they are removed
59   - // from the ProcessStore.
60   - //
61   - this.processes.on('removed', function (file) {
62   - self._remove(file);
63   - self.emit('removed', file);
64   - haibu.emit('balancer:removed', 'info', { file: file });
65   - });
66   -
67   - //
68   - // Load all relevant files on initial load.
69   - //
70   - this.processes.once('load', function (files) {
71   - function checkFile (file, next) {
72   - return files[file].isFile() ? self._add(file, next) : next();
73   - }
74   -
75   - async.forEach(Object.keys(files), checkFile, function (err) {
76   - return err ? self.emit('error', err) : self.emit('ready', self.active);
77   - });
78   - })
79   -
80   - //
81   - // Start monitoring for process files.
82   - //
83   - this.processes.monitor();
84   -
85   - //
86   - // Setup the balancing proxy using `node-http-proxy`.
87   - //
88   - this.httpProxy = new httpProxy.HttpProxy(options);
89   -
90   - this.httpProxy.on('end', function (req, res) {
91   - var diff = Date.now() - req.ptime;
92   - haibu.emit('balancer:proxy', 'info', {
93   - url: req.url,
94   - method: req.method,
95   - time: diff + 'ms'
96   - });
97   - });
98   -};
99   -
100   -//
101   -// Inherit from `events.EventEmitter`.
102   -//
103   -util.inherits(Balancer, events.EventEmitter);
104   -
105   -//
106   -// ### function handle (req, res)
107   -// #### @req {ServerRequest} Incoming server request to balancer
108   -// #### @res {ServerResponse} Outoing server request to write to.
109   -// Attempts to proxy the incoming request to the specified application
110   -// by using the `req.headers.host` property.
111   -//
112   -Balancer.prototype.handle = function (req, res) {
113   - var record = this.findDrone(req), drone;
114   -
115   - req.ptime = Date.now();
116   - haibu.emit('balancer:incoming', 'info', req.headers);
117   -
118   - if (!record) {
119   - return this.serveText(req, res, {
120   - code: 400,
121   - message: 'Application not found for: ' + req.headers.host
122   - });
123   - }
124   - else if (!record.drones || record.drones.length === 0) {
125   - return this.serveText(req, res, {
126   - code: 400,
127   - message: 'No drones for: ' + req.headers.host
128   - });
129   - }
130   -
131   - drone = record.drones.shift();
132   - this.httpProxy.proxyRequest(req, res, drone);
133   - record.drones.push(drone);
134   -};
135   -
136   -//
137   -// ### function findDrone (req)
138   -// #### @req {ServerRequest} Incoming server request to find drones against
139   -// Attempts to find a drone for the incoming server request
140   -// by cross-referencing `req.headers.host` against the `domain`
141   -// or `domains` property of each application known to `haibu`.
142   -//
143   -Balancer.prototype.findDrone = function (req) {
144   - var self = this,
145   - host = req.headers.host.split('.'),
146   - domain = host.slice(-2).join('.');
147   -
148   - return Object.keys(this.active).map(function (app) {
149   - return self.active[app];
150   - }).filter(function (rec) {
151   - return rec.app.domain === domain || (rec.app.domains
152   - && rec.app.domains.indexOf(domain) !== -1);
153   - })[0];
154   -};
155   -
156   -//
157   -// ### function serveText (req, res, data)
158   -// #### @req {ServerRequest} Incoming server request
159   -// #### @res {ServerResponse} Outoing server request to write to.
160   -// Writes `data.message` to the outgoing `res` along with any
161   -// metadata passed as `data.meta`.
162   -//
163   -Balancer.prototype.serveText = function (req, res, data) {
164   - var text = data.message,
165   - diff = Date.now() - req.ptime;
166   -
167   - if (data.meta) {
168   - text = [message, qs.unescape(qs.stringify(data.meta, ', '))].join(' | ');
169   - }
170   -
171   - res.writeHead(data.code, {
172   - 'Content-Length': text.length,
173   - 'Content-Type': 'text/plain'
174   - });
175   -
176   - if (req.method !== 'HEAD') {
177   - res.write(text);
178   - }
179   -
180   - haibu.emit('balancer:serve', 'info', {
181   - text: text,
182   - code: data.code,
183   - time: diff + 'ms'
184   - });
185   -
186   - res.end();
187   -};
188   -
189   -//
190   -// ### function close ()
191   -// Closes this balancer by shutting down the child
192   -// `HttpProxy` instance.
193   -//
194   -Balancer.prototype.close = function () {
195   - this.httpProxy.close();
196   -};
197   -
198   -//
199   -// ### function _add (file, callback)
200   -// #### @file {string} Filename to add to this instance.
201   -// #### @callback {function} **Optional** Continuation to respond to when complete.
202   -// Adds the data in the specified `file` to the managed state for
203   -// this balancer instance.
204   -//
205   -Balancer.prototype._add = function (file, callback) {
206   - var self = this;
207   -
208   - fs.readFile(file, function (err, data) {
209   - if (err) {
210   - return callback ? callback(err) : null;
211   - }
212   -
213   - try {
214   - var json = JSON.parse(data.toString()),
215   - app = self._parseFilename(file);
216   -
217   - if (app && app.type) {
218   - switch (app.type) {
219   - case 'package':
220   - self.active[app.name] = self.active[app.name] || {
221   - drones: []
222   - };
223   -
224   - self.active[app.name].app = json;
225   - break;
226   - case 'pid':
227   - self.active[app.name] = self.active[app.name] || {
228   - drones: []
229   - };
230   -
231   - self.active[app.name].drones.push({
232   - pid: json.pid,
233   - host: json.host,
234   - port: json.port
235   - });
236   - break;
237   - }
238   - }
239   -
240   - return callback ? callback() : null;
241   - }
242   - catch (ex) {
243   - return callback ? callback(ex) : null;
244   - }
245   - });
246   -};
247   -
248   -//
249   -// ### function _remove (file)
250   -// #### @file {string} Filename to remove from this instance
251   -// Removes the data in the specified `file` from the managed state
252   -// for this instance.
253   -//
254   -Balancer.prototype._remove = function (file) {
255   - var app = this._parseFilename(file), index;
256   -
257   - if (app && app.type) {
258   - switch (app.type) {
259   - case 'package':
260   - delete this.active[app.name];
261   - break;
262   - case 'pid':
263   - if (this.active[app.name] && this.active[app.name].drones) {
264   - index = this.active[app.name].drones.map(function (d) {
265   - return d.pid;
266   - }).indexOf(parseInt(app.drone, 10));
267   -
268   - this.active[app.name].drones.splice(index, 1);
269   - }
270   - break;
271   - }
272   - }
273   -};
274   -
275   -//
276   -// ### function _parseFilename (file)
277   -// #### @file {string} Filename to parse
278   -// Parses the data out of the specified `file` to be used
279   -// in the managed application state for this instance.
280   -//
281   -Balancer.prototype._parseFilename = function (file) {
282   - var base = path.basename(file),
283   - match, drone, app;
284   -
285   - if (matchers.app.test(base)) {
286   - return {
287   - type: 'package',
288   - name: base.match(matchers.app)[1]
289   - }
290   - }
291   - else if (matchers.pid.test(base)) {
292   - match = base.match(matchers.pid);
293   - return {
294   - type: 'pid',
295   - name: match[1],
296   - drone: match[2]
297   - };
298   - }
299   -
300   - return null;
301   -};
63 lib/haibu/balancer/index.js
... ... @@ -1,63 +0,0 @@
1   -/*
2   - * index.js: Responsible for balancing across all the instances available to haibu.
3   - *
4   - * (C) 2010, Nodejitsu Inc.
5   - *
6   - */
7   -
8   -var http = require('http'),
9   - haibu = require('haibu');
10   -
11   -//
12   -// ### Include Exports
13   -// Export other components in the module
14   -//
15   -exports.Balancer = require('./balancer').Balancer;
16   -
17   -//
18   -// ### function createServer (options)
19   -// #### @options {Object} Options to use when creating this server
20   -// Creates a server for the haibu `balancer` webservice.
21   -//
22   -exports.createServer = function (options, balancer) {
23   - balancer = balancer || new exports.Balancer(options);
24   -
25   - var server = http.createServer(function (request, response) {
26   - balancer.handle(request, response);
27   - });
28   -
29   - if (options.port) {
30   - server.listen(options.port);
31   - }
32   -
33   - balancer.once('ready', function (active) {
34   - server.emit('ready', active);
35   - });
36   -
37   - return server;
38   -};
39   -
40   -//
41   -// ### function start (options, callback)
42   -// #### @options {Object} Options to use when starting this module.
43   -// #### @callback {function} Continuation to respond to when complete.
44   -// Starts the haibu `balancer` webservice with the specified options.
45   -//
46   -exports.start = function (options, callback) {
47   - function startServer (err) {
48   - if (err) {
49   - return callback(err);
50   - }
51   -
52   - var balancer = new exports.Balancer(options),
53   - server = exports.createServer(options, balancer);
54   -
55   - balancer.once('ready', function (active) {
56   - callback(null, server, balancer, active);
57   - });
58   - }
59   -
60   - return options.init !== false
61   - ? haibu.init(options, startServer)
62   - : startServer();
63   -};
230 lib/haibu/core/config.js
... ... @@ -1,230 +0,0 @@
1   -/*
2   - * index.js: Top level module include for config module.
3   - *
4   - * (C) 2010, Nodejitsu Inc.
5   - *
6   - */
7   -
8   -var fs = require('fs'),
9   - eyes = require('eyes'),
10   - path = require('path'),
11   - exec = require('child_process').exec,
12   - async = require('async'),
13   - nconf = require('nconf'),
14   - haibu = require('haibu');
15   -
16   -var root = path.join(__dirname, '..', '..', '..'),
17   - config = module.exports = nconf;
18   -
19   -//
20   -// ### function (options)
21   -// #### @options {Object} Options to setup for flexibility
22   -// Sets up configuration options with default options for
23   -// maximum flexibility in usage.
24   -//
25   -function setupOptions (options) {
26   - options = options || {};
27   - options.env = options.env || 'development';
28   - options.data = options.data || {};
29   - options.config = options.config || {};
30   - options.config.files = options.config.files || [];
31   - options.config.namespace = options.config.namespace || options.env;
32   - return options;
33   -}
34   -
35   -//
36   -// ### Expose the simple routes for the haibu config API.
37   -//
38   -config.addRoutes = function () {
39   - return function (map) {
40   - map.post(/\/reload/).bind(function (response, data) {
41   - //
42   - // **TODO: _(indexzero)_**: Ensure this request is authenticated.
43   - //
44   -
45   - data = data.config ? data : haibu.config.get('config');
46   - haibu.config.load(data, function (err) {
47   - if (err) {
48   - return response.send(500, {}, { error: err.message });
49   - }
50   -
51   - response.send(200);
52   - });
53   - });
54   - };
55   -};
56   -
57   -//
58   -// ### function seed (options, callback)
59   -// #### @options {Object} Options for nconf store.
60   -// #### @callback {function} Continuation to respond to when complete.
61   -// Seeds any existing data from `options.files` into a Redis store
62   -// for distributed use.
63   -//
64   -config.seed = function (options, callback) {
65   - if (!callback) {
66   - callback = options;
67   - options = {};
68   - }
69   -
70   - //
71   - // Setup `options` to be _really_ flexible.
72   - //
73   - options = setupOptions(options);
74   - var store = new nconf.stores.Redis(options.config), keys;
75   -
76   - config.loadFiles(options.config.files, function (err, loaded) {
77   - if (err) {
78   - return callback(err);
79   - }
80   -
81   - function storeValue (key, next) {
82   - store.set(key, options.data[key], next);
83   - }
84   -
85   - Object.keys(loaded).forEach(function (key) {
86   - options.data[key] = loaded[key];
87   - });
88   -
89   - var keys = Object.keys(options.data);
90   - async.forEach(keys, storeValue, function (err) {
91   - if (err) {
92   - return callback(err);
93   - }
94   -
95   - store.set('loaded', true, function (err) {
96   - store.redis.quit();
97   - return err ? callback(err) : callback();
98   - });
99   - });
100   - });
101   -};
102   -
103   -//
104   -// ### function load (options, callback)
105   -// #### @options {Object} Options for nconf store.
106   -// #### @callback {function} Continuation to respond to when complete.
107   -// Basically a complex version of `nconf.load` which will skip the `load`
108   -// operation when using the memory engine instead of throwing an exception
109   -// and always be async. Also loads any existing configuration from a remote
110   -// Redis server into memory for usage with this process.
111   -//
112   -config.load = function (options, callback) {
113   - if (!callback) {
114   - callback = options;
115   - options = {};
116   - }
117   -
118   - //
119   - // Setup `options` to be _really_ flexible.
120   - //
121   - options = setupOptions(options);
122   -
123   - function setObject (obj) {
124   - if (!obj) {
125   - return;
126   - }
127   -
128   - Object.keys(obj).forEach(function (key) {
129   - config.set(key, obj[key]);
130   - });
131   - }
132   -