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,31 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Logging
|
3
|
+
class BasicFormat < Logger::Formatter
|
4
|
+
def call(severity, datetime, progname, msg)
|
5
|
+
message = msg.to_s
|
6
|
+
|
7
|
+
if match = message.match(/NODE\[.+?\]/)
|
8
|
+
format_matched_lines match, message
|
9
|
+
else
|
10
|
+
"[#{datetime.utc.iso8601}] PID[#{Process.pid}] TID[#{Thread.current.object_id.to_s(36)}] #{severity}: #{message}\n"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def format_matched_lines(match, message)
|
17
|
+
lines = message.lines.to_a
|
18
|
+
|
19
|
+
lines.map! do |line|
|
20
|
+
if line.start_with? match.to_s
|
21
|
+
line
|
22
|
+
else
|
23
|
+
"#{match} #{line}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
lines.join("\n") << "\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/mb/manifest.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
class Manifest < Hash
|
3
|
+
class << self
|
4
|
+
# @param [#to_s] path
|
5
|
+
#
|
6
|
+
# @raise [ManifestNotFound] if the manifest file is not found
|
7
|
+
#
|
8
|
+
# @return [Manifest]
|
9
|
+
def from_file(path)
|
10
|
+
path = File.expand_path(path.to_s)
|
11
|
+
data = File.read(path)
|
12
|
+
obj = new.from_json(data)
|
13
|
+
obj.path = path
|
14
|
+
obj
|
15
|
+
rescue Errno::ENOENT
|
16
|
+
raise ManifestNotFound, "No manifest found at: '#{path}'"
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param [#to_s] data
|
20
|
+
#
|
21
|
+
# @return [Manifest]
|
22
|
+
def from_json(data)
|
23
|
+
new.from_json(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
# @param [Hash] data
|
27
|
+
#
|
28
|
+
# @return [Manifest]
|
29
|
+
def from_hash(data)
|
30
|
+
new.from_hash(data)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# return [String]
|
35
|
+
attr_accessor :path
|
36
|
+
|
37
|
+
# @param [Hash] attributes (Hash.new)
|
38
|
+
def initialize(attributes = Hash.new)
|
39
|
+
if attributes && attributes.any?
|
40
|
+
from_hash(attributes)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param [String] json
|
45
|
+
# @param [Hash] options
|
46
|
+
# @see MultiJson.decode
|
47
|
+
#
|
48
|
+
# @raise [InvalidManifest] if the given string is not valid JSON
|
49
|
+
#
|
50
|
+
# @return [Manifest]
|
51
|
+
def from_json(json, options = {})
|
52
|
+
from_hash(MultiJson.decode(json, options))
|
53
|
+
rescue MultiJson::DecodeError => error
|
54
|
+
raise InvalidManifest, error
|
55
|
+
end
|
56
|
+
|
57
|
+
# @param [Hash] hash
|
58
|
+
#
|
59
|
+
# @return [Manifest]
|
60
|
+
def from_hash(hash)
|
61
|
+
mass_assign(hash)
|
62
|
+
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param [String] path
|
67
|
+
#
|
68
|
+
# @raise [MB::InternalError] if the path attribute is nil or an empty string
|
69
|
+
#
|
70
|
+
# @return [Manifest]
|
71
|
+
def save(path = nil)
|
72
|
+
self.path = path || self.path
|
73
|
+
|
74
|
+
unless path.present?
|
75
|
+
raise InternalError, "Cannot save manifest without a destination. Set the 'path' attribute on your object."
|
76
|
+
end
|
77
|
+
|
78
|
+
FileUtils.mkdir_p(File.dirname(path))
|
79
|
+
File.open(path, 'w+') do |file|
|
80
|
+
file.write(MultiJson.dump(self, pretty: true))
|
81
|
+
end
|
82
|
+
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
# @return [Array]
|
87
|
+
def node_groups
|
88
|
+
self[:nodes] || []
|
89
|
+
end
|
90
|
+
|
91
|
+
# @return [Hash]
|
92
|
+
def options
|
93
|
+
self[:options] || {}
|
94
|
+
end
|
95
|
+
|
96
|
+
# Returns the number of nodes expected to be created by this manifest regardless of type
|
97
|
+
#
|
98
|
+
# @return [Integer]
|
99
|
+
def node_count
|
100
|
+
node_groups.reduce(0) { |total, node|
|
101
|
+
total + (node[:count] || 1)
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
# Assign the key value pairs of the given hash to self
|
108
|
+
#
|
109
|
+
# @param [Hash] hash
|
110
|
+
def mass_assign(hash)
|
111
|
+
hash.each_pair do |key, value|
|
112
|
+
self[key] = value
|
113
|
+
end
|
114
|
+
|
115
|
+
deep_symbolize_keys!
|
116
|
+
|
117
|
+
each do |key, value|
|
118
|
+
if value.is_a?(Array)
|
119
|
+
value.each do |object|
|
120
|
+
if object.respond_to?(:deep_symbolize_keys!)
|
121
|
+
object.deep_symbolize_keys!
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/lib/mb/mixin.rb
ADDED
@@ -0,0 +1,265 @@
|
|
1
|
+
require 'mb/mixin/services'
|
2
|
+
|
3
|
+
module MotherBrain
|
4
|
+
module Mixin
|
5
|
+
module AttributeSetting
|
6
|
+
extend Forwardable
|
7
|
+
include MB::Logging
|
8
|
+
include MB::Mixin::Services
|
9
|
+
|
10
|
+
# Set the appropriate attributes at the environment level to the desired version
|
11
|
+
# for each component given
|
12
|
+
#
|
13
|
+
# @param [String] env_id
|
14
|
+
# the name identifier of the environment to modify
|
15
|
+
# @param [MB::Plugin] plugin
|
16
|
+
# the plugin to use for finding the appropriate version attributes
|
17
|
+
# @param [Hash] component_versions
|
18
|
+
# Hash of components and the versions to set them to
|
19
|
+
#
|
20
|
+
# @raise [MB::EnvironmentNotFound] if the environment does not exist
|
21
|
+
#
|
22
|
+
# @example setting the versions of multiple components on an environment
|
23
|
+
#
|
24
|
+
# set_component_versions("test-environment",
|
25
|
+
# "component_one" => "1.0.0",
|
26
|
+
# "component_two" => "2.3.0"
|
27
|
+
# )
|
28
|
+
def set_component_versions(env_id, plugin, component_versions)
|
29
|
+
default_attributes = Hash.new
|
30
|
+
|
31
|
+
component_versions.each do |component_name, version|
|
32
|
+
version_hash = Hash.from_dotted_path(version_attribute(plugin, component_name), version)
|
33
|
+
default_attributes.deep_merge!(version_hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
log.info "Setting component versions #{component_versions}"
|
37
|
+
|
38
|
+
unless env = chef_connection.environment.find(env_id)
|
39
|
+
raise EnvironmentNotFound.new(env_id)
|
40
|
+
end
|
41
|
+
|
42
|
+
env.default_attributes.merge!(default_attributes)
|
43
|
+
env.save
|
44
|
+
end
|
45
|
+
|
46
|
+
# Lock the cookbook versions on the target environment from the given hash of
|
47
|
+
# cookbooks and versions
|
48
|
+
#
|
49
|
+
# @param [String] env_id
|
50
|
+
# the name identifier of the environment to modify
|
51
|
+
# @param [Hash] cookbook_versions
|
52
|
+
# Hash of cookbooks and the versions to set them to
|
53
|
+
#
|
54
|
+
# @raise [MB::EnvironmentNotFound] if the environment does not exist
|
55
|
+
#
|
56
|
+
# @example setting cookbook versions on an environment
|
57
|
+
#
|
58
|
+
# set_cookbook_versions("test-environment",
|
59
|
+
# "league" => "1.74.2",
|
60
|
+
# "pvpnet" => "3.2.0"
|
61
|
+
# )
|
62
|
+
#
|
63
|
+
# @raise [ArgumentError] if given invalid version constraints
|
64
|
+
def set_cookbook_versions(env_id, cookbook_versions)
|
65
|
+
cookbook_versions = expand_constraints(expand_latest_versions(cookbook_versions))
|
66
|
+
satisfies_constraints?(cookbook_versions)
|
67
|
+
log.info "Setting cookbook versions #{cookbook_versions}"
|
68
|
+
|
69
|
+
unless env = chef_connection.environment.find(env_id)
|
70
|
+
raise EnvironmentNotFound.new(env_id)
|
71
|
+
end
|
72
|
+
|
73
|
+
env.cookbook_versions.merge!(cookbook_versions)
|
74
|
+
env.save
|
75
|
+
end
|
76
|
+
|
77
|
+
# Set environment level attributes on an environment from a hash containing
|
78
|
+
# keys identifying attributes using the dot notation syntax and values of those
|
79
|
+
# attributes as values.
|
80
|
+
#
|
81
|
+
# @param [String] env_id
|
82
|
+
# the name identifier of the environment to modify
|
83
|
+
# @param [Hash] new_attributes
|
84
|
+
# Hash of attributes and values
|
85
|
+
#
|
86
|
+
# @example setting multiple attributes on an environment
|
87
|
+
#
|
88
|
+
# set_environment_attributes("test-environment",
|
89
|
+
# "foo" => "bar",
|
90
|
+
# "baz.quux" => 42
|
91
|
+
# )
|
92
|
+
def set_environment_attributes(env_id, new_attributes)
|
93
|
+
default_attributes = Hash.new
|
94
|
+
|
95
|
+
new_attributes.each do |attribute, value|
|
96
|
+
attribute_hash = Hash.from_dotted_path(attribute.to_s, value.to_s)
|
97
|
+
default_attributes.deep_merge!(attribute_hash)
|
98
|
+
end
|
99
|
+
|
100
|
+
set_environment_attributes_from_hash(env_id, default_attributes)
|
101
|
+
end
|
102
|
+
|
103
|
+
# Set arbitrary attributes at the environment level
|
104
|
+
#
|
105
|
+
# @param [String] env_id
|
106
|
+
# the name of the environment to modify
|
107
|
+
# @param [Hash] new_attributes
|
108
|
+
# Hash of attributes to set on the environment
|
109
|
+
#
|
110
|
+
# @raise [MB::EnvironmentNotFound] if the environment does not exist
|
111
|
+
#
|
112
|
+
# @example setting multiple attributes on an environment
|
113
|
+
#
|
114
|
+
# set_environment_attributes_from_hash("test-environment",
|
115
|
+
# "foo" => "bar",
|
116
|
+
# "baz => {
|
117
|
+
# "quux" => 42
|
118
|
+
# }
|
119
|
+
# )
|
120
|
+
def set_environment_attributes_from_hash(env_id, new_attributes)
|
121
|
+
log.info "Setting environment attributes: #{new_attributes}"
|
122
|
+
|
123
|
+
unless env = chef_connection.environment.find(env_id)
|
124
|
+
raise EnvironmentNotFound.new(env_id)
|
125
|
+
end
|
126
|
+
|
127
|
+
env.default_attributes.deep_merge!(new_attributes)
|
128
|
+
env.save
|
129
|
+
end
|
130
|
+
|
131
|
+
# Set environment level attributes on an environment from the contents of the file
|
132
|
+
# at the given filepath
|
133
|
+
#
|
134
|
+
# @param [String] env_id
|
135
|
+
# the name of the environment to modify
|
136
|
+
# @param [String] filepath
|
137
|
+
# path to the file to read attributes from
|
138
|
+
# @param [Symbol] type
|
139
|
+
# the type of the contents found within the file
|
140
|
+
#
|
141
|
+
# @raise [InvalidAttributesFile]
|
142
|
+
# if the contents of the attributes file are missing or not well formed
|
143
|
+
# @raise [ArgumentError]
|
144
|
+
# if an unknown value for the 'type' parameter is specified
|
145
|
+
def set_environment_attributes_from_file(env_id, filepath, type = :json)
|
146
|
+
contents = File.read(filepath)
|
147
|
+
|
148
|
+
case type
|
149
|
+
when :json
|
150
|
+
set_environment_attributes_from_json(env_id, contents)
|
151
|
+
else
|
152
|
+
raise ArgumentError, "cannot read attributes from files of type: #{type}"
|
153
|
+
end
|
154
|
+
rescue MultiJson::DecodeError => ex
|
155
|
+
raise InvalidAttributesFile, "Invalid JSON in #{filepath}: #{ex}"
|
156
|
+
end
|
157
|
+
|
158
|
+
# Set environment level attributes on an environment from the given JSON string
|
159
|
+
#
|
160
|
+
# @param [String] env_id
|
161
|
+
# the name of the environment to modify
|
162
|
+
# @param [String] json
|
163
|
+
# the json to use as attributes
|
164
|
+
def set_environment_attributes_from_json(env_id, json)
|
165
|
+
set_environment_attributes_from_hash(env_id, MultiJson.decode(json))
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
# retrieve the version attribute of a given component and raise if the
|
171
|
+
# component is not versioned
|
172
|
+
#
|
173
|
+
# @param [MB::Plugin] plugin
|
174
|
+
# @param [#to_s] component_name
|
175
|
+
#
|
176
|
+
# @raise [ComponentNotVersioned]
|
177
|
+
#
|
178
|
+
# @return [String]
|
179
|
+
def version_attribute(plugin, component_name)
|
180
|
+
result = plugin.component!(component_name).version_attribute
|
181
|
+
|
182
|
+
unless result
|
183
|
+
raise ComponentNotVersioned.new component_name
|
184
|
+
end
|
185
|
+
|
186
|
+
log.info "Component '#{component_name}' versioned with '#{result}'"
|
187
|
+
|
188
|
+
result
|
189
|
+
end
|
190
|
+
|
191
|
+
# Expand the "latest" cookbook versions to the latest verison number
|
192
|
+
# for the cookbook
|
193
|
+
#
|
194
|
+
# @param [Hash] cookbook_versions
|
195
|
+
# Hash of cookbooks and the versions
|
196
|
+
#
|
197
|
+
# @example expanding versions when 3.1.0 is the latest pvpnet cookbook
|
198
|
+
#
|
199
|
+
# expand_latest_versions(
|
200
|
+
# "league" => "1.74.2",
|
201
|
+
# "pvpnet" => "latest"
|
202
|
+
# )
|
203
|
+
#
|
204
|
+
# # => {"league" => "1.74.2", "pvpnet" => "3.1.0"}
|
205
|
+
def expand_latest_versions(cookbook_versions)
|
206
|
+
expanded_cookbook_versions = cookbook_versions.map do |name, version|
|
207
|
+
if version.downcase == "latest"
|
208
|
+
version = chef_connection.cookbook.latest_version(name)
|
209
|
+
end
|
210
|
+
|
211
|
+
[name, version]
|
212
|
+
end
|
213
|
+
|
214
|
+
Hash[expanded_cookbook_versions]
|
215
|
+
end
|
216
|
+
|
217
|
+
# Expand constraints strings given to their fully qualified constraint operators
|
218
|
+
#
|
219
|
+
# @param [Hash] cookbook_versions
|
220
|
+
# Hash of cookbooks and the versions
|
221
|
+
#
|
222
|
+
# @raise [ArgumentError] if the array of constraints contains an entry which is not in the
|
223
|
+
# correct version constraint format
|
224
|
+
#
|
225
|
+
# @example
|
226
|
+
# expand_constraints(
|
227
|
+
# "league" => "1.74.2",
|
228
|
+
# "pvpnet" => ">= 1.2.3"
|
229
|
+
# )
|
230
|
+
#
|
231
|
+
# # => { "league" => "= 1.74.2", "pvpnet" => ">= 1.2.3" }
|
232
|
+
#
|
233
|
+
# @return [Hash]
|
234
|
+
def expand_constraints(cookbook_versions)
|
235
|
+
expanded = cookbook_versions.collect do |name, constraint|
|
236
|
+
[name, Solve::Constraint.new(constraint).to_s]
|
237
|
+
end
|
238
|
+
|
239
|
+
Hash[expanded]
|
240
|
+
rescue Solve::Errors::InvalidConstraintFormat => ex
|
241
|
+
raise ArgumentError, ex
|
242
|
+
end
|
243
|
+
|
244
|
+
# Ensure the chef server can satisfy the desired cookbook version constraints
|
245
|
+
#
|
246
|
+
# @param [Hash] cookbook_versions
|
247
|
+
# Hash of cookbooks and the versions
|
248
|
+
#
|
249
|
+
# @raise [MB::CookbookConstraintNotSatisfied]
|
250
|
+
# if the constraints cannot be satisfied
|
251
|
+
def satisfies_constraints?(cookbook_versions)
|
252
|
+
failures = cookbook_versions.concurrent_map do |name, constraint|
|
253
|
+
if chef_connection.cookbook.satisfy(name, constraint).nil?
|
254
|
+
"#{name} (#{constraint})"
|
255
|
+
end
|
256
|
+
end.compact
|
257
|
+
|
258
|
+
unless failures.empty?
|
259
|
+
raise MB::CookbookConstraintNotSatisfied,
|
260
|
+
"couldn't satisfy constraints for cookbook version(s): #{failures.join(', ')}"
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Mixin
|
3
|
+
# Include a collection of helper functions for exiting with exit statuses from error constants
|
4
|
+
# and determining exit statuses for string representations of motherbrain exceptions.
|
5
|
+
module CodedExit
|
6
|
+
class << self
|
7
|
+
def included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
base.send(:include, ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
def extended(base)
|
13
|
+
base.extend(ClassMethods)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
# Exit the application with the exit status associated with the given motherbrain error
|
19
|
+
#
|
20
|
+
# @param [String, #exit_code] obj
|
21
|
+
#
|
22
|
+
# @example exit the application with an exit status for InvalidConfig (14)
|
23
|
+
# exit_with(MB::InvalidConfig)
|
24
|
+
#
|
25
|
+
# @raise [SystemExit]
|
26
|
+
def exit_with(obj)
|
27
|
+
err_const = obj.is_a?(String) ? constant_for(obj) : obj
|
28
|
+
exit_code = err_const.try(:exit_code) || MBError::DEFAULT_EXIT_CODE
|
29
|
+
|
30
|
+
Kernel.exit(exit_code)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Look up the exit status for motherbrain error matching the given name
|
34
|
+
#
|
35
|
+
# @param [String] const_name
|
36
|
+
# name of the error constant to lookup
|
37
|
+
#
|
38
|
+
# @example retrieving the exit status for MB::InvalidConfig
|
39
|
+
# exit_code_for("InvalidConfig") #=> 14
|
40
|
+
#
|
41
|
+
# @return [Integer]
|
42
|
+
def exit_code_for(const_name)
|
43
|
+
MB.const_get(const_name).exit_code
|
44
|
+
end
|
45
|
+
alias_method :exit_status_for, :exit_code_for
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|