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
data/lib/mb/plugin.rb ADDED
@@ -0,0 +1,300 @@
1
+ module MotherBrain
2
+ class Plugin
3
+ class << self
4
+ # Create a new plugin instance from the given content
5
+ #
6
+ # @param [MB::CookbookMetadata] metadata
7
+ #
8
+ # @raise [PluginLoadError]
9
+ #
10
+ # @yieldreturn [MotherBrain::Plugin]
11
+ def load(metadata, &block)
12
+ new(metadata, &block).validate!
13
+ rescue PluginSyntaxError => ex
14
+ ErrorHandler.wrap(ex)
15
+ end
16
+
17
+ # Load the contents of a directory into an instance of MB::Plugin
18
+ #
19
+ # @param [#to_s] path
20
+ # a path to a directory containing a motherbrain plugin file and cookbook
21
+ # metadata file
22
+ #
23
+ # @raise [PluginLoadError]
24
+ #
25
+ # @return [MotherBrain::Plugin]
26
+ def from_path(path)
27
+ unless Dir.has_mb_plugin?(path)
28
+ raise PluginLoadError, "Expected a motherbrain and metadata file at: #{path}"
29
+ end
30
+
31
+ plugin_filename = File.join(path, PLUGIN_FILENAME)
32
+ plugin_contents = File.read(plugin_filename)
33
+ metadata = CookbookMetadata.from_path(path)
34
+
35
+ load(metadata) { eval(plugin_contents, binding, plugin_filename, 1) }
36
+ rescue PluginSyntaxError => ex
37
+ raise PluginSyntaxError, ErrorHandler.new(ex, file_path: plugin_filename).message
38
+ end
39
+
40
+ def key_for(name, version)
41
+ "#{name}-#{version}".to_sym
42
+ end
43
+ end
44
+
45
+ NODE_GROUP_ID_REGX = /^(.+)::(.+)$/.freeze
46
+ PLUGIN_FILENAME = 'motherbrain.rb'.freeze
47
+
48
+ extend Forwardable
49
+ include Comparable
50
+ include VariaModel
51
+
52
+ attribute :bootstrap_routine,
53
+ type: MB::Bootstrap::Routine
54
+
55
+ # @return [MB::CookbookMetadata]
56
+ attr_reader :metadata
57
+ # @return [Set<MB::Component>]
58
+ attr_reader :components
59
+ # @return [Set<MB::Command>]
60
+ attr_reader :commands
61
+
62
+ def_delegator :metadata, :name
63
+ def_delegator :metadata, :maintainer
64
+ def_delegator :metadata, :maintainer_email
65
+ def_delegator :metadata, :license
66
+ def_delegator :metadata, :description
67
+ def_delegator :metadata, :long_description
68
+ def_delegator :metadata, :version
69
+
70
+ # @param [MB::CookbookMetadata] metadata
71
+ def initialize(metadata, &block)
72
+ @metadata = metadata
73
+ @components = Set.new
74
+ @commands = Set.new
75
+
76
+ if block_given?
77
+ dsl_eval(&block)
78
+ end
79
+ end
80
+
81
+ # @return [Symbol]
82
+ def id
83
+ self.class.key_for(self.name, self.version)
84
+ end
85
+
86
+ # @param [#to_s] name
87
+ #
88
+ # @return [MB::Component, nil]
89
+ def component(name)
90
+ components.find { |component| component.name == name.to_s }
91
+ end
92
+
93
+ # @param [#to_s] name
94
+ #
95
+ # @raise [ComponentNotFound] if a component of the given name is not a part of this plugin
96
+ #
97
+ # @return [MB::Component]
98
+ def component!(name)
99
+ component = component(name)
100
+
101
+ if component.nil?
102
+ raise ComponentNotFound.new(name, self)
103
+ end
104
+
105
+ component
106
+ end
107
+
108
+ # @param [#to_s] name
109
+ #
110
+ # @return [Boolean]
111
+ def has_component?(name)
112
+ component(name).present?
113
+ end
114
+
115
+ # Return a command from the plugins list of commands.
116
+ #
117
+ # @param [#to_s] name
118
+ # name of the command to find and return
119
+ #
120
+ # @return [MB::Command, nil]
121
+ def command(name)
122
+ commands.find { |command| command.name == name.to_s }
123
+ end
124
+
125
+ # Return a command from the plugin's list of commands. If a command is not found an exception will be rasied.
126
+ #
127
+ # @param [#to_s] name
128
+ # name of the command to find and return
129
+ #
130
+ # @raise [CommandNotFound] if a command matching the given name is not found on this plugin
131
+ #
132
+ # @return [MB::Command]
133
+ def command!(name)
134
+ found = command(name)
135
+
136
+ if found.nil?
137
+ raise CommandNotFound.new(name, self)
138
+ end
139
+
140
+ found
141
+ end
142
+
143
+ # Finds the nodes for the given environment for each {Component} of the plugin groups them
144
+ # by Component#name and Group#name into a Hash where the keys are Component#name and
145
+ # values are a hash where the keys are Group#name and the values are a Hash representing
146
+ # a node from Chef.
147
+ #
148
+ # @param [#to_s] environment
149
+ #
150
+ # @raise [MB::EnvironmentNotFound] if the target environment does not exist
151
+ # @raise [MB::ChefConnectionError] if there was an error communicating to the Chef Server
152
+ #
153
+ # @example
154
+ #
155
+ # {
156
+ # "activemq" => {
157
+ # database_masters" => [
158
+ # {
159
+ # "name" => "db-master1",
160
+ # ...
161
+ # }
162
+ # ],
163
+ # "database_slaves" => [
164
+ # {
165
+ # "name" => "db-slave1",
166
+ # ...
167
+ # },
168
+ # {
169
+ # "name" => "db-slave2",
170
+ # ...
171
+ # }
172
+ # ]
173
+ # }
174
+ # }
175
+ #
176
+ # @return [Hash]
177
+ def nodes(environment)
178
+ unless Application.ridley.environment.find(environment)
179
+ raise EnvironmentNotFound.new(environment)
180
+ end
181
+
182
+ {}.tap do |nodes|
183
+ self.components.each do |component|
184
+ nodes[component.name] = component.nodes(environment)
185
+ end
186
+ end
187
+ end
188
+
189
+ # @param [MB::Component] component
190
+ def add_component(component)
191
+ self.components.add(component)
192
+ end
193
+
194
+ # @param [MB::Command] command
195
+ def add_command(command)
196
+ self.commands.add(command)
197
+ end
198
+
199
+ # Completely validate a loaded plugin and raise an exception of errors
200
+ #
201
+ # @return [self]
202
+ def validate!
203
+ errors = validate
204
+
205
+ if errors.any?
206
+ ErrorHandler.wrap PluginSyntaxError,
207
+ backtrace: [],
208
+ plugin_name: try(:name),
209
+ plugin_version: try(:version),
210
+ text: messages_from_errors(errors)
211
+ end
212
+
213
+ self
214
+ end
215
+
216
+ # Creates an error message from an error hash, where the keys are attributes
217
+ # and the values are an array of error messages.
218
+ #
219
+ # @param [Hash] errors
220
+ #
221
+ # @return [String]
222
+ def messages_from_errors(errors)
223
+ buffer = []
224
+
225
+ errors.each do |attribute, messages|
226
+ buffer |= messages
227
+ end
228
+
229
+ buffer.join "\n"
230
+ end
231
+
232
+ def <=>(other)
233
+ unless other.is_a?(self.class)
234
+ return 0
235
+ end
236
+
237
+ if self.name == other.name
238
+ self.version <=> other.version
239
+ else
240
+ self.name <=> other.name
241
+ end
242
+ end
243
+
244
+ def eql?(other)
245
+ other.is_a?(self.class) && self == other
246
+ end
247
+
248
+ def to_s
249
+ "#{self.name} (#{self.version})"
250
+ end
251
+
252
+ def to_hash
253
+ {
254
+ name: name,
255
+ version: version,
256
+ maintainer: maintainer,
257
+ maintainer_email: maintainer_email,
258
+ description: description,
259
+ long_description: long_description
260
+ }
261
+ end
262
+
263
+ # @param [Hash] options
264
+ # a set of options to pass to MultiJson.encode
265
+ #
266
+ # @return [String]
267
+ def to_json(options = {})
268
+ MultiJson.encode(self.to_hash, options)
269
+ end
270
+ alias_method :as_json, :to_json
271
+
272
+ private
273
+
274
+ def dsl_eval(&block)
275
+ CleanRoom.new(self).instance_eval(&block)
276
+ end
277
+
278
+ # @api private
279
+ class CleanRoom < CleanRoomBase
280
+ # @param [#to_s] name
281
+ def command(name, &block)
282
+ real_model.add_command Command.new(name, real_model, &block)
283
+ end
284
+
285
+ # @param [#to_s] name
286
+ def component(name, &block)
287
+ real_model.add_component Component.new(name, real_model, &block)
288
+ end
289
+
290
+ def stack_order(&block)
291
+ real_model.bootstrap_routine = Bootstrap::Routine.new(real_model, &block)
292
+ end
293
+
294
+ def cluster_bootstrap(&block)
295
+ MB.log.warn "#{real_model}: cluster_bootstrap is now stack_order, and will be removed in motherbrain 1.0"
296
+ stack_order(&block)
297
+ end
298
+ end
299
+ end
300
+ end
@@ -0,0 +1,589 @@
1
+ module MotherBrain
2
+ class PluginManager
3
+ class << self
4
+ # @raise [Celluloid::DeadActorError] if Node Querier has not been started
5
+ #
6
+ # @return [Celluloid::Actor(PluginManager)]
7
+ def instance
8
+ MB::Application[:plugin_manager] or raise Celluloid::DeadActorError, "plugin manager not running"
9
+ end
10
+ end
11
+
12
+ include Celluloid
13
+ include Celluloid::Notifications
14
+ include MB::Logging
15
+ include MB::Mixin::Services
16
+
17
+ # @return [Pathname]
18
+ attr_reader :berkshelf_path
19
+
20
+ # Tracks when the plugin manager will attempt to load remote plugins from the Chef Server. If
21
+ # remote loading is disabled this will return nil.
22
+ #
23
+ # @return [Timers::Timer, nil]
24
+ attr_reader :eager_load_timer
25
+
26
+ finalizer :finalize_callback
27
+
28
+ def initialize
29
+ log.debug { "Plugin Manager starting..." }
30
+ @berkshelf_path = MB::Berkshelf.path
31
+ @plugins = Set.new
32
+
33
+ MB::Berkshelf.init
34
+
35
+ async_loading? ? async(:load_all) : load_all
36
+
37
+ if eager_loading?
38
+ @eager_load_timer = every(eager_load_interval, &method(:load_all_remote))
39
+ end
40
+
41
+ subscribe(ConfigManager::UPDATE_MSG, :reconfigure)
42
+ end
43
+
44
+ # Add a plugin to the set of plugins
45
+ #
46
+ # @param [MB::Plugin] plugin
47
+ #
48
+ # @option options [Boolean] :force
49
+ # load a plugin even if a plugin of the same name and version is already loaded
50
+ #
51
+ # @return [MB::Plugin, nil]
52
+ # returns the set of plugins on success or nil if the plugin was not added
53
+ def add(plugin, options = {})
54
+ if options[:force]
55
+ remove(plugin)
56
+ end
57
+
58
+ if find(plugin.name, plugin.version, remote: false)
59
+ return nil
60
+ end
61
+
62
+ @plugins.add(plugin)
63
+ plugin
64
+ end
65
+
66
+ # Should the plugin manager perform plugin loading operations in the background?
67
+ #
68
+ # @note should be disabled if running motherbrain from the CLIGateway to ensure
69
+ # all plugins are loaded before being accessed
70
+ #
71
+ # @return [Boolean]
72
+ def async_loading?
73
+ Application.config.plugin_manager.async_loading
74
+ end
75
+
76
+ # Clear list of known plugins
77
+ #
78
+ # @return [Set]
79
+ def clear_plugins
80
+ @plugins.clear
81
+ end
82
+
83
+ # If enabled the plugin manager will automatically discover plugins on the remote Chef Server
84
+ # and load them into the plugin set.
85
+ #
86
+ # @note to change this option set it in the {Config} of {ConfigManager}
87
+ #
88
+ # @return [Boolean]
89
+ def eager_loading?
90
+ Application.config.plugin_manager.eager_loading
91
+ end
92
+
93
+ # The time between each poll of the remote Chef server to eagerly load discovered plugins
94
+ #
95
+ # @note to change this option set it in the {Config} of {ConfigManager}
96
+ #
97
+ # @return [Integer]
98
+ def eager_load_interval
99
+ Application.config.plugin_manager.eager_load_interval
100
+ end
101
+
102
+ # Load all of the plugins from the Berkshelf
103
+ #
104
+ # @option options [Boolean] :force (false)
105
+ def load_all_installed(options = {})
106
+ options = options.reverse_merge(force: false)
107
+
108
+ installed_cookbooks.each do |path|
109
+ load_installed(path, options)
110
+ end
111
+ end
112
+
113
+ # Load all of the plugins from the remote Chef Server. Plugins with a name and version that have
114
+ # already been loaded will not be loaded again unless forced.
115
+ #
116
+ # @option options [String] :name
117
+ # @option options [Boolean] :force (false)
118
+ def load_all_remote(options = {})
119
+ options = options.reverse_merge(force: false)
120
+
121
+ if options[:name].present?
122
+ remote_cookbook_versions(options[:name]).collect do |version|
123
+ load_remote(options[:name], version, options)
124
+ end
125
+ else
126
+ [].tap do |remotes|
127
+ remote_cookbooks.each do |name, versions|
128
+ versions.each { |version| remotes << future(:load_remote, name, version, options) }
129
+ end
130
+ end.map(&:value)
131
+ end
132
+ end
133
+
134
+ # Find and return a registered plugin of the given name and version. If no
135
+ # version attribute is specified the latest version of the plugin is
136
+ # returned.
137
+ #
138
+ # @param [String] name
139
+ # name of the plugin
140
+ # @param [#to_s] version
141
+ # version of the plugin to find
142
+ #
143
+ # @option options [Boolean] :remote (false)
144
+ # search for the plugin on the remote Chef Server if it isn't installed
145
+ #
146
+ # @return [MB::Plugin, nil]
147
+ def find(name, version = nil, options = {})
148
+ options = options.reverse_merge(remote: false)
149
+
150
+ return latest(name, options) unless version
151
+
152
+ installed = @plugins.find { |plugin| plugin.name == name && plugin.version.to_s == version.to_s }
153
+
154
+ return installed if installed
155
+
156
+ if options[:remote]
157
+ remote = load_remote(name, version.to_s)
158
+ return remote if remote
159
+ end
160
+
161
+ nil
162
+ end
163
+
164
+ # Determine the best version of a plugin to use when communicating to the given environment
165
+ #
166
+ # @param [String] plugin_id
167
+ # name of the plugin
168
+ # @param [String] environment_id
169
+ # name of the environment
170
+ #
171
+ # @option options [Boolean] :remote (false)
172
+ # include plugins on the remote Chef Server which aren't installed
173
+ #
174
+ # @raise [EnvironmentNotFound] if the given environment does not exist
175
+ # @raise [PluginNotFound] if a plugin of the given name is not found
176
+ #
177
+ # @return [MB::Plugin]
178
+ def for_environment(plugin_id, environment_id, options = {})
179
+ options = options.reverse_merge(remote: false)
180
+ environment = environment_manager.find(environment_id)
181
+ constraint = environment.cookbook_versions[plugin_id] || ">= 0.0.0"
182
+
183
+ satisfy(plugin_id, constraint, options)
184
+ rescue MotherBrain::EnvironmentNotFound => ex
185
+ abort ex
186
+ end
187
+
188
+ # Finds the plugin for the cookbook specified in the run list entry
189
+ #
190
+ # @param [String] run_list_entry
191
+ # Chef standard run list entry
192
+ # @param [String] environment
193
+ # name of the environment
194
+ #
195
+ # @return [MB::Plugin]
196
+ def for_run_list_entry(run_list_entry, environment = nil, options = {})
197
+ item = MotherBrain::Chef::RunListItem.new(run_list_entry)
198
+ if item.version
199
+ # version will be defined in run list entries such as
200
+ # recipe[foo::server@1.1.2], which takes precidence over the
201
+ # environment.
202
+ find(item.cookbook_name, item.version, options)
203
+ elsif !environment.nil?
204
+ for_environment(item.cookbook_name, environment, options)
205
+ else
206
+ find(item.cookbook_name, nil, options)
207
+ end
208
+ end
209
+
210
+ # @param [String] name
211
+ # name of the plugin
212
+ # @param [#to_s] version
213
+ # version of the plugin to find
214
+ def has_plugin?(name, version)
215
+ !find(name, version).nil?
216
+ end
217
+
218
+ # Download and install the cookbook containing a motherbrain plugin matching the
219
+ # given name and optional version into the user's Berkshelf.
220
+ #
221
+ # @param [String] name
222
+ # Name of the plugin
223
+ # @param [#to_s] version
224
+ # The version of the plugin to install. If left blank the latest version will be installed
225
+ #
226
+ # @return [MB::Plugin]
227
+ #
228
+ # @raise [MB::PluginNotFound]
229
+ def install(name, version = nil)
230
+ unless plugin = find(name, version, remote: true)
231
+ abort MB::PluginNotFound.new(name, version)
232
+ end
233
+
234
+ chef_connection.cookbook.download(plugin.name, plugin.version, install_path_for(plugin))
235
+ reload(plugin)
236
+ end
237
+
238
+ # The filepath that a plugin would be or should be installed to
239
+ #
240
+ # @param [MB::Plugin] plugin
241
+ #
242
+ # @return [Pathname]
243
+ def install_path_for(plugin)
244
+ Berkshelf.cookbooks_path.join("#{plugin.name}-#{plugin.version}")
245
+ end
246
+
247
+ # Return most current version of the plugin of the given name
248
+ #
249
+ # @param [String] name
250
+ # name of the plugin
251
+ #
252
+ # @option options [Boolean] :remote (false)
253
+ # include plugins on the remote Chef server which haven't been cached or installed
254
+ #
255
+ # @return [MB::Plugin, nil]
256
+ def latest(name, options = {})
257
+ options = options.reverse_merge(remote: false)
258
+
259
+ potentials = list(name: name, remote: false).map(&:version)
260
+ potentials += remote_cookbook_versions(name) if options[:remote]
261
+ potentials = potentials.collect { |version| Solve::Version.new(version) }.uniq.sort.reverse
262
+
263
+ potentials.find do |version|
264
+ found = find(name, version.to_s, options.slice(:remote))
265
+ return found if found
266
+ end
267
+
268
+ nil
269
+ end
270
+
271
+ # @return [Array<MotherBrain::Plugin>]
272
+ def load_all
273
+ load_all_installed
274
+ load_all_remote if eager_loading?
275
+ end
276
+
277
+ # Load a plugin from a file
278
+ #
279
+ # @param [#to_s] path
280
+ #
281
+ # @option options [Boolean] :force (true)
282
+ # load a plugin even if a plugin of the same name and version is already loaded
283
+ # @option options [Boolean] :allow_failure (true)
284
+ # allow for loading failure
285
+ #
286
+ # @return [MB::Plugin, nil]
287
+ # returns the loaded plugin or nil if the plugin was not loaded successfully
288
+ def load_installed(path, options = {})
289
+ options = options.reverse_merge(force: true, allow_failure: true)
290
+ load_file(path, options)
291
+ rescue PluginSyntaxError, PluginLoadError => ex
292
+ err_msg = "could not load plugin at '#{path}': #{ex.message}"
293
+ options[:allow_failure] ? log.debug(err_msg) : abort(PluginLoadError.new(err_msg))
294
+ nil
295
+ end
296
+
297
+ # Load a plugin of the given name and version from the remote Chef server
298
+ #
299
+ # @param [String] name
300
+ # name of the plugin to load
301
+ # @param [String] version
302
+ # version of the plugin to load
303
+ #
304
+ # @option options [Boolean] :force (false)
305
+ # load a plugin even if a plugin of the same name and version is already loaded
306
+ # @option options [Boolean] :allow_failure (true)
307
+ # allow for loading failure
308
+ #
309
+ # @raise [PluginSyntaxError]
310
+ # @raise [PluginLoadError]
311
+ #
312
+ # @return [MB::Plugin, nil]
313
+ # returns the loaded plugin or nil if the remote does not contain a plugin of the given
314
+ # name and version or if there was a failure loading the plugin
315
+ def load_remote(name, version, options = {})
316
+ options = options.reverse_merge(force: false, allow_failure: true)
317
+ resource = ridley.cookbook.find(name, version)
318
+
319
+ return unless resource && resource.has_motherbrain_plugin?
320
+
321
+ begin
322
+ scratch_dir = FileSystem.tmpdir("cbplugin")
323
+ metadata_path = File.join(scratch_dir, CookbookMetadata::JSON_FILENAME)
324
+ plugin_path = File.join(scratch_dir, Plugin::PLUGIN_FILENAME)
325
+
326
+ File.write(metadata_path, resource.metadata.to_json)
327
+
328
+ unless resource.download_file(:root_file, Plugin::PLUGIN_FILENAME, plugin_path)
329
+ raise PluginLoadError, "failure downloading plugin file for #{resource.name}"
330
+ end
331
+
332
+ load_file(scratch_dir, options)
333
+ rescue PluginSyntaxError, PluginLoadError => ex
334
+ err_msg = "could not load remote plugin #{name} (#{version}): #{ex.message}"
335
+ options[:allow_failure] ? log.debug(err_msg) : abort(PluginLoadError.new(err_msg))
336
+ nil
337
+ ensure
338
+ FileUtils.rm_rf(scratch_dir)
339
+ end
340
+ end
341
+
342
+ # List all installed versions of a plugin with the given name of plugins. An empty
343
+ # array will be returned if no versions of a plugin are installed.
344
+ #
345
+ # @example
346
+ # plugin_manager.installed_versions("nginx") #=> [ "1.2.3", "2.0.0", "3.1.2" ]
347
+ #
348
+ # @param [#to_s] name
349
+ # name of the plugin
350
+ #
351
+ # @return [Array<String>]
352
+ def installed_versions(name)
353
+ installed_cookbooks.collect do |path|
354
+ plugin = load_installed(path)
355
+ next unless plugin
356
+
357
+ if plugin.name == name
358
+ plugin.version.to_s
359
+ end
360
+ end.compact
361
+ end
362
+
363
+ # A set of all the registered plugins
364
+ #
365
+ # @option options [String] :name
366
+ # filter the results to include only plugins of the given name
367
+ # @option options [Boolean] :remote (false)
368
+ # eargly search for plugins on the remote Chef server and include them in the returned list
369
+ #
370
+ # @return [Array<MB::Plugin>]
371
+ def list(options = {})
372
+ options = options.reverse_merge(remote: false)
373
+
374
+ if options[:remote]
375
+ load_all_remote(options.slice(:name))
376
+ end
377
+
378
+ result = options[:name].nil? ? @plugins : @plugins.select { |plugin| plugin.name == options[:name] }
379
+ result.sort.reverse
380
+ end
381
+
382
+ # Remove and Add the given plugin from the set of plugins
383
+ #
384
+ # @param [MB::Plugin] plugin
385
+ def reload(plugin)
386
+ add(plugin, force: true)
387
+ end
388
+
389
+ # Reload plugins from Chef Server and from the Berkshelf
390
+ #
391
+ # @return [Array<MotherBrain::Plugin>]
392
+ def reload_all
393
+ clear_plugins
394
+ load_all
395
+ end
396
+
397
+ # Reload plugins from the Berkshelf
398
+ #
399
+ # @return [Array<MotherBrain::Plugin>]
400
+ def reload_installed
401
+ load_all_installed(force: true)
402
+ end
403
+
404
+ # Remove the given plugin from the set of plugins
405
+ #
406
+ # @param [Set<MB::Plugin>] plugin
407
+ def remove(plugin)
408
+ @plugins.delete(plugin)
409
+ end
410
+
411
+ # Return the best version of the plugin to use for the given constraint
412
+ #
413
+ # @param [String] plugin_name
414
+ # name of the plugin
415
+ # @param [String, Solve::Constraint] constraint
416
+ # constraint to satisfy
417
+ #
418
+ # @option options [Boolean] :remote (false)
419
+ # include plugins on the remote Chef Server which aren't installed
420
+ #
421
+ # @raise [PluginNotFound] if a plugin of the given name which satisfies the given constraint
422
+ # is not found
423
+ #
424
+ # @return [MB::Plugin]
425
+ def satisfy(plugin_name, constraint, options = {})
426
+ options = options.reverse_merge(remote: false)
427
+ constraint = Solve::Constraint.new(constraint)
428
+
429
+ # Optimize for equality operator. Don't need to find all of the versions if
430
+ # we only care about one.
431
+ if constraint.operator == "="
432
+ find(plugin_name, constraint.version, options.slice(:remote))
433
+ elsif constraint.to_s == ">= 0.0.0"
434
+ latest(plugin_name, options.slice(:remote))
435
+ else
436
+ graph = Solve::Graph.new
437
+ versions(plugin_name, options[:remote]).each do |version|
438
+ graph.artifacts(plugin_name, version)
439
+ end
440
+
441
+ solution = Solve.it!(graph, [[plugin_name, constraint]])
442
+ version = solution[plugin_name]
443
+ # don't search the remote for the plugin again; we would have already done that by
444
+ # calling versions() and including a {remote: true} option.
445
+ find(plugin_name, version, remote: false)
446
+ end
447
+ rescue Solve::Errors::NoSolutionError
448
+ abort PluginNotFound.new(plugin_name, constraint)
449
+ end
450
+
451
+ # Uninstall an installed plugin
452
+ #
453
+ # @param [String] name
454
+ # Name of the plugin
455
+ # @param [#to_s] version
456
+ # The version of the plugin to uninstall
457
+ #
458
+ # @return [MB::Plugin, nil]
459
+ def uninstall(name, version)
460
+ unless plugin = find(name, version, remote: false)
461
+ return nil
462
+ end
463
+
464
+ FileUtils.rm_rf(install_path_for(plugin))
465
+ remove(plugin)
466
+
467
+ plugin
468
+ end
469
+
470
+ # List all of the versions of the plugin of the given name
471
+ #
472
+ # @param [#to_s] name
473
+ # name of the plugin
474
+ # @param [Boolean] remote (false)
475
+ # include plugins on the remote Chef server in the results
476
+ #
477
+ # @raise [PluginNotFound] if a plugin of the given name has no versions loaded
478
+ #
479
+ # @return [Array<String>]
480
+ def versions(name, remote = false)
481
+ all_versions = installed_versions(name)
482
+
483
+ if remote
484
+ all_versions += remote_versions(name)
485
+ end
486
+
487
+ if all_versions.empty?
488
+ abort PluginNotFound.new(name)
489
+ end
490
+
491
+ all_versions
492
+ end
493
+
494
+ # List all versions of a plugin with the given name that are present on the remote Chef
495
+ # server. An empty array will be returned if no versions are present.
496
+ #
497
+ # @example
498
+ # plugin_manager.remote_versions("nginx") #=> [ "1.2.3", "2.0.0", "3.1.2" ]
499
+ #
500
+ # @param [#to_s] name
501
+ # name of the plugin
502
+ #
503
+ # @return [Array<String>]
504
+ def remote_versions(name)
505
+ remote_cookbook_versions(name).collect do |version|
506
+ (plugin = load_remote(name, version)).nil? ? nil : plugin.version.to_s
507
+ end.compact
508
+ rescue Ridley::Errors::HTTPNotFound
509
+ []
510
+ end
511
+
512
+ protected
513
+
514
+ def reconfigure(_msg, config)
515
+ log.debug { "[Plugin Manager] received new configuration" }
516
+
517
+ unless Berkshelf.path == self.berkshelf_path
518
+ log.debug { "[Plugin Manager] The location of the Berkshelf has changed; reloading plugins" }
519
+
520
+ @berkshelf_path = Berkshelf.path
521
+ MB::Berkshelf.init
522
+ reload_all
523
+ end
524
+ end
525
+
526
+ private
527
+
528
+ def finalize_callback
529
+ log.debug { "Plugin Manager stopping..." }
530
+ end
531
+
532
+ # Load a plugin from a file
533
+ #
534
+ # @param [#to_s] path
535
+ # path to the file to load
536
+ #
537
+ # @option options [Boolean] :force (true)
538
+ # load a plugin even if a plugin of the same name and version is already loaded
539
+ #
540
+ # @raise [PluginSyntaxError] if there was a syntax error in the plugin loaded
541
+ # @raise [PluginLoadError]
542
+ #
543
+ # @return [MB::Plugin]
544
+ # the loaded plugin
545
+ def load_file(path, options = {})
546
+ options = options.reverse_merge(force: true)
547
+ plugin = Plugin.from_path(path.to_s)
548
+
549
+ add(plugin, options)
550
+ plugin
551
+ end
552
+
553
+ # @return [Array<Pathname>]
554
+ def installed_cookbooks
555
+ Berkshelf.cookbooks(with_plugin: true)
556
+ end
557
+
558
+ # List all the versions of the given cookbook on the remote Chef server
559
+ #
560
+ # @param [String] name
561
+ # name of the cookbook to retrieve versions of
562
+ #
563
+ # @return [Array<String>]
564
+ def remote_cookbook_versions(name)
565
+ chef_connection.cookbook.versions(name)
566
+ rescue Ridley::Errors::ResourceNotFound
567
+ []
568
+ end
569
+
570
+ # List all of the cookbooks and their versions present on the remote
571
+ #
572
+ # @example return value
573
+ # {
574
+ # "ant" => [
575
+ # "0.10.1"
576
+ # ],
577
+ # "apache2" => [
578
+ # "1.4.0"
579
+ # ]
580
+ # }
581
+ #
582
+ # @return [Hash]
583
+ # a hash containing keys which represent cookbook names and values which contain
584
+ # an array of strings representing the available versions
585
+ def remote_cookbooks
586
+ chef_connection.cookbook.all
587
+ end
588
+ end
589
+ end