motherbrain 0.0.0.placeholder → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.md +196 -0
  6. data/COMMANDS.md +9 -0
  7. data/CONTRIBUTING.md +24 -0
  8. data/Dockerfile +26 -0
  9. data/Gemfile +60 -2
  10. data/Guardfile +30 -0
  11. data/LICENSE +10 -0
  12. data/MANIFESTS.md +90 -0
  13. data/OPERATORS_GUIDE.md +195 -0
  14. data/PLUGINS.md +268 -0
  15. data/README.md +304 -16
  16. data/Thorfile +123 -0
  17. data/VAGRANT.md +116 -0
  18. data/bin/boot +9 -0
  19. data/bin/mb +5 -0
  20. data/bin/mbsrv +5 -0
  21. data/config.json +32 -0
  22. data/features/cli/bootstrap_command/configurable_scripts.feature +32 -0
  23. data/features/cli/configure_command.feature +57 -0
  24. data/features/cli/environment_command/create_command.feature +22 -0
  25. data/features/cli/environment_command/destroy_command.feature +33 -0
  26. data/features/cli/environment_command/from_command.feature +29 -0
  27. data/features/cli/environment_command/list_command.feature +0 -0
  28. data/features/cli/node_limiting.feature +47 -0
  29. data/features/cli/plugin_command/list_command.feature +46 -0
  30. data/features/cli/service_command/service_command.feature +21 -0
  31. data/features/cli/template_command.feature +10 -0
  32. data/features/cli/validate_config.feature +46 -0
  33. data/features/step_definitions/bootstrap_steps.rb +57 -0
  34. data/features/step_definitions/chef_server_steps.rb +3 -0
  35. data/features/step_definitions/configuration_steps.rb +18 -0
  36. data/features/step_definitions/core_cli_steps.rb +33 -0
  37. data/features/step_definitions/environment_steps.rb +43 -0
  38. data/features/step_definitions/node_steps.rb +3 -0
  39. data/features/step_definitions/plugin_steps.rb +15 -0
  40. data/features/step_definitions/template_steps.rb +7 -0
  41. data/features/support/env.rb +68 -0
  42. data/lib/mb/api.rb +8 -0
  43. data/lib/mb/api/application.rb +7 -0
  44. data/lib/mb/api/endpoint.rb +5 -0
  45. data/lib/mb/api/helpers.rb +38 -0
  46. data/lib/mb/api/v1.rb +56 -0
  47. data/lib/mb/api/v1/config_endpoint.rb +12 -0
  48. data/lib/mb/api/v1/environments_endpoint.rb +174 -0
  49. data/lib/mb/api/v1/jobs_endpoint.rb +31 -0
  50. data/lib/mb/api/v1/plugins_endpoint.rb +90 -0
  51. data/lib/mb/api/validators.rb +5 -0
  52. data/lib/mb/api/validators/sem_ver.rb +18 -0
  53. data/lib/mb/application.rb +148 -0
  54. data/lib/mb/berkshelf.rb +50 -0
  55. data/lib/mb/bootstrap.rb +9 -0
  56. data/lib/mb/bootstrap/manager.rb +250 -0
  57. data/lib/mb/bootstrap/manifest.rb +131 -0
  58. data/lib/mb/bootstrap/routine.rb +199 -0
  59. data/lib/mb/bootstrap/template.rb +73 -0
  60. data/lib/mb/bootstrap/worker.rb +227 -0
  61. data/lib/mb/chef.rb +6 -0
  62. data/lib/mb/chef/config.rb +69 -0
  63. data/lib/mb/chef/run_list_item.rb +115 -0
  64. data/lib/mb/chef_mutex.rb +304 -0
  65. data/lib/mb/clean_room_base.rb +39 -0
  66. data/lib/mb/cli.rb +50 -0
  67. data/lib/mb/cli/base.rb +51 -0
  68. data/lib/mb/cli/shell.rb +29 -0
  69. data/lib/mb/cli/shell/basic.rb +11 -0
  70. data/lib/mb/cli/shell/color.rb +11 -0
  71. data/lib/mb/cli/shell/ext.rb +41 -0
  72. data/lib/mb/cli/sub_command.rb +95 -0
  73. data/lib/mb/cli/sub_command/component.rb +56 -0
  74. data/lib/mb/cli/sub_command/plugin.rb +232 -0
  75. data/lib/mb/cli_client.rb +178 -0
  76. data/lib/mb/cli_gateway.rb +426 -0
  77. data/lib/mb/cli_gateway/sub_commands.rb +3 -0
  78. data/lib/mb/cli_gateway/sub_commands/environment.rb +124 -0
  79. data/lib/mb/cli_gateway/sub_commands/plugin.rb +148 -0
  80. data/lib/mb/command.rb +88 -0
  81. data/lib/mb/command_invoker.rb +235 -0
  82. data/lib/mb/command_invoker/worker.rb +40 -0
  83. data/lib/mb/command_runner.rb +233 -0
  84. data/lib/mb/component.rb +245 -0
  85. data/lib/mb/config.rb +275 -0
  86. data/lib/mb/config_manager.rb +75 -0
  87. data/lib/mb/console.rb +35 -0
  88. data/lib/mb/cookbook_metadata.rb +73 -0
  89. data/lib/mb/core_ext.rb +3 -0
  90. data/lib/mb/core_ext/dir.rb +37 -0
  91. data/lib/mb/core_ext/enumerable.rb +48 -0
  92. data/lib/mb/core_ext/file.rb +24 -0
  93. data/lib/mb/core_ext/signal.rb +11 -0
  94. data/lib/mb/environment_manager.rb +195 -0
  95. data/lib/mb/error_handler.rb +212 -0
  96. data/lib/mb/errors.rb +693 -0
  97. data/lib/mb/file_system.rb +60 -0
  98. data/lib/mb/file_system/tempfile.rb +25 -0
  99. data/lib/mb/gear.rb +154 -0
  100. data/lib/mb/gears.rb +8 -0
  101. data/lib/mb/gears/dynamic_service.rb +218 -0
  102. data/lib/mb/gears/jmx.rb +24 -0
  103. data/lib/mb/gears/jmx/action.rb +46 -0
  104. data/lib/mb/gears/mysql.rb +20 -0
  105. data/lib/mb/gears/mysql/action.rb +190 -0
  106. data/lib/mb/gears/service.rb +163 -0
  107. data/lib/mb/gears/service/action.rb +58 -0
  108. data/lib/mb/gears/service/action_runner.rb +161 -0
  109. data/lib/mb/grape_ext.rb +3 -0
  110. data/lib/mb/grape_ext/endpoint.rb +13 -0
  111. data/lib/mb/group.rb +143 -0
  112. data/lib/mb/job.rb +183 -0
  113. data/lib/mb/job/state_machine.rb +34 -0
  114. data/lib/mb/job/states.rb +46 -0
  115. data/lib/mb/job_manager.rb +96 -0
  116. data/lib/mb/job_record.rb +67 -0
  117. data/lib/mb/job_ticket.rb +25 -0
  118. data/lib/mb/lock_manager.rb +116 -0
  119. data/lib/mb/logging.rb +134 -0
  120. data/lib/mb/logging/basic_format.rb +31 -0
  121. data/lib/mb/manifest.rb +128 -0
  122. data/lib/mb/mixin.rb +3 -0
  123. data/lib/mb/mixin/attribute_setting.rb +265 -0
  124. data/lib/mb/mixin/coded_exit.rb +49 -0
  125. data/lib/mb/mixin/locks.rb +54 -0
  126. data/lib/mb/mixin/services.rb +100 -0
  127. data/lib/mb/node_filter.rb +97 -0
  128. data/lib/mb/node_querier.rb +527 -0
  129. data/lib/mb/plugin.rb +300 -0
  130. data/lib/mb/plugin_manager.rb +589 -0
  131. data/lib/mb/provisioner.rb +186 -0
  132. data/lib/mb/provisioner/manager.rb +213 -0
  133. data/lib/mb/provisioner/manifest.rb +125 -0
  134. data/lib/mb/provisioner/provision_data.rb +96 -0
  135. data/lib/mb/provisioners.rb +5 -0
  136. data/lib/mb/provisioners/aws.rb +395 -0
  137. data/lib/mb/rest_gateway.rb +72 -0
  138. data/lib/mb/ridley_ext.rb +5 -0
  139. data/lib/mb/ridley_ext/cookbook_object.rb +15 -0
  140. data/lib/mb/srv_ctl.rb +183 -0
  141. data/lib/mb/test.rb +104 -0
  142. data/lib/mb/thor_ext.rb +49 -0
  143. data/lib/mb/upgrade.rb +6 -0
  144. data/lib/mb/upgrade/manager.rb +85 -0
  145. data/lib/mb/upgrade/worker.rb +149 -0
  146. data/lib/mb/version.rb +1 -1
  147. data/lib/motherbrain.rb +166 -2
  148. data/man/man_helper.rb +81 -0
  149. data/man/mb.1 +494 -0
  150. data/man/mb.1.html +300 -0
  151. data/man/mb.1.ronn.erb +62 -0
  152. data/motherbrain.gemspec +56 -20
  153. data/scripts/node_name.rb +14 -0
  154. data/spec/fixtures/cb_metadata.json +7 -0
  155. data/spec/fixtures/cb_metadata.rb +14 -0
  156. data/spec/fixtures/fake_id_rsa +27 -0
  157. data/spec/fixtures/fake_key.pem +27 -0
  158. data/spec/fixtures/myface-0.1.0/metadata.rb +14 -0
  159. data/spec/fixtures/myface-0.1.0/motherbrain.rb +0 -0
  160. data/spec/fixtures/test_env.json +15 -0
  161. data/spec/spec_helper.rb +67 -0
  162. data/spec/support/actor_mocking.rb +7 -0
  163. data/spec/support/berkshelf.rb +24 -0
  164. data/spec/support/chef_server.rb +102 -0
  165. data/spec/support/doubles.rb +11 -0
  166. data/spec/support/klass.rb +10 -0
  167. data/spec/support/matchers/each.rb +12 -0
  168. data/spec/support/matchers/error_codes.rb +5 -0
  169. data/spec/support/matchers/exit_codes.rb +57 -0
  170. data/spec/support/matchers/jobs.rb +11 -0
  171. data/spec/support/spec_helpers.rb +145 -0
  172. data/spec/unit/mb/api/application_spec.rb +11 -0
  173. data/spec/unit/mb/api/helpers_spec.rb +5 -0
  174. data/spec/unit/mb/api/v1/config_endpoint_spec.rb +19 -0
  175. data/spec/unit/mb/api/v1/environments_endpoint_spec.rb +71 -0
  176. data/spec/unit/mb/api/v1/jobs_endpoint_spec.rb +24 -0
  177. data/spec/unit/mb/api/v1/plugins_endpoint_spec.rb +298 -0
  178. data/spec/unit/mb/api/v1_spec.rb +37 -0
  179. data/spec/unit/mb/api/validators/sem_ver_spec.rb +5 -0
  180. data/spec/unit/mb/application_spec.rb +19 -0
  181. data/spec/unit/mb/berkshelf_spec.rb +38 -0
  182. data/spec/unit/mb/bootstrap/manager_spec.rb +347 -0
  183. data/spec/unit/mb/bootstrap/manifest_spec.rb +333 -0
  184. data/spec/unit/mb/bootstrap/routine_spec.rb +393 -0
  185. data/spec/unit/mb/bootstrap/template_spec.rb +100 -0
  186. data/spec/unit/mb/bootstrap/worker_spec.rb +194 -0
  187. data/spec/unit/mb/chef/config_spec.rb +33 -0
  188. data/spec/unit/mb/chef/run_list_item_spec.rb +34 -0
  189. data/spec/unit/mb/chef_mutex_spec.rb +314 -0
  190. data/spec/unit/mb/clean_room_base_spec.rb +31 -0
  191. data/spec/unit/mb/cli/base_spec.rb +43 -0
  192. data/spec/unit/mb/cli/shell/basic_spec.rb +5 -0
  193. data/spec/unit/mb/cli/shell/color_spec.rb +5 -0
  194. data/spec/unit/mb/cli/shell/ext_spec.rb +11 -0
  195. data/spec/unit/mb/cli/shell_spec.rb +38 -0
  196. data/spec/unit/mb/cli/sub_command/base_spec.rb +102 -0
  197. data/spec/unit/mb/cli/sub_command/component_spec.rb +5 -0
  198. data/spec/unit/mb/cli/sub_command/plugin_spec.rb +91 -0
  199. data/spec/unit/mb/cli/sub_command_spec.rb +43 -0
  200. data/spec/unit/mb/cli/ui.rb +0 -0
  201. data/spec/unit/mb/cli_client_spec.rb +51 -0
  202. data/spec/unit/mb/cli_gateway_spec.rb +386 -0
  203. data/spec/unit/mb/command_invoker/worker_spec.rb +43 -0
  204. data/spec/unit/mb/command_invoker_spec.rb +230 -0
  205. data/spec/unit/mb/command_runner_spec.rb +299 -0
  206. data/spec/unit/mb/command_spec.rb +76 -0
  207. data/spec/unit/mb/component_spec.rb +185 -0
  208. data/spec/unit/mb/config_manager_spec.rb +31 -0
  209. data/spec/unit/mb/config_spec.rb +408 -0
  210. data/spec/unit/mb/cookbook_metadata_spec.rb +89 -0
  211. data/spec/unit/mb/core_ext/dir_spec.rb +92 -0
  212. data/spec/unit/mb/core_ext/enumerable_spec.rb +104 -0
  213. data/spec/unit/mb/core_ext/file_spec.rb +58 -0
  214. data/spec/unit/mb/core_ext/signal_spec.rb +24 -0
  215. data/spec/unit/mb/environment_manager_spec.rb +166 -0
  216. data/spec/unit/mb/error_handler_spec.rb +173 -0
  217. data/spec/unit/mb/errors_spec.rb +132 -0
  218. data/spec/unit/mb/file_system/tempfile_spec.rb +14 -0
  219. data/spec/unit/mb/file_system_spec.rb +69 -0
  220. data/spec/unit/mb/gear_spec.rb +125 -0
  221. data/spec/unit/mb/gears/dynamic_service_spec.rb +187 -0
  222. data/spec/unit/mb/gears/jmx/action_spec.rb +34 -0
  223. data/spec/unit/mb/gears/jmx_spec.rb +32 -0
  224. data/spec/unit/mb/gears/mysql/action_spec.rb +118 -0
  225. data/spec/unit/mb/gears/mysql_spec.rb +21 -0
  226. data/spec/unit/mb/gears/service/action_runner_spec.rb +182 -0
  227. data/spec/unit/mb/gears/service/action_spec.rb +44 -0
  228. data/spec/unit/mb/gears/service_spec.rb +124 -0
  229. data/spec/unit/mb/group_spec.rb +280 -0
  230. data/spec/unit/mb/job_manager_spec.rb +56 -0
  231. data/spec/unit/mb/job_record_spec.rb +60 -0
  232. data/spec/unit/mb/job_spec.rb +201 -0
  233. data/spec/unit/mb/locks_manager_spec.rb +88 -0
  234. data/spec/unit/mb/logging_spec.rb +133 -0
  235. data/spec/unit/mb/manifest_spec.rb +105 -0
  236. data/spec/unit/mb/mixin/attribute_setting_spec.rb +180 -0
  237. data/spec/unit/mb/mixin/coded_exit_spec.rb +25 -0
  238. data/spec/unit/mb/mixin/locks_spec.rb +32 -0
  239. data/spec/unit/mb/mixin/services_spec.rb +75 -0
  240. data/spec/unit/mb/node_filter_spec.rb +86 -0
  241. data/spec/unit/mb/node_querier_spec.rb +532 -0
  242. data/spec/unit/mb/plugin_manager_spec.rb +724 -0
  243. data/spec/unit/mb/plugin_spec.rb +247 -0
  244. data/spec/unit/mb/provisioner/manager_spec.rb +141 -0
  245. data/spec/unit/mb/provisioner/manifest_spec.rb +182 -0
  246. data/spec/unit/mb/provisioner/provision_data_spec.rb +113 -0
  247. data/spec/unit/mb/provisioner_spec.rb +251 -0
  248. data/spec/unit/mb/provisioners/aws_spec.rb +392 -0
  249. data/spec/unit/mb/provisioners/environment_factory_spec.rb +108 -0
  250. data/spec/unit/mb/rest_gateway_spec.rb +13 -0
  251. data/spec/unit/mb/ridley_ext/cookbook_object_spec.rb +105 -0
  252. data/spec/unit/mb/srv_ctl_spec.rb +142 -0
  253. data/spec/unit/mb/upgrade/manager_spec.rb +37 -0
  254. data/spec/unit/mb/upgrade/worker_spec.rb +219 -0
  255. data/spec/unit/motherbrain_spec.rb +58 -0
  256. data/templates/bootstrap.json +8 -0
  257. data/templates/motherbrain.rb +44 -0
  258. metadata +694 -15
  259. data/Rakefile +0 -1
