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,12 @@
1
+ module MotherBrain::API
2
+ class V1
3
+ class ConfigEndpoint < MB::API::Endpoint
4
+ helpers MB::Mixin::Services
5
+
6
+ desc "display the loaded configuration"
7
+ get 'config' do
8
+ config_manager.config
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,174 @@
1
+ module MotherBrain::API
2
+ class V1
3
+ class EnvironmentsEndpoint < MB::API::Endpoint
4
+ helpers MB::API::Helpers
5
+
6
+ rescue_from MB::NoBootstrapRoutine do |ex|
7
+ rack_response(ex.to_json, 405, "Content-type" => "application/json")
8
+ end
9
+
10
+ rescue_from MB::InvalidProvisionManifest, MB::InvalidBootstrapManifest do |ex|
11
+ rack_response(ex.to_json, 400, "Content-type" => "application/json")
12
+ end
13
+
14
+ namespace 'environments' do
15
+ desc "list all of the environments"
16
+ get do
17
+ environment_manager.list
18
+ end
19
+
20
+ params do
21
+ requires :environment_id, type: String, desc: "environment name"
22
+ end
23
+ namespace ':environment_id' do
24
+ desc "destroy a provisioned environment"
25
+ delete do
26
+ provisioner.async_destroy(params[:environment_id])
27
+ end
28
+
29
+ desc "create (provision) a new cluster of nodes"
30
+ params do
31
+ requires :manifest, type: Hash, desc: "a Hash representation of the node group to create"
32
+ group :plugin do
33
+ requires :name, type: String, desc: "name of the plugin to use"
34
+ optional :version, sem_ver: true, desc: "version of the plugin to use"
35
+ end
36
+ optional :chef_version, type: String, desc: "version of Chef to install on the node(s)"
37
+ optional :component_versions, type: Hash, desc: "component versions to set with default attributes"
38
+ optional :cookbook_versions, type: Hash, desc: "cookbook versions to set on the environment"
39
+ optional :environment_attributes, type: Hash, desc: "additional attributes to set on the environment"
40
+ optional :skip_bootstrap, type: Boolean, desc: "skip automatic bootstrapping of the created environment"
41
+ optional :force, type: Boolean, desc: "force provisioning nodes to the environment even if the environment is locked"
42
+ end
43
+ post do
44
+ plugin = find_plugin!(params[:plugin][:name], params[:plugin][:version])
45
+ manifest = Provisioner::Manifest.new(params[:manifest])
46
+ manifest.validate!(plugin)
47
+
48
+ provisioner.async_provision(
49
+ params[:environment_id].freeze,
50
+ manifest.freeze,
51
+ plugin.freeze,
52
+ params.except(:environment_id, :manifest, :plugin).freeze
53
+ )
54
+ end
55
+
56
+ desc "update (bootstrap) an existing cluster of nodes"
57
+ params do
58
+ requires :manifest, desc: "a Hash representation of the node group to update"
59
+ group :plugin do
60
+ requires :name, type: String, desc: "name of the plugin to use"
61
+ optional :version, sem_ver: true, desc: "version of the plugin to use"
62
+ end
63
+ optional :chef_version, type: String, desc: "version of Chef to install on the node(s)"
64
+ optional :component_versions, type: Hash, desc: "component versions to set with default attributes"
65
+ optional :cookbook_versions, type: Hash, desc: "cookbook versions to set on the environment"
66
+ optional :environment_attributes, type: Hash, desc: "additional attributes to set on the environment"
67
+ optional :force, type: Boolean
68
+ optional :hints
69
+ end
70
+ put do
71
+ plugin = find_plugin!(params[:plugin][:name], params[:plugin][:version])
72
+ manifest = Bootstrap::Manifest.new(params[:manifest])
73
+ manifest.validate!(plugin)
74
+
75
+ bootstrapper.async_bootstrap(
76
+ params[:environment_id].freeze,
77
+ manifest.freeze,
78
+ plugin.freeze,
79
+ params.slice(:chef_version, :component_versions, :cookbook_versions, :environment_attributes, :force, :bootstrap_proxy, :hints).freeze
80
+ )
81
+ end
82
+
83
+ desc "lock an environment"
84
+ post 'lock' do
85
+ lock_manager.async_lock(params[:environment_id])
86
+ end
87
+
88
+ desc "unlock an environment"
89
+ delete 'lock' do
90
+ lock_manager.async_unlock(params[:environment_id])
91
+ end
92
+
93
+ desc "configure an existing environment cluster"
94
+ params do
95
+ requires :attributes, type: Hash, desc: "a hash of attributes to set on the environment"
96
+ optional :force, type: Boolean, desc: "force configure even if the environment is locked"
97
+ end
98
+ post 'configure' do
99
+ environment_manager.async_configure(params[:environment_id], params.slice(:attributes, :force))
100
+ end
101
+
102
+ desc "upgrade an environment to the specified versions"
103
+ params do
104
+ group :plugin do
105
+ requires :name, type: String, desc: "name of the plugin to use"
106
+ optional :version, sem_ver: true, desc: "version of the plugin to use"
107
+ end
108
+ optional :component_versions, type: Hash, desc: "the component versions to set with default attributes"
109
+ optional :cookbook_versions, type: Hash, desc: "the cookbook versions to set on the environment"
110
+ optional :environment_attributes, type: Hash, desc: "any additional attributes to set on the environment"
111
+ optional :force, type: Boolean, desc: "force upgrade even if the environment is locked"
112
+ end
113
+ post 'upgrade' do
114
+ options = params.slice(:component_versions, :cookbook_versions, :environment_attributes, :force)
115
+ plugin = plugin_manager.find(params[:plugin][:name], params[:plugin][:version])
116
+
117
+ upgrade_manager.async_upgrade(params[:environment_id], plugin, options)
118
+ end
119
+
120
+ params do
121
+ requires :plugin_id, type: String, desc: "plugin name"
122
+ end
123
+ namespace 'commands/:plugin_id' do
124
+ desc "list of commands the plugin associated with the environment supports"
125
+ get do
126
+ plugin_manager.for_environment(params[:plugin_id], params[:environment_id]).commands
127
+ end
128
+
129
+ desc "invoke a plugin level command on the target environment"
130
+ params do
131
+ requires :command_id, type: String, desc: "command name"
132
+ optional :arguments, type: Array, desc: "optional array of arguments for the command"
133
+ optional :force, type: Boolean, desc: "force command an environment even if it is locked"
134
+ end
135
+ post ':command_id' do
136
+ command_invoker.async_invoke(params[:command_id],
137
+ plugin: params[:plugin_id],
138
+ component: params[:component_id],
139
+ environment: params[:environment_id],
140
+ arguments: params[:arguments],
141
+ force: params[:force]
142
+ )
143
+ end
144
+
145
+ desc "list of commands the component of the plugin associated with the environment supports"
146
+ params do
147
+ requires :component_id, type: String, desc: "plugin component name"
148
+ end
149
+ get ':component_id' do
150
+ plugin = plugin_manager.for_environment(params[:plugin_id], params[:environment_id])
151
+ plugin.component!(params[:component_id]).commands
152
+ end
153
+
154
+ desc "invoke a plugin component level command on the target environment"
155
+ params do
156
+ requires :component_id, type: String, desc: "plugin component name"
157
+ requires :command_id, type: String, desc: "command name"
158
+ optional :arguments, type: Array, desc: "optional array of arguments for the command"
159
+ end
160
+ post ':component_id/:command_id' do
161
+ command_invoker.invoke_component(
162
+ params[:plugin_id],
163
+ params[:component_id],
164
+ params[:command_id],
165
+ params[:environment_id],
166
+ params.slice(:arguments)
167
+ )
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,31 @@
1
+ module MotherBrain::API
2
+ class V1
3
+ class JobsEndpoint < MB::API::Endpoint
4
+ helpers MB::API::Helpers
5
+
6
+ rescue_from MB::JobNotFound do |ex|
7
+ rack_response(ex.to_json, 404, "Content-type" => "application/json")
8
+ end
9
+
10
+ namespace 'jobs' do
11
+ desc "list all jobs (completed and active)"
12
+ get do
13
+ job_manager.list
14
+ end
15
+
16
+ desc "list all active jobs"
17
+ get 'active' do
18
+ job_manager.active
19
+ end
20
+
21
+ desc "find and return the Job with the given ID"
22
+ params do
23
+ requires :job_id, type: String, desc: "job id"
24
+ end
25
+ get ':job_id' do
26
+ find_job!(params[:job_id])
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,90 @@
1
+ module MotherBrain::API
2
+ class V1
3
+ class PluginsEndpoint < MB::API::Endpoint
4
+ helpers MB::API::Helpers
5
+ helpers MB::Mixin::Services
6
+
7
+ rescue_from MB::PluginNotFound do |ex|
8
+ rack_response(ex.to_json, 404, "Content-type" => "application/json")
9
+ end
10
+
11
+ rescue_from Solve::Errors::InvalidVersionFormat do |ex|
12
+ rack_response(ex.to_json, 400, "Content-type" => "application/json")
13
+ end
14
+
15
+ namespace 'plugins' do
16
+ desc "list all loaded plugins and their versions"
17
+ get do
18
+ plugin_manager.list
19
+ end
20
+
21
+ params do
22
+ requires :name, type: String, desc: "plugin name"
23
+ end
24
+ namespace ':name' do
25
+ desc "display all the versions of the given plugin"
26
+ get do
27
+ plugin_manager.list(name: params[:name])
28
+ end
29
+
30
+ namespace 'latest' do
31
+ desc "display the latest version of the plugin of the given name"
32
+ get do
33
+ find_plugin!(params[:name])
34
+ end
35
+
36
+ desc "list of all the commands the latest plugin can do"
37
+ get 'commands' do
38
+ find_plugin!(params[:name]).commands
39
+ end
40
+
41
+ namespace 'components' do
42
+ desc "list of all the components the latest plugin has"
43
+ get do
44
+ find_plugin!(params[:name]).components
45
+ end
46
+
47
+ desc "list of all the commands the component of the latest plugin version has"
48
+ params do
49
+ requires :component_id, type: String, desc: "component name"
50
+ end
51
+ get ':component_id/commands' do
52
+ find_plugin!(params[:name]).component(params[:component_id])
53
+ end
54
+ end
55
+ end
56
+
57
+ params do
58
+ requires :plugin_version, type: String, desc: "plugin version"
59
+ end
60
+ namespace ':plugin_version' do
61
+ desc "display the plugin of the given name and version"
62
+ get do
63
+ find_plugin!(params[:name], params[:plugin_version])
64
+ end
65
+
66
+ desc "list of all the commands the specified plugin version can do"
67
+ get 'commands' do
68
+ find_plugin!(params[:name], params[:plugin_version]).commands
69
+ end
70
+
71
+ namespace 'components' do
72
+ desc "list of all the components the specified plugin version has"
73
+ get do
74
+ find_plugin!(params[:name], params[:plugin_version]).components
75
+ end
76
+
77
+ desc "list of all the commands the component of the specified plugin version has"
78
+ params do
79
+ requires :component_id, type: String, desc: "component name"
80
+ end
81
+ get ':component_id/commands' do
82
+ find_plugin!(params[:name], params[:plugin_version]).component!(params[:component_id]).commands
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,5 @@
1
+ module MotherBrain::API
2
+ module Validators
3
+ require_relative 'validators/sem_ver'
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ module MotherBrain::API::Validators
2
+ # Coerce a URI friendly version string into a SemVer string
3
+ #
4
+ # @example coercing a URI friendly string into a SemVer string
5
+ #
6
+ # "1_0_0" => "1.0.0"
7
+ class SemVer < Grape::Validations::Validator
8
+ def validate_param!(attr_name, params)
9
+ return nil if params[attr_name].nil?
10
+
11
+ ver_string = params[attr_name].gsub('_', '.')
12
+ Solve::Version.split(ver_string)
13
+ params[attr_name] = ver_string
14
+ rescue Solve::Errors::InvalidVersionFormat => ex
15
+ throw :error, status: 400, message: ex.to_s
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,148 @@
1
+ # TODO: Fix this when Celluloid does for Ruby 2.0
2
+ # https://github.com/celluloid/celluloid/pull/121
3
+ r, w = IO.pipe
4
+ Thread.new {
5
+ while c = r.read(1)
6
+ case c
7
+ when 's'
8
+ MB.log.info "Shutting down..."
9
+ MB::Application.instance.interrupt
10
+ when 'h'
11
+ MB.log.info "Reloading configuration..."
12
+ MB::ConfigManager.instance.reload
13
+ else
14
+ MB.log.warn "Unknown fake symbol #{c} in fake signal handler!"
15
+ end
16
+ end
17
+ }
18
+ Signal.trap('INT') { w.write('s') }
19
+ Signal.trap('TERM') { w.write('s') }
20
+ Signal.trap('HUP') { w.write('h') } if Signal.supported?('HUP')
21
+
22
+ module MotherBrain
23
+ # Main application supervisor for motherbrain
24
+ #
25
+ # @example running the application in the foreground
26
+ # MB::Application.run(config)
27
+ #
28
+ # @example running the application in the background
29
+ # MB::Application.run!(config)
30
+ module Application
31
+ class << self
32
+ extend Forwardable
33
+ include MB::Mixin::Services
34
+ include MB::Logging
35
+
36
+ def_delegator :upgrade_manager, :upgrade
37
+ def_delegator :config_manager, :config
38
+
39
+ def_delegators :registry, :[], :[]=
40
+
41
+ # @raise [Celluloid::DeadActorError] if Application has not been started
42
+ #
43
+ # @return [Celluloid::SupervisionGroup(MB::Application::SupervisionGroup)]
44
+ def instance
45
+ return @instance unless @instance.nil?
46
+
47
+ raise Celluloid::DeadActorError, "application not running"
48
+ end
49
+
50
+ # The Actor registry for motherbrain.
51
+ #
52
+ # @note motherbrain uses it's own registry instead of Celluloid::Registry.root to
53
+ # avoid conflicts in the larger namespace. Use MB::Application[] to access motherbrain
54
+ # actors instead of Celluloid::Actor[].
55
+ #
56
+ # @return [Celluloid::Registry]
57
+ def registry
58
+ @registry ||= Celluloid::Registry.new
59
+ end
60
+
61
+ # Run the application asynchronously (terminate after execution)
62
+ #
63
+ # @param [MB::Config] config
64
+ def run!(config)
65
+ Celluloid.boot
66
+ log.info { "motherbrain starting..." }
67
+ setup
68
+ @instance = Application::SupervisionGroup.new(config)
69
+ end
70
+
71
+ # Run the application in the foreground (sleep on main thread)
72
+ #
73
+ # @param [MB::Config] config
74
+ def run(config)
75
+ loop do
76
+ supervisor = run!(config)
77
+
78
+ sleep 0.1 while supervisor.alive?
79
+
80
+ break if supervisor.interrupted
81
+
82
+ log.fatal { "!!! #{self} crashed. Restarting..." }
83
+ end
84
+ end
85
+
86
+ # Prepare the application and environment to run motherbrain
87
+ def setup
88
+ MB::Test.mock(:setup) if MB.testing?
89
+ MB::FileSystem.init
90
+ end
91
+
92
+ # Stop the running application
93
+ def stop
94
+ instance.terminate
95
+ end
96
+ end
97
+
98
+ class SupervisionGroup < ::Celluloid::SupervisionGroup
99
+ include Celluloid::Notifications
100
+ include MB::Logging
101
+
102
+ attr_reader :interrupted
103
+
104
+ def initialize(config)
105
+ super(MB::Application.registry)
106
+
107
+ supervise_as(:config_manager, MB::ConfigManager, config)
108
+ supervise_as(:ridley, Ridley::Client, config.to_ridley)
109
+ supervise_as(:job_manager, MB::JobManager)
110
+ supervise_as(:lock_manager, MB::LockManager)
111
+ supervise_as(:plugin_manager, MB::PluginManager)
112
+ supervise_as(:command_invoker, MB::CommandInvoker)
113
+ supervise_as(:node_querier, MB::NodeQuerier)
114
+ supervise_as(:environment_manager, MB::EnvironmentManager)
115
+ supervise_as(:bootstrap_manager, MB::Bootstrap::Manager)
116
+ supervise_as(:provisioner_manager, MB::Provisioner::Manager)
117
+ supervise_as(:upgrade_manager, MB::Upgrade::Manager)
118
+
119
+ if config.rest_gateway.enable
120
+ supervise_as(:rest_gateway, MB::RestGateway, config.to_rest_gateway)
121
+ end
122
+
123
+ @interrupt_mutex = Mutex.new
124
+ @interrupted = false
125
+ subscribe(ConfigManager::UPDATE_MSG, :reconfigure)
126
+ MB::Test.mock(:init) if MB.testing?
127
+ end
128
+
129
+ def reconfigure(_msg, new_config)
130
+ log.debug { "[Application] ConfigManager has changed: re-configuring components..." }
131
+ @registry[:ridley].async.configure(new_config.to_ridley)
132
+ end
133
+
134
+ def interrupt
135
+ interrupt_mutex.synchronize do
136
+ unless interrupted
137
+ @interrupted = true
138
+ terminate
139
+ end
140
+ end
141
+ end
142
+
143
+ private
144
+
145
+ attr_reader :interrupt_mutex
146
+ end
147
+ end
148
+ end