amqp-daemon-kit 0.1.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/.gitignore +5 -0
  2. data/Configuration.txt +110 -0
  3. data/Deployment.txt +113 -0
  4. data/History.txt +131 -0
  5. data/Logging.txt +96 -0
  6. data/PostInstall.txt +6 -0
  7. data/README.rdoc +132 -0
  8. data/Rakefile +29 -0
  9. data/RuoteParticipants.txt +113 -0
  10. data/TODO.txt +27 -0
  11. data/bin/daemon-kit +18 -0
  12. data/config/website.yml +2 -0
  13. data/daemon-kit.gemspec +265 -0
  14. data/lib/daemon_kit.rb +60 -0
  15. data/lib/daemon_kit/abstract_logger.rb +249 -0
  16. data/lib/daemon_kit/application.rb +230 -0
  17. data/lib/daemon_kit/arguments.rb +165 -0
  18. data/lib/daemon_kit/commands/console.rb +38 -0
  19. data/lib/daemon_kit/commands/destroy.rb +10 -0
  20. data/lib/daemon_kit/commands/generate.rb +10 -0
  21. data/lib/daemon_kit/config.rb +113 -0
  22. data/lib/daemon_kit/console_daemon.rb +2 -0
  23. data/lib/daemon_kit/core_ext.rb +1 -0
  24. data/lib/daemon_kit/core_ext/configurable.rb +96 -0
  25. data/lib/daemon_kit/core_ext/string.rb +22 -0
  26. data/lib/daemon_kit/cron.rb +67 -0
  27. data/lib/daemon_kit/cucumber/world.rb +38 -0
  28. data/lib/daemon_kit/deployment/capistrano.rb +516 -0
  29. data/lib/daemon_kit/dk_amqp.rb +39 -0
  30. data/lib/daemon_kit/em.rb +43 -0
  31. data/lib/daemon_kit/error_handlers/base.rb +32 -0
  32. data/lib/daemon_kit/error_handlers/hoptoad.rb +180 -0
  33. data/lib/daemon_kit/exceptions.rb +15 -0
  34. data/lib/daemon_kit/generators.rb +67 -0
  35. data/lib/daemon_kit/generators/base.rb +60 -0
  36. data/lib/daemon_kit/initializer.rb +449 -0
  37. data/lib/daemon_kit/jabber.rb +171 -0
  38. data/lib/daemon_kit/nanite.rb +7 -0
  39. data/lib/daemon_kit/nanite/agent.rb +77 -0
  40. data/lib/daemon_kit/pid_file.rb +61 -0
  41. data/lib/daemon_kit/ruote_participants.rb +125 -0
  42. data/lib/daemon_kit/ruote_pseudo_participant.rb +68 -0
  43. data/lib/daemon_kit/ruote_workitem.rb +187 -0
  44. data/lib/daemon_kit/safety.rb +84 -0
  45. data/lib/daemon_kit/tasks.rb +2 -0
  46. data/lib/daemon_kit/tasks/environment.rake +11 -0
  47. data/lib/daemon_kit/tasks/framework.rake +123 -0
  48. data/lib/daemon_kit/tasks/god.rake +62 -0
  49. data/lib/daemon_kit/tasks/log.rake +8 -0
  50. data/lib/daemon_kit/tasks/monit.rake +29 -0
  51. data/lib/daemon_kit/vendor/thor-0.13.6/CHANGELOG.rdoc +89 -0
  52. data/lib/daemon_kit/vendor/thor-0.13.6/LICENSE +20 -0
  53. data/lib/daemon_kit/vendor/thor-0.13.6/README.rdoc +297 -0
  54. data/lib/daemon_kit/vendor/thor-0.13.6/Thorfile +69 -0
  55. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor.rb +244 -0
  56. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions.rb +296 -0
  57. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/create_file.rb +103 -0
  58. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/directory.rb +91 -0
  59. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/empty_directory.rb +134 -0
  60. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/file_manipulation.rb +223 -0
  61. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/inject_into_file.rb +104 -0
  62. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/base.rb +540 -0
  63. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/file_binary_read.rb +9 -0
  64. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  65. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/ordered_hash.rb +100 -0
  66. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/error.rb +30 -0
  67. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/group.rb +271 -0
  68. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/invocation.rb +180 -0
  69. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser.rb +4 -0
  70. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/argument.rb +67 -0
  71. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/arguments.rb +150 -0
  72. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/option.rb +128 -0
  73. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/options.rb +169 -0
  74. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/rake_compat.rb +66 -0
  75. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/runner.rb +314 -0
  76. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell.rb +83 -0
  77. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell/basic.rb +239 -0
  78. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell/color.rb +108 -0
  79. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/task.rb +102 -0
  80. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb +224 -0
  81. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/version.rb +3 -0
  82. data/lib/daemon_kit/xmpp.rb +100 -0
  83. data/lib/generators/daemon_kit/amqp/USAGE +5 -0
  84. data/lib/generators/daemon_kit/amqp/amqp_generator.rb +24 -0
  85. data/lib/generators/daemon_kit/amqp/templates/config/amqp.yml +28 -0
  86. data/lib/generators/daemon_kit/amqp/templates/config/pre-daemonize/amqp.rb +7 -0
  87. data/lib/generators/daemon_kit/amqp/templates/libexec/%app_name%-daemon.rb +37 -0
  88. data/lib/generators/daemon_kit/app/USAGE +7 -0
  89. data/lib/generators/daemon_kit/app/app_generator.rb +140 -0
  90. data/lib/generators/daemon_kit/app/templates/Gemfile +12 -0
  91. data/lib/generators/daemon_kit/app/templates/README.tt +58 -0
  92. data/lib/generators/daemon_kit/app/templates/Rakefile +6 -0
  93. data/lib/generators/daemon_kit/app/templates/bin/daemon.tt +7 -0
  94. data/lib/generators/daemon_kit/app/templates/config/arguments.rb +12 -0
  95. data/lib/generators/daemon_kit/app/templates/config/boot.rb +70 -0
  96. data/lib/generators/daemon_kit/app/templates/config/environment.rb.tt +26 -0
  97. data/lib/generators/daemon_kit/app/templates/config/environments/development.rb +2 -0
  98. data/lib/generators/daemon_kit/app/templates/config/environments/production.rb +5 -0
  99. data/lib/generators/daemon_kit/app/templates/config/environments/test.rb +2 -0
  100. data/lib/generators/daemon_kit/app/templates/config/post-daemonize/readme +5 -0
  101. data/lib/generators/daemon_kit/app/templates/config/pre-daemonize/readme +12 -0
  102. data/lib/generators/daemon_kit/app/templates/lib/%app_name%.rb +2 -0
  103. data/lib/generators/daemon_kit/app/templates/libexec/%app_name%-daemon.rb +18 -0
  104. data/lib/generators/daemon_kit/app/templates/script/console +3 -0
  105. data/lib/generators/daemon_kit/app/templates/script/destroy +3 -0
  106. data/lib/generators/daemon_kit/app/templates/script/generate +3 -0
  107. data/lib/generators/daemon_kit/capistrano/capistrano_generator.rb +26 -0
  108. data/lib/generators/daemon_kit/capistrano/templates/Capfile +10 -0
  109. data/lib/generators/daemon_kit/capistrano/templates/USAGE +10 -0
  110. data/lib/generators/daemon_kit/capistrano/templates/config/deploy.rb.tt +67 -0
  111. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/logrotate.erb +13 -0
  112. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/production.rb.tt +6 -0
  113. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/staging.rb.tt +6 -0
  114. data/lib/generators/daemon_kit/capistrano/templates/config/environments/staging.rb +0 -0
  115. data/lib/generators/daemon_kit/cron/USAGE +5 -0
  116. data/lib/generators/daemon_kit/cron/cron_generator.rb +24 -0
  117. data/lib/generators/daemon_kit/cron/templates/config/pre-daemonize/cron.rb +11 -0
  118. data/lib/generators/daemon_kit/cron/templates/libexec/%app_name%-daemon.rb +48 -0
  119. data/lib/generators/daemon_kit/cucumber/USAGE +11 -0
  120. data/lib/generators/daemon_kit/cucumber/cucumber_generator.rb +45 -0
  121. data/lib/generators/daemon_kit/cucumber/templates/config/environments/cucumber.rb +2 -0
  122. data/lib/generators/daemon_kit/cucumber/templates/features/step_definitions/.empty_directory +0 -0
  123. data/lib/generators/daemon_kit/cucumber/templates/features/support/env.rb +7 -0
  124. data/lib/generators/daemon_kit/cucumber/templates/script/cucumber +7 -0
  125. data/lib/generators/daemon_kit/cucumber/templates/tasks/cucumber.rake +13 -0
  126. data/lib/generators/daemon_kit/nanite_agent/USAGE +5 -0
  127. data/lib/generators/daemon_kit/nanite_agent/nanite_agent_generator.rb +29 -0
  128. data/lib/generators/daemon_kit/nanite_agent/templates/config/nanite.yml +35 -0
  129. data/lib/generators/daemon_kit/nanite_agent/templates/config/pre-daemonize/nanite_agent.rb +6 -0
  130. data/lib/generators/daemon_kit/nanite_agent/templates/lib/actors/sample.rb +11 -0
  131. data/lib/generators/daemon_kit/nanite_agent/templates/libexec/%app_name%-daemon.rb +31 -0
  132. data/lib/generators/daemon_kit/rspec/USAGE +5 -0
  133. data/lib/generators/daemon_kit/rspec/rspec_generator.rb +20 -0
  134. data/lib/generators/daemon_kit/rspec/templates/spec/%app_name%_spec.rb +11 -0
  135. data/lib/generators/daemon_kit/rspec/templates/spec/spec.opts +1 -0
  136. data/lib/generators/daemon_kit/rspec/templates/spec/spec_helper.rb +23 -0
  137. data/lib/generators/daemon_kit/rspec/templates/tasks/rspec.rake +21 -0
  138. data/lib/generators/daemon_kit/ruote/USAGE +5 -0
  139. data/lib/generators/daemon_kit/ruote/ruote_generator.rb +29 -0
  140. data/lib/generators/daemon_kit/ruote/templates/config/amqp.yml +30 -0
  141. data/lib/generators/daemon_kit/ruote/templates/config/pre-daemonize/ruote.rb +13 -0
  142. data/lib/generators/daemon_kit/ruote/templates/config/ruote.yml +23 -0
  143. data/lib/generators/daemon_kit/ruote/templates/lib/%app_name%.rb +4 -0
  144. data/lib/generators/daemon_kit/ruote/templates/lib/sample.rb +26 -0
  145. data/lib/generators/daemon_kit/ruote/templates/libexec/%app_name%-daemon.rb +33 -0
  146. data/lib/generators/daemon_kit/test_unit/USAGE +5 -0
  147. data/lib/generators/daemon_kit/test_unit/templates/tasks/test_unit.rake +7 -0
  148. data/lib/generators/daemon_kit/test_unit/templates/test/%app_name%_test.rb.tt +9 -0
  149. data/lib/generators/daemon_kit/test_unit/templates/test/test_helper.rb +6 -0
  150. data/lib/generators/daemon_kit/test_unit/test_unit_generator.rb +20 -0
  151. data/lib/generators/daemon_kit/xmpp/templates/config/pre-daemonize/xmpp.rb +6 -0
  152. data/lib/generators/daemon_kit/xmpp/templates/config/xmpp.yml +29 -0
  153. data/lib/generators/daemon_kit/xmpp/templates/libexec/%app_name%-daemon.rb +27 -0
  154. data/lib/generators/daemon_kit/xmpp/xmpp_generator.rb +24 -0
  155. data/script/console +10 -0
  156. data/script/destroy +14 -0
  157. data/script/generate +14 -0
  158. data/script/txt2html +71 -0
  159. data/spec/abstract_logger_spec.rb +126 -0
  160. data/spec/argument_spec.rb +70 -0
  161. data/spec/config_spec.rb +83 -0
  162. data/spec/configurable_spec.rb +56 -0
  163. data/spec/daemon_kit_spec.rb +7 -0
  164. data/spec/error_handlers_spec.rb +23 -0
  165. data/spec/fixtures/env.yml +15 -0
  166. data/spec/fixtures/noenv.yml +4 -0
  167. data/spec/initializer_spec.rb +26 -0
  168. data/spec/spec.opts +1 -0
  169. data/spec/spec_helper.rb +27 -0
  170. data/tasks/cucumber.rake +13 -0
  171. data/tasks/rspec.rake +20 -0
  172. data/tasks/tests.rake +6 -0
  173. data/templates/god/god.erb +69 -0
  174. data/templates/monit/monit.erb +14 -0
  175. data/test/test_amqp_generator.rb +48 -0
  176. data/test/test_cron_generator.rb +45 -0
  177. data/test/test_daemon-kit_generator.rb +84 -0
  178. data/test/test_daemon_kit_config.rb +28 -0
  179. data/test/test_deploy_capistrano_generator.rb +48 -0
  180. data/test/test_generator_helper.rb +29 -0
  181. data/test/test_helper.rb +7 -0
  182. data/test/test_nanite_agent_generator.rb +49 -0
  183. data/test/test_ruote_generator.rb +51 -0
  184. data/test/test_test_unit_generator.rb +46 -0
  185. metadata +302 -0
