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,58 @@
1
+ module MotherBrain
2
+ module Gear
3
+ class Service
4
+ # @api private
5
+ class Action
6
+ include MB::Mixin::Services
7
+
8
+ # @return [String]
9
+ attr_reader :name
10
+
11
+ # @param [String] name
12
+ # @param [MB::Component] component
13
+ #
14
+ # @raise [ArgumentError] if no block is given
15
+ def initialize(name, component, &block)
16
+ unless block_given?
17
+ raise ArgumentError, "block required for action '#{name}' on component '#{component.name}'"
18
+ end
19
+
20
+ @name = name
21
+ @component = component
22
+ @block = block
23
+ end
24
+
25
+ # Run this action on the specified nodes.
26
+ #
27
+ # @param [MB::Job] job
28
+ # a job to update with status
29
+ # @param [String] environment
30
+ # the environment this command is being run on
31
+ # @param [Array<Ridley::Node>]
32
+ # nodes the nodes to run this action on
33
+ #
34
+ # @return [Service::Action]
35
+ def run(job, environment, nodes, run_chef = true)
36
+ job.set_status("Running component: #{component.name} service action: #{name} on #{nodes.collect(&:name).join(', ')}")
37
+
38
+ runner = Service::ActionRunner.new(environment, nodes, &block)
39
+ runner.run(job)
40
+
41
+ if run_chef || runner.toggle_callbacks.any?
42
+ node_querier.bulk_chef_run(job, nodes, runner.service_recipe)
43
+ end
44
+
45
+ self
46
+ ensure
47
+ runner.reset(job)
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :component
53
+ attr_reader :runner
54
+ attr_reader :block
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,161 @@
1
+ module MotherBrain
2
+ module Gear
3
+ class Service
4
+ # @api private
5
+ class ActionRunner
6
+ include MB::Mixin::Services
7
+
8
+ attr_reader :environment
9
+ attr_reader :nodes
10
+ attr_reader :toggle_callbacks
11
+ attr_reader :service_recipe
12
+ attr_reader :environment_attributes
13
+ attr_reader :node_attributes
14
+
15
+ # @param [String] environment
16
+ # @param [Array<Ridley::Node>] nodes
17
+ def initialize(environment, nodes, &block)
18
+ @environment = environment
19
+ @nodes = Array(nodes)
20
+
21
+ @environment_attributes = Array.new
22
+ @node_attributes = Array.new
23
+ @toggle_callbacks = Array.new
24
+
25
+ if block_given?
26
+ dsl_eval(&block)
27
+ end
28
+ end
29
+
30
+ def reset(job)
31
+ toggle_callbacks.concurrent_map { |callback| callback.call(job) }
32
+ end
33
+
34
+ def run(job)
35
+ set_node_attributes(job)
36
+ set_environment_attributes(job)
37
+ end
38
+
39
+ # Set an environment level attribute to the given value. The key is represented
40
+ # by a dotted path.
41
+ #
42
+ # @param [String] key
43
+ # @param [Object] value
44
+ #
45
+ # @option options [Boolean] :toggle (false)
46
+ # set this environment attribute only for a single chef run
47
+ def add_environment_attribute(key, value, options = {})
48
+ @environment_attributes << { key: key, value: value, options: options }
49
+ end
50
+
51
+ # Set a node level attribute on all nodes for this action to the given value.
52
+ # The key is represented by a dotted path.
53
+ #
54
+ # @param [String] key
55
+ # @param [Object] value
56
+ #
57
+ # @option options [Boolean] :toggle (false)
58
+ # set this node attribute only for a single chef run
59
+ def add_node_attribute(key, value, options)
60
+ @node_attributes << { key: key, value: value, options: options }
61
+ end
62
+
63
+ def set_service_recipe(recipe)
64
+ @service_recipe = recipe
65
+ end
66
+
67
+ private
68
+
69
+ def dsl_eval(&block)
70
+ CleanRoom.new(self).instance_eval(&block)
71
+ end
72
+
73
+ class CleanRoom < CleanRoomBase
74
+ dsl_attr_writer :component
75
+
76
+ def service_recipe(recipe)
77
+ real_model.set_service_recipe(recipe)
78
+ end
79
+
80
+ def environment_attribute(key, value, options = {})
81
+ options = options.reverse_merge(toggle: false)
82
+ real_model.add_environment_attribute(key, value, options)
83
+ end
84
+
85
+ def node_attribute(key, value, options = {})
86
+ options = options.reverse_merge(toggle: false)
87
+ real_model.add_node_attribute(key, value, options)
88
+ end
89
+ end
90
+
91
+ def set_environment_attributes(job)
92
+ return unless environment_attributes.any?
93
+
94
+ unless env_chef_object = ridley.environment.find(environment)
95
+ raise MB::EnvironmentNotFound.new(environment)
96
+ end
97
+
98
+ environment_attributes.each do |attribute|
99
+ key, value, options = attribute[:key], attribute[:value], attribute[:options]
100
+
101
+ if options[:toggle]
102
+ toggle_callbacks << ->(job) {
103
+ message = "Toggling (removing) environment attribute '#{key}' on #{environment}"
104
+ job.set_status(message)
105
+ env_chef_object.delete_default_attribute(key)
106
+ env_chef_object.save
107
+ }
108
+ end
109
+
110
+ job.set_status("Setting environment attribute '#{key}' to #{value.inspect} in #{environment}")
111
+ env_chef_object.set_default_attribute(key, value)
112
+ end
113
+
114
+ job.set_status("Saving environment #{environment}")
115
+ env_chef_object.save
116
+ end
117
+
118
+ # Set all node level attributes to the given node
119
+ #
120
+ # @param [Ridley::Job] job
121
+ # a job to send status updates to
122
+ # @param [Ridley::NodeObject] node
123
+ # the node to set the attribute on
124
+ def set_node_attributes(job)
125
+ return if node_attributes.empty?
126
+
127
+ nodes.concurrent_map do |node|
128
+ node.reload
129
+
130
+ node_attributes.each do |attribute|
131
+ key, value, options = attribute[:key], attribute[:value], attribute[:options]
132
+
133
+ if options[:toggle]
134
+ original_value = node.chef_attributes.dig(key)
135
+
136
+ toggle_callbacks << ->(job) {
137
+ message = if original_value.nil?
138
+ "Toggling off node attribute '#{key}' on #{node.name}"
139
+ elsif !options[:force_value_to].nil?
140
+ "Forcing node attribute to '#{options[:force_value_to]}' on #{node.name}"
141
+ else
142
+ "Toggling node attribute '#{key}' back to '#{original_value.inspect}' on #{node.name}"
143
+ end
144
+ job.set_status(message)
145
+ value_to_set = options[:force_value_to].nil? ? original_value : options[:force_value_to]
146
+ node.set_chef_attribute(key, value_to_set)
147
+ node.save
148
+ }
149
+ end
150
+
151
+ job.set_status("Setting node attribute '#{key}' to #{value.inspect} on #{node.name}")
152
+ node.set_chef_attribute(key, value)
153
+ end
154
+
155
+ node.save
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/grape_ext/*.rb"].sort.each do |path|
2
+ require "mb/grape_ext/#{File.basename(path, '.rb')}"
3
+ end
@@ -0,0 +1,13 @@
1
+ module Grape
2
+ class Endpoint
3
+ alias_method :old_params, :params
4
+
5
+ # Grape::Endpoint#params is an attr_reader that returns Grape::Request#params.
6
+ # The latter params method returns a Hashie::Mash with Strings as keys.
7
+ #
8
+ # https://github.com/intridea/grape/blob/v0.6.1/lib/grape/http/request.rb#L6
9
+ def params
10
+ old_params.to_hash.deep_symbolize_keys
11
+ end
12
+ end
13
+ end
data/lib/mb/group.rb ADDED
@@ -0,0 +1,143 @@
1
+ module MotherBrain
2
+ class Group
3
+ include VariaModel
4
+
5
+ attribute :name,
6
+ type: String,
7
+ required: true
8
+
9
+ # @return [Set]
10
+ attr_reader :roles
11
+ # @return [Set]
12
+ attr_reader :recipes
13
+ # @return [Hashie::Mash]
14
+ attr_reader :chef_attributes
15
+
16
+ # @param [#to_s] name
17
+ def initialize(name, &block)
18
+ set_attribute(:name, name.to_s)
19
+ @recipes = Set.new
20
+ @roles = Set.new
21
+ @chef_attributes = Hashie::Mash.new
22
+
23
+ if block_given?
24
+ dsl_eval(&block)
25
+ end
26
+ end
27
+
28
+ # @return [Symbol]
29
+ def id
30
+ self.name.to_sym
31
+ end
32
+
33
+ # Returns an Array Ridley::Node objects from Chef that match this {Group}'s
34
+ # signature.
35
+ #
36
+ # A signature is any combination of a recipe(s) or role(s) in a node's run_list or
37
+ # an attribute(s) on a node.
38
+ #
39
+ # @param [#to_s] environment
40
+ #
41
+ # @return [Array<Ridley::Node>]
42
+ def nodes(environment)
43
+ Application.ridley.partial_search(:node, search_query(environment), [ "public_ipv4", "public_hostname" ])
44
+ end
45
+
46
+ # Returns an escape search query for Solr from the roles, rescipes, and chef_attributes
47
+ # assigned to this Group.
48
+ #
49
+ # @param [#to_s] environment
50
+ #
51
+ # @return [String]
52
+ def search_query(environment)
53
+ items = ["chef_environment:#{environment}"]
54
+
55
+ items += chef_attributes.collect do |key, value|
56
+ key = key.gsub(/\./, "_")
57
+ "#{attribute_escape(key)}:#{value}"
58
+ end
59
+
60
+ items += roles.collect { |role| "roles:#{solr_escape(role)}" }
61
+ items += recipes.collect { |recipe| "recipes:#{solr_escape(recipe)}" }
62
+
63
+ items.join(' AND ')
64
+ end
65
+
66
+ def add_role(name)
67
+ self.roles.add(name)
68
+ end
69
+
70
+ def add_recipe(name)
71
+ self.recipes.add(name)
72
+ end
73
+
74
+ def add_chef_attribute(key, value)
75
+ if chef_attribute(key).present?
76
+ raise DuplicateChefAttribute, "An attribute '#{key}' has already been defined on group '#{_attributes_[:name]}'"
77
+ end
78
+
79
+ self.chef_attributes[key] = value
80
+ end
81
+
82
+ # @param [#to_sym] name
83
+ def chef_attribute(name)
84
+ self.chef_attributes.fetch(name.to_sym, nil)
85
+ end
86
+
87
+ # Combines the recipes and roles of this group into a run_list that can be passed to
88
+ # Chef or Ridley
89
+ #
90
+ # [ "role[web_server]", "recipe[nginx::default]" ]
91
+ #
92
+ # @return [Array<String>]
93
+ def run_list
94
+ self.roles.collect { |role| "role[#{role}]" } +
95
+ self.recipes.collect { |recipe| "recipe[#{recipe}]" }
96
+ end
97
+
98
+ # Indicates whether the run list contains the recipe
99
+ #
100
+ # @return [TrueClass, FalseClass]
101
+ def includes_recipe?(recipe)
102
+ # todo expand roles?
103
+ self.run_list.include?("#{recipe}")
104
+ end
105
+
106
+ private
107
+
108
+ def dsl_eval(&block)
109
+ CleanRoom.new(self).instance_eval(&block)
110
+ end
111
+
112
+ def attribute_escape(value)
113
+ value.gsub(/\./, "_")
114
+ end
115
+
116
+ def solr_escape(value)
117
+ value.gsub(/[\:\[\]\+\!\^\(\)\{\}]/) { |x| "\\#{x}" }
118
+ end
119
+
120
+ # @api private
121
+ class CleanRoom < CleanRoomBase
122
+ # @param [#to_s] value
123
+ #
124
+ # @return [Set<String>]
125
+ def recipe(value)
126
+ real_model.add_recipe(value.to_s)
127
+ end
128
+
129
+ # @param [#to_s] value
130
+ #
131
+ # @return [Set<String>]
132
+ def role(value)
133
+ real_model.add_role(value.to_s)
134
+ end
135
+
136
+ # @param [#to_s] attr_key
137
+ # @param [Object] attr_value
138
+ def chef_attribute(attr_key, attr_value)
139
+ real_model.add_chef_attribute(attr_key, attr_value)
140
+ end
141
+ end
142
+ end
143
+ end
data/lib/mb/job.rb ADDED
@@ -0,0 +1,183 @@
1
+ module MotherBrain
2
+ # A Celluloid actor representing an active job. Jobs are handled by the {JobManager}
3
+ # and should not be returned to a consumer or user from the Public API.
4
+ #
5
+ # Jobs start in the 'pending' state and can only be in any one state at a given
6
+ # time. A Job is completed when in the 'success' or 'failure' state.
7
+ #
8
+ # The progress of a Job is recorded by the {JobManager} as a {JobRecord}. API
9
+ # consumers should reference the status of a running Job by it's {JobRecord}.
10
+ #
11
+ # Returning a {JobTicket} from the Public API will give a consumer or user an easy
12
+ # way to check the status of a job by polling a Job's {JobRecord}.
13
+ #
14
+ # @example running a job and checking it's status
15
+ #
16
+ # job = Job.new('example_job')
17
+ # ticket = job.ticket
18
+ #
19
+ # ticket.completed? => false
20
+ # ticket.state => :pending
21
+ #
22
+ # job.transition(:success, 'done!')
23
+ #
24
+ # ticket.completed? => true
25
+ # ticket.state => :success
26
+ #
27
+ # @api private
28
+ class Job
29
+ autoload :StateMachine, 'mb/job/state_machine'
30
+ autoload :States, 'mb/job/states'
31
+
32
+ extend Forwardable
33
+
34
+ include Celluloid
35
+ include MB::Logging
36
+ include MB::Job::States
37
+ include MB::Mixin::Services
38
+
39
+ attr_reader :id
40
+ attr_reader :type
41
+ attr_reader :result
42
+
43
+ # Set when the state of the Job changes from 'pending' to 'running'
44
+ #
45
+ # @note do not modify outside of the state machine
46
+ #
47
+ # @return [Time]
48
+ attr_accessor :time_start
49
+
50
+ # Set when the state of the Job changes from 'running' to 'sucess' or 'failure'
51
+ #
52
+ # @note do not modify outside of the state machine
53
+ #
54
+ # @return [Time]
55
+ attr_accessor :time_end
56
+
57
+ def_delegator :machine, :state
58
+
59
+ finalizer :finalize_callback
60
+
61
+ # @param [#to_s] type
62
+ def initialize(type)
63
+ @machine = StateMachine.new
64
+ @type = type.to_s
65
+ @id = job_manager.uuid
66
+ @result = nil
67
+ job_manager.add(Actor.current)
68
+ end
69
+
70
+ # @param [#to_json] result
71
+ # a result which can be converted to JSON
72
+ # @param [Hash] options
73
+ # options to pass to the state machine transition
74
+ #
75
+ # @return [Job]
76
+ def report_failure(result = nil, options = {})
77
+ log.fatal { "Job (#{id}) failure: #{result}" }
78
+ transition(:failure, result, options)
79
+ end
80
+
81
+ # @param [#to_json] result
82
+ # a result which can be converted to JSON
83
+ # @param [Hash] options
84
+ # options to pass to the state machine transition
85
+ #
86
+ # @return [Job]
87
+ def report_pending(result = nil, options = {})
88
+ log.info { "Job (#{id}) pending: #{result}" }
89
+ transition(:pending, result, options)
90
+ end
91
+
92
+ # @param [#to_json] result
93
+ # a result which can be converted to JSON
94
+ # @param [Hash] options
95
+ # options to pass to the state machine transition
96
+ #
97
+ # @return [Job]
98
+ def report_running(result = nil, options = {})
99
+ log.info { "Job (#{id}) running: #{result}" }
100
+ transition(:running, result, options)
101
+ end
102
+
103
+ # @param [#to_json] result
104
+ # a result which can be converted to JSON
105
+ # @param [Hash] options
106
+ # options to pass to the state machine transition
107
+ #
108
+ # @return [Job]
109
+ def report_success(result = nil, options = {})
110
+ log.info { "Job (#{id}) success: #{result}" }
111
+ transition(:success, result, options)
112
+ end
113
+
114
+ # @param [Boolean] boolean
115
+ # a boolean value representing a success or failure
116
+ # @param [#to_json] result
117
+ # a result which can be converted to JSON
118
+ # @param [Hash] options
119
+ # options to pass to the state machine transition
120
+ #
121
+ # @return [Job]
122
+ def report_boolean(boolean, result = nil, options = {})
123
+ if boolean
124
+ report_success(result, options)
125
+ else
126
+ report_failure(result, options)
127
+ end
128
+ end
129
+
130
+ # @return [self]
131
+ def save
132
+ job_manager.update(Actor.current)
133
+ end
134
+
135
+ def status
136
+ @status || state.to_s.capitalize
137
+ end
138
+
139
+ def status=(string)
140
+ log.info { "Job (#{id}) status: #{string}" }
141
+ @status = string
142
+ status_buffer << string
143
+ save
144
+ end
145
+ alias_method :set_status, :status=
146
+
147
+ def status_buffer
148
+ @status_buffer ||= []
149
+ end
150
+
151
+ # @return [JobTicket]
152
+ def ticket
153
+ @ticket ||= JobTicket.new(id)
154
+ end
155
+
156
+ # @param [Symbol] state
157
+ # the state to transition to in the Job's state machine
158
+ # @param [#cause, #to_s] result
159
+ # a result which can be converted to JSON
160
+ # @param [Hash] options
161
+ # options to pass to the state machine transition
162
+ #
163
+ # @return [Job]
164
+ def transition(state, result = nil, options = {})
165
+ @result = result.respond_to?(:cause) ? result.cause : result
166
+ machine.transition(state, options)
167
+ Actor.current
168
+ end
169
+
170
+ def to_s
171
+ "#<Job @type=#{type.inspect} @machine.state=#{state.inspect}>"
172
+ end
173
+ alias_method :inspect, :to_s
174
+
175
+ private
176
+
177
+ attr_reader :machine
178
+
179
+ def finalize_callback
180
+ job_manager.async.complete_job(Actor.current)
181
+ end
182
+ end
183
+ end