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,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.each do |path|
2
+ require "mb/core_ext/#{File.basename(path, '.rb')}"
3
+ end
@@ -0,0 +1,37 @@
1
+ class Dir
2
+ class << self
3
+ # Check if the given directory contains a cookbook with a motherbrain plugin
4
+ #
5
+ # @param [#to_s] path
6
+ # the directory path
7
+ #
8
+ # @return [Boolean]
9
+ def has_mb_plugin?(path)
10
+ return false unless exists?(path)
11
+ return false unless has_mb_file?(path)
12
+ return false unless has_chef_metadata?(path)
13
+ true
14
+ end
15
+
16
+ # Check if the given directory contains a motherbrain plugin file
17
+ #
18
+ # @param [#to_s] path
19
+ # the directory path
20
+ #
21
+ # @return [Boolean]
22
+ def has_mb_file?(path)
23
+ File.exist?(File.join(path, MB::Plugin::PLUGIN_FILENAME))
24
+ end
25
+
26
+ # Check if the given directory contains a Chef metadata file
27
+ #
28
+ # @param [#to_s] path
29
+ # the directory path
30
+ #
31
+ # @return [Boolean]
32
+ def has_chef_metadata?(path)
33
+ File.exist?(File.join(path, MB::CookbookMetadata::RUBY_FILENAME)) ||
34
+ File.exist?(File.join(path, MB::CookbookMetadata::JSON_FILENAME))
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,48 @@
1
+ module Enumerable
2
+ # Map across all members using Celluloid futures, and wait for the results.
3
+ #
4
+ # This chooses the best behavior based on each item, and whether a method and
5
+ # argument list or block are passed.
6
+ #
7
+ # @param [Symbol] method_name
8
+ # The name of the method to call on each item in the collection
9
+ # @param [Array] args
10
+ # The argument list, if any, to pass to each method send
11
+ # @param [Proc] block
12
+ # A block to yield each item to
13
+ #
14
+ # @example Passing a method and arguments
15
+ #
16
+ # [1, 2, 3].concurrent_map(:next)
17
+ # # => [2, 3, 4]
18
+ #
19
+ # [1, 2, 3].concurrent_map(:modulo, 2)
20
+ # # => [1, 0, 1]
21
+ #
22
+ # @example Passing a block
23
+ #
24
+ # [1, 2, 3].concurrent_map { |n| n + 1 }
25
+ # # => [2, 3, 4]
26
+ #
27
+ # @return [Array] a new array containing the values returned by the futures
28
+ def concurrent_map(method_name = nil, *args, &block)
29
+ futures = if method_name
30
+ map { |item|
31
+ if item.respond_to?(:future)
32
+ item.future(method_name, *args)
33
+ else
34
+ Celluloid::Future.new { item.send(method_name, *args) }
35
+ end
36
+ }
37
+ elsif block_given?
38
+ map { |item|
39
+ Celluloid::Future.new { block.yield item }
40
+ }
41
+ else
42
+ raise ArgumentError, "Requires method and argument list, or a block"
43
+ end
44
+
45
+ futures.map(&:value)
46
+ end
47
+ alias_method :concurrent_collect, :concurrent_map
48
+ end
@@ -0,0 +1,24 @@
1
+ class File
2
+ class << self
3
+ # Determine if the given filepath points to a Chef metadata file
4
+ #
5
+ # @param [#to_s] filepath
6
+ #
7
+ # @return [Boolean]
8
+ def is_chef_metadata?(filepath)
9
+ return false unless exists?(filepath)
10
+ filename = basename(filepath)
11
+ filename == MB::CookbookMetadata::RUBY_FILENAME || filename == MB::CookbookMetadata::JSON_FILENAME
12
+ end
13
+
14
+ # Determine if the given filepath points to a motherbrain plugin file
15
+ #
16
+ # @param [#to_s] filepath
17
+ #
18
+ # @return [Boolean]
19
+ def is_mb_plugin?(filepath)
20
+ return false unless exists?(filepath)
21
+ basename(filepath) == MB::Plugin::PLUGIN_FILENAME
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module Signal
2
+ class << self
3
+ # Check if a given signal is supported on the current platform
4
+ #
5
+ # @param [#to_s] id
6
+ # the signal ID
7
+ def supported?(id)
8
+ list.has_key?(id.to_s)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,195 @@
1
+ module MotherBrain
2
+ class EnvironmentManager
3
+ class << self
4
+ # @raise [Celluloid::DeadActorError] if the environment manager has not been started
5
+ #
6
+ # @return [Celluloid::Actor(EnvironmentManager)]
7
+ def instance
8
+ MB::Application[:environment_manager] or raise Celluloid::DeadActorError, "environment manager not running"
9
+ end
10
+ end
11
+
12
+ include Celluloid
13
+ include MB::Logging
14
+ include MB::Mixin::Locks
15
+ include MB::Mixin::Services
16
+
17
+ finalizer :finalize_callback
18
+
19
+ def initialize
20
+ log.debug { "Environment Manager starting..." }
21
+ end
22
+
23
+ # Asynchronously configure a target environment with the given attributes
24
+ #
25
+ # @param [String] id
26
+ # identifier of the environment to configure
27
+ #
28
+ # @option options [Hash] :attributes (Hash.new)
29
+ # a hash of attributes to merge with the existing attributes of an environment
30
+ # @option options [Boolean] :force (false)
31
+ # force configure even if the environment is locked
32
+ #
33
+ # @note attributes will be set at the 'default' level and will be merged into the
34
+ # existing attributes of the environment
35
+ #
36
+ # @return [JobTicket]
37
+ def async_configure(id, options = {})
38
+ job = Job.new(:environment_configure)
39
+ async(:configure, job, id, options)
40
+
41
+ job.ticket
42
+ end
43
+
44
+ # Configure a target environment with the given attributes
45
+ #
46
+ # @param [MB::Job] job
47
+ # a job to update with progress
48
+ # @param [String] id
49
+ # identifier of the environment to configure
50
+ #
51
+ # @option options [Hash] :attributes (Hash.new)
52
+ # a hash of attributes to merge with the existing attributes of an environment
53
+ # @option options [Boolean] :force (false)
54
+ # force configure even if the environment is locked
55
+ #
56
+ # @api private
57
+ def configure(job, id, options = {})
58
+ options = options.reverse_merge(
59
+ attributes: Hash.new,
60
+ force: false
61
+ )
62
+
63
+ node_successes_count = 0
64
+ node_successes = Array.new
65
+
66
+ node_failures_count = 0
67
+ node_failures = Array.new
68
+
69
+ environment = find(id)
70
+ job.report_running("Finding environment #{environment.name}")
71
+
72
+ chef_synchronize(chef_environment: environment.name, force: options[:force], job: job) do
73
+ job.set_status("Saving updated environment with - #{options[:attributes]}")
74
+ environment.default_attributes.deep_merge!(options[:attributes])
75
+ environment.save
76
+
77
+ job.set_status("Searching for nodes in the environment")
78
+ nodes = nodes_for_environment(environment.name)
79
+
80
+ job.set_status("Performing a chef client run on #{nodes.length} nodes")
81
+ nodes.collect do |node|
82
+ node_querier.future(:chef_run, node.public_hostname)
83
+ end.each do |future|
84
+ begin
85
+ response = future.value
86
+ node_successes_count += 1
87
+ node_successes << response.host
88
+ rescue RemoteCommandError => error
89
+ node_failures_count += 1
90
+ node_failures << error.host
91
+ end
92
+ end
93
+ end
94
+
95
+ if node_failures_count > 0
96
+ job.report_failure("chef client run failed on #{node_failures_count} node(s) - #{node_failures.join(', ')}")
97
+ else
98
+ job.report_success("Finished chef client run on #{node_successes_count} node(s) - #{node_successes.join(', ')}")
99
+ end
100
+ rescue => ex
101
+ job.report_failure(ex)
102
+ ensure
103
+ job.terminate if job && job.alive?
104
+ end
105
+
106
+ # Find an environment on the remote Chef server
107
+ #
108
+ # @param [#to_s] id
109
+ # identifier for the environment to find
110
+ #
111
+ # @raise [EnvironmentNotFound] if the given environment does not exist
112
+ #
113
+ # @return [Ridley::EnvironmentResource]
114
+ def find(id)
115
+ unless environment = ridley.environment.find(id)
116
+ abort EnvironmentNotFound.new(id)
117
+ end
118
+
119
+ environment
120
+ end
121
+
122
+ # Creates an environment
123
+ #
124
+ # @param [#to_s] name
125
+ #
126
+ # @return [Ridley::EnvironmentResource]
127
+ def create(name)
128
+ ridley.environment.create(name: name)
129
+ rescue Ridley::Errors::HTTPConflict
130
+ abort EnvironmentExists.new(name)
131
+ rescue => error
132
+ abort error
133
+ end
134
+
135
+ # Creates an environment from JSON contained in a file
136
+ #
137
+ # @param [#to_s] path
138
+ #
139
+ # @return [Ridley::EnvironmentResource]
140
+ def create_from_file(path)
141
+ abort FileNotFound.new(path) unless File.exist? path
142
+
143
+ env = ridley.environment.from_file(path)
144
+ ridley.environment.create(env)
145
+ rescue Ridley::Errors::HTTPConflict
146
+ abort EnvironmentExists.new(env.name)
147
+ end
148
+
149
+ # Destroys an environment
150
+ #
151
+ # @param [#to_s] name
152
+ #
153
+ # @return [Ridley::EnvironmentResource, nil]
154
+ def destroy(name)
155
+ ridley.environment.delete(name)
156
+ end
157
+
158
+ # Returns a list of environments present on the remote server
159
+ #
160
+ # @return [Array<Ridley::EnvironmentResource>]
161
+ def list
162
+ ridley.environment.all
163
+ end
164
+
165
+ # Removes all nodes and clients from the Chef server for a given environment
166
+ #
167
+ # @param [String] name
168
+ def purge_nodes(name)
169
+ nodes = nodes_for_environment(name)
170
+ futures = []
171
+
172
+ nodes.each do |node|
173
+ futures << ridley.client.future(:delete, node)
174
+ futures << ridley.node.future(:delete, node)
175
+ end
176
+
177
+ futures.map(&:value)
178
+ end
179
+
180
+ # Returns an array of nodes for an environment
181
+ #
182
+ # @param [String] name
183
+ #
184
+ # @return [Array(Ridley::NodeObject)]
185
+ def nodes_for_environment(name)
186
+ ridley.search(:node, "chef_environment:#{name}")
187
+ end
188
+
189
+ private
190
+
191
+ def finalize_callback
192
+ log.debug { "Environment Manager stopping..." }
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,212 @@
1
+ module MotherBrain
2
+ # An object to help with the display of errors in a more user-friendly format.
3
+ # An ErrorHandler is created with an error and a set of options to control the
4
+ # display of the error. Some options can be inferred from the error itself.
5
+ # A typical use case would be to wrap an error generated deep in a call stack,
6
+ # and then add data to the error as it bubbles up.
7
+ #
8
+ # @example Wrapping and raising an error with more data
9
+ #
10
+ # ErrorHandler.wrap StandardError.new,
11
+ # file_path: "/a/b/c.rb",
12
+ # method_name: :wat,
13
+ # plugin_name: "hi",
14
+ # plugin_version: "1.2.3",
15
+ # text: "Invalid thing"
16
+ #
17
+ # # Would raise an error with a message of:
18
+ #
19
+ # hi (1.2.3)
20
+ # /a/b/c.rb, on line 1, in 'wat'
21
+ # Invalid thing
22
+ #
23
+ # @example Wrapping an error at multiple points in the call chain
24
+ #
25
+ # def load_file(path)
26
+ # load File.read(path)
27
+ # rescue => error
28
+ # ErrorHandler.wrap error, file_path: path
29
+ # end
30
+ #
31
+ # def load(code)
32
+ # eval code
33
+ # rescue => error
34
+ # ErrorHandler.wrap error, plugin_name: code.lines.to_a.first
35
+ # end
36
+ #
37
+ # def method_missing(method_name, *args, &block)
38
+ # ErrorHandler.wrap CodeError.new, method_name: method_name
39
+ # end
40
+ #
41
+ #
42
+ class ErrorHandler
43
+ NEWLINE = "\n"
44
+ SOURCE_RANGE = 5
45
+
46
+ class << self
47
+ # Wraps an error with additional data and raises it.
48
+ #
49
+ # @raise [StandardError]
50
+ # @see #initialize
51
+ def wrap(error, options = {})
52
+ error_handler = new(error, options)
53
+
54
+ raise error_handler.error, error_handler.message
55
+ end
56
+ end
57
+
58
+ attr_reader :error
59
+
60
+ OPTIONS = %w[
61
+ backtrace
62
+ file_path
63
+ method_name
64
+ plugin_name
65
+ plugin_version
66
+ text
67
+ ].map(&:to_sym)
68
+
69
+ OPTIONS.each do |option|
70
+ attr_reader option
71
+ end
72
+
73
+ # @param [StandardError] error
74
+ #
75
+ # @option options [Array] backtrace
76
+ # An array of strings containing filenames, line numbers, and method
77
+ # names. Typically comes from `caller`.
78
+ #
79
+ # @option options [String] file_path
80
+ # The location of a file on disk to display to the user.
81
+ #
82
+ # @option options [Symbol] method_name
83
+ # The name of the method or keyword which generated the error.
84
+ #
85
+ # @option options [String] plugin_name
86
+ # The name of the plugin the error relates to.
87
+ #
88
+ # @option options [String] plugin_version
89
+ # The version of the plugin the error relates to.
90
+ #
91
+ # @option options [String] text
92
+ # A custom error message to display to the user.
93
+ #
94
+ def initialize(error, options = {})
95
+ error = error.new if error.is_a? Class
96
+
97
+ @error = error
98
+
99
+ extract_data_from_options options
100
+ extract_data_from_error
101
+
102
+ embed_data_in_error
103
+ end
104
+
105
+ # Extracts data from an options hash and stores it in instance variables.
106
+ #
107
+ # @param [Hash] options
108
+ #
109
+ # @see #initialize
110
+ def extract_data_from_options(options)
111
+ OPTIONS.each do |option|
112
+ instance_variable_set "@#{option}", options[option]
113
+ end
114
+ end
115
+
116
+ # Extracts data from the error and stores it in instance variables. Does not
117
+ # overwrite existing instance variables.
118
+ def extract_data_from_error
119
+ OPTIONS.each do |option|
120
+ data = error.instance_variable_get "@_error_handler_#{option}"
121
+
122
+ unless instance_variable_get "@#{option}"
123
+ instance_variable_set "@#{option}", data
124
+ end
125
+ end
126
+
127
+ @backtrace ||= error.backtrace
128
+ @method_name ||= error.name if error.respond_to? :name
129
+ @text ||= error.message
130
+ end
131
+
132
+ # Stores the data in the error and defines getters.
133
+ def embed_data_in_error
134
+ OPTIONS.each do |option|
135
+ data = instance_variable_get "@#{option}"
136
+
137
+ if data
138
+ error.instance_variable_set "@_error_handler_#{option}", data
139
+ end
140
+ end
141
+ end
142
+
143
+ # @return [String]
144
+ def message
145
+ result = [
146
+ plugin_name_and_plugin_version,
147
+ file_path_and_line_number_and_method_name,
148
+ text,
149
+ relevant_source_lines
150
+ ].compact.join NEWLINE
151
+
152
+ result << NEWLINE unless result.end_with? NEWLINE
153
+
154
+ result
155
+ end
156
+
157
+ # @return [String]
158
+ def file_contents
159
+ return unless file_path and File.exist? file_path
160
+
161
+ File.read file_path
162
+ end
163
+
164
+ # @return [String]
165
+ def relevant_source_lines
166
+ return unless file_contents and line_number
167
+
168
+ beginning = line_number - (SOURCE_RANGE / 2) - 1
169
+ beginning = [beginning, 0].max
170
+ numbered_source_lines[beginning, SOURCE_RANGE].join NEWLINE
171
+ end
172
+
173
+ # @return [Array]
174
+ def numbered_source_lines
175
+ lines = file_contents.lines.to_a.map(&:rstrip)
176
+ rjust_size = lines.count.to_s.length
177
+
178
+ result = []
179
+
180
+ lines.each_with_index do |line, index|
181
+ current_line_number = index + 1
182
+
183
+ result << "#{current_line_number.to_s.rjust rjust_size}#{line_number == current_line_number ? '>>' : ': '}#{line}"
184
+ end
185
+
186
+ result
187
+ end
188
+
189
+ # @return [String]
190
+ def file_path_and_line_number_and_method_name
191
+ buffer = []
192
+ buffer << file_path if file_path
193
+ buffer << "on line #{line_number}" if line_number
194
+ buffer << "in '#{method_name}'" if method_name
195
+ buffer.join ", "
196
+ end
197
+
198
+ # @return [String]
199
+ def plugin_name_and_plugin_version
200
+ "#{plugin_name} (#{plugin_version})" if plugin_name and plugin_version
201
+ end
202
+
203
+ # Extracts the first line number from the backtrace.
204
+ #
205
+ # @return [Fixnum]
206
+ def line_number
207
+ return unless backtrace and backtrace[0]
208
+
209
+ backtrace[0].split(":")[1].to_i
210
+ end
211
+ end
212
+ end