@@ -0,0 +1,224 @@
1
+ require 'rbconfig'
2
+
3
+ class Thor
4
+ module Sandbox #:nodoc:
5
+ end
6
+
7
+ # This module holds several utilities:
8
+ #
9
+ # 1) Methods to convert thor namespaces to constants and vice-versa.
10
+ #
11
+ # Thor::Utils.namespace_from_thor_class(Foo::Bar::Baz) #=> "foo:bar:baz"
12
+ #
13
+ # 2) Loading thor files and sandboxing:
14
+ #
15
+ # Thor::Utils.load_thorfile("~/.thor/foo")
16
+ #
17
+ module Util
18
+
19
+ # Receives a namespace and search for it in the Thor::Base subclasses.
20
+ #
21
+ # ==== Parameters
22
+ # namespace<String>:: The namespace to search for.
23
+ #
24
+ def self.find_by_namespace(namespace)
25
+ namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
26
+ Thor::Base.subclasses.find { |klass| klass.namespace == namespace }
27
+ end
28
+
29
+ # Receives a constant and converts it to a Thor namespace. Since Thor tasks
30
+ # can be added to a sandbox, this method is also responsable for removing
31
+ # the sandbox namespace.
32
+ #
33
+ # This method should not be used in general because it's used to deal with
34
+ # older versions of Thor. On current versions, if you need to get the
35
+ # namespace from a class, just call namespace on it.
36
+ #
37
+ # ==== Parameters
38
+ # constant<Object>:: The constant to be converted to the thor path.
39
+ #
40
+ # ==== Returns
41
+ # String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
42
+ #
43
+ def self.namespace_from_thor_class(constant)
44
+ constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
45
+ constant = snake_case(constant).squeeze(":")
46
+ constant
47
+ end
48
+
49
+ # Given the contents, evaluate it inside the sandbox and returns the
50
+ # namespaces defined in the sandbox.
51
+ #
52
+ # ==== Parameters
53
+ # contents<String>
54
+ #
55
+ # ==== Returns
56
+ # Array[Object]
57
+ #
58
+ def self.namespaces_in_content(contents, file=__FILE__)
59
+ old_constants = Thor::Base.subclasses.dup
60
+ Thor::Base.subclasses.clear
61
+
62
+ load_thorfile(file, contents)
63
+
64
+ new_constants = Thor::Base.subclasses.dup
65
+ Thor::Base.subclasses.replace(old_constants)
66
+
67
+ new_constants.map!{ |c| c.namespace }
68
+ new_constants.compact!
69
+ new_constants
70
+ end
71
+
72
+ # Returns the thor classes declared inside the given class.
73
+ #
74
+ def self.thor_classes_in(klass)
75
+ stringfied_constants = klass.constants.map { |c| c.to_s }
76
+ Thor::Base.subclasses.select do |subclass|
77
+ next unless subclass.name
78
+ stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
79
+ end
80
+ end
81
+
82
+ # Receives a string and convert it to snake case. SnakeCase returns snake_case.
83
+ #
84
+ # ==== Parameters
85
+ # String
86
+ #
87
+ # ==== Returns
88
+ # String
89
+ #
90
+ def self.snake_case(str)
91
+ return str.downcase if str =~ /^[A-Z_]+$/
92
+ str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
93
+ return $+.downcase
94
+ end
95
+
96
+ # Receives a string and convert it to camel case. camel_case returns CamelCase.
97
+ #
98
+ # ==== Parameters
99
+ # String
100
+ #
101
+ # ==== Returns
102
+ # String
103
+ #
104
+ def self.camel_case(str)
105
+ return str if str !~ /_/ && str =~ /[A-Z]+.*/
106
+ str.split('_').map { |i| i.capitalize }.join
107
+ end
108
+
109
+ # Receives a namespace and tries to retrieve a Thor or Thor::Group class
110
+ # from it. It first searches for a class using the all the given namespace,
111
+ # if it's not found, removes the highest entry and searches for the class
112
+ # again. If found, returns the highest entry as the class name.
113
+ #
114
+ # ==== Examples
115
+ #
116
+ # class Foo::Bar < Thor
117
+ # def baz
118
+ # end
119
+ # end
120
+ #
121
+ # class Baz::Foo < Thor::Group
122
+ # end
123
+ #
124
+ # Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default task
125
+ # Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
126
+ # Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
127
+ #
128
+ # ==== Parameters
129
+ # namespace<String>
130
+ #
131
+ def self.find_class_and_task_by_namespace(namespace, fallback = true)
132
+ if namespace.include?(?:) # look for a namespaced task
133
+ pieces = namespace.split(":")
134
+ task = pieces.pop
135
+ klass = Thor::Util.find_by_namespace(pieces.join(":"))
136
+ end
137
+ unless klass # look for a Thor::Group with the right name
138
+ klass, task = Thor::Util.find_by_namespace(namespace), nil
139
+ end
140
+ if !klass && fallback # try a task in the default namespace
141
+ task = namespace
142
+ klass = Thor::Util.find_by_namespace('')
143
+ end
144
+ return klass, task
145
+ end
146
+
147
+ # Receives a path and load the thor file in the path. The file is evaluated
148
+ # inside the sandbox to avoid namespacing conflicts.
149
+ #
150
+ def self.load_thorfile(path, content=nil)
151
+ content ||= File.binread(path)
152
+
153
+ begin
154
+ Thor::Sandbox.class_eval(content, path)
155
+ rescue Exception => e
156
+ $stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.message}"
157
+ end
158
+ end
159
+
160
+ def self.user_home
161
+ @@user_home ||= if ENV["HOME"]
162
+ ENV["HOME"]
163
+ elsif ENV["USERPROFILE"]
164
+ ENV["USERPROFILE"]
165
+ elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
166
+ File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
167
+ elsif ENV["APPDATA"]
168
+ ENV["APPDATA"]
169
+ else
170
+ begin
171
+ File.expand_path("~")
172
+ rescue
173
+ if File::ALT_SEPARATOR
174
+ "C:/"
175
+ else
176
+ "/"
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ # Returns the root where thor files are located, dependending on the OS.
183
+ #
184
+ def self.thor_root
185
+ File.join(user_home, ".thor").gsub(/\\/, '/')
186
+ end
187
+
188
+ # Returns the files in the thor root. On Windows thor_root will be something
189
+ # like this:
190
+ #
191
+ # C:\Documents and Settings\james\.thor
192
+ #
193
+ # If we don't #gsub the \ character, Dir.glob will fail.
194
+ #
195
+ def self.thor_root_glob
196
+ files = Dir["#{thor_root}/*"]
197
+
198
+ files.map! do |file|
199
+ File.directory?(file) ? File.join(file, "main.thor") : file
200
+ end
201
+ end
202
+
203
+ # Where to look for Thor files.
204
+ #
205
+ def self.globs_for(path)
206
+ ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
207
+ end
208
+
209
+ # Return the path to the ruby interpreter taking into account multiple
210
+ # installations and windows extensions.
211
+ #
212
+ def self.ruby_command
213
+ @ruby_command ||= begin
214
+ ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
215
+ ruby << Config::CONFIG['EXEEXT']
216
+
217
+ # escape string in case path to ruby executable contain spaces.
218
+ ruby.sub!(/.*\s.*/m, '"\&"')
219
+ ruby
220
+ end
221
+ end
222
+
223
+ end
224
+ end
@@ -0,0 +1,3 @@
1
+ class Thor
2
+ VERSION = "0.13.6".freeze
3
+ end
@@ -0,0 +1,100 @@
1
+ require 'blather/client/client'
2
+ require 'blather/client/dsl'
3
+
4
+ module DaemonKit
5
+ # Thin wrapper around the blather DSL
6
+ class XMPP
7
+ include ::Blather::DSL
8
+
9
+ class << self
10
+
11
+ def run( &block )
12
+ DaemonKit.trap('INT') { ::EM.stop }
13
+ DaemonKit.trap('TERM') { ::EM.stop }
14
+
15
+ DaemonKit::EM.run {
16
+
17
+ xmpp = new
18
+ xmpp.instance_eval( &block )
19
+ xmpp.run
20
+ }
21
+ end
22
+ end
23
+
24
+ def initialize
25
+ @config = DaemonKit::Config.load('xmpp')
26
+
27
+ if @config.enable_logging
28
+ Blather.logger = DaemonKit.logger
29
+ end
30
+
31
+ jid = if @config.resource
32
+ "#{@config.jabber_id}/#{@config.resource}"
33
+ else
34
+ @config.jabber_id
35
+ end
36
+
37
+ setup jid, @config.password
38
+
39
+ when_ready do
40
+ configure_roster!
41
+ become_available
42
+ end
43
+
44
+ return if @config['require_master'] == false
45
+
46
+ message do |m|
47
+ trusted?( m ) ? pass : halt
48
+ end
49
+ end
50
+
51
+ def configure_roster!
52
+ DaemonKit.logger.debug 'Configuring roster'
53
+
54
+ my_roster.each do |(jid, item)|
55
+ unless contacts.include?( jid )
56
+ DaemonKit.logger.debug "Removing #{jid} from roster"
57
+
58
+ my_roster.delete( item.jid )
59
+ next
60
+ end
61
+ end
62
+
63
+ contacts.each do |jid|
64
+ DaemonKit.logger.debug "Adding #{jid} to roster"
65
+
66
+ my_roster.add( Blather::JID.new( jid ) )
67
+ end
68
+
69
+ my_roster.each do |(jid,item)|
70
+ item.subscription = :both
71
+ item.ask = :subscribe
72
+ end
73
+ end
74
+
75
+ def become_available
76
+ set_status( :chat, "#{DaemonKit.configuration.daemon_name} is available" )
77
+ end
78
+
79
+ def trusted?( message )
80
+ @config.masters.include?( message.from.stripped.to_s )
81
+ end
82
+
83
+ def contacts
84
+ @config.masters + @config.supporters
85
+ end
86
+
87
+ def run
88
+ client.run
89
+ end
90
+
91
+ def busy( message = nil, &block )
92
+ set_status( :dnd, message )
93
+
94
+ block.call
95
+
96
+ become_available
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,5 @@
1
+ Description:
2
+
3
+
4
+ Usage:
5
+
@@ -0,0 +1,24 @@
1
+ module DaemonKit
2
+ module Generators
3
+ class AmqpGenerator < Base
4
+
5
+ def update_gemfile
6
+ append_file 'Gemfile', "gem 'amqp'\n"
7
+ end
8
+
9
+ def create_config_files
10
+ directory 'config'
11
+ end
12
+
13
+ def create_daemon
14
+ directory 'libexec'
15
+ end
16
+
17
+ protected
18
+
19
+ def self.source_root
20
+ File.expand_path( File.join( File.dirname(__FILE__), 'templates') )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ # AMQP client configuration file
2
+
3
+ # These values will be used to configure the ampq gem, any values
4
+ # omitted will let the gem use it's own defaults.
5
+
6
+ # The configuration specifies the following keys:
7
+ # * user - Username for the broker
8
+ # * pass - Password for the broker
9
+ # * host - Hostname where the broker is running
10
+ # * vhost - Vhost to connect to
11
+ # * port - Port where the broker is running
12
+ # * ssl - Use ssl or not
13
+ # * timeout - Timeout
14
+
15
+ defaults: &defaults
16
+ user: guest
17
+ pass: guest
18
+ host: localhost
19
+ vhost: /
20
+
21
+ development:
22
+ <<: *defaults
23
+
24
+ test:
25
+ <<: *defaults
26
+
27
+ production:
28
+ <<: *defaults
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'amqp'
3
+ require 'mq'
4
+ rescue LoadError
5
+ $stderr.puts "Missing amqp gem. Please run 'bundle install'."
6
+ exit 1
7
+ end
@@ -0,0 +1,37 @@
1
+ # Generated amqp daemon
2
+
3
+ # Do your post daemonization configuration here
4
+ # At minimum you need just the first line (without the block), or a lot
5
+ # of strange things might start happening...
6
+ DaemonKit::Application.running! do |config|
7
+ # Trap signals with blocks or procs
8
+ # config.trap( 'INT' ) do
9
+ # # do something clever
10
+ # end
11
+ # config.trap( 'TERM', Proc.new { puts 'Going down' } )
12
+ end
13
+
14
+ # IMPORTANT CONFIGURATION NOTE
15
+ #
16
+ # Please review and update 'config/amqp.yml' accordingly or this
17
+ # daemon won't work as advertised.
18
+
19
+ # Run an event-loop for processing
20
+ DaemonKit::AMQP.run do
21
+ # Inside this block we're running inside the reactor setup by the
22
+ # amqp gem. Any code in the examples (from the gem) would work just
23
+ # fine here.
24
+
25
+ # Uncomment this for connection keep-alive
26
+ # AMQP.conn.connection_status do |status|
27
+ # DaemonKit.logger.debug("AMQP connection status changed: #{status}")
28
+ # if status == :disconnected
29
+ # AMQP.conn.reconnect(true)
30
+ # end
31
+ # end
32
+
33
+ amq = ::MQ.new
34
+ amq.queue('test').subscribe do |msg|
35
+ DaemonKit.logger.debug "Received message: #{msg.inspect}"
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ Description:
2
+ This generator creates a new skeleton project suitable for easy
3
+ Ruby daemon development. The generated project is preconfigured
4
+ and just requires your task specific code.
5
+
6
+ Use one of the provided generators to stub out even more
7
+ functionality for common daemon patterns.
@@ -0,0 +1,140 @@
1
+ module DaemonKit
2
+ module Generators
3
+ class AppGenerator < Base
4
+ INSTALLERS = %w( default amqp cron nanite_agent ruote xmpp )
5
+ DEPLOYERS = %w( capistrano )
6
+ TEST_FRAMEWORKS = %w( none rspec test_unit )
7
+ add_shebang_option!
8
+
9
+ argument :app_path, :type => :string
10
+
11
+ class_option :installer, :type => :string, :aliases => '-i', :default => 'default',
12
+ :desc => "Pre-configured daemon to generate (options: #{INSTALLERS.join(', ')})"
13
+
14
+ class_option :deployer, :type => :string, :aliases => '-d',
15
+ :desc => "Select an optional deployment mechanism (options: #{DEPLOYERS.join(', ')})"
16
+
17
+ class_option :test_framework, :type => :string, :aliases => '-t', :default => 'rspec',
18
+ :desc => "Select your prefered test framework (options: #{TEST_FRAMEWORKS.join(', ')})"
19
+
20
+ class_option :cucumber, :type => :boolean, :aliases => '-c', :default => false,
21
+ :desc => "Install cucumber support"
22
+
23
+ def initialize( *args )
24
+ super
25
+
26
+ unless INSTALLERS.include?( options[:installer] )
27
+ raise Error, "Invalid value for --installer option. Supported installers are: #{INSTALLERS.join(', ')}"
28
+ end
29
+
30
+ if options[:deployer] && !DEPLOYERS.include?( options[:deployer] )
31
+ raise Error, "Invalid value for --deployer option. Supported deployers are: #{DEPLOYERS.join(', ')}"
32
+ end
33
+
34
+ unless TEST_FRAMEWORKS.include?( options[:test_framework] )
35
+ raise Error, "Invalid value for --test_framework option. Supported frameworks are: #{TEST_FRAMEWORKS.join(', ')}"
36
+ end
37
+ end
38
+
39
+ def create_root
40
+ self.destination_root = File.expand_path( app_path, destination_root )
41
+ empty_directory('.')
42
+ FileUtils.cd( destination_root )
43
+ end
44
+
45
+ def create_root_files
46
+ template 'README.tt', 'README'
47
+ copy_file 'Rakefile'
48
+ copy_file 'Gemfile'
49
+ end
50
+
51
+ def create_bin_files
52
+ empty_directory 'bin'
53
+ template 'bin/daemon.tt', "bin/#{app_name}" do |content|
54
+ "#{shebang}\n" + content
55
+ end
56
+ chmod "bin", 0755, :verbose => false
57
+ end
58
+
59
+ def create_config_files
60
+ directory 'config'
61
+ end
62
+
63
+ def create_script_files
64
+ directory 'script' do |content|
65
+ "#{shebang}\n" + content
66
+ end
67
+ chmod 'script', 0755, :verbose => false
68
+ end
69
+
70
+ def create_task_directory
71
+ empty_directory 'tasks'
72
+ end
73
+
74
+ def create_log_directory
75
+ empty_directory 'log'
76
+ end
77
+
78
+ def create_tmp_directory
79
+ empty_directory 'tmp'
80
+ end
81
+
82
+ def create_vendor_directory
83
+ empty_directory 'vendor'
84
+ end
85
+
86
+ def create_lib_files
87
+ directory 'lib'
88
+ end
89
+
90
+ def create_lib_exec
91
+ case options[:installer]
92
+ when 'default'
93
+ directory 'libexec'
94
+ when 'amqp'
95
+ invoke DaemonKit::Generators::AmqpGenerator
96
+ when 'cron'
97
+ invoke DaemonKit::Generators::CronGenerator
98
+ when 'nanite_agent'
99
+ invoke DaemonKit::Generators::NaniteAgentGenerator
100
+ when 'ruote'
101
+ invoke DaemonKit::Generators::RuoteGenerator
102
+ when 'xmpp'
103
+ invoke DaemonKit::Generators::XmppGenerator
104
+ end
105
+ end
106
+
107
+ def create_deployment_config
108
+ return unless options[:deployer]
109
+
110
+ case options[:deployer]
111
+ when 'capistrano'
112
+ invoke DaemonKit::Generators::CapistranoGenerator
113
+ end
114
+ end
115
+
116
+ def create_test_environment
117
+ return if options[:test_framework] == 'none'
118
+
119
+ case options[:test_framework]
120
+ when 'rspec'
121
+ invoke DaemonKit::Generators::SpecGenerator
122
+ when 'test_unit'
123
+ invoke DaemonKit::Generators::TestUnitGenerator
124
+ end
125
+ end
126
+
127
+ def create_cucumber
128
+ return unless options.cucumber?
129
+ invoke DaemonKit::Generators::CucumberGenerator
130
+ end
131
+
132
+ protected
133
+
134
+ def self.source_root
135
+ File.expand_path( File.join( File.dirname(__FILE__), 'templates') )
136
+ end
137
+
138
+ end
139
+ end
140
+ end