@@ -0,0 +1,40 @@
1
+ module MotherBrain
2
+ class CommandInvoker
3
+ class Worker
4
+ include Celluloid
5
+
6
+ # @return [MB::Command]
7
+ attr_reader :command
8
+ # @return [String]
9
+ attr_reader :environment
10
+ # @return [Array]
11
+ attr_reader :node_filter
12
+
13
+ # @param [MB::Command] command
14
+ # command to invoke
15
+ # @param [String] environment
16
+ # environment to invoke this command on
17
+ # @param [Array] node_filter = nil
18
+ # list of nodes to limit the command to
19
+ def initialize(command, environment, node_filter = nil)
20
+ @command = command
21
+ @environment = environment
22
+ @node_filter = node_filter
23
+ end
24
+
25
+ # @param [MB::Job] job
26
+ # @param [Array] arguments
27
+ def run(job, arguments = Array.new)
28
+ arguments ||= Array.new
29
+
30
+ msg = "Invoking #{command.type} command #{command.scope.name} #{command.name} on #{environment}"
31
+ msg << " with arguments: #{arguments}" if arguments.any?
32
+ job.set_status(msg)
33
+
34
+ command.invoke(job, environment, node_filter, *arguments)
35
+ rescue RemoteCommandError => ex
36
+ abort(ex)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,233 @@
1
+ module MotherBrain
2
+ class CommandRunner
3
+ include MB::Mixin::Services
4
+
5
+ # @return [String]
6
+ attr_reader :environment
7
+ # @return [MB::Plugin, MB::Component]
8
+ attr_reader :scope
9
+ # @return [Proc]
10
+ attr_reader :execute
11
+ # @return [Array]
12
+ attr_reader :node_filter
13
+ # @return [Array]
14
+ attr_reader :args
15
+
16
+ # @param [String] environment
17
+ # environment to run this command on
18
+ # @param [MB::Plugin, MB::Component] scope
19
+ # scope to execute this command in.
20
+ #
21
+ # * executing the command in the scope of an entire plugin will give you easy access to
22
+ # component commands and other plugin level commands
23
+ # * executing the command in the scope of a component will give you easy access to the
24
+ # other commands available in that component
25
+ # @param [Proc] execute
26
+ # the code to execute when the command runner is run
27
+ #
28
+ # @example
29
+ # proc {
30
+ # on("some_nodes") { service("nginx").run("stop") }
31
+ # }
32
+ # @param [Array] node_filter
33
+ # a list of nodes to filter commands on
34
+ # @param [Array] args
35
+ # any additional arguments to pass to the execution block
36
+ def initialize(job, environment, scope, execute, node_filter, *args)
37
+ @job = job
38
+ @environment = environment
39
+ @scope = scope
40
+ @execute = execute
41
+ @node_filter = node_filter
42
+ @args = args
43
+
44
+ @on_procs = []
45
+ @async = false
46
+
47
+ run
48
+ end
49
+
50
+ def run
51
+ if execute.arity.nonzero?
52
+ curried_execute = proc { execute.call(*args) }
53
+ instance_eval(&curried_execute)
54
+ else
55
+ instance_eval(&execute)
56
+ end
57
+ end
58
+
59
+ # Run the stored procs created by on() that have not been ran yet.
60
+ def apply
61
+ # TODO: This needs to happen in parallel but can't due to the way running multiple
62
+ # actions on a single node works. Actions work on a node and don't know about other
63
+ # actions which are being run on that node, so in a single node environment the
64
+ # state of a node can get weird when actions stomp all over each other.
65
+ #
66
+ # We should refactor this to APPLY actions to nodes and then allow them to converge
67
+ # together before we run them. This will allow us to execute multiple actions on a node at once
68
+ # without getting weird race conditions.
69
+ @on_procs.each do |on_proc|
70
+ on_proc.call
71
+ end
72
+
73
+ @on_procs.clear
74
+ end
75
+
76
+ # Are we inside an async block?
77
+ #
78
+ # @return [Boolean]
79
+ def async?
80
+ !!@async
81
+ end
82
+
83
+ # Run the block asynchronously.
84
+ def async(&block)
85
+ @nodes = []
86
+
87
+ @async = true
88
+ instance_eval(&block)
89
+ @async = false
90
+
91
+ apply
92
+
93
+ node_querier.bulk_chef_run job, @nodes
94
+ end
95
+
96
+ # Run the block specified on the nodes in the groups specified.
97
+ #
98
+ # @param [Array<String>] group_names groups to run on
99
+ #
100
+ # @option options [Integer] :any
101
+ # the number of nodes to run on, which nodes are chosen doesn't matter
102
+ # @option options [Integer] :max_concurrent
103
+ # the number of nodes to run on at a time
104
+ #
105
+ # @example running on masters and slaves, only 2 of them, 1 at a time
106
+ #
107
+ # on("masters", "slaves", any: 2, max_concurrent: 1) do
108
+ # # actions
109
+ # end
110
+ def on(*group_names, &block)
111
+ options = group_names.last.kind_of?(Hash) ? group_names.pop : {}
112
+
113
+ unless block_given?
114
+ raise PluginSyntaxError, "Block required"
115
+ end
116
+
117
+ clean_room = CleanRoom.new(scope)
118
+ clean_room.instance_eval(&block)
119
+ actions = clean_room.send(:actions)
120
+
121
+ nodes = group_names.map do |name|
122
+ scope.group!(name.to_s)
123
+ end.flat_map do |group|
124
+ group.nodes(environment)
125
+ end.uniq
126
+
127
+ return unless nodes.any?
128
+
129
+ nodes = MB::NodeFilter.filter(node_filter, nodes) if node_filter
130
+
131
+ if options[:any]
132
+ nodes = nodes.sample(options[:any])
133
+ end
134
+
135
+ options[:max_concurrent] ||= nodes.count
136
+ node_groups = nodes.each_slice(options[:max_concurrent]).to_a
137
+
138
+ run_chef = !async?
139
+
140
+ @on_procs << -> {
141
+ node_groups.each do |nodes|
142
+ actions.each do |action|
143
+ action.run(job, environment, nodes, run_chef)
144
+ end
145
+ end
146
+ }
147
+
148
+ if async?
149
+ @nodes |= nodes
150
+ else
151
+ apply
152
+ end
153
+ end
154
+
155
+ # Select a component for the purposes of invoking a command.
156
+ # NB: returns a proxy object
157
+ #
158
+ # @param [String] component_name the name of the component you want to
159
+ # invoke a command on
160
+ # @return [InvokableComponent] proxy for the actual component,
161
+ # only useful if you call #invoke on it
162
+ def component(component_name)
163
+ InvokableComponent.new(job, environment, scope.component(component_name))
164
+ end
165
+
166
+ def command(command_name)
167
+ scope.command!(command_name).invoke(job, environment, node_filter)
168
+ end
169
+
170
+ private
171
+
172
+ attr_reader :job
173
+
174
+ # @api private
175
+ class CleanRoom < CleanRoomBase
176
+ def initialize(*args)
177
+ super
178
+ @actions = Array.new
179
+
180
+ Gear.all.each do |klass|
181
+ clean_room = self
182
+
183
+ klass.instance_eval do
184
+ define_method :run, ->(job, *args, &block) do
185
+ clean_room.send(:actions) << action = action(job, *args, &block)
186
+ action
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ Gear.all.each do |klass|
193
+ define_method Gear.get_fun(klass), ->(*args) do
194
+ real_model.send(Gear.get_fun(klass), *args)
195
+ end
196
+ end
197
+
198
+ protected
199
+
200
+ attr_reader :actions
201
+
202
+ # @param [Fixnum] seconds
203
+ def wait(seconds)
204
+ Celluloid.sleep(seconds)
205
+ end
206
+ end
207
+
208
+ # Proxy for invoking components in the DSL
209
+ #
210
+ # @api private
211
+ class InvokableComponent
212
+ attr_reader :job
213
+ attr_reader :environment
214
+ attr_reader :component
215
+
216
+ # @param [Job] job
217
+ # @param [String] environment the environment on which to
218
+ # eventually invoke a command
219
+ # @param [Component] component the component we'll be invoking
220
+ def initialize(job, environment, component)
221
+ @job = job
222
+ @environment = environment
223
+ @component = component
224
+ end
225
+
226
+ # @param [String] command the command to invoke in the component
227
+ # @param [Array] args additional arguments for the command
228
+ def invoke(command, *args)
229
+ component.invoke(job, environment, command, args)
230
+ end
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,245 @@
1
+ module MotherBrain
2
+ class Component
3
+ include VariaModel
4
+
5
+ attribute :name,
6
+ type: String,
7
+ required: true
8
+
9
+ attribute :description,
10
+ type: String
11
+
12
+ attribute :version_attribute,
13
+ type: String
14
+
15
+ # @return [MB::Plugin]
16
+ attr_reader :plugin
17
+ attr_reader :groups
18
+ attr_reader :commands
19
+
20
+ # @param [#to_s] name
21
+ # @param [MB::Plugin] plugin
22
+ def initialize(name, plugin, &block)
23
+ set_attribute(:name, name.to_s)
24
+ @plugin = plugin
25
+ @groups = Set.new
26
+ @commands = Set.new
27
+ @gears = Hash.new
28
+
29
+ if block_given?
30
+ dsl_eval(&block)
31
+ end
32
+ end
33
+
34
+ # @return [String]
35
+ def description
36
+ _attributes_.description || "#{name} component commands"
37
+ end
38
+
39
+ # @return [Symbol]
40
+ def id
41
+ self.name.to_sym
42
+ end
43
+
44
+ # @param [#to_sym] name
45
+ def group(name)
46
+ self.groups.find { |group| group.name == name }
47
+ end
48
+
49
+ # @param [#to_sym] group_name
50
+ #
51
+ # @raise [GroupNotFound] if the group is not found
52
+ #
53
+ # @return [MB::Group]
54
+ def group!(group_name)
55
+ group = group(group_name)
56
+
57
+ if group.nil?
58
+ raise GroupNotFound, "Group #{group_name} does not exist on #{name}!"
59
+ end
60
+
61
+ group
62
+ end
63
+
64
+ # @param [#to_s] name
65
+ #
66
+ # @return [Boolean]
67
+ def has_group?(name)
68
+ group(name.to_s).present?
69
+ end
70
+
71
+ # @param [#to_sym] name
72
+ def command(name)
73
+ self.commands.find { |command| command.name == name }
74
+ end
75
+
76
+ # Return a command from the component's list of commands. If a command is not found an exception will be rasied.
77
+ #
78
+ # @param [#to_s] name
79
+ # name of the command to find and return
80
+ #
81
+ # @raise [CommandNotFound] if a command matching the given name is not found on this component
82
+ #
83
+ # @return [MB::Command]
84
+ def command!(name)
85
+ found = command(name)
86
+
87
+ if found.nil?
88
+ raise CommandNotFound.new(name, self)
89
+ end
90
+
91
+ found
92
+ end
93
+
94
+ # Run a command of the given name on the component.
95
+ #
96
+ # @param [Job] job
97
+ # @param [String] environment
98
+ # @param [String, Symbol] name
99
+ # @param [Array] args
100
+ def invoke(job, environment, name, *args)
101
+ self.command(name).invoke(job, environment, args)
102
+ end
103
+
104
+ # Finds the nodes for the given environment for each {Group} and groups them
105
+ # by Group#name into a Hash where the keys are Group#name and values are a Hash
106
+ # representation of a node from Chef.
107
+ #
108
+ # @param [#to_s] environment
109
+ #
110
+ # @raise [MB::EnvironmentNotFound] if the target environment does not exist
111
+ # @raise [MB::ChefConnectionError] if there was an error communicating to the Chef Server
112
+ #
113
+ # @example
114
+ #
115
+ # {
116
+ # "database_masters" => [
117
+ # {
118
+ # "name" => "db-master1",
119
+ # ...
120
+ # }
121
+ # ],
122
+ # "database_slaves" => [
123
+ # {
124
+ # "name" => "db-slave1",
125
+ # ...
126
+ # },
127
+ # {
128
+ # "name" => "db-slave2"
129
+ # ...
130
+ # }
131
+ # ]
132
+ # }
133
+ #
134
+ # @return [Hash]
135
+ def nodes(environment)
136
+ unless Application.ridley.environment.find(environment)
137
+ raise EnvironmentNotFound.new(environment)
138
+ end
139
+
140
+ {}.tap do |nodes|
141
+ self.groups.each do |group|
142
+ nodes[group.name] = group.nodes(environment)
143
+ end
144
+ end
145
+ end
146
+
147
+ def add_group(group)
148
+ self.groups.add(group)
149
+ end
150
+
151
+ # @param [MB::Command] command
152
+ def add_command(command)
153
+ self.commands.add(command)
154
+ end
155
+
156
+ # Returns the gears of class klass defined on this component.
157
+ #
158
+ # @param [MB::Gear] klass the class of the gears to find
159
+ #
160
+ # @return [Array<MB::Gear>]
161
+ def gears(klass)
162
+ @gears[klass.keyword] ||= Set.new
163
+ end
164
+
165
+ # Adds a gear to this component.
166
+ #
167
+ # @param [MB::Gear] gear the gear
168
+ def add_gear(gear)
169
+ klass = gear.class
170
+
171
+ unless get_gear(klass, gear.name).nil?
172
+ raise DuplicateGear, "#{klass.keyword.capitalize} '#{gear.name}' already defined"
173
+ end
174
+
175
+ gears(klass).add(gear)
176
+ end
177
+
178
+ # Finds a gear of class klass identified by *args.
179
+ #
180
+ # @param [MB::Gear] klass the class of the gear to search for
181
+ # @param [Array] args the identifiers for the gear to find
182
+ #
183
+ # @example searching for a service gear
184
+ #
185
+ # get_gear(MB::Gear::Service, "service_name")
186
+ #
187
+ # @example searching for a jmx gear
188
+ #
189
+ # get_gear(MB::Gear::Jmx)
190
+ #
191
+ # @return [MB::Gear]
192
+ def get_gear(klass, *args)
193
+ if klass.respond_to? :find
194
+ klass.find(gears(klass), *args)
195
+ else
196
+ klass.new(*args)
197
+ end
198
+ end
199
+
200
+ Gear.all.each do |klass|
201
+ define_method Gear.get_fun(klass) do |*args|
202
+ get_gear(klass, *args)
203
+ end
204
+ end
205
+
206
+ def get_service(name)
207
+ get_gear(MB::Gear::Service, name)
208
+ end
209
+
210
+ def to_s
211
+ "#{name}: #{description}"
212
+ end
213
+
214
+ private
215
+
216
+ def dsl_eval(&block)
217
+ CleanRoom.new(self).instance_eval(&block)
218
+ end
219
+
220
+ # @api private
221
+ class CleanRoom < CleanRoomBase
222
+ dsl_attr_writer :description
223
+ dsl_attr_writer :version_attribute
224
+
225
+ def versioned(attribute_name = nil)
226
+ version_attribute attribute_name || "#{@real_model.name}.version"
227
+ end
228
+ alias_method :versioned_with, :versioned
229
+
230
+ def group(name, &block)
231
+ real_model.add_group Group.new(name, &block)
232
+ end
233
+
234
+ def command(name, &block)
235
+ real_model.add_command Command.new(name, real_model, &block)
236
+ end
237
+
238
+ Gear.all.each do |klass|
239
+ define_method Gear.element_name(klass) do |*args, &block|
240
+ real_model.add_gear(klass.new(real_model, *args, &block))
241
+ end
242
+ end
243
+ end
244
+ end
245
+ end