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,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe MotherBrain::Provisioner::ProvisionData do
4
+ subject { provision_data }
5
+
6
+ let(:provision_data) { described_class.new(environment_name) }
7
+
8
+ let(:data_bag) { ridley.data_bag.find(described_class::DATA_BAG) }
9
+ let(:data_bag_attributes) { data_bag_item.attributes }
10
+ let(:data_bag_item) { data_bag.item.find(environment_name) }
11
+ let(:environment_name) { "test" }
12
+ let(:instances) {
13
+ [
14
+ {
15
+ instance_id: "i-abcdefb1",
16
+ instance_type: "m1.large",
17
+ public_hostname: "box1.cloud.example.com"
18
+ },
19
+ {
20
+ instance_id: "i-abcdefb2",
21
+ instance_type: "m1.large",
22
+ public_hostname: "box2.cloud.example.com"
23
+ }
24
+ ]
25
+ }
26
+ let(:provisioner_name) { "aws" }
27
+
28
+ describe "#instances" do
29
+ it "returns an empty hash by default" do
30
+ expect(provision_data.instances).to eq({})
31
+ end
32
+
33
+ it "saves an empty hash to the data bag" do
34
+ provision_data.instances
35
+
36
+ provision_data.save
37
+
38
+ expect(data_bag_attributes[:instances]).to eq({})
39
+ end
40
+ end
41
+
42
+ describe "#add_instances_to_provisioner" do
43
+ it "adds the instances to the data bag" do
44
+ provision_data.add_instances_to_provisioner provisioner_name, instances
45
+
46
+ provision_data.save
47
+
48
+ expect(
49
+ data_bag_attributes[:instances][provisioner_name].map(&:to_hash).map(&:symbolize_keys)
50
+ ).to match_array(instances)
51
+ end
52
+
53
+ it "is idempotent" do
54
+ provision_data.add_instances_to_provisioner provisioner_name, instances
55
+ provision_data.add_instances_to_provisioner provisioner_name, instances
56
+
57
+ provision_data.save
58
+
59
+ expect(
60
+ data_bag_attributes[:instances][provisioner_name].map(&:to_hash).map(&:symbolize_keys)
61
+ ).to match_array(instances)
62
+ end
63
+ end
64
+
65
+ describe "#remove_instance_from_provisioner" do
66
+ it "removes an instance from a provisioner by key/value pair" do
67
+ provision_data.add_instances_to_provisioner provisioner_name, instances
68
+ provision_data.remove_instance_from_provisioner provisioner_name,
69
+ :instance_id, instances.first[:instance_id]
70
+
71
+ expect(provision_data.instances[provisioner_name]).to match_array([instances.last])
72
+ end
73
+ end
74
+
75
+ describe "#provisioners" do
76
+ it "lists all provisioners with instances for this environment" do
77
+ provision_data.add_instances_to_provisioner provisioner_name, instances
78
+
79
+ expect(provision_data.provisioners).to match_array([provisioner_name])
80
+ end
81
+ end
82
+
83
+ describe "#instances_for_provisioner" do
84
+ it "returns an empty array by default" do
85
+ expect(provision_data.instances_for_provisioner(:aws)).to eq([])
86
+ end
87
+
88
+ it "returns all instances for a provisioner" do
89
+ provision_data.add_instances_to_provisioner provisioner_name, instances
90
+
91
+ expect(
92
+ provision_data.instances_for_provisioner(:aws)
93
+ ).to match_array(instances)
94
+ end
95
+ end
96
+
97
+ describe "#save" do
98
+ it "creates a data bag item" do
99
+ provision_data.save
100
+
101
+ expect(data_bag_item).to_not be_nil
102
+ end
103
+ end
104
+
105
+ describe "#destroy" do
106
+ it "creates a data bag item" do
107
+ provision_data.save
108
+ provision_data.destroy
109
+
110
+ expect(data_bag_item).to be_nil
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,251 @@
1
+ require 'spec_helper'
2
+
3
+ describe MB::Provisioner do
4
+ subject { described_class }
5
+
6
+ before(:each) do
7
+ @original = described_class.all
8
+ described_class.clear!
9
+ end
10
+
11
+ after(:each) do
12
+ described_class.clear!
13
+ @original.each { |k| described_class.register(k) }
14
+ end
15
+
16
+ describe "::all" do
17
+ it "returns a set" do
18
+ subject.all.should be_a(Set)
19
+ end
20
+ end
21
+
22
+ describe "::register" do
23
+ let(:provisioner_class) do
24
+ Class.new(described_class::Base) do
25
+ @provisioner_id = :hello
26
+ end
27
+ end
28
+
29
+ it "adds the given class to the list of registered provisioners" do
30
+ subject.register(provisioner_class)
31
+
32
+ subject.all.should have(1).item
33
+ end
34
+
35
+ describe "registered class" do
36
+ it "has the correct provisioner_id" do
37
+ subject.register(provisioner_class)
38
+
39
+ subject.all.first.provisioner_id.should eql(:hello)
40
+ end
41
+ end
42
+
43
+ context "given a class that does not respond to provisioner_id" do
44
+ let(:provisioner_class) { Class.new }
45
+
46
+ it "raises an InvalidProvisionerClass exception" do
47
+ expect {
48
+ subject.register(provisioner_class)
49
+ }.to raise_error(MB::InvalidProvisionerClass)
50
+ end
51
+ end
52
+
53
+ context "given a class with a nil value for provisioner_id" do
54
+ let(:provisioner_class) do
55
+ Class.new(described_class::Base) do
56
+ @provisioner_id = nil
57
+ end
58
+ end
59
+
60
+ it "raises an InvalidProvisionerClass exception" do
61
+ expect {
62
+ subject.register(provisioner_class)
63
+ }.to raise_error(MB::InvalidProvisionerClass)
64
+ end
65
+ end
66
+
67
+ context "when a provisioner with the given id has already been registered" do
68
+ it "raises an ProvisionerRegistrationError" do
69
+ rspec_provisioner = Class.new(described_class::Base) do
70
+ register_provisioner :rspec_provisioner
71
+ end
72
+
73
+ expect {
74
+ Class.new(described_class::Base) do
75
+ register_provisioner :rspec_provisioner
76
+ end
77
+ }.to raise_error(MB::ProvisionerRegistrationError)
78
+ end
79
+ end
80
+
81
+ context "given the :default option set to true" do
82
+ it "sets the given class as the default class" do
83
+ rspec_provisioner = Class.new(described_class::Base) do
84
+ register_provisioner :rspec_provisioner, default: true
85
+ end
86
+
87
+ subject.default.should eql(rspec_provisioner)
88
+ end
89
+
90
+ it "raises if there is already a default class" do
91
+ rspec_provisioner = Class.new(described_class::Base) do
92
+ register_provisioner :rspec_provisioner, default: true
93
+ end
94
+
95
+ expect {
96
+ Class.new(described_class::Base) do
97
+ register_provisioner :rspec_provisioner_two, default: true
98
+ end
99
+ }.to raise_error(MB::ProvisionerRegistrationError)
100
+ end
101
+ end
102
+ end
103
+
104
+ describe "::get" do
105
+ it "returns the provisioner class with the given provisioner_id" do
106
+ rspec_provisioner = Class.new(described_class::Base) do
107
+ register_provisioner :rspec_provisioner
108
+ end
109
+
110
+ subject.get(:rspec_provisioner).should eql(rspec_provisioner)
111
+ end
112
+ end
113
+
114
+ describe "::default" do
115
+ context "given there is a default provisioner class" do
116
+ it "returns the default provisioner class" do
117
+ rspec_provisioner = Class.new(described_class::Base) do
118
+ register_provisioner :rspec_provisioner, default: true
119
+ end
120
+
121
+ subject.default.should eql(rspec_provisioner)
122
+ end
123
+ end
124
+
125
+ context "when there is no default provisioner class" do
126
+ it "returns nil" do
127
+ subject.default.should be_nil
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ describe MB::Provisioner::Base do
134
+ describe "ClassMethods" do
135
+ subject { described_class }
136
+
137
+ describe "::validate_create" do
138
+ it "does not raise an error if the number of nodes in the response matches the expected in manifest" do
139
+ manifest = MB::Provisioner::Manifest.new.from_json(
140
+ {
141
+ nodes: [
142
+ {
143
+ type: "x1.large",
144
+ count: 2,
145
+ components: ["activemq::master"]
146
+ },
147
+ {
148
+ type: "x1.small",
149
+ count: 1,
150
+ components: ["nginx::server"]
151
+ }
152
+ ]
153
+ }.to_json)
154
+
155
+ response = [
156
+ {
157
+ name: "a1.riotgames.com",
158
+ instance_type: "x1.large"
159
+ },
160
+ {
161
+ name: "a2.riotgames.com",
162
+ instance_type: "x1.large"
163
+ },
164
+ {
165
+ name: "a3.riotgames.com",
166
+ instance_type: "x1.small"
167
+ }
168
+ ]
169
+
170
+ expect {
171
+ subject.validate_create(response, manifest)
172
+ }.to_not raise_error
173
+ end
174
+
175
+ it "raises an error if there are less nodes than the manifest expects" do
176
+ manifest = MB::Provisioner::Manifest.new.from_json({
177
+ nodes: [
178
+ {
179
+ type: "x1.large",
180
+ count: 2,
181
+ components: ["activemq::master"]
182
+ },
183
+ {
184
+ type: "x1.small",
185
+ components: ["nginx::server"]
186
+ }
187
+ ]
188
+ }.to_json)
189
+
190
+ response = [
191
+ {
192
+ name: "a1.riotgames.com",
193
+ instance_type: "x1.large"
194
+ }
195
+ ]
196
+
197
+ expect {
198
+ subject.validate_create(response, manifest)
199
+ }.to raise_error(MB::UnexpectedProvisionCount)
200
+ end
201
+
202
+ it "raises an error if there are more nodes than the manifest expects" do
203
+ manifest = MB::Provisioner::Manifest.new.from_json({
204
+ nodes: [
205
+ {
206
+ type: "x1.large",
207
+ components: ["activemq::master"]
208
+ }
209
+ ]
210
+ }.to_json)
211
+
212
+ response = [
213
+ {
214
+ name: "a1.riotgames.com",
215
+ instance_type: "x1.large"
216
+ },
217
+ {
218
+ name: "a2.riotgames.com",
219
+ instance_type: "x1.large"
220
+ }
221
+ ]
222
+
223
+ expect {
224
+ subject.validate_create(response, manifest)
225
+ }.to raise_error(MB::UnexpectedProvisionCount)
226
+ end
227
+ end
228
+ end
229
+
230
+ subject { described_class.new }
231
+
232
+ describe "#up" do
233
+ let(:job) { double('job') }
234
+ let(:env_name) { "rpsec" }
235
+ let(:manifest) { double('manifest') }
236
+ let(:plugin) { double('plugin') }
237
+
238
+ it "raises an AbstractFunction error" do
239
+ expect { subject.up(job, env_name, manifest, plugin) }.to raise_error(MB::AbstractFunction)
240
+ end
241
+ end
242
+
243
+ describe "#down" do
244
+ let(:job) { double('job') }
245
+ let(:env_name) { "rpsec" }
246
+
247
+ it "raises an AbstractFunction error" do
248
+ expect { subject.down(job, env_name) }.to raise_error(MB::AbstractFunction)
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,392 @@
1
+ require 'spec_helper'
2
+
3
+ describe MB::Provisioner::AWS do
4
+ subject { aws }
5
+ let(:aws) { described_class.new }
6
+
7
+ let(:job) { double('job') }
8
+
9
+ before :all do
10
+ Fog::Mock.delay = 0
11
+ Fog.mock!
12
+ end
13
+
14
+ before :each do
15
+ aws.stub :sleep
16
+ job.stub :set_status
17
+ end
18
+
19
+ let(:environment_name) { "mbtest" }
20
+ let(:plugin) { double('plugin') }
21
+
22
+ let(:manifest) do
23
+ MB::Provisioner::Manifest.new.from_json({
24
+ options: {
25
+ access_key: "ABCDEFG",
26
+ secret_key: "abcdefgh123456789",
27
+ endpoint: "http://euca.example.com/services/Eucalyptus",
28
+ image_id: "emi-1234ABCD",
29
+ key_name: "mb",
30
+ security_groups: ["foo", "bar"],
31
+ availability_zone: "riot1"
32
+ },
33
+ nodes: [
34
+ {
35
+ type: "m1.large",
36
+ count: 4,
37
+ components: ["activemq::master"]
38
+ },
39
+ {
40
+ type: "m1.large",
41
+ count: 2,
42
+ components: ["activemq::slave"]
43
+ },
44
+ {
45
+ type: "m1.small",
46
+ count: 2,
47
+ components: ["nginx::server"]
48
+ }
49
+ ]
50
+ }.to_json)
51
+ end
52
+
53
+ let(:response) do
54
+ [
55
+ {instance_type: "m1.large", public_hostname: "ex1.cloud.example.com"},
56
+ {instance_type: "m1.large", public_hostname: "ex2.cloud.example.com"},
57
+ {instance_type: "m1.large", public_hostname: "ex3.cloud.example.com"},
58
+ {instance_type: "m1.large", public_hostname: "ex4.cloud.example.com"},
59
+ {instance_type: "m1.large", public_hostname: "ex5.cloud.example.com"},
60
+ {instance_type: "m1.large", public_hostname: "ex6.cloud.example.com"},
61
+ {instance_type: "m1.small", public_hostname: "ex7.cloud.example.com"},
62
+ {instance_type: "m1.small", public_hostname: "ex8.cloud.example.com"}
63
+ ]
64
+ end
65
+
66
+ let(:aws_nodes) do
67
+ 1.upto(3).collect do |n|
68
+ Ridley::NodeObject.new(double('resource'),
69
+ name: "awsnode#{n}",
70
+ automatic: {
71
+ ec2: {
72
+ instance_id: "i-ABCDEFG#{n}"
73
+ }
74
+ }
75
+ )
76
+ end
77
+ end
78
+
79
+ let(:euca_nodes) do
80
+ 1.upto(3).collect do |n|
81
+ Ridley::NodeObject.new(double('resource'),
82
+ name: "eucanode#{n}",
83
+ automatic: {
84
+ eucalyptus: {
85
+ instance_id: "i-EBCDEFG#{n}"
86
+ }
87
+ }
88
+ )
89
+ end
90
+ end
91
+
92
+ describe "#up" do
93
+ it "does all the steps" do
94
+ aws.should_receive(:validate_manifest_options).and_return(true)
95
+ aws.should_receive(:create_instances).and_return(true)
96
+ aws.should_receive(:store_provision_data)
97
+ aws.should_receive(:verify_instances).and_return(true)
98
+ aws.should_receive(:verify_connection).and_return(true)
99
+ aws.should_receive(:instances_as_manifest).twice.and_return(response)
100
+ expect(aws.up(job, environment_name, manifest, plugin, skip_bootstrap: true)).to eq(response)
101
+ end
102
+ end
103
+
104
+ describe "#down" do
105
+ subject(:down) { aws.down job, environment_name }
106
+
107
+ let(:instances) { aws.up(job, environment_name, manifest, plugin) }
108
+ let(:instance_ids) {
109
+ instances.collect { |instance| instance[:instance_id] }
110
+ }
111
+ let(:fog) { aws.send(:fog_connection, manifest) }
112
+
113
+ before do
114
+ aws.stub(fog_connection: Fog::Compute.new(provider: 'aws', aws_access_key_id: "", aws_secret_access_key: ""))
115
+ fog.create_key_pair('mb')
116
+ aws.stub(instance_ids_for_environment: instance_ids)
117
+ end
118
+
119
+ it "terminates the instances" do
120
+ instance_ids.each do |instance_id|
121
+ fog.should_receive(:terminate_instances).with(instance_id)
122
+ end
123
+
124
+ down
125
+ end
126
+ end
127
+
128
+ context "with a manifest" do
129
+ before do
130
+ fog.create_key_pair('mb') rescue Fog::Compute::AWS::Error
131
+ end
132
+
133
+ let(:fog) { aws.send(:fog_connection, manifest) }
134
+
135
+ context "auth settings" do
136
+ context "without manifest keys" do
137
+ before do
138
+ ENV['EC2_ACCESS_KEY'] = ENV['EC2_SECRET_KEY'] = ENV['AWS_ACCESS_KEY'] = ENV['AWS_SECRET_KEY'] = ENV['EC2_URL'] = nil
139
+ manifest.options.delete :access_key
140
+ manifest.options.delete :secret_key
141
+ manifest.options.delete :endpoint
142
+ end
143
+
144
+ it "should error on access_key" do
145
+ expect { aws.send(:access_key, manifest) }.to raise_error(MB::ConfigOptionMissing)
146
+ end
147
+
148
+ it "should error on secret_key" do
149
+ expect { aws.send(:secret_key, manifest) }.to raise_error(MB::ConfigOptionMissing)
150
+ end
151
+
152
+ context "with Euca environment variables" do
153
+ before do
154
+ ENV['EC2_ACCESS_KEY'] = 'EC2ABCDEFG'
155
+ ENV['EC2_SECRET_KEY'] = 'EC2abcdefgh123456789'
156
+ ENV['EC2_URL'] = 'http://euca2.example.com/services/Eucalyptus'
157
+ end
158
+
159
+ it "should get from the Euca environment variables" do
160
+ expect(aws.send(:access_key, manifest)).to eq('EC2ABCDEFG')
161
+ expect(aws.send(:secret_key, manifest)).to eq('EC2abcdefgh123456789')
162
+ expect(aws.send(:endpoint, manifest)).to eq('http://euca2.example.com/services/Eucalyptus')
163
+ end
164
+ end
165
+
166
+ context "with AWS environment variables" do
167
+ before do
168
+ ENV['AWS_ACCESS_KEY'] = 'AWSABCDEFG'
169
+ ENV['AWS_SECRET_KEY'] = 'AWSabcdefgh123456789'
170
+ ENV['EC2_URL'] = 'http://ec2.ap-southeast-1.amazonaws.com'
171
+ end
172
+
173
+ it "should get from the AWS environment variables" do
174
+ expect(aws.send(:access_key, manifest)).to eq('AWSABCDEFG')
175
+ expect(aws.send(:secret_key, manifest)).to eq('AWSabcdefgh123456789')
176
+ expect(aws.send(:endpoint, manifest)).to eq('http://ec2.ap-southeast-1.amazonaws.com')
177
+ end
178
+ end
179
+ end
180
+
181
+ context "with manifest keys" do
182
+ it "should get from the manifest options" do
183
+ expect(aws.send(:access_key, manifest)).to eq('ABCDEFG')
184
+ expect(aws.send(:secret_key, manifest)).to eq('abcdefgh123456789')
185
+ expect(aws.send(:endpoint, manifest)).to eq('http://euca.example.com/services/Eucalyptus')
186
+ end
187
+ end
188
+ end
189
+
190
+ describe "#validate_manifest_options(job,manifest)" do
191
+ subject(:validate_manifest_options) {
192
+ aws.send(:validate_manifest_options, job, manifest)
193
+ }
194
+
195
+ context "with a valid options hash in the manifest" do
196
+ it { should be_true }
197
+
198
+ it "does not raise when SecurityGroups is not set" do
199
+ manifest[:options].delete :security_groups
200
+ expect { validate_manifest_options }.not_to raise_error
201
+ end
202
+ end
203
+
204
+ context "with an invalid options hash in the manifest" do
205
+ it { should be_true }
206
+
207
+ it "raises on no options" do
208
+ manifest.delete :options
209
+ expect { validate_manifest_options }.to raise_error(MB::InvalidProvisionManifest)
210
+ end
211
+
212
+ it "raises on no ImageId" do
213
+ manifest[:options].delete :image_id
214
+ expect { validate_manifest_options }.to raise_error(MB::InvalidProvisionManifest)
215
+ end
216
+
217
+ it "raises on no KeyName" do
218
+ manifest[:options].delete :key_name
219
+ expect { validate_manifest_options }.to raise_error(MB::InvalidProvisionManifest)
220
+ end
221
+
222
+ it "raises on no AvailabilityZone" do
223
+ manifest[:options].delete :availability_zone
224
+ expect { validate_manifest_options }.to raise_error(MB::InvalidProvisionManifest)
225
+ end
226
+
227
+ it "raises on SecurityGroups not being an array" do
228
+ manifest[:options][:security_groups] = :fleeble
229
+ expect { validate_manifest_options }.to raise_error(MB::InvalidProvisionManifest)
230
+ end
231
+ end
232
+ end
233
+
234
+ context "ssh configuration" do
235
+ context "set in manifest" do
236
+ before do
237
+ manifest[:options][:ssh] ||= {}
238
+ manifest[:options][:ssh][:user] = "dauser"
239
+ manifest[:options][:ssh][:keys] = ["/home/dauser/.ssh/dakey"]
240
+ end
241
+
242
+ it "finds the ssh username from the manifest" do
243
+ expect(aws.send(:ssh_username, manifest.options)).to eq("dauser")
244
+ end
245
+
246
+ it "finds the ssh keys from the manifest" do
247
+ expect(aws.send(:ssh_keys, manifest.options).first).to eq("/home/dauser/.ssh/dakey")
248
+ end
249
+ end
250
+
251
+ context "not set in manifest" do
252
+ before do
253
+ manifest[:options].delete(:ssh)
254
+ end
255
+
256
+ context "set in config" do
257
+ before do
258
+ MB::Application.config[:ssh] ||= {}
259
+ MB::Application.config[:ssh][:user] = "dauser2"
260
+ MB::Application.config[:ssh][:keys] = ["/home/dauser2/.ssh/dakey"]
261
+ end
262
+
263
+ it "finds the ssh username from the config" do
264
+ expect(aws.send(:ssh_username, manifest.options)).to eq("dauser2")
265
+ end
266
+
267
+ it "finds the ssh keys from the config" do
268
+ expect(aws.send(:ssh_keys, manifest.options).first).to eq("/home/dauser2/.ssh/dakey")
269
+ end
270
+ end
271
+
272
+ context "not set in config" do
273
+ before do
274
+ MB::Application.config[:ssh] = nil
275
+ end
276
+
277
+ it "fails when trying to find the ssh username" do
278
+ expect { aws.send(:ssh_username, manifest.options) }.to raise_error(MB::InvalidProvisionManifest)
279
+ end
280
+
281
+ it "fails when trying to find the ssh keys" do
282
+ expect { aws.send(:ssh_keys, manifest.options) }.to raise_error(MB::InvalidProvisionManifest)
283
+ end
284
+ end
285
+ end
286
+ end
287
+
288
+ describe "#instance_counts" do
289
+ subject(:instance_counts) { aws.send(:instance_counts, manifest) }
290
+
291
+ it { should be_a(Hash) }
292
+
293
+ it "counts the m1.large instances" do
294
+ expect(instance_counts['m1.large']).to eq(6)
295
+ end
296
+
297
+ it "counts the m1.small instances" do
298
+ expect(instance_counts['m1.small']).to eq(2)
299
+ end
300
+ end
301
+
302
+ describe "#create_instances" do
303
+ it "makes calls by instance type" do
304
+ aws.should_receive(:run_instances).exactly(2).times.and_return(true)
305
+ aws.send :create_instances, job, manifest, fog
306
+ end
307
+ end
308
+
309
+ describe "#run_instances" do
310
+ it "keeps track of the instances" do
311
+ result = aws.send(:run_instances, job, fog, {}, "m1.large", 3, manifest.options)
312
+
313
+ expect(result).to be_a(Hash)
314
+ expect(result).to have(3).instances
315
+
316
+ result.each do |type, instance|
317
+ expect(instance[:type]).to eq("m1.large")
318
+ expect(instance[:ipaddress]).to be_nil
319
+ end
320
+ end
321
+ end
322
+
323
+ describe "#verify_instances" do
324
+ it "should check the instance status" do
325
+ instances = aws.send(:create_instances, job, manifest, fog)
326
+ fog.should_receive(:describe_instances).and_call_original
327
+ aws.send :verify_instances, job, fog, instances
328
+ end
329
+ end
330
+
331
+
332
+ describe "#verify_connection" do
333
+ it "should wait for SSH" do
334
+ instances = aws.send(:create_instances, job, manifest, fog)
335
+ aws.send :verify_instances, job, fog, instances
336
+ Fog.should_receive(:wait_for)
337
+ aws.send :verify_connection, job, fog, manifest, instances
338
+ end
339
+ end
340
+
341
+ describe "#instances_as_manifest" do
342
+ let(:instances) { aws.send(:create_instances, job, manifest, fog) }
343
+
344
+ before do
345
+ instances.each do |instance_id, instance|
346
+ instance[:ipaddress] = "172.16.1.#{rand(253)+1}"
347
+ instance[:status] = 16
348
+ end
349
+ end
350
+
351
+ it "returns an array" do
352
+ expect(aws.send(:instances_as_manifest, instances)).to be_an(Array)
353
+ end
354
+
355
+ it "has 8 instances" do
356
+ expect(aws.send(:instances_as_manifest, instances)).to have(8).instances
357
+ end
358
+ end
359
+ end
360
+
361
+ describe "#instance_ids" do
362
+ subject(:instance_ids) { aws.send(:instance_ids, environment_name) }
363
+
364
+ context "AWS" do
365
+ before do
366
+ aws.ridley.should_receive(:search).with(:node, "chef_environment:#{environment_name}").and_return(aws_nodes)
367
+ end
368
+
369
+ it { should have(3).instances }
370
+
371
+ it "should find all 3" do
372
+ ["i-ABCDEFG1", "i-ABCDEFG2", "i-ABCDEFG3"].each do |i|
373
+ expect(instance_ids).to include(i)
374
+ end
375
+ end
376
+ end
377
+
378
+ context "Eucalyptus" do
379
+ before do
380
+ aws.ridley.should_receive(:search).with(:node, "chef_environment:#{environment_name}").and_return(euca_nodes)
381
+ end
382
+
383
+ it { should have(3).instances }
384
+
385
+ it "should find all 3" do
386
+ ["i-EBCDEFG1", "i-EBCDEFG2", "i-EBCDEFG3"].each do |i|
387
+ expect(instance_ids).to include(i)
388
+ end
389
+ end
390
+ end
391
+ end
392
+ end