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__)}/sub_commands/*.rb"].sort.each do |path|
2
+ require "mb/cli_gateway/sub_commands/#{File.basename(path, '.rb')}"
3
+ end
@@ -0,0 +1,124 @@
1
+ module MotherBrain
2
+ class CliGateway
3
+ module SubCommand
4
+ class Environment < Cli::Base
5
+ namespace :environment
6
+
7
+ method_option :force,
8
+ type: :boolean,
9
+ default: false,
10
+ desc: "perform the configuration even if the environment is locked",
11
+ aliases: "-f"
12
+ desc "configure ENVIRONMENT FILE", "configure a Chef environment"
13
+ def configure(environment, attributes_file)
14
+ attributes_file = File.expand_path(attributes_file)
15
+
16
+ begin
17
+ content = File.read(attributes_file)
18
+ rescue Errno::ENOENT
19
+ ui.say "No attributes file found at: '#{attributes_file}'"
20
+ exit(1)
21
+ end
22
+
23
+ begin
24
+ attributes = MultiJson.decode(content)
25
+ rescue MultiJson::DecodeError => ex
26
+ ui.say "Error decoding JSON from: '#{attributes_file}'"
27
+ ui.say ex
28
+ exit(1)
29
+ end
30
+
31
+ job = environment_manager.async_configure(environment, attributes: attributes, force: options[:force])
32
+
33
+ CliClient.new(job).display
34
+ end
35
+
36
+ method_option :yes,
37
+ type: :boolean,
38
+ default: false,
39
+ desc: "Don't confirm, just destroy the environment",
40
+ aliases: '-y'
41
+ method_option :force,
42
+ type: :boolean,
43
+ default: false,
44
+ desc: "Force destruction of a locked environment",
45
+ aliases: '-f'
46
+ method_option :provisioner,
47
+ type: :string,
48
+ desc: "Provisioner to use for destroying the environment"
49
+ desc "destroy ENVIRONMENT", "Destroy a provisioned environment"
50
+ def destroy(environment)
51
+ options[:with] = options[:provisioner] # TODO: rename with to provisioner
52
+ options[:yes] ||= options[:force]
53
+ destroy_options = Hash.new.merge(options).deep_symbolize_keys
54
+
55
+ if !options[:force] && ChefMutex.new(chef_environment: environment).locked?
56
+ raise MB::ResourceLocked,
57
+ "The environment \"#{environment}\" is locked. You may use --force to override this safeguard."
58
+ end
59
+
60
+ dialog = "This will destroy the '#{environment}' environment.\nAre you sure? (yes|no): "
61
+ really_destroy = options[:yes] || ui.yes?(dialog)
62
+
63
+ if really_destroy
64
+ job = provisioner.async_destroy(environment, destroy_options)
65
+ CliClient.new(job).display
66
+ else
67
+ ui.say("Aborting destruction of '#{environment}'")
68
+ end
69
+ end
70
+
71
+ desc "list", "List all environments"
72
+ def list
73
+ ui.say "\n"
74
+ ui.say "** listing environments"
75
+ ui.say "\n"
76
+
77
+ environment_manager.list.each do |env|
78
+ ui.say env.name
79
+ end
80
+ end
81
+
82
+ desc "lock ENVIRONMENT", "Lock an environment"
83
+ def lock(environment)
84
+ job = lock_manager.async_lock(environment)
85
+
86
+ CliClient.new(job).display
87
+ end
88
+
89
+ desc "unlock ENVIRONMENT", "Unlock an environment"
90
+ def unlock(environment)
91
+ job = lock_manager.async_unlock(environment)
92
+
93
+ CliClient.new(job).display
94
+ end
95
+
96
+ desc "from FILE", "Create an environment from JSON in a file"
97
+ def from(environment_file)
98
+ ui.say "Creating environment from #{environment_file}"
99
+
100
+ begin
101
+ environment_manager.create_from_file(environment_file)
102
+ rescue => e
103
+ ui.error e.message
104
+ exit(1)
105
+ end
106
+ end
107
+
108
+ desc "create ENVIRONMENT", "Create an empty environment"
109
+ def create(environment)
110
+ ui.say "Creating empty environment #{environment}"
111
+
112
+ begin
113
+ environment_manager.create(environment)
114
+ rescue => e
115
+ ui.error e.message
116
+ exit(1)
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ register(SubCommand::Environment, :environment, "environment", "Environment level commands")
123
+ end
124
+ end
@@ -0,0 +1,148 @@
1
+ module MotherBrain
2
+ class CliGateway
3
+ module SubCommand
4
+ class Plugin < Cli::Base
5
+ namespace :plugin
6
+
7
+ source_root MB.app_root.join('templates')
8
+
9
+ desc "init [PATH]", "Generate a new motherbrain plugin in the target cookbook"
10
+ def init(path = Dir.pwd)
11
+ metadata = File.join(path, 'metadata.rb')
12
+
13
+ unless File.exist?(metadata)
14
+ ui.say "#{path} is not a cookbook"
15
+ exit(1)
16
+ end
17
+
18
+ cookbook = CookbookMetadata.from_file(metadata)
19
+ config = { name: cookbook.name, groups: %w[default] }
20
+ template 'bootstrap.json', File.join(path, 'bootstrap.json'), config
21
+ template 'motherbrain.rb', File.join(path, 'motherbrain.rb'), config
22
+
23
+ ui.say [
24
+ "",
25
+ "motherbrain plugin created.",
26
+ "",
27
+ "Take a look at motherbrain.rb and bootstrap.json,",
28
+ "and then bootstrap with:",
29
+ "",
30
+ " mb #{cookbook.name} bootstrap bootstrap.json",
31
+ "",
32
+ "To see all available commands, run:",
33
+ "",
34
+ " mb #{cookbook.name} help",
35
+ "\n"
36
+ ].join("\n")
37
+ end
38
+
39
+ method_option :version,
40
+ type: :string,
41
+ desc: "The version of the plugin to install"
42
+ desc "install NAME", "Install a plugin from the remote Chef server"
43
+ def install(name)
44
+ plugin = plugin_manager.install(name, options[:version])
45
+ ui.say "Successfully installed #{plugin}"
46
+ end
47
+
48
+ method_option :remote,
49
+ type: :boolean,
50
+ default: false,
51
+ desc: "search the remote Chef server and include plugins from the results"
52
+ desc "list", "List all installed plugins"
53
+ def list
54
+ if options[:remote]
55
+ ui.say "\n"
56
+ ui.say "** listing installed and remote plugins..."
57
+ ui.say "\n"
58
+ else
59
+ ui.say "\n"
60
+ ui.say "** listing installed plugins...\n"
61
+ ui.say "\n"
62
+ end
63
+
64
+ plugins = plugin_manager.list(remote: options[:remote])
65
+
66
+ if plugins.empty?
67
+ errmsg = "No plugins found in your Berkshelf: '#{Application.plugin_manager.berkshelf_path}'"
68
+
69
+ if options[:remote]
70
+ errmsg << " or on remote: '#{Application.config.chef.api_url}'"
71
+ end
72
+
73
+ ui.say errmsg
74
+ exit(0)
75
+ end
76
+
77
+ plugins.group_by(&:name).each do |name, plugins|
78
+ versions = plugins.collect(&:version).reverse!
79
+ ui.say "#{name}: #{versions.join(', ')}"
80
+ end
81
+ end
82
+
83
+ method_option :version,
84
+ type: :string,
85
+ desc: "The version of the plugin to display information for"
86
+ method_option :remote,
87
+ type: :boolean,
88
+ default: false,
89
+ desc: "Include plugins found on the remote Chef server"
90
+ desc "show NAME", "Show information about a plugin"
91
+ def show(name)
92
+ unless plugin = plugin_manager.find(name, options[:version], remote: options[:remote])
93
+ if options[:version]
94
+ errmsg = "#{name} (#{options[:version]}) not"
95
+ else
96
+ errmsg = "No versions of #{name}"
97
+ end
98
+ errmsg << " found in your Berkshelf"
99
+ errmsg << " or on the remote Chef server" if options[:remote]
100
+ ui.say errmsg
101
+ ui.say "You can search the Chef server with '--remote'" unless options[:remote]
102
+ exit(1)
103
+ end
104
+
105
+ pp_plugin(plugin)
106
+ end
107
+
108
+ method_option :version,
109
+ type: :string,
110
+ desc: "The version of the plugin to install",
111
+ required: true
112
+ desc "uninstall NAME", "Uninstall a plugin from your Berkshelf"
113
+ def uninstall(name)
114
+ if plugin = plugin_manager.uninstall(name, options[:version])
115
+ ui.say "Successfully uninstalled #{plugin}"
116
+ else
117
+ ui.say "#{name} (#{options[:version]}) was not installed"
118
+ end
119
+ end
120
+
121
+ no_tasks do
122
+ def pp_plugin(plugin)
123
+ info = [].tap do |a|
124
+ a.push " Name: #{plugin.name}"
125
+ a.push " Version: #{plugin.version}"
126
+ a.push " Description: #{plugin.description}" unless plugin.description.blank?
127
+ a.push " Author: #{plugin.maintainer}" unless plugin.maintainer.blank?
128
+ a.push " Email: #{plugin.maintainer_email}" unless plugin.maintainer_email.blank?
129
+ a.push " License: #{plugin.license}" unless plugin.license.blank?
130
+ a.push " Commands: #{pp_enumerable(plugin.commands, 14)}" unless plugin.commands.blank?
131
+ a.push " Components: #{pp_enumerable(plugin.components, 14)}" unless plugin.components.blank?
132
+ end.join("\n")
133
+
134
+ ui.say info
135
+ end
136
+ end
137
+
138
+ private
139
+
140
+ def pp_enumerable(enum, padding)
141
+ enum.map { |item| "#{item}" }.join("\n" + ' '*padding)
142
+ end
143
+ end
144
+ end
145
+
146
+ register(SubCommand::Plugin, :plugin, "plugin", "Plugin level commands")
147
+ end
148
+ end
data/lib/mb/command.rb ADDED
@@ -0,0 +1,88 @@
1
+ module MotherBrain
2
+ class Command
3
+ include VariaModel
4
+ include MB::Mixin::Locks
5
+
6
+ attribute :name,
7
+ type: String,
8
+ required: true
9
+
10
+ attribute :description,
11
+ type: String,
12
+ required: true
13
+
14
+ attribute :execute,
15
+ type: Proc,
16
+ required: true
17
+
18
+ # @return [MB::Plugin, MB::Component]
19
+ attr_reader :scope
20
+ # @return [MB::Plugin]
21
+ attr_reader :plugin
22
+ # @return [Symbol]
23
+ attr_reader :type
24
+
25
+ # @param [#to_s] name
26
+ # @param [MB::Plugin, MB::Component] scope
27
+ def initialize(name, scope, &block)
28
+ set_attribute(:name, name.to_s)
29
+ @scope = scope
30
+
31
+ case @scope
32
+ when MB::Plugin
33
+ @plugin = @scope
34
+ @type = :plugin
35
+ when MB::Component
36
+ @plugin = @scope.plugin
37
+ @type = :component
38
+ else
39
+ raise RuntimeError, "no matching command type for the given scope: #{scope}."
40
+ end
41
+
42
+ if block_given?
43
+ dsl_eval(&block)
44
+ end
45
+ end
46
+
47
+ # @return [String]
48
+ def description
49
+ _attributes_.description || "run #{name} command on #{scope.name}"
50
+ end
51
+
52
+ # @return [Symbol]
53
+ def id
54
+ self.name.to_sym
55
+ end
56
+
57
+ # Run the command on the given environment
58
+ #
59
+ # @param [MB::Job] job
60
+ # a job to update with progress
61
+ # @param [String] environment
62
+ # the environment to invoke the command on
63
+ # @param [Array] node_filter
64
+ # list of nodes to limit the command to
65
+ # @param [Array] args
66
+ # additional arguments to pass to the command
67
+ #
68
+ # @raise [MB::ChefConnectionError] if there was an error communicating to the Chef Server
69
+ def invoke(job, environment, node_filter, *args)
70
+ CommandRunner.new(job, environment, scope, execute, node_filter, *args)
71
+ end
72
+
73
+ private
74
+
75
+ def dsl_eval(&block)
76
+ CleanRoom.new(self).instance_eval(&block)
77
+ end
78
+
79
+ # @api private
80
+ class CleanRoom < CleanRoomBase
81
+ dsl_attr_writer :description
82
+
83
+ def execute(&block)
84
+ real_model.execute = block
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,235 @@
1
+ module MotherBrain
2
+ class CommandInvoker
3
+ autoload :Worker, 'mb/command_invoker/worker'
4
+
5
+ class << self
6
+ # @raise [Celluloid::DeadActorError] if command invoker has not been started
7
+ #
8
+ # @return [Celluloid::Actor(CommandInvoker)]
9
+ def instance
10
+ MB::Application[:command_invoker] or raise Celluloid::DeadActorError, "command invoker not running"
11
+ end
12
+ end
13
+
14
+ include Celluloid
15
+ include MB::Logging
16
+ include MB::Mixin::Services
17
+ include MB::Mixin::Locks
18
+
19
+ finalizer :finalize_callback
20
+
21
+ def initialize
22
+ log.debug { "Command Invoker starting..." }
23
+ end
24
+
25
+ # Asynchronously invoke a command on a plugin or a component of a plugin.
26
+ #
27
+ # @param [String] command_name
28
+ # Name of the command to invoke
29
+ #
30
+ # @option options [String] :plugin
31
+ # @option options [String] :component (optional)
32
+ # @option options [String] :version (optional)
33
+ # @option options [String] :environment
34
+ # @option options [Array] :arguments (Array.new)
35
+ # @option options [Boolean] :force (false)
36
+ #
37
+ # @return [MB::Job]
38
+ def async_invoke(command_name, options = {})
39
+ job = Job.new(:command)
40
+ async(:invoke, job, command_name, options)
41
+ job.ticket
42
+ end
43
+
44
+ # Find a plugin or component level which has already been loaded by a plugin.
45
+ #
46
+ # @param [String] command_name
47
+ # Name of the command to find
48
+ # @param [String] plugin_name
49
+ # The name of the plugin that the command you are looking for belongs to
50
+ #
51
+ # @option options [String] :component (optional)
52
+ # Name of the component that this command belongs to. If no component name is specified then
53
+ # it is assumed that you are searching for a plugin level command and not a component level
54
+ # command.
55
+ # @option options [String] :environment (optional)
56
+ # The environment the command will be executed on. The best version of the command to run on
57
+ # that environment will be returned. If no environment is specified the latest version of the
58
+ # command will be returned.
59
+ # @option options [String] :version (optional)
60
+ # The specific version of the plugin you are looking for the command on
61
+ #
62
+ # @raise [MB::CommandNotFound]
63
+ # @raise [MB::PluginNotFound]
64
+ # @raise [MB::ComponentNotFound]
65
+ # @raise [MB::EnvironmentNotFound]
66
+ #
67
+ # @return [MB::Command]
68
+ def find(command_name, plugin_name, options = {})
69
+ if options[:version]
70
+ for_version(options[:version], command_name, plugin_name, options[:component])
71
+ elsif options[:environment]
72
+ for_environment(options[:environment], command_name, plugin_name, options[:component])
73
+ else
74
+ find_latest(command_name, plugin_name, options[:component])
75
+ end
76
+ end
77
+
78
+ # Invoke a command on a plugin or a component of a plugin
79
+ #
80
+ # @param [MB::Job] job
81
+ # A job to update with progress
82
+ # @param [String] command_name
83
+ # Name of the command to invoke
84
+ #
85
+ # @option options [String] :plugin
86
+ # @option options [String] :component (optional)
87
+ # @option options [String] :environment
88
+ # @option options [String] :version (optional)
89
+ # @option options [Array] :arguments (Array.new)
90
+ # @option options [Boolean] :force (false)
91
+ # @option options [Array] :node_filter (nil)
92
+ #
93
+ # @raise [MB::ArgumentError]
94
+ #
95
+ # @return [Boolean]
96
+ def invoke(job, command_name, options = {})
97
+ options = options.reverse_merge(arguments: Array.new, force: false)
98
+
99
+ job.report_running
100
+
101
+ if options[:plugin].nil?
102
+ job.report_failure MB::ArgumentError.new("must specify a plugin that the command belongs to")
103
+ return false
104
+ end
105
+
106
+ if options[:environment].nil?
107
+ job.report_failure MB::ArgumentError.new("must specify an environment to run this command on")
108
+ return false
109
+ end
110
+
111
+ job.set_status("Finding environment #{options[:environment]}")
112
+ environment_manager.find(options[:environment])
113
+
114
+ command = find(command_name, options[:plugin], options.slice(:component, :environment, :version))
115
+ worker = Worker.new(command, options[:environment], options[:node_filter])
116
+
117
+ chef_synchronize(chef_environment: options[:environment], force: options[:force], job: job) do
118
+ worker.run(job, options[:arguments])
119
+ end
120
+
121
+ job.report_success("successfully executed command")
122
+ true
123
+ rescue => ex
124
+ job.report_failure(ex)
125
+ false
126
+ ensure
127
+ job.terminate if job && job.alive?
128
+ worker.terminate if worker && worker.alive?
129
+ end
130
+
131
+ private
132
+
133
+ def finalize_callback
134
+ log.debug { "Command Invoker stopping..." }
135
+ end
136
+
137
+ # Return a plugin level or component level command from the given plugin based on
138
+ # the arguments given.
139
+ #
140
+ # * If a component name is not included it is assumed the command to return is a plugin level command.
141
+ # * If a component name is included it is assumed that the command to return is a component level command.
142
+ #
143
+ # @param [MB::Plugin] plugin
144
+ # Plugin to find the command on
145
+ # @param [String] command_name
146
+ # Name of the command to find
147
+ # @param [String] component_name
148
+ # Name of the component of the plugin that the command belongs to
149
+ #
150
+ # @raise [MB::CommandNotFound]
151
+ # @raise [MB::PluginNotFound]
152
+ # @raise [MB::ComponentNotFound]
153
+ #
154
+ # @return [MB::Command]
155
+ def command_for(plugin, command_name, component_name = nil)
156
+ if component_name.nil?
157
+ plugin.command!(command_name)
158
+ else
159
+ plugin.component!(component_name).command!(command_name)
160
+ end
161
+ rescue PluginNotFound, CommandNotFound, ComponentNotFound => ex
162
+ abort(ex)
163
+ end
164
+
165
+ # Return the latest version of a plugin or component level command the given command name, plugin name,
166
+ # and optional component name.
167
+ #
168
+ # @param [String] command_name
169
+ # Name of the command to find
170
+ # @param [String] plugin_name
171
+ # Name of the plugin the command you are looking for belongs to
172
+ # @param [String] component_name
173
+ # Name of the component of the plugin that the command belongs to
174
+ #
175
+ # @raise [MB::CommandNotFound]
176
+ # @raise [MB::PluginNotFound]
177
+ # @raise [MB::ComponentNotFound]
178
+ #
179
+ # @return [MB::Command]
180
+ def find_latest(command_name, plugin_name, component_name = nil)
181
+ plugin = plugin_manager.latest(plugin_name)
182
+ command_for(plugin, command_name, component_name)
183
+ end
184
+
185
+ # Return the best version of a plugin or component level command the given command name, plugin name,
186
+ # and optional component name to use when communicating with the given environment.
187
+ #
188
+ # @param [String] environment_name
189
+ # name of the environment
190
+ # @param [String] command_name
191
+ # Name of the command to find
192
+ # @param [String] plugin_name
193
+ # Name of the plugin the command you are looking for belongs to
194
+ # @param [String] component_name
195
+ # Name of the component of the plugin that the command belongs to
196
+ #
197
+ # @raise [MB::EnvironmentNotFound]
198
+ # @raise [MB::CommandNotFound]
199
+ # @raise [MB::PluginNotFound]
200
+ # @raise [MB::ComponentNotFound]
201
+ #
202
+ # @return [MB::Command]
203
+ def for_environment(environment_name, command_name, plugin_name, component_name = nil)
204
+ plugin = plugin_manager.for_environment(plugin_name, environment_name)
205
+ command_for(plugin, command_name, component_name)
206
+ end
207
+
208
+ # Return the specific version of a plugin or component level command the given command name, plugin name,
209
+ # and optional component name.
210
+ #
211
+ # @param [String] plugin_version
212
+ # Version of the plugin to find the command on
213
+ # @param [String] command_name
214
+ # Name of the command to find
215
+ # @param [String] plugin_name
216
+ # Name of the plugin to find the command on
217
+ # @param [String] component_name
218
+ # Name of the component of the plugin that the command belongs to
219
+ #
220
+ # @raise [MB::CommandNotFound]
221
+ # @raise [MB::PluginNotFound]
222
+ # @raise [MB::ComponentNotFound]
223
+ #
224
+ # @return [MB::Command]
225
+ def for_version(plugin_version, command_name, plugin_name, component_name = nil)
226
+ plugin = plugin_manager.find(plugin_name, plugin_version)
227
+
228
+ if plugin.nil?
229
+ abort MB::PluginNotFound.new(plugin_name, plugin_version)
230
+ end
231
+
232
+ command_for(plugin, command_name, component_name)
233
+ end
234
+ end
235
+ end