rightscale-nanite-dev 0.4.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/LICENSE +201 -0
  2. data/README.rdoc +430 -0
  3. data/Rakefile +78 -0
  4. data/TODO +24 -0
  5. data/bin/nanite-admin +65 -0
  6. data/bin/nanite-agent +79 -0
  7. data/bin/nanite-mapper +50 -0
  8. data/lib/nanite.rb +74 -0
  9. data/lib/nanite/actor.rb +71 -0
  10. data/lib/nanite/actor_registry.rb +26 -0
  11. data/lib/nanite/admin.rb +138 -0
  12. data/lib/nanite/agent.rb +274 -0
  13. data/lib/nanite/amqp.rb +58 -0
  14. data/lib/nanite/cluster.rb +256 -0
  15. data/lib/nanite/config.rb +111 -0
  16. data/lib/nanite/console.rb +39 -0
  17. data/lib/nanite/daemonize.rb +13 -0
  18. data/lib/nanite/identity.rb +16 -0
  19. data/lib/nanite/job.rb +104 -0
  20. data/lib/nanite/local_state.rb +38 -0
  21. data/lib/nanite/log.rb +66 -0
  22. data/lib/nanite/log/formatter.rb +39 -0
  23. data/lib/nanite/mapper.rb +315 -0
  24. data/lib/nanite/mapper_proxy.rb +75 -0
  25. data/lib/nanite/nanite_dispatcher.rb +92 -0
  26. data/lib/nanite/packets.rb +401 -0
  27. data/lib/nanite/pid_file.rb +52 -0
  28. data/lib/nanite/reaper.rb +39 -0
  29. data/lib/nanite/redis_tag_store.rb +141 -0
  30. data/lib/nanite/security/cached_certificate_store_proxy.rb +24 -0
  31. data/lib/nanite/security/certificate.rb +55 -0
  32. data/lib/nanite/security/certificate_cache.rb +66 -0
  33. data/lib/nanite/security/distinguished_name.rb +34 -0
  34. data/lib/nanite/security/encrypted_document.rb +46 -0
  35. data/lib/nanite/security/rsa_key_pair.rb +53 -0
  36. data/lib/nanite/security/secure_serializer.rb +68 -0
  37. data/lib/nanite/security/signature.rb +46 -0
  38. data/lib/nanite/security/static_certificate_store.rb +35 -0
  39. data/lib/nanite/security_provider.rb +47 -0
  40. data/lib/nanite/serializer.rb +52 -0
  41. data/lib/nanite/state.rb +135 -0
  42. data/lib/nanite/streaming.rb +125 -0
  43. data/lib/nanite/util.rb +78 -0
  44. metadata +111 -0
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.rdoc ADDED
@@ -0,0 +1,430 @@
1
+ = Nanite : A self-assembling fabric of Ruby daemons
2
+
3
+ Google Group: http://groups.google.com/group/nanite
4
+ irc.freenode.net: #nanite
5
+
6
+ == Intro
7
+
8
+ Nanite is a new way of thinking about building cloud ready web applications. Having
9
+ a scalable message queueing back-end with all the discovery and dynamic load based
10
+ dispatch that Nanite has is a very scalable way to construct web application back-ends.
11
+
12
+ A Nanite system has two types of components. There are nanite agents, these are the
13
+ daemons where you implement your code functionality as actors. And then there are
14
+ mappers.
15
+
16
+ Mappers are the control nodes of the system. There can be any number of mappers, these
17
+ typically run inside of your merb or rails app running on the thin webserver
18
+ (eventmachine is needed) but you can also run command line mappers from the shell.
19
+
20
+ Each Nanite agent sends a ping to the mapper exchange every @ping_time seconds. All of
21
+ the mappers are subscribed to this exchange and they all get a copy of the ping with your
22
+ status update. If the mappers do not get a ping from a certain agent within a timeout
23
+ @ping_time the mappers will remove that agent from any dispatch. When the agent comes
24
+ back online or gets less busy it will re-advertise itself to the mapper exchange therefore
25
+ adding itself back to the dispatch. This makes for a very nice self-healing cluster of
26
+ worker processes and a cluster of front-end mapper processes.
27
+
28
+ In your Nanites you can have any number of actor classes. These actors are like controllers
29
+ in Rails or Merb and this is where you implement your own custom functionality. An actor looks
30
+ like:
31
+
32
+ class Foo
33
+ include Nanite::Actor
34
+ expose :bar
35
+ def bar(payload)
36
+ "got payload: #{payload}"
37
+ end
38
+ end
39
+ Nanite::Dispatcher.register(Foo.new)
40
+
41
+ The methods that you 'expose' on the actors are advertised to the mappers like:
42
+
43
+ Foo#bar => /foo/bar
44
+ Mock#list => /mock/list
45
+
46
+ Every agent advertises its status every time it pings the mapper cluster.
47
+ The default status that is advertised is the load average as a float. This
48
+ is used for the default request dispatching based on least loaded server.
49
+
50
+ You can change what is advertised as status to anything you want that is
51
+ comparable(<=>) by doing something like this in your agent's init.rb file:
52
+
53
+ status_proc = lambda { MyApp.some_statistic_indicating_load }
54
+
55
+ This proc will be recalled every @ping_time and sent to the mappers.
56
+
57
+ This is the 'fitness function' for selecting the least loaded Nanite. You can
58
+ dream up any scheme of populating this function so the mappers can select the
59
+ right Nanites based on their status.
60
+
61
+
62
+ == A quick note about security:
63
+
64
+ Nanite security is based upon RabbitMQ vhosts so anything attached to one vhost
65
+ can talk to anything else on the same vhost. So you generally want one vhost
66
+ per app space.
67
+
68
+
69
+ == Installation
70
+
71
+ Nanite has a lot of moving parts. Follow the directions below and we'll get you up
72
+ and running in no-time.
73
+
74
+ === Install Erlang (OS X)
75
+
76
+ See the Erlang website for the latest info : http://www.erlang.org/download.html
77
+
78
+ In your chosen source dir which we'll refer to as <SRC>:
79
+
80
+ cd <SRC>
81
+ wget http://www.erlang.org/download/otp_src_R12B-5.tar.gz
82
+ tar -zxvf otp_src_R12B-5.tar.gz
83
+ cd otp_src_R12B-5
84
+ ./configure --enable-hipe --enable-darwin-universal
85
+ make
86
+ sudo make install
87
+
88
+ === Install Erlang (Linux .deb)
89
+
90
+ sudo apt-get install erlang-nox
91
+
92
+ === Install Nanite Ruby Gem
93
+
94
+ Installing the gem gives us access to the various Nanite commands in the default binary path.
95
+
96
+ cd <SRC>
97
+ git clone git://github.com/ezmobius/nanite.git
98
+ cd nanite
99
+ rake gem
100
+ sudo gem install pkg/nanite-<VERSION>.gem
101
+
102
+
103
+ === Install EventMachine Ruby Gem
104
+
105
+ sudo gem install eventmachine
106
+
107
+ === Install AMQP Ruby Gem
108
+
109
+ # install version >= 0.6.0 from RubyForge
110
+ sudo gem install amqp
111
+
112
+ # or install from source
113
+ cd <SRC>
114
+ git clone git://github.com/tmm1/amqp.git
115
+ cd amqp && rake gem && sudo gem install amqp-<VERSION>.gem
116
+
117
+ === Install RabbitMQ from source tarball (OS X and generic Linux)
118
+
119
+ In short, you'll need a working erlang installation, python, and simplejson. If your python ("python -V") is 2.6, simplejson is included
120
+ in your distribution. Otherwise, install simplejson:
121
+
122
+ easy_install simplejson
123
+
124
+ If you don't have easy_install, install setuptools from http://pypi.python.org/pypi/setuptools:
125
+
126
+ # Get the latest .egg file for your version of python ("python -V"), then run it:
127
+ wget http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py2.5.egg
128
+ sh setuptools-0.6c9-py2.5.egg
129
+
130
+ and install simplejson, which is needed for the "make" step below.
131
+
132
+ These instructions assume the latest RabbitMQ release 1.5.3:
133
+
134
+ # Download somewhere
135
+ cd /root
136
+ wget http://www.rabbitmq.com/releases/rabbitmq-server/v1.5.3/rabbitmq-server-1.5.3.tar.gz
137
+
138
+ # Go to your erlang lib directory, usually /usr/lib/erlang/lib or:
139
+ cd /usr/local/lib/erlang/lib
140
+
141
+ tar -zxf ~/rabbitmq-server-1.5.3.tar.gz
142
+ cd rabbitmq-server-1.5.3
143
+ make
144
+
145
+ # There is no "make install" phase.
146
+
147
+ Be sure to add the /usr/local/lib/erlang/lib/rabbitmq-server-1.5.3/scripts to your $PATH.
148
+
149
+ The following websites may also be useful:
150
+ * RabbitMQ website for the latest info : http://www.rabbitmq.com/download.html
151
+ * RabbitMQ server source download : http://www.rabbitmq.com/server.html
152
+ * RabbitMQ build instructions : http://www.rabbitmq.com/build-server.html
153
+ * RabbitMQ install instructions : http://www.rabbitmq.com/install.html
154
+
155
+
156
+ === Install RabbitMQ (Linux .deb)
157
+
158
+ wget http://www.rabbitmq.com/releases/rabbitmq-server/v1.5.3/rabbitmq-server_1.5.3-1_all.deb
159
+ sudo apt-get install logrotate
160
+ sudo dpkg -i rabbitmq-server_1.5.3-1_all.deb
161
+
162
+ == Test your installation
163
+
164
+ === Test your Erlang install
165
+
166
+ Start an Erlang shell
167
+
168
+ erl
169
+
170
+ Enter the following commands in the Erlang shell (When installed correctly each should print a great deal of erlang config info):
171
+
172
+ rabbit:module_info().
173
+
174
+ Exit the Erlang shell with the following command (or Ctrl-c):
175
+
176
+ q().
177
+
178
+
179
+ === Start RabbitMQ
180
+
181
+ All directories will be automatically set up on first run.
182
+
183
+ To run RabbitMQ in the foreground:
184
+ sudo rabbitmq-server
185
+
186
+ To run RabbitMQ in the background:
187
+ sudo rabbitmq-server -detached
188
+
189
+ To check status:
190
+ rabbitmqctl status
191
+
192
+ To stop server:
193
+ rabbitmqctl stop
194
+
195
+ You can learn more about RabbitMQ admin here: http://www.rabbitmq.com/admin-guide.html
196
+
197
+
198
+ === Test Ruby/EventMachine/AMQP end-to-end with a running RabbitMQ instance
199
+
200
+ You can test that all the moving parts are working end-to-end by running one of the AMQP example programs. There are a number of example tests provided with AMQP but the one we will try simply queues and prints two messages immediately, and another one five seconds later.
201
+
202
+ # this test can only be run if you made a local clone
203
+ # of the amqp repository during the amqp installation above
204
+ # (it does not matter however if the gem was installed from src or rubyforge)
205
+ cd <SRC>/amqp
206
+ ruby examples/mq/simple-get.rb
207
+
208
+
209
+ === Test Nanite (finally)
210
+
211
+ First we'll do a little setup and run a script which adds an agent account (nanite), a mapper account (mapper)
212
+ and a '/nanite' vhost to RabbitMQ. RabbitMQ broker must of course be running before you run this script.
213
+
214
+ cd nanite
215
+ ./examples/rabbitconf.rb
216
+
217
+ Now lets run a few agents. Each of these is a long running process and needs to run in its own shell. Each agent also needs its own unique identity, or token to differentiate it from the other running agents. You can try opening a couple of them now if you like. The agents will generally provide no output when running. You can always get more help about the nanite command with 'nanite --help'.
218
+
219
+ First shell
220
+
221
+ cd examples/simpleagent
222
+ nanite-agent --token fred
223
+
224
+ Second shell
225
+
226
+ cd examples/simpleagent
227
+ nanite-agent --token bob
228
+
229
+ Now run a mapper. Mappers can be run from within your Merb or Rails app, from an interactive irb shell, or from the command line. For this example we'll run it from the command line so open a third shell window and run the following:
230
+
231
+ cd examples/simpleagent
232
+ ./cli.rb
233
+
234
+ Which should soon return something like the following.
235
+
236
+ {"bob"=>"hello nanite"} # where the '--token bob' parameter was passed
237
+ {"55a7f300c454203eacc218b6fbd2edc6"=>"hello nanite"} # where no '--token' was passed. auto-generated identity.
238
+
239
+ Now if you want to make this interesting, you can issue a Ctrl-c in one of the agent's windows to kill it. And then run cli.rb again. You should see that you still get a result back since the mapper is finding the remaining agent to do its work.
240
+
241
+ If you want to try requesting work to be done by an agent from an interactive command shell try the following. This assumes that you have the agents running as indicated in the example above ('>>' is the nanite shell prompt).
242
+
243
+ cd nanite
244
+ ./bin/nanite-mapper -i -u mapper -p testing -v /nanite
245
+ Starting mapper console
246
+ >> request('/simple/echo') {|res| p res }
247
+
248
+ By default this will dispatch to the agent with the lowest reported load average.
249
+
250
+ There are a few other selectors as well:
251
+
252
+ # run this request on *all* agents that expose the /foo/bar Foo#bar actor
253
+ >> request('/foo/bar', 'hi', :selector => :all) {|res| p res }
254
+
255
+ # run this request on one random agent that expose the /whatever/hello Whatever#hello actor
256
+ >> request('/whatever/hello', 42, :selector => :random) {|res| p res }
257
+
258
+ You can create your own selectors based on arbitrary stuff you put in status from your agents see cluster.rb for examples of how least_loaded, all and random are implemented.
259
+
260
+ You can run as many mappers as you want, they will all be hot masters.
261
+
262
+ The calls are asynchronous. This means the block you pass to Nanite::Agent#request is not run until the response from the agent(s) have returned. So keep that in mind. Should you need to poll from an ajax web app for results you should have your block stuff the results in the database for any web front end to pick up with the next poll.
263
+
264
+ Another option to test your agents is to use nanite-admin
265
+
266
+ $ nanite-admin
267
+ starting nanite-admin
268
+ >> Thin web server (v1.0.1 codename ?)
269
+ >> Maximum connections set to 1024
270
+ >> Listening on 0.0.0.0:4000, CTRL+C to stop
271
+
272
+ and bring up the Nanite Control Tower at http://localhost:4000 .
273
+
274
+ Have fun!
275
+
276
+ == Web Framework Integration
277
+
278
+ The integration of Nanite into web frameworks depends on the web server running our application and, more importantly, if it uses EventMachine or not.
279
+
280
+ Thin is EventMachine-based, so we only need to make sure that the EventMachine reactor is already 'heated' up
281
+
282
+ Thread.new do
283
+ until EM.reactor_running?
284
+ sleep 1
285
+ end
286
+ Nanite.start_mapper(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite', :log_level => 'info')
287
+ end
288
+
289
+ Mongrel on the other hand does not use EventMachine and therefore requires to wrap the start of our mapper
290
+
291
+ Thread.new do
292
+ EM.run do
293
+ Nanite.start_mapper(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite', :log_level => 'info')
294
+ end
295
+ end
296
+
297
+ Using nanite with Passenger:
298
+
299
+ if defined?(PhusionPassenger)
300
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
301
+ if forked
302
+ if EM.reactor_running?
303
+ EM.stop_event_loop
304
+ EM.release_machine
305
+ EM.instance_variable_set( '@reactor_running', false )
306
+ end
307
+ Thread.current[:mq] = nil
308
+ AMQP.instance_variable_set('@conn', nil)
309
+ end
310
+
311
+ th = Thread.current
312
+ Thread.new{
313
+ Nanite.start_mapper(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite', :log_level => 'info')
314
+ }
315
+ Thread.stop
316
+ end
317
+ end
318
+
319
+ =======
320
+ Where to put the mapper initialization code depends on the framework and our preference.
321
+ For Rails the canonical place to start our mapper is within nanite.rb (or any other filename you prefer) in config/initalizers.
322
+ In Merb we can use init.rb in config.
323
+
324
+ == Security
325
+
326
+ Nanite implements a secure serializer which can be used in place of the other serializers to encrypt the
327
+ AMQP messages exchanged between the mappers and the agents.
328
+
329
+ The secure serializer uses X.509 certificates and cryptographic keys to sign and encrypt the messages.
330
+
331
+ It is important to understand that:
332
+ 1. A certificate only includes the public key component of a cryptographic key
333
+ pair.
334
+ 2. Signing requires the use of a certificate and its private key, checking the
335
+ signature then only requires the certificate (the idea is that only the
336
+ signer has the secret private key and thus can sign but anyone can check the
337
+ signature).
338
+ 3. Encrypting only requires the certificate but decrypting also requires the
339
+ private key (anyone can encrypt the data but only the intended recipient can
340
+ decrypt it).
341
+
342
+ A signing serializer thus needs access to the signer certificate and private
343
+ key. An encrypting serializer *also* needs access to the intended recipients
344
+ certificates. There needs to be a way to dynamically retrieve the corresponding
345
+ certificates. This is done using certificate stores.
346
+
347
+ Certificate stores associate identities with certificates. The identity is
348
+ associated when the data is serialized and can be keyed off to retrieve the
349
+ right certificate upon deserialization.
350
+
351
+ Nanite provides a static store implementation which can be used when the
352
+ certificates used for serialization are always the same and can be kept in
353
+ memory. Nanite also provides a certificate store proxy cache which can be
354
+ associated with any store implementation and will cache the most used
355
+ certificates.
356
+
357
+ The serializer should be initialized prior to being used by calling the 'init'
358
+ method:
359
+
360
+ # Initialize serializer, must be called prior to using it.
361
+ #
362
+ # - 'identity': Identity associated with serialized messages
363
+ # - 'cert': Certificate used to sign serialized messages and
364
+ # decrypt encrypted messages
365
+ # - 'key': Private key corresponding to 'cert'
366
+ # - 'store': Certificate store. Exposes certificates used for
367
+ # encryption and signature validation.
368
+ # - 'encrypt': Whether data should be signed and encrypted ('true')
369
+ # or just signed ('false'), 'true' by default.
370
+ #
371
+ def SecureSerializer.init(identity, cert, key, store, encrypt = true)
372
+
373
+ The 'secure' agent example (examples\secure) shows how the mappers and agents should be
374
+ configured to use the secure serializer.
375
+
376
+ == Troubleshooting
377
+
378
+ ** IMPORTANT **
379
+ If you are using Apple's built in Ruby that comes with Leopard (10.5.x) or Tiger (10.4.x) then your
380
+ READLINE lib is hosed and the interactive shell will not work. As soon as you drop into a shell
381
+ Apple's fakey READLINE will halt the event machine loop so you won't see any nanites
382
+ registering. I don't have a good workaround except to tell you not to use Apple's Ruby,
383
+ build your own or use ports.
384
+
385
+
386
+ ** What to do if rabbitconf.rb dies with: {badrpc,nodedown} and nothing you do seems to matter **
387
+
388
+ If rabbitconf.rb dies saying "{badrpc,nodedown}" it means that for some reason,
389
+ the rabbitmqctl program rabbitconf.rb is using to setup the agent accounts for nanite is
390
+ not able to connect to your RabbitMQ server. Assuming RabbitMQ is running and is
391
+ known to work (try the examples that come with the amqp library), then there's a chance
392
+ something is going wrong with using short node names in your Erlang install.
393
+
394
+ The easiest way to verify it is by starting two separate Erlang shells like this (note that "odin" is my hostname):
395
+
396
+ $ erl -sname fred
397
+ (fred@odin)1>
398
+
399
+ $ erl -sname bob
400
+ (bob@odin)1>
401
+
402
+ And then trying to 'ping' one from the other. In the 'fred' node you can do that like this:
403
+
404
+ (fred@odin)1> net_adm:ping(bob@odin).
405
+ pang
406
+
407
+ Note : If your hostname has a hyphen in it (e.g. macbook-pro) you should include the user@hostname in quotes like:
408
+
409
+ net_adm:ping('bob@macbook-pro').
410
+
411
+ If you see 'pang' (which is apparently Swedish for something like "crap! it's broken.") then short name distribution isn't working for you, and you need to fall back to using a full name. If you see 'pong', then that's not actually your problem.
412
+
413
+ First, verify that your system's hostname is set to a fully qualified domain name. On OS X it can end in '.local':
414
+
415
+ hostname
416
+ odin.local
417
+
418
+ Then test that *this* will work by starting a node with the full name of 'fred@<your hostname>':
419
+
420
+ erl -name fred@odin.local
421
+ (fred@odin.local)1>
422
+
423
+ then bob@<yourhostname>, and then finally try to ping fred:
424
+
425
+ erl -name bob@odin.local
426
+ (bob@odin.local)1> net_adm:ping(fred@odin.local).
427
+ pong
428
+
429
+ In my case, it looks like that worked. Now... on to getting rabbitconf.rb to run! To do that, you need to edit the 'rabbitmq-server' and 'rabbitmqctl' scripts in your RabbitMQ distribution and edit the -sname arguments to use -name and a full name.
430
+