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,186 @@
1
+ module MotherBrain
2
+ module Provisioner
3
+ autoload :Manager, 'mb/provisioner/manager'
4
+ autoload :Manifest, 'mb/provisioner/manifest'
5
+ autoload :ProvisionData, 'mb/provisioner/provision_data'
6
+
7
+ class << self
8
+ attr_reader :default_id
9
+
10
+ # @param [Class] klass
11
+ # @option options [Boolean] :default
12
+ #
13
+ # @raise [ProvisionerRegistrationError] if a provisioner is registered as the default provisioner when
14
+ # a default provisioner already exists
15
+ #
16
+ # @return [Set]
17
+ def register(klass, options = {})
18
+ validate_provisioner_class(klass)
19
+
20
+ unless get(klass.provisioner_id).nil?
21
+ raise ProvisionerRegistrationError,
22
+ "A provisioner with the id '#{klass.provisioner_id}' has already been registered"
23
+ end
24
+
25
+ if options[:default]
26
+ unless @default_id.nil?
27
+ raise ProvisionerRegistrationError, "A default provisioner has already been defined (#{default_id})"
28
+ end
29
+
30
+ @default_id = klass.provisioner_id
31
+ end
32
+
33
+ all.add(klass)
34
+ end
35
+
36
+ # List of all the registered provisioners
37
+ #
38
+ # @return [Set<MB::Provisioner::Base>]
39
+ def all
40
+ @all ||= Set.new
41
+ end
42
+
43
+ # Get registered provisioner class from the given ID. Return nil if no provisioner with
44
+ # the corresponding ID is found
45
+ #
46
+ # @param [#to_sym] id
47
+ #
48
+ # @return [Class, nil]
49
+ def get(id)
50
+ all.find { |klass| klass.provisioner_id == id.to_sym }
51
+ end
52
+
53
+ # Get registered provisioner class fromt he given ID. Raise an error if no provisioner with
54
+ # the corresponding ID is found
55
+ #
56
+ # @raise [ProvisionerNotRegistered] if no provisioner with the corresponding ID is found
57
+ #
58
+ # @return [Class]
59
+ def get!(id)
60
+ provisioner = get(id)
61
+
62
+ if provisioner.nil?
63
+ raise ProvisionerNotRegistered, "No provisioner registered with the ID: '#{id}'"
64
+ end
65
+
66
+ provisioner
67
+ end
68
+
69
+ # Return the default provisioner if one has been registered as the default
70
+ #
71
+ # @return [Class, nil]
72
+ def default
73
+ _default_id = ENV['MB_DEFAULT_PROVISIONER'] || self.default_id
74
+ get!(_default_id) if _default_id
75
+ end
76
+
77
+ # Clears all of the registered Provisioners.
78
+ #
79
+ # @return [Set]
80
+ # an empty Set
81
+ def clear!
82
+ @default_id = nil
83
+ @all = Set.new
84
+ end
85
+
86
+ # @param [Symbol] klass
87
+ #
88
+ # @raise [InvalidProvisionerClass] if the class does not respond to provisioner_id
89
+ #
90
+ # @return [Boolean]
91
+ def validate_provisioner_class(klass)
92
+ unless klass.respond_to?(:provisioner_id)
93
+ raise InvalidProvisionerClass, "Cannot register provisioner: all provisioners must respond to ':provisioner_id'"
94
+ end
95
+
96
+ if klass.provisioner_id.nil?
97
+ raise InvalidProvisionerClass, "Cannot register provisioner: invalid provisioner_id '#{klass.provisioner_id}'"
98
+ end
99
+
100
+ true
101
+ end
102
+ end
103
+
104
+ class Base
105
+ class << self
106
+ # The identifier for the Provisioner
107
+ #
108
+ # @return [Symbol]
109
+ attr_reader :provisioner_id
110
+
111
+ # @param [#to_sym] provisioner_id
112
+ def register_provisioner(provisioner_id, options = {})
113
+ @provisioner_id = provisioner_id.to_sym
114
+ Provisioner.register(self, options)
115
+ end
116
+
117
+ # Validate that the return created nodes contains the expected number of nodes and the proper
118
+ # instance types
119
+ #
120
+ # @param [Array<Hash>] created
121
+ # @param [Provisioner::Manifest] manifest
122
+ #
123
+ # @raise [UnexpectedProvisionCount] if an unexpected amount of nodes was returned by the
124
+ # request to the provisioner
125
+ def validate_create(created, manifest)
126
+ unless created.length == manifest.node_count
127
+ raise UnexpectedProvisionCount.new(manifest.node_count, created.length)
128
+ end
129
+ end
130
+ end
131
+
132
+ include Celluloid
133
+ include MB::Logging
134
+ include MB::Mixin::Services
135
+
136
+ attr_reader :options
137
+
138
+ def initialize(options = {})
139
+ @options = options
140
+ end
141
+
142
+ # Request a provisioner to generate a set of nodes described by the given manifest
143
+ #
144
+ # @param [MB::Job] job
145
+ # @param [String] env_name
146
+ # name of the set of nodes to be created
147
+ # @param [MB::Provisioner::Manifest] manifest
148
+ # manifest describing how many and what kind of nodes to create
149
+ # @param [MB::Plugin] plugin
150
+ # @param [Hash] options
151
+ #
152
+ # @example
153
+ # [
154
+ # {
155
+ # instance_type: "m1.large",
156
+ # public_hostname: "cloud-1.riotgames.com"
157
+ # },
158
+ # {
159
+ # instance_type: "m1.small",
160
+ # public_hostname: "cloud-2.riotgames.com"
161
+ # }
162
+ # ]
163
+ #
164
+ # @return [Array]
165
+ # an array of hashes representing nodes generated of given sizes
166
+ def up(job, env_name, manifest, plugin, options = {})
167
+ raise AbstractFunction
168
+ end
169
+
170
+ # Destroy a set of provisioned nodes
171
+ #
172
+ # @param [MB::Job] job
173
+ # @param [String] environment_name
174
+ # name of the set of nodes to destroy
175
+ # @param [Hash] options
176
+ #
177
+ # @raise [MB::ProvisionError]
178
+ # if a caught error occurs during provisioning
179
+ #
180
+ # @return [Boolean]
181
+ def down(job, environment_name, options = {})
182
+ raise AbstractFunction
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,213 @@
1
+ module MotherBrain
2
+ class ProvisionerSupervisor < Celluloid::SupervisionGroup; end
3
+
4
+ module Provisioner
5
+ # Handles provisioning of nodes and joining them to a Chef Server. Requests are
6
+ # delegated to a provisioner of the desired type or 'Environment Factory' by
7
+ # default.
8
+ class Manager
9
+ class << self
10
+ # @raise [Celluloid::DeadActorError] if Provisioner Manager has not been started
11
+ #
12
+ # @return [Celluloid::Actor(Provisioner::Manager)]
13
+ def instance
14
+ MB::Application[:provisioner_manager] or raise Celluloid::DeadActorError, "provisioner manager not running"
15
+ end
16
+ end
17
+
18
+ WORKER_OPTS = [
19
+ :component_versions,
20
+ :cookbook_versions,
21
+ :environment_attributes,
22
+ :environment_attributes_file,
23
+ :skip_bootstrap,
24
+ :force
25
+ ]
26
+
27
+ include Celluloid
28
+ include MB::Logging
29
+ include MB::Mixin::Locks
30
+ include MB::Mixin::Services
31
+
32
+ finalizer :finalize_callback
33
+
34
+ attr_reader :provisioner_registry
35
+
36
+ def initialize
37
+ log.debug { "Provision Manager starting..." }
38
+ @provisioner_registry = Celluloid::Registry.new
39
+ @provisioner_supervisor = ProvisionerSupervisor.new_link(@provisioner_registry)
40
+ Provisioner.all.each do |provisioner|
41
+ @provisioner_supervisor.supervise_as(provisioner.provisioner_id, provisioner)
42
+ end
43
+ end
44
+
45
+ # Asynchronously destroy an environment that was created with motherbrain
46
+ #
47
+ # @param [String] environment
48
+ # name of the environment to destroy
49
+ #
50
+ # @option options [#to_sym] :with
51
+ # id of provisioner to use
52
+ def async_destroy(environment, options = {})
53
+ job = Job.new(:destroy)
54
+ async(:destroy, job, environment, options)
55
+
56
+ job.ticket
57
+ end
58
+
59
+ # Asynchronously create a new environment
60
+ #
61
+ # @param [#to_s] environment
62
+ # name of the environment to create or append to
63
+ # @param [MB::Provisioner::Manifest] manifest
64
+ # manifest of nodes to create
65
+ # @param [MB::Plugin] plugin
66
+ # the plugin we are creating these nodes for
67
+ #
68
+ # @see {#provision} for options
69
+ #
70
+ # @return [MB::JobRecord]
71
+ # a reference to the executing job
72
+ def async_provision(environment, manifest, plugin, options = {})
73
+ options = options.reverse_merge(skip_bootstrap: false)
74
+
75
+ job_type = options[:skip_bootstrap] ? :provision : :provision_and_bootstrap
76
+ job = Job.new(job_type)
77
+
78
+ async(:provision, job, environment, manifest, plugin, options)
79
+
80
+ job.ticket
81
+ end
82
+
83
+ # Retrieve the running provisioner for the given ID. The default provisioner will be returned
84
+ # if nil is provided.
85
+ #
86
+ # @param [#to_sym, nil] id ({Provisioner.default_id})
87
+ #
88
+ # @raise [ProvisionerNotStarted]
89
+ # if no provisioner is registered with the given ID
90
+ #
91
+ # @return [MB::Provisioner::Base]
92
+ def choose_provisioner(id)
93
+ id ||= Provisioner.default_id
94
+
95
+ unless provisioner = @provisioner_registry[id.to_sym]
96
+ abort ProvisionerNotStarted.new(id)
97
+ end
98
+
99
+ provisioner
100
+ end
101
+
102
+ # Destroy an environment that was created with motherbrain
103
+ #
104
+ # @param [MB::Job] job
105
+ # a job to update with progress
106
+ # @param [String] environment
107
+ # name of the environment to destroy
108
+ #
109
+ # @option options [#to_sym] :with
110
+ # id of provisioner to use
111
+ def destroy(job, environment, options = {})
112
+ job.report_running
113
+ options = options.dup
114
+
115
+ chef_synchronize(chef_environment: environment, force: options[:force]) do
116
+ provision_data = ProvisionData.new(environment)
117
+
118
+ provision_data.provisioners.each do |provisioner_name|
119
+ provisioner = choose_provisioner(provisioner_name)
120
+ job.set_status "Destroying nodes provisioned with #{provisioner_name.upcase}"
121
+ provisioner.down job, environment, options
122
+ end
123
+
124
+ job.set_status "Removing nodes and clients from Chef"
125
+ environment_manager.purge_nodes(environment)
126
+
127
+ job.set_status "Destroying Chef environment #{environment}"
128
+ environment_manager.destroy(environment)
129
+
130
+ job.set_status "Destroying provision data"
131
+ provision_data.destroy
132
+ end
133
+
134
+ job.report_success("Environment #{environment} destroyed")
135
+ rescue => ex
136
+ job.report_failure(ex)
137
+ ensure
138
+ job.terminate if job && job.alive?
139
+ end
140
+
141
+ # Create a new environment
142
+ #
143
+ # @param [MB::Job] job
144
+ # a job to update with progress
145
+ # @param [String] environment
146
+ # name of the environment to create or append to
147
+ # @param [MB::Provisioner::Manifest] manifest
148
+ # manifest of nodes to create
149
+ # @param [MB::Plugin] plugin
150
+ # the plugin we are creating these nodes for
151
+ #
152
+ # @option options [String] :chef_version
153
+ # version of Chef to install on the node
154
+ # @option options [Hash] :component_versions (Hash.new)
155
+ # Hash of components and the versions to set them to
156
+ # @option options [Hash] :cookbook_versions (Hash.new)
157
+ # Hash of cookbooks and the versions to set them to
158
+ # @option options [Hash] :environment_attributes (Hash.new)
159
+ # Hash of additional attributes to set on the environment
160
+ # @option options [Boolean] :skip_bootstrap (false)
161
+ # skip automatic bootstrapping of the created environment
162
+ # @option options [Boolean] :force (false)
163
+ # force provisioning nodes to the environment even if the environment is locked
164
+ def provision(job, environment, manifest, plugin, options = {})
165
+ job.report_running("preparing to provision")
166
+
167
+ options = options.reverse_merge(
168
+ component_versions: Hash.new,
169
+ cookbook_versions: Hash.new,
170
+ environment_attributes: Hash.new,
171
+ skip_bootstrap: false,
172
+ force: false
173
+ )
174
+
175
+ manifest.validate!(plugin)
176
+ response = choose_provisioner(manifest.provisioner).up(job, environment, manifest, plugin, options)
177
+
178
+ unless options[:skip_bootstrap]
179
+ bootstrap_manifest = Bootstrap::Manifest.from_provisioner(response, manifest)
180
+ write_bootstrap_manifest(job, environment, bootstrap_manifest, plugin)
181
+ bootstrapper.bootstrap(job, environment, bootstrap_manifest, plugin, options)
182
+ end
183
+
184
+ job.report_success unless job.completed?
185
+ rescue => ex
186
+ job.report_failure(ex)
187
+ ensure
188
+ job.terminate if job && job.alive?
189
+ end
190
+
191
+ private
192
+
193
+ # @param [MB::Job] job
194
+ # @param [String] environment
195
+ # @param [MB::Manifest] manifest
196
+ # @param [MB::Plugin] plugin
197
+ def write_bootstrap_manifest(job, environment, manifest, plugin)
198
+ filename = "#{plugin.name}_#{environment}_#{Time.now.to_i}.json"
199
+ path = MB::FileSystem.manifests.join(filename)
200
+ contents = JSON.pretty_generate(manifest.to_hash)
201
+
202
+ job.set_status("Writing bootstrap manifest to #{path}")
203
+
204
+ File.open(path, 'w') { |file| file.write contents }
205
+ end
206
+
207
+ def finalize_callback
208
+ log.debug { "Provision Manager stopping..." }
209
+ @provisioner_supervisor.terminate
210
+ end
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,125 @@
1
+ module MotherBrain
2
+ module Provisioner
3
+ # Manifest for creating a set of nodes of a given instance type for a set of node groups
4
+ #
5
+ # @example valid manifest structure
6
+ # {
7
+ # "provisioner": "aws",
8
+ # "options": {
9
+ # "provisioner_specific_option": "yellow",
10
+ # },
11
+ # "nodes": [
12
+ # {
13
+ # "groups": ["activemq::master"],
14
+ # "type": "m1.large",
15
+ # "count": 4
16
+ # },
17
+ # {
18
+ # "groups": ["activemq::slave"],
19
+ # "type": "m1.large",
20
+ # "count": 2
21
+ # }
22
+ # ]
23
+ # }
24
+ class Manifest < MotherBrain::Manifest
25
+ class << self
26
+ # Validate the given parameter contains a Hash or Manifest with a valid structure
27
+ #
28
+ # @param [Provisioner::Manifest] manifest
29
+ # @param [MotherBrain::Plugin] plugin
30
+ #
31
+ # @raise [InvalidProvisionManifest] if the given manifest is not well formed
32
+ def validate!(manifest, plugin)
33
+ unless manifest.is_a?(Hash)
34
+ raise InvalidProvisionManifest,
35
+ "The provisioner manifest needs to be a hash, but you provided a(n) #{manifest.class}"
36
+ end
37
+
38
+ nodes = manifest[:nodes]
39
+
40
+ unless nodes
41
+ raise InvalidProvisionManifest,
42
+ "The provisioner manifest needs to have a key 'nodes' containing an array"
43
+ end
44
+
45
+ unless nodes.is_a?(Array)
46
+ raise InvalidProvisionManifest,
47
+ "The provisioner manifest needs to have a key 'nodes' containing an array, but it was a(n) #{nodes.class}"
48
+ end
49
+
50
+ nodes.each do |node|
51
+ unless node.is_a?(Hash)
52
+ raise InvalidProvisionManifest,
53
+ "The provisioner manifest needs to have an array of hashes at 'nodes', but there was a #{node.class}: #{node.inspect}"
54
+ end
55
+
56
+ unless node.has_key?(:type) && !node[:type].nil?
57
+ raise InvalidProvisionManifest,
58
+ "A node entry in a provision manifest needs to contain key 'type' with a value."
59
+ end
60
+
61
+ unless node.has_key?(:groups) && !node[:groups].nil?
62
+ raise InvalidProvisionManifest,
63
+ "A node entry in a provision manifest needs to contain a key 'groups' with a value."
64
+ end
65
+
66
+ type = node[:type]
67
+ count = node[:count]
68
+ groups = node[:groups]
69
+
70
+ unless type.to_s.match(/\w+\.\w+/)
71
+ raise InvalidProvisionManifest,
72
+ "Provision manifest contained an entry not in the proper format: '#{type}'. Expected: 'a1.size'"
73
+ end
74
+
75
+ if count
76
+ unless count.is_a?(Fixnum) && count >= 0
77
+ raise InvalidProvisionManifest,
78
+ "Provision manifest contained an invalid value for count: '#{count}'. Expected an integer greater than 0."
79
+ end
80
+ end
81
+
82
+ unless groups.is_a?(Array)
83
+ raise InvalidProvisionManifest,
84
+ "The provisioner manifest contains a node group without an array of groups: #{node.inspect}"
85
+ end
86
+
87
+ groups.each do |group|
88
+ match = group.match(Plugin::NODE_GROUP_ID_REGX)
89
+
90
+ unless match
91
+ raise InvalidProvisionManifest,
92
+ "Provision manifest contained an entry not in the proper format: '#{name}'. Expected: 'component::group'"
93
+ end
94
+
95
+ component = match[1]
96
+ group = match[2]
97
+
98
+ unless plugin.has_component?(component)
99
+ raise InvalidProvisionManifest,
100
+ "Provision manifest describes the component: '#{component}' but '#{plugin.name}' does not have this component"
101
+ end
102
+
103
+ unless plugin.component(component).has_group?(group)
104
+ raise InvalidProvisionManifest,
105
+ "Provision manifest describes the group: '#{group}' in the component '#{component}' but that component does not have this group"
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ # @param [Plugin] plugin
113
+ #
114
+ # @raise [InvalidProvisionManifest] if the given manifest is not well formed
115
+ def validate!(plugin)
116
+ self.class.validate!(self, plugin)
117
+ end
118
+
119
+ # @return [String] the provisioner token from the manifest
120
+ def provisioner
121
+ self[:provisioner]
122
+ end
123
+ end
124
+ end
125
+ end