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
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
|
3
|
+
module MotherBrain
|
4
|
+
module FileSystem
|
5
|
+
autoload :Tempfile, 'mb/file_system/tempfile'
|
6
|
+
|
7
|
+
class << self
|
8
|
+
# Create the directory structure for motherbrain
|
9
|
+
def init
|
10
|
+
FileUtils.mkdir_p(logs)
|
11
|
+
FileUtils.mkdir_p(manifests)
|
12
|
+
FileUtils.mkdir_p(root)
|
13
|
+
FileUtils.mkdir_p(templates)
|
14
|
+
FileUtils.mkdir_p(tmp)
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [Pathname]
|
18
|
+
def logs
|
19
|
+
root.join("logs")
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [Pathname]
|
23
|
+
def manifests
|
24
|
+
root.join("manifests")
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Pathname]
|
28
|
+
def root
|
29
|
+
Pathname.new(default_root_path)
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Pathname]
|
33
|
+
def tmp
|
34
|
+
root.join("tmp")
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Pathname]
|
38
|
+
def templates
|
39
|
+
root.join("templates")
|
40
|
+
end
|
41
|
+
|
42
|
+
# Create a temporary directory in the tmp directory of the motherbrain
|
43
|
+
# file system
|
44
|
+
#
|
45
|
+
# @param [String] prefix (nil)
|
46
|
+
# a prefix suffix to attach to name of the generated directory
|
47
|
+
#
|
48
|
+
# @return [String]
|
49
|
+
def tmpdir(prefix = nil)
|
50
|
+
Dir.mktmpdir(prefix, tmp)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def default_root_path
|
56
|
+
File.expand_path(ENV["MOTHERBRAIN_PATH"] || "~/.mb")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module MotherBrain
|
4
|
+
module FileSystem
|
5
|
+
# Thin wrapper around ::Tempfile to ensure we always write temporary files
|
6
|
+
# into motherbrain's configured temporary directory
|
7
|
+
class Tempfile < ::Tempfile
|
8
|
+
BASENAME = 'mb_'.freeze
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# @param [Hash] options
|
12
|
+
# options to pass to ::Tempfile.open
|
13
|
+
def open(options = {})
|
14
|
+
super(options)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param [Hash] options
|
19
|
+
# options to pass to ::Tempfile.new
|
20
|
+
def initialize(options = {})
|
21
|
+
super(BASENAME, FileSystem.tmpdir, options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/mb/gear.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
# The base module for defining new Gears for a plugin. Any class including this module
|
3
|
+
# and registered with {MotherBrain} as a {Gear} will be available for use in the plugin DSL.
|
4
|
+
#
|
5
|
+
# Gears represent the proverbial knobs and levers that can be used to manipulate a
|
6
|
+
# {Component}.
|
7
|
+
#
|
8
|
+
# @example Defining a new Gear
|
9
|
+
#
|
10
|
+
# class Twitter < MB::Gear::Base
|
11
|
+
# register_gear :twitter
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @example Plugin DSL usage
|
15
|
+
#
|
16
|
+
# component "news_post" do
|
17
|
+
#
|
18
|
+
# twitter do
|
19
|
+
# ...
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
module Gear
|
24
|
+
class Base
|
25
|
+
class << self
|
26
|
+
# The identifier for the Gear. The keyword is automatically populated based on the name
|
27
|
+
# of the Class including {MotherBrain::Gear}. The keyword must be unique among the other
|
28
|
+
# registered Gears. Also used to define a Gear in the plugin DSL.
|
29
|
+
#
|
30
|
+
# @return [Symbol]
|
31
|
+
attr_reader :keyword
|
32
|
+
|
33
|
+
# Register the gear with {MotherBrain::Gear} with the given keyword. This is how a gear is
|
34
|
+
# identified within a plugin.
|
35
|
+
#
|
36
|
+
# @param [#to_sym] keyword
|
37
|
+
def register_gear(keyword)
|
38
|
+
@keyword = keyword.to_sym
|
39
|
+
Gear.register(self)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
include VariaModel
|
44
|
+
|
45
|
+
# @param [MB::Job] job
|
46
|
+
# a job to update with status
|
47
|
+
# @param [String] environment
|
48
|
+
# the environment this command is being run on
|
49
|
+
def run(job, environment, *args)
|
50
|
+
raise AbstractFunction, "#run(environment, *args) must be implemented on #{self.class}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
RESERVED_KEYWORDS = [
|
55
|
+
:name,
|
56
|
+
:version,
|
57
|
+
:description,
|
58
|
+
:author,
|
59
|
+
:email,
|
60
|
+
:depends,
|
61
|
+
:command,
|
62
|
+
:component,
|
63
|
+
:group,
|
64
|
+
:execute
|
65
|
+
].freeze
|
66
|
+
|
67
|
+
class << self
|
68
|
+
# Registers a given Class as a Gear to be used within MotherBrain plugins. This
|
69
|
+
# function is automatically called when {MotherBrain::Gear} is included into
|
70
|
+
# a Class. You probably don't want to call this function directly.
|
71
|
+
#
|
72
|
+
# @api private
|
73
|
+
#
|
74
|
+
# @param [~MotherBrain::Gear] klass
|
75
|
+
def register(klass)
|
76
|
+
validate_keyword(klass.keyword)
|
77
|
+
|
78
|
+
all.add(klass)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Return a Set containing all of the registered Gears that can be used within
|
82
|
+
# a MotherBrain plugin.
|
83
|
+
#
|
84
|
+
# @return [Set<MotherBrain::Gear>]
|
85
|
+
def all
|
86
|
+
@all ||= Set.new
|
87
|
+
end
|
88
|
+
|
89
|
+
# Clears all of the registered Gears and then traverses the ObjectSpace to find
|
90
|
+
# all classes which include {MotherBrain::Gear} and calls {::register} with each.
|
91
|
+
#
|
92
|
+
# @return [Set<MotherBrain::Gear>]
|
93
|
+
# the Set of registered Gears
|
94
|
+
def reload!
|
95
|
+
clear!
|
96
|
+
ObjectSpace.each_object(::Module).each do |mod|
|
97
|
+
if mod < MB::Gear
|
98
|
+
register(mod)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
all
|
103
|
+
end
|
104
|
+
|
105
|
+
# Clears all of the registered Gears.
|
106
|
+
#
|
107
|
+
# @return [Set]
|
108
|
+
# an empty Set
|
109
|
+
def clear!
|
110
|
+
@all = Set.new
|
111
|
+
end
|
112
|
+
|
113
|
+
# @param [Symbol] keyword
|
114
|
+
#
|
115
|
+
# @return [MotherBrain::Gear, nil]
|
116
|
+
def find_by_keyword(keyword)
|
117
|
+
all.find { |klass| klass.keyword == keyword }
|
118
|
+
end
|
119
|
+
|
120
|
+
def element_name(klass)
|
121
|
+
klass.keyword
|
122
|
+
end
|
123
|
+
|
124
|
+
def get_fun(klass)
|
125
|
+
element_name(klass)
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
# Determine if the given keyword is valid
|
131
|
+
#
|
132
|
+
# @param [Symbol] keyword
|
133
|
+
#
|
134
|
+
# @raise [ReservedGearKeyword] if the given keyword is reserved
|
135
|
+
# @raise [DuplicateGearKeyword] if the given keyword has already been registered
|
136
|
+
#
|
137
|
+
# @return [Boolean]
|
138
|
+
def validate_keyword(keyword)
|
139
|
+
if RESERVED_KEYWORDS.include?(keyword)
|
140
|
+
reserved = RESERVED_KEYWORDS.collect { |key| "'#{key}'" }
|
141
|
+
raise ReservedGearKeyword, "'#{keyword}' is a reserved keyword. Reserved Keywords: #{reserved.join(', ')}."
|
142
|
+
end
|
143
|
+
|
144
|
+
culprit = find_by_keyword(keyword)
|
145
|
+
|
146
|
+
unless culprit.nil?
|
147
|
+
raise DuplicateGearKeyword, "Keyword '#{keyword}' already used by #{culprit}"
|
148
|
+
end
|
149
|
+
|
150
|
+
true
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/lib/mb/gears.rb
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Gear
|
3
|
+
class DynamicService < Gear::Base
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Parses a service, creates a new instance of DynamicService
|
7
|
+
# and executes a Chef run to change the state of the service.
|
8
|
+
#
|
9
|
+
# @param service [String]
|
10
|
+
# a dotted string "component.service_name"
|
11
|
+
# @param plugin [MB::Plugin]
|
12
|
+
# the plugin currently in use
|
13
|
+
# @param environment [String]
|
14
|
+
# the environment to operate on
|
15
|
+
# @param state [String]
|
16
|
+
# the state of the service to change to
|
17
|
+
# @param options [Hash]
|
18
|
+
#
|
19
|
+
# @return [MB::JobTicket]
|
20
|
+
def change_service_state(service, plugin, environment, state, run_chef = true, options = {})
|
21
|
+
job = Job.new(:dynamic_service_state_change)
|
22
|
+
|
23
|
+
component, service_name = service.split('.')
|
24
|
+
raise InvalidDynamicService.new(component, service_name) unless component && service_name
|
25
|
+
dynamic_service = new(component, service_name)
|
26
|
+
dynamic_service.state_change(job, plugin, environment, state, run_chef, options)
|
27
|
+
job.report_success
|
28
|
+
job.ticket
|
29
|
+
rescue => ex
|
30
|
+
job.report_failure(ex)
|
31
|
+
ensure
|
32
|
+
job.terminate if job && job.alive?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
include MB::Mixin::Services
|
37
|
+
include MB::Mixin::Locks
|
38
|
+
include MB::Logging
|
39
|
+
|
40
|
+
START = "start".freeze
|
41
|
+
STOP = "stop".freeze
|
42
|
+
RESTART = "restart".freeze
|
43
|
+
COMMON_STATES = [
|
44
|
+
START,
|
45
|
+
STOP,
|
46
|
+
RESTART
|
47
|
+
].freeze
|
48
|
+
|
49
|
+
# @return [String]
|
50
|
+
attr_reader :component
|
51
|
+
# @return [String]
|
52
|
+
attr_reader :name
|
53
|
+
|
54
|
+
def initialize(component, name)
|
55
|
+
@name = name
|
56
|
+
@component = component
|
57
|
+
end
|
58
|
+
|
59
|
+
# Executes a bulk chef run on a group of nodes using the service recipe.
|
60
|
+
# Default behavior is to set a node attribute on each individual node serially
|
61
|
+
# and then execute the chef run.
|
62
|
+
#
|
63
|
+
# @param job [MB::Job]
|
64
|
+
# @param plugin [MB::Plugin]
|
65
|
+
# the plugin currently in use
|
66
|
+
# @param environment [String]
|
67
|
+
# the environment to execute on
|
68
|
+
# @param state [String]
|
69
|
+
# the state to change the service to
|
70
|
+
# @param options [Hash]
|
71
|
+
#
|
72
|
+
# @return [MB::JobTicket]
|
73
|
+
def state_change(job, plugin, environment, state, run_chef = true, options = {})
|
74
|
+
log.warn {
|
75
|
+
"Component's service state is being changed to #{state}, which is not one of #{COMMON_STATES}"
|
76
|
+
} unless COMMON_STATES.include?(state)
|
77
|
+
|
78
|
+
chef_synchronize(chef_environment: environment, force: options[:force]) do
|
79
|
+
component_object = plugin.component(component)
|
80
|
+
service_object = component_object.get_service(name)
|
81
|
+
group = component_object.group(service_object.service_group)
|
82
|
+
nodes = group.nodes(environment)
|
83
|
+
job.report_running("preparing to change the #{name} service to #{state}")
|
84
|
+
|
85
|
+
if options[:cluster_override]
|
86
|
+
set_environment_attribute(job, environment, service_object.service_attribute, state)
|
87
|
+
else
|
88
|
+
unset_environment_attribute(job, environment, service_object.service_attribute)
|
89
|
+
set_node_attributes(job, nodes, service_object.service_attribute, state)
|
90
|
+
end
|
91
|
+
if run_chef
|
92
|
+
node_querier.bulk_chef_run(job, nodes, service_object.service_recipe)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def node_state_change(job, plugin, node, state, run_chef = true)
|
98
|
+
log.warn {
|
99
|
+
"Component's service state is being changed to #{state}, which is not one of #{COMMON_STATES}"
|
100
|
+
} unless COMMON_STATES.include?(state)
|
101
|
+
|
102
|
+
component_object = plugin.component(component)
|
103
|
+
service = component_object.get_service(name)
|
104
|
+
set_node_attributes(job, [node], service.service_attribute, state)
|
105
|
+
if run_chef
|
106
|
+
node_querier.bulk_chef_run(job, [node], service.service_recipe)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def remove_node_state_change(job, plugin, node, run_chef = true)
|
111
|
+
component_object = plugin.component(component)
|
112
|
+
service = component_object.get_service(name)
|
113
|
+
|
114
|
+
unset_node_attributes(job, [node], service.service_attribute)
|
115
|
+
if run_chef
|
116
|
+
node_querier.bulk_chef_run(job, [node], service.service_recipe)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Finds the environment object, sets an attribute at the
|
121
|
+
# override level, and saves the environment back to the
|
122
|
+
# Chef server
|
123
|
+
#
|
124
|
+
# @param job [MB::Job]
|
125
|
+
# the job to track status
|
126
|
+
# @param environment [String]
|
127
|
+
# the environment being operated on
|
128
|
+
# @param attribute_keys [Array<String>]
|
129
|
+
# an array of dotted paths to attribute keys
|
130
|
+
# @param state [String]
|
131
|
+
# the state to set the attribute to
|
132
|
+
#
|
133
|
+
# @return [Boolean]
|
134
|
+
def set_environment_attribute(job, environment, attribute_keys, state)
|
135
|
+
chef_environment = get_chef_environment(environment)
|
136
|
+
|
137
|
+
attribute_keys.each do |attribute_key|
|
138
|
+
job.set_status("Setting environment attribute '#{attribute_key}' to #{state} on #{environment}")
|
139
|
+
chef_environment.set_override_attribute(attribute_key, state)
|
140
|
+
end
|
141
|
+
chef_environment.save
|
142
|
+
end
|
143
|
+
|
144
|
+
# Finds and returns the current Chef environment
|
145
|
+
#
|
146
|
+
# @param environment [String]
|
147
|
+
#
|
148
|
+
# @return [Ridley::EnvironmentObject]
|
149
|
+
def get_chef_environment(environment)
|
150
|
+
chef_connection.environment.find(environment)
|
151
|
+
end
|
152
|
+
|
153
|
+
# Deletes an override attribute from the environment
|
154
|
+
#
|
155
|
+
# @param job [MB::Job]
|
156
|
+
# @param environment [String]
|
157
|
+
# @param attribute_keys [Array<String>]
|
158
|
+
#
|
159
|
+
# @return [Boolean]
|
160
|
+
def unset_environment_attribute(job, environment, attribute_keys)
|
161
|
+
chef_environment = get_chef_environment(environment)
|
162
|
+
|
163
|
+
attribute_keys.each do |attribute_key|
|
164
|
+
job.set_status("Unsetting environment attribute '#{attribute_key}' on #{environment}")
|
165
|
+
chef_environment.delete_override_attribute(attribute_key)
|
166
|
+
end
|
167
|
+
chef_environment.save
|
168
|
+
end
|
169
|
+
|
170
|
+
# Sets a default node attribute on the provided array
|
171
|
+
# of nodes.
|
172
|
+
#
|
173
|
+
# @param job [MB::Job]
|
174
|
+
# the job to track status
|
175
|
+
# @param nodes [Array<Ridley::NodeObject>]
|
176
|
+
# the nodes being operated on
|
177
|
+
# @param attribute_keys [Array<String>]
|
178
|
+
# an array of dotted paths to attribute keys
|
179
|
+
# @param state [String]
|
180
|
+
# the state to set the attribute to
|
181
|
+
#
|
182
|
+
# @return [Boolean]
|
183
|
+
def set_node_attributes(job, nodes, attribute_keys, state)
|
184
|
+
nodes.concurrent_map do |node|
|
185
|
+
node.reload
|
186
|
+
attribute_keys.each do |attribute_key|
|
187
|
+
job.set_status("Setting node attribute '#{attribute_key}' to #{state.nil? ? 'nil' : state} on #{node.name}")
|
188
|
+
node.set_chef_attribute(attribute_key, state)
|
189
|
+
end
|
190
|
+
node.save
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
# Removes a default node attribute on the provided array
|
195
|
+
# of nodes.
|
196
|
+
#
|
197
|
+
# @param job [MB::Job]
|
198
|
+
# the job to track status
|
199
|
+
# @param nodes [Array<Ridley::NodeObject>]
|
200
|
+
# the nodes being operated on
|
201
|
+
# @param attribute_keys [Array<String>]
|
202
|
+
# an array of dotted paths to attribute keys
|
203
|
+
#
|
204
|
+
# @return [Boolean]
|
205
|
+
def unset_node_attributes(job, nodes, attribute_keys)
|
206
|
+
nodes.concurrent_map do |node|
|
207
|
+
node.reload
|
208
|
+
attribute_keys.each do |attribute_key|
|
209
|
+
job.set_status("Unsetting node attribute '#{attribute_key}' to respect default and environment attributes.")
|
210
|
+
node.unset_chef_attribute(attribute_key)
|
211
|
+
end
|
212
|
+
node.save
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|