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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.ruby-version +1 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.md +196 -0
- data/COMMANDS.md +9 -0
- data/CONTRIBUTING.md +24 -0
- data/Dockerfile +26 -0
- data/Gemfile +60 -2
- data/Guardfile +30 -0
- data/LICENSE +10 -0
- data/MANIFESTS.md +90 -0
- data/OPERATORS_GUIDE.md +195 -0
- data/PLUGINS.md +268 -0
- data/README.md +304 -16
- data/Thorfile +123 -0
- data/VAGRANT.md +116 -0
- data/bin/boot +9 -0
- data/bin/mb +5 -0
- data/bin/mbsrv +5 -0
- data/config.json +32 -0
- data/features/cli/bootstrap_command/configurable_scripts.feature +32 -0
- data/features/cli/configure_command.feature +57 -0
- data/features/cli/environment_command/create_command.feature +22 -0
- data/features/cli/environment_command/destroy_command.feature +33 -0
- data/features/cli/environment_command/from_command.feature +29 -0
- data/features/cli/environment_command/list_command.feature +0 -0
- data/features/cli/node_limiting.feature +47 -0
- data/features/cli/plugin_command/list_command.feature +46 -0
- data/features/cli/service_command/service_command.feature +21 -0
- data/features/cli/template_command.feature +10 -0
- data/features/cli/validate_config.feature +46 -0
- data/features/step_definitions/bootstrap_steps.rb +57 -0
- data/features/step_definitions/chef_server_steps.rb +3 -0
- data/features/step_definitions/configuration_steps.rb +18 -0
- data/features/step_definitions/core_cli_steps.rb +33 -0
- data/features/step_definitions/environment_steps.rb +43 -0
- data/features/step_definitions/node_steps.rb +3 -0
- data/features/step_definitions/plugin_steps.rb +15 -0
- data/features/step_definitions/template_steps.rb +7 -0
- data/features/support/env.rb +68 -0
- data/lib/mb/api.rb +8 -0
- data/lib/mb/api/application.rb +7 -0
- data/lib/mb/api/endpoint.rb +5 -0
- data/lib/mb/api/helpers.rb +38 -0
- data/lib/mb/api/v1.rb +56 -0
- data/lib/mb/api/v1/config_endpoint.rb +12 -0
- data/lib/mb/api/v1/environments_endpoint.rb +174 -0
- data/lib/mb/api/v1/jobs_endpoint.rb +31 -0
- data/lib/mb/api/v1/plugins_endpoint.rb +90 -0
- data/lib/mb/api/validators.rb +5 -0
- data/lib/mb/api/validators/sem_ver.rb +18 -0
- data/lib/mb/application.rb +148 -0
- data/lib/mb/berkshelf.rb +50 -0
- data/lib/mb/bootstrap.rb +9 -0
- data/lib/mb/bootstrap/manager.rb +250 -0
- data/lib/mb/bootstrap/manifest.rb +131 -0
- data/lib/mb/bootstrap/routine.rb +199 -0
- data/lib/mb/bootstrap/template.rb +73 -0
- data/lib/mb/bootstrap/worker.rb +227 -0
- data/lib/mb/chef.rb +6 -0
- data/lib/mb/chef/config.rb +69 -0
- data/lib/mb/chef/run_list_item.rb +115 -0
- data/lib/mb/chef_mutex.rb +304 -0
- data/lib/mb/clean_room_base.rb +39 -0
- data/lib/mb/cli.rb +50 -0
- data/lib/mb/cli/base.rb +51 -0
- data/lib/mb/cli/shell.rb +29 -0
- data/lib/mb/cli/shell/basic.rb +11 -0
- data/lib/mb/cli/shell/color.rb +11 -0
- data/lib/mb/cli/shell/ext.rb +41 -0
- data/lib/mb/cli/sub_command.rb +95 -0
- data/lib/mb/cli/sub_command/component.rb +56 -0
- data/lib/mb/cli/sub_command/plugin.rb +232 -0
- data/lib/mb/cli_client.rb +178 -0
- data/lib/mb/cli_gateway.rb +426 -0
- data/lib/mb/cli_gateway/sub_commands.rb +3 -0
- data/lib/mb/cli_gateway/sub_commands/environment.rb +124 -0
- data/lib/mb/cli_gateway/sub_commands/plugin.rb +148 -0
- data/lib/mb/command.rb +88 -0
- data/lib/mb/command_invoker.rb +235 -0
- data/lib/mb/command_invoker/worker.rb +40 -0
- data/lib/mb/command_runner.rb +233 -0
- data/lib/mb/component.rb +245 -0
- data/lib/mb/config.rb +275 -0
- data/lib/mb/config_manager.rb +75 -0
- data/lib/mb/console.rb +35 -0
- data/lib/mb/cookbook_metadata.rb +73 -0
- data/lib/mb/core_ext.rb +3 -0
- data/lib/mb/core_ext/dir.rb +37 -0
- data/lib/mb/core_ext/enumerable.rb +48 -0
- data/lib/mb/core_ext/file.rb +24 -0
- data/lib/mb/core_ext/signal.rb +11 -0
- data/lib/mb/environment_manager.rb +195 -0
- data/lib/mb/error_handler.rb +212 -0
- data/lib/mb/errors.rb +693 -0
- data/lib/mb/file_system.rb +60 -0
- data/lib/mb/file_system/tempfile.rb +25 -0
- data/lib/mb/gear.rb +154 -0
- data/lib/mb/gears.rb +8 -0
- data/lib/mb/gears/dynamic_service.rb +218 -0
- data/lib/mb/gears/jmx.rb +24 -0
- data/lib/mb/gears/jmx/action.rb +46 -0
- data/lib/mb/gears/mysql.rb +20 -0
- data/lib/mb/gears/mysql/action.rb +190 -0
- data/lib/mb/gears/service.rb +163 -0
- data/lib/mb/gears/service/action.rb +58 -0
- data/lib/mb/gears/service/action_runner.rb +161 -0
- data/lib/mb/grape_ext.rb +3 -0
- data/lib/mb/grape_ext/endpoint.rb +13 -0
- data/lib/mb/group.rb +143 -0
- data/lib/mb/job.rb +183 -0
- data/lib/mb/job/state_machine.rb +34 -0
- data/lib/mb/job/states.rb +46 -0
- data/lib/mb/job_manager.rb +96 -0
- data/lib/mb/job_record.rb +67 -0
- data/lib/mb/job_ticket.rb +25 -0
- data/lib/mb/lock_manager.rb +116 -0
- data/lib/mb/logging.rb +134 -0
- data/lib/mb/logging/basic_format.rb +31 -0
- data/lib/mb/manifest.rb +128 -0
- data/lib/mb/mixin.rb +3 -0
- data/lib/mb/mixin/attribute_setting.rb +265 -0
- data/lib/mb/mixin/coded_exit.rb +49 -0
- data/lib/mb/mixin/locks.rb +54 -0
- data/lib/mb/mixin/services.rb +100 -0
- data/lib/mb/node_filter.rb +97 -0
- data/lib/mb/node_querier.rb +527 -0
- data/lib/mb/plugin.rb +300 -0
- data/lib/mb/plugin_manager.rb +589 -0
- data/lib/mb/provisioner.rb +186 -0
- data/lib/mb/provisioner/manager.rb +213 -0
- data/lib/mb/provisioner/manifest.rb +125 -0
- data/lib/mb/provisioner/provision_data.rb +96 -0
- data/lib/mb/provisioners.rb +5 -0
- data/lib/mb/provisioners/aws.rb +395 -0
- data/lib/mb/rest_gateway.rb +72 -0
- data/lib/mb/ridley_ext.rb +5 -0
- data/lib/mb/ridley_ext/cookbook_object.rb +15 -0
- data/lib/mb/srv_ctl.rb +183 -0
- data/lib/mb/test.rb +104 -0
- data/lib/mb/thor_ext.rb +49 -0
- data/lib/mb/upgrade.rb +6 -0
- data/lib/mb/upgrade/manager.rb +85 -0
- data/lib/mb/upgrade/worker.rb +149 -0
- data/lib/mb/version.rb +1 -1
- data/lib/motherbrain.rb +166 -2
- data/man/man_helper.rb +81 -0
- data/man/mb.1 +494 -0
- data/man/mb.1.html +300 -0
- data/man/mb.1.ronn.erb +62 -0
- data/motherbrain.gemspec +56 -20
- data/scripts/node_name.rb +14 -0
- data/spec/fixtures/cb_metadata.json +7 -0
- data/spec/fixtures/cb_metadata.rb +14 -0
- data/spec/fixtures/fake_id_rsa +27 -0
- data/spec/fixtures/fake_key.pem +27 -0
- data/spec/fixtures/myface-0.1.0/metadata.rb +14 -0
- data/spec/fixtures/myface-0.1.0/motherbrain.rb +0 -0
- data/spec/fixtures/test_env.json +15 -0
- data/spec/spec_helper.rb +67 -0
- data/spec/support/actor_mocking.rb +7 -0
- data/spec/support/berkshelf.rb +24 -0
- data/spec/support/chef_server.rb +102 -0
- data/spec/support/doubles.rb +11 -0
- data/spec/support/klass.rb +10 -0
- data/spec/support/matchers/each.rb +12 -0
- data/spec/support/matchers/error_codes.rb +5 -0
- data/spec/support/matchers/exit_codes.rb +57 -0
- data/spec/support/matchers/jobs.rb +11 -0
- data/spec/support/spec_helpers.rb +145 -0
- data/spec/unit/mb/api/application_spec.rb +11 -0
- data/spec/unit/mb/api/helpers_spec.rb +5 -0
- data/spec/unit/mb/api/v1/config_endpoint_spec.rb +19 -0
- data/spec/unit/mb/api/v1/environments_endpoint_spec.rb +71 -0
- data/spec/unit/mb/api/v1/jobs_endpoint_spec.rb +24 -0
- data/spec/unit/mb/api/v1/plugins_endpoint_spec.rb +298 -0
- data/spec/unit/mb/api/v1_spec.rb +37 -0
- data/spec/unit/mb/api/validators/sem_ver_spec.rb +5 -0
- data/spec/unit/mb/application_spec.rb +19 -0
- data/spec/unit/mb/berkshelf_spec.rb +38 -0
- data/spec/unit/mb/bootstrap/manager_spec.rb +347 -0
- data/spec/unit/mb/bootstrap/manifest_spec.rb +333 -0
- data/spec/unit/mb/bootstrap/routine_spec.rb +393 -0
- data/spec/unit/mb/bootstrap/template_spec.rb +100 -0
- data/spec/unit/mb/bootstrap/worker_spec.rb +194 -0
- data/spec/unit/mb/chef/config_spec.rb +33 -0
- data/spec/unit/mb/chef/run_list_item_spec.rb +34 -0
- data/spec/unit/mb/chef_mutex_spec.rb +314 -0
- data/spec/unit/mb/clean_room_base_spec.rb +31 -0
- data/spec/unit/mb/cli/base_spec.rb +43 -0
- data/spec/unit/mb/cli/shell/basic_spec.rb +5 -0
- data/spec/unit/mb/cli/shell/color_spec.rb +5 -0
- data/spec/unit/mb/cli/shell/ext_spec.rb +11 -0
- data/spec/unit/mb/cli/shell_spec.rb +38 -0
- data/spec/unit/mb/cli/sub_command/base_spec.rb +102 -0
- data/spec/unit/mb/cli/sub_command/component_spec.rb +5 -0
- data/spec/unit/mb/cli/sub_command/plugin_spec.rb +91 -0
- data/spec/unit/mb/cli/sub_command_spec.rb +43 -0
- data/spec/unit/mb/cli/ui.rb +0 -0
- data/spec/unit/mb/cli_client_spec.rb +51 -0
- data/spec/unit/mb/cli_gateway_spec.rb +386 -0
- data/spec/unit/mb/command_invoker/worker_spec.rb +43 -0
- data/spec/unit/mb/command_invoker_spec.rb +230 -0
- data/spec/unit/mb/command_runner_spec.rb +299 -0
- data/spec/unit/mb/command_spec.rb +76 -0
- data/spec/unit/mb/component_spec.rb +185 -0
- data/spec/unit/mb/config_manager_spec.rb +31 -0
- data/spec/unit/mb/config_spec.rb +408 -0
- data/spec/unit/mb/cookbook_metadata_spec.rb +89 -0
- data/spec/unit/mb/core_ext/dir_spec.rb +92 -0
- data/spec/unit/mb/core_ext/enumerable_spec.rb +104 -0
- data/spec/unit/mb/core_ext/file_spec.rb +58 -0
- data/spec/unit/mb/core_ext/signal_spec.rb +24 -0
- data/spec/unit/mb/environment_manager_spec.rb +166 -0
- data/spec/unit/mb/error_handler_spec.rb +173 -0
- data/spec/unit/mb/errors_spec.rb +132 -0
- data/spec/unit/mb/file_system/tempfile_spec.rb +14 -0
- data/spec/unit/mb/file_system_spec.rb +69 -0
- data/spec/unit/mb/gear_spec.rb +125 -0
- data/spec/unit/mb/gears/dynamic_service_spec.rb +187 -0
- data/spec/unit/mb/gears/jmx/action_spec.rb +34 -0
- data/spec/unit/mb/gears/jmx_spec.rb +32 -0
- data/spec/unit/mb/gears/mysql/action_spec.rb +118 -0
- data/spec/unit/mb/gears/mysql_spec.rb +21 -0
- data/spec/unit/mb/gears/service/action_runner_spec.rb +182 -0
- data/spec/unit/mb/gears/service/action_spec.rb +44 -0
- data/spec/unit/mb/gears/service_spec.rb +124 -0
- data/spec/unit/mb/group_spec.rb +280 -0
- data/spec/unit/mb/job_manager_spec.rb +56 -0
- data/spec/unit/mb/job_record_spec.rb +60 -0
- data/spec/unit/mb/job_spec.rb +201 -0
- data/spec/unit/mb/locks_manager_spec.rb +88 -0
- data/spec/unit/mb/logging_spec.rb +133 -0
- data/spec/unit/mb/manifest_spec.rb +105 -0
- data/spec/unit/mb/mixin/attribute_setting_spec.rb +180 -0
- data/spec/unit/mb/mixin/coded_exit_spec.rb +25 -0
- data/spec/unit/mb/mixin/locks_spec.rb +32 -0
- data/spec/unit/mb/mixin/services_spec.rb +75 -0
- data/spec/unit/mb/node_filter_spec.rb +86 -0
- data/spec/unit/mb/node_querier_spec.rb +532 -0
- data/spec/unit/mb/plugin_manager_spec.rb +724 -0
- data/spec/unit/mb/plugin_spec.rb +247 -0
- data/spec/unit/mb/provisioner/manager_spec.rb +141 -0
- data/spec/unit/mb/provisioner/manifest_spec.rb +182 -0
- data/spec/unit/mb/provisioner/provision_data_spec.rb +113 -0
- data/spec/unit/mb/provisioner_spec.rb +251 -0
- data/spec/unit/mb/provisioners/aws_spec.rb +392 -0
- data/spec/unit/mb/provisioners/environment_factory_spec.rb +108 -0
- data/spec/unit/mb/rest_gateway_spec.rb +13 -0
- data/spec/unit/mb/ridley_ext/cookbook_object_spec.rb +105 -0
- data/spec/unit/mb/srv_ctl_spec.rb +142 -0
- data/spec/unit/mb/upgrade/manager_spec.rb +37 -0
- data/spec/unit/mb/upgrade/worker_spec.rb +219 -0
- data/spec/unit/motherbrain_spec.rb +58 -0
- data/templates/bootstrap.json +8 -0
- data/templates/motherbrain.rb +44 -0
- metadata +694 -15
- data/Rakefile +0 -1
data/lib/mb/gears/jmx.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Gear
|
3
|
+
class JMX < Gear::Base
|
4
|
+
register_gear :jmx
|
5
|
+
|
6
|
+
# @param [Fixnum] port
|
7
|
+
# the port to connect over
|
8
|
+
# @param [String] object_name
|
9
|
+
# the name of the jmx object
|
10
|
+
#
|
11
|
+
# @raise [ActionNotSupported] if not running JRuby
|
12
|
+
# @raise [ArgumentError]
|
13
|
+
def action(port, object_name, &block)
|
14
|
+
unless jruby?
|
15
|
+
raise ActionNotSupported, "The JMX Gear is only supported on JRuby"
|
16
|
+
end
|
17
|
+
|
18
|
+
JMX::Action.new(port, object_name, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
require_relative 'jmx/action'
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
begin
|
2
|
+
require 'jmx4r'
|
3
|
+
rescue LoadError; end
|
4
|
+
|
5
|
+
module MotherBrain
|
6
|
+
module Gear
|
7
|
+
class JMX
|
8
|
+
# @api private
|
9
|
+
class Action
|
10
|
+
attr_reader :port
|
11
|
+
attr_reader :object_name
|
12
|
+
attr_reader :block
|
13
|
+
|
14
|
+
# @param [Fixnum] port the port to connect over
|
15
|
+
# @param [String] object_name the name of the jmx object
|
16
|
+
#
|
17
|
+
# @raise [ArgumentError]
|
18
|
+
def initialize(port, object_name, &block)
|
19
|
+
unless block_given? && block.arity == 1
|
20
|
+
raise ArgumentError, "block with one argument required to run JMX actions"
|
21
|
+
end
|
22
|
+
|
23
|
+
@port = port
|
24
|
+
@object_name = object_name
|
25
|
+
@block = block
|
26
|
+
end
|
27
|
+
|
28
|
+
# Run this action on the specified nodes.
|
29
|
+
#
|
30
|
+
# @param [MB::Job] job
|
31
|
+
# a job to update with status
|
32
|
+
# @param [String] environment
|
33
|
+
# the environment this command is being run on
|
34
|
+
# @param [Array<Ridley::Node>] nodes
|
35
|
+
# the nodes to run this action on
|
36
|
+
def run(job, environment, nodes)
|
37
|
+
nodes.each do |node|
|
38
|
+
connection = ::JMX::MBean.connection(host: node.public_hostname, port: port)
|
39
|
+
mbean = ::JMX::MBean.find_by_name(object_name, connection: connection)
|
40
|
+
block.call(mbean)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Gear
|
3
|
+
class MySQL < Gear::Base
|
4
|
+
register_gear :mysql
|
5
|
+
|
6
|
+
# @param [String] sql
|
7
|
+
# the sql to run
|
8
|
+
#
|
9
|
+
# @option options [Hash] :data_bag
|
10
|
+
# specify the data bag, item, and location inside the item to find the MySQL credentials
|
11
|
+
#
|
12
|
+
# @return [MB::Gear::MySQL::Action]
|
13
|
+
def action(sql, options)
|
14
|
+
MySQL::Action.new(sql, options)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require_relative 'mysql/action'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
if jruby?
|
2
|
+
require 'jdbc/mysql'
|
3
|
+
require 'java'
|
4
|
+
else
|
5
|
+
require 'mysql2'
|
6
|
+
end
|
7
|
+
|
8
|
+
module MotherBrain
|
9
|
+
module Gear
|
10
|
+
class MySQL
|
11
|
+
# @api private
|
12
|
+
class Action
|
13
|
+
include MB::Mixin::Services
|
14
|
+
|
15
|
+
# @return [String]
|
16
|
+
attr_reader :sql
|
17
|
+
# @return [Hash]
|
18
|
+
attr_reader :options
|
19
|
+
|
20
|
+
class << self
|
21
|
+
# @param [Hash] options
|
22
|
+
# the options to validate
|
23
|
+
#
|
24
|
+
# @raise [MB::ArgumentError] if the options are invalid
|
25
|
+
def validate_options(options)
|
26
|
+
unless options.key?(:data_bag)
|
27
|
+
raise ArgumentError, "You are missing a :data_bag key in your MySQL gear options!"
|
28
|
+
end
|
29
|
+
|
30
|
+
unless options[:data_bag].key?(:name)
|
31
|
+
raise ArgumentError, "You are missing a :name key in your MySQL gear data bag options!"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# @param [String] sql
|
37
|
+
# the sql to run
|
38
|
+
#
|
39
|
+
# @option options [Hash] :data_bag
|
40
|
+
# specify the data bag, item, and location inside the item to find the MySQL credentials
|
41
|
+
def initialize(sql, options)
|
42
|
+
self.class.validate_options(options)
|
43
|
+
@sql = sql
|
44
|
+
@options = options
|
45
|
+
end
|
46
|
+
|
47
|
+
# Run this action on the specified nodes
|
48
|
+
#
|
49
|
+
# @param [MB::Job] job
|
50
|
+
# a job to update with status
|
51
|
+
# @param [String] environment
|
52
|
+
# the environment this command is being run on
|
53
|
+
# @param [Array<Ridley::Node>] nodes
|
54
|
+
# the nodes to run this action on
|
55
|
+
def run(job, environment, nodes)
|
56
|
+
threads = []
|
57
|
+
|
58
|
+
nodes.each do |node|
|
59
|
+
threads << Thread.new(node) do |node|
|
60
|
+
query(environment, sql, node)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
threads.each(&:join)
|
65
|
+
end
|
66
|
+
|
67
|
+
# The MySQL connection information/credentials for the specified node.
|
68
|
+
#
|
69
|
+
# @param [String] environment
|
70
|
+
# name of the environment to retrieve credentials from
|
71
|
+
# @param [Ridley::Node] node
|
72
|
+
# the node to to find connection information for
|
73
|
+
#
|
74
|
+
# @raise [MB::GearError] if the MySQL credentials cannot be found or are malformed
|
75
|
+
#
|
76
|
+
# @return [Hash] MySQL connection information for the node
|
77
|
+
def connection_info(environment, node)
|
78
|
+
credentials(environment).merge(host: node.public_hostname)
|
79
|
+
rescue MB::DataBagNotFound, MB::DataBagItemNotFound => ex
|
80
|
+
raise MB::GearError.new(ex)
|
81
|
+
end
|
82
|
+
|
83
|
+
# @return [Hash] The keys used to look up MySQL connection information in a data bag item.
|
84
|
+
def data_bag_keys
|
85
|
+
hash = data_bag_spec[:location][:hash]
|
86
|
+
|
87
|
+
if hash
|
88
|
+
Hash[data_bag_spec[:location][:keys].map { |k, v| [k, "#{hash}.#{v}"] }]
|
89
|
+
else
|
90
|
+
data_bag_spec[:location][:keys]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
# Runs a sql query on a node.
|
97
|
+
#
|
98
|
+
# @param [String] sql
|
99
|
+
# the sql query
|
100
|
+
# @param [Ridley::Node] node
|
101
|
+
# the node to run the query on
|
102
|
+
def query(environment, sql, node)
|
103
|
+
jruby? ? jdbc_query(environment, sql, node) : mysql2_query(environment, sql, node)
|
104
|
+
end
|
105
|
+
|
106
|
+
# Runs a sql query on a node using jdbc.
|
107
|
+
#
|
108
|
+
# @param [String] sql
|
109
|
+
# the sql query
|
110
|
+
# @param [Ridley::Node] node
|
111
|
+
# the node to run the query on
|
112
|
+
def jdbc_query(environment, sql, node)
|
113
|
+
info = connection_info(environment, node)
|
114
|
+
Java::com.mysql.jdbc.Driver
|
115
|
+
connection_url = "jdbc:mysql://#{info[:host]}:#{info[:port]}/#{info[:database]}"
|
116
|
+
connection = java.sql.DriverManager.get_connection(connection_url, info[:username], info[:password])
|
117
|
+
connection.create_statement.execute(sql)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Runs a sql query on a node using mysql2.
|
121
|
+
#
|
122
|
+
# @param [String] sql
|
123
|
+
# the sql query
|
124
|
+
# @param [Ridley::Node] node
|
125
|
+
# the node to run the query on
|
126
|
+
def mysql2_query(environment, sql, node)
|
127
|
+
connection = Mysql2::Client.new(connection_info(environment, node))
|
128
|
+
connection.query(sql)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Retrieves the MySQL credentials from the data bag.
|
132
|
+
#
|
133
|
+
# @param [String] environment
|
134
|
+
#
|
135
|
+
# @raise [MB::DataBagNotFound] if the data bag is not found
|
136
|
+
# @raise [MB::DataBagItemNotFound] if an item with the name of the given environment is not found
|
137
|
+
# in the credentials data bag
|
138
|
+
# @raise [MB::GearError] if any MySQL credentials are missing
|
139
|
+
#
|
140
|
+
# @return [Hash] MySQL credentials
|
141
|
+
def credentials(environment)
|
142
|
+
return @credentials if @credentials
|
143
|
+
|
144
|
+
unless data_bag = ridley.data_bag.find(data_bag_spec[:name])
|
145
|
+
raise MB::DataBagNotFound.new(data_bag_spec[:name])
|
146
|
+
end
|
147
|
+
|
148
|
+
unless dbi = data_bag.item.find(environment)
|
149
|
+
raise MB::DataBagItemNotFound.new(data_bag_spec[:name], environment)
|
150
|
+
end
|
151
|
+
|
152
|
+
dbi = dbi.decrypt
|
153
|
+
|
154
|
+
@credentials = Hash[data_bag_keys.map { |key, dbi_key| [key, dbi.dig(dbi_key)] }]
|
155
|
+
|
156
|
+
@credentials.each do |key, value|
|
157
|
+
if value.nil?
|
158
|
+
err_msg = "Missing a MySQL credential. Could not find a #{key} at the location you specified. "
|
159
|
+
err_msg << "You specified that the #{key} can be found at '#{data_bag_keys[key]}' "
|
160
|
+
err_msg << "in the '#{environment}' data bag item inside the '#{data_bag_spec[:name]}' "
|
161
|
+
err_msg << "data bag."
|
162
|
+
raise GearError, err_msg
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
@credentials
|
167
|
+
end
|
168
|
+
|
169
|
+
# @return [Hash] where to find the MySQL connection information
|
170
|
+
def data_bag_spec
|
171
|
+
@data_bag_spec ||= default_data_bag_spec.deep_merge(options[:data_bag])
|
172
|
+
end
|
173
|
+
|
174
|
+
# @return [Hash] the default specification for where to find MySQL connection information
|
175
|
+
def default_data_bag_spec
|
176
|
+
{
|
177
|
+
location: {
|
178
|
+
keys: {
|
179
|
+
username: "username",
|
180
|
+
password: "password",
|
181
|
+
database: "database",
|
182
|
+
port: "port"
|
183
|
+
}
|
184
|
+
}
|
185
|
+
}
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Gear
|
3
|
+
class Service < Gear::Base
|
4
|
+
register_gear :service
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# Finds a gear identified by name in the list of gears supplied.
|
8
|
+
#
|
9
|
+
# @param [Array<MB::Gear::Service>] gears
|
10
|
+
# the list of gears to search in
|
11
|
+
# @param [#to_s] name
|
12
|
+
# the name of the gear to search for
|
13
|
+
#
|
14
|
+
# @return [MB::Gear::Service]
|
15
|
+
def find(gears, name)
|
16
|
+
gears.find { |obj| obj.name == name.to_s }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [String]
|
21
|
+
attr_reader :name
|
22
|
+
# @return [Set<Action>]
|
23
|
+
attr_reader :actions
|
24
|
+
# @return [MB::Component]
|
25
|
+
attr_reader :component
|
26
|
+
|
27
|
+
attr_reader :service_group
|
28
|
+
attr_reader :service_recipe
|
29
|
+
attr_reader :service_attribute
|
30
|
+
|
31
|
+
# @param [MB::Component] component
|
32
|
+
# @param [#to_s] name
|
33
|
+
def initialize(component, name, &block)
|
34
|
+
@name = name.to_s
|
35
|
+
@component = component
|
36
|
+
@actions = Set.new
|
37
|
+
|
38
|
+
if block_given?
|
39
|
+
dsl_eval(&block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Find and return the given action
|
44
|
+
#
|
45
|
+
# @param [String] name
|
46
|
+
#
|
47
|
+
# @raise [ActionNotFound] if there is no action of the given name defined
|
48
|
+
#
|
49
|
+
# @return [Gear::Action]
|
50
|
+
def action!(name)
|
51
|
+
action = action(name)
|
52
|
+
|
53
|
+
unless action
|
54
|
+
raise ActionNotFound, "#{self.class.keyword} '#{_attributes_[:name]}' does not have the action '#{name}'"
|
55
|
+
end
|
56
|
+
|
57
|
+
action
|
58
|
+
end
|
59
|
+
|
60
|
+
# Find and return the given action
|
61
|
+
#
|
62
|
+
# @param [String] name
|
63
|
+
#
|
64
|
+
# @raise [ActionNotFound] if there is no action of the given name defined
|
65
|
+
#
|
66
|
+
# @return [Gear::Action]
|
67
|
+
def action(name)
|
68
|
+
actions.find { |action| action.name == name }
|
69
|
+
end
|
70
|
+
|
71
|
+
# Find and return the stop action
|
72
|
+
#
|
73
|
+
# @return [Gear::Action]
|
74
|
+
def stop_action
|
75
|
+
action(:stop)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Add a new action to this Service
|
79
|
+
#
|
80
|
+
# @param [Service::Action] new_action
|
81
|
+
#
|
82
|
+
# @return [Set<Action>]
|
83
|
+
def add_action(new_action)
|
84
|
+
if action(new_action.name)
|
85
|
+
raise DuplicateAction, "Action '#{new_action.name}' already defined on service '#{_attributes_[:name]}'"
|
86
|
+
end
|
87
|
+
|
88
|
+
actions << new_action
|
89
|
+
end
|
90
|
+
|
91
|
+
def set_service_group(group)
|
92
|
+
@service_group = group
|
93
|
+
end
|
94
|
+
|
95
|
+
def set_service_recipe(recipe)
|
96
|
+
@service_recipe = recipe
|
97
|
+
end
|
98
|
+
|
99
|
+
def set_service_attribute(attribute)
|
100
|
+
@service_attribute = attribute
|
101
|
+
end
|
102
|
+
|
103
|
+
# Convert a Service to a DynamicService
|
104
|
+
#
|
105
|
+
# @return [MB::Gears::DynamicService]
|
106
|
+
def to_dynamic_service
|
107
|
+
log {
|
108
|
+
"Service '#{self.name}' does not appear to by a dynamic service. It does not define the following fields which are required for dynamic services: #{self.missing_fields_for_dynamic_service.join(",")}"
|
109
|
+
} unless dynamic_service?
|
110
|
+
|
111
|
+
MotherBrain::Gear::DynamicService.new(component.name, name)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Indicates whether this service conforms to the dyanamic service pattern
|
115
|
+
#
|
116
|
+
# @return TrueClass, FalseClass
|
117
|
+
def dynamic_service?
|
118
|
+
missing_fields_for_dynamic_service.empty?
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
def dsl_eval(&block)
|
124
|
+
CleanRoom.new(self).instance_eval do
|
125
|
+
instance_eval(&block)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def missing_fields_for_dynamic_service
|
130
|
+
%w[service_group service_recipe service_attribute].collect do |field|
|
131
|
+
field if self.instance_variable_get("@#{field}").nil?
|
132
|
+
end.compact
|
133
|
+
end
|
134
|
+
|
135
|
+
# @api private
|
136
|
+
class CleanRoom < CleanRoomBase
|
137
|
+
# @param [String] name
|
138
|
+
def action(name, &block)
|
139
|
+
real_model.add_action Service::Action.new(name, real_model.component, &block)
|
140
|
+
end
|
141
|
+
|
142
|
+
def service_group(group)
|
143
|
+
real_model.set_service_group(group)
|
144
|
+
end
|
145
|
+
|
146
|
+
def service_recipe(recipe)
|
147
|
+
real_model.set_service_recipe([recipe].flatten)
|
148
|
+
end
|
149
|
+
|
150
|
+
def service_attribute(attribute)
|
151
|
+
real_model.set_service_attribute([attribute].flatten)
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
|
156
|
+
attr_reader :component
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
require_relative 'service/action'
|
161
|
+
require_relative 'service/action_runner'
|
162
|
+
end
|
163
|
+
end
|