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,73 @@
|
|
1
|
+
require 'faraday_middleware/response/follow_redirects'
|
2
|
+
|
3
|
+
module MotherBrain
|
4
|
+
module Bootstrap
|
5
|
+
class Template
|
6
|
+
class << self
|
7
|
+
# Install a bootstrap template into the templates directory
|
8
|
+
#
|
9
|
+
# @param [String] name
|
10
|
+
# the name of the template
|
11
|
+
# @param [String] filename_or_url
|
12
|
+
# a local filename or URL to download
|
13
|
+
#
|
14
|
+
# @raise [MB::BootstrapTemplateNotFound] if the file cannot be installed
|
15
|
+
def install(name, filename_or_url)
|
16
|
+
template_path = MB::FileSystem.templates.join("#{name}.erb")
|
17
|
+
if template_path.exist?
|
18
|
+
raise MB::BootstrapTemplateNotFound, "Template named `#{name}` already installed"
|
19
|
+
end
|
20
|
+
MB.log.info "Installing bootstrap template `#{name}` from #{filename_or_url}"
|
21
|
+
name += ".erb"
|
22
|
+
if filename_or_url.match(URI.regexp)
|
23
|
+
if filename_or_url.match(URI.regexp(['http','https']))
|
24
|
+
uri = URI.parse(filename_or_url)
|
25
|
+
begin
|
26
|
+
conn = Faraday.new do |b|
|
27
|
+
b.use FaradayMiddleware::FollowRedirects
|
28
|
+
b.adapter :net_http
|
29
|
+
end
|
30
|
+
response = conn.get filename_or_url
|
31
|
+
template_path.open("w+") do |file|
|
32
|
+
file.write(response.body)
|
33
|
+
end
|
34
|
+
rescue Exception => ex
|
35
|
+
raise MB::BootstrapTemplateNotFound, ex
|
36
|
+
end
|
37
|
+
else
|
38
|
+
raise MB::BootstrapTemplateNotFound, "Only http/https URLs are supported"
|
39
|
+
end
|
40
|
+
elsif File.exists?(filename_or_url)
|
41
|
+
FileUtils.copy(filename_or_url, template_path.to_s)
|
42
|
+
else
|
43
|
+
raise MB::BootstrapTemplateNotFound, "Couldn't find the template to install or the protocol given is not supported."
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Find and validate a user-chosen template
|
48
|
+
#
|
49
|
+
# @param [String] name_or_path
|
50
|
+
# User's choice of template
|
51
|
+
#
|
52
|
+
# @return [String] the actual path the the chosen template
|
53
|
+
# @return [nil] if there is no template
|
54
|
+
#
|
55
|
+
# @raise [MB::BootstrapTemplateNotFound] if the template file
|
56
|
+
# does not exist
|
57
|
+
def find(name_or_path=nil)
|
58
|
+
name_or_path ||= MB::Application.config.bootstrap.default_template
|
59
|
+
return unless name_or_path
|
60
|
+
installed = MB::FileSystem.templates.join("#{name_or_path}.erb").to_s
|
61
|
+
if File.exists?(installed)
|
62
|
+
return installed
|
63
|
+
end
|
64
|
+
if File.exists?(name_or_path)
|
65
|
+
return name_or_path
|
66
|
+
else
|
67
|
+
raise MB::BootstrapTemplateNotFound
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,227 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Bootstrap
|
3
|
+
# @api private
|
4
|
+
class Worker
|
5
|
+
# Used internally within the {Bootstrap::Worker} to identify hosts which should be
|
6
|
+
# partially or fully bootstrapped.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Host
|
10
|
+
# The fully qualified hostname of the machine
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# "reset.riotgames.com"
|
14
|
+
#
|
15
|
+
# @return [String]
|
16
|
+
attr_reader :hostname
|
17
|
+
|
18
|
+
# @param [String] hostname
|
19
|
+
# A fully qualified hostname for a machine
|
20
|
+
def initialize(hostname)
|
21
|
+
@hostname = hostname
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Boolean]
|
25
|
+
def full_bootstrap?
|
26
|
+
node_name.nil?
|
27
|
+
end
|
28
|
+
|
29
|
+
# The name of the machine as seen in Chef
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# "reset"
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
def node_name
|
36
|
+
@node_name ||= NodeQuerier.instance.registered_as(hostname)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Boolean]
|
40
|
+
def partial_bootstrap?
|
41
|
+
node_name.present?
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_s
|
45
|
+
"#{node_name}(#{hostname})"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
include Celluloid
|
50
|
+
include MB::Logging
|
51
|
+
include MB::Mixin::Services
|
52
|
+
|
53
|
+
# Run a bootstrap on each of the hosts given to this instance of {Worker}. There are two different kinds of
|
54
|
+
# bootstrap processes which may be run on a node; a partial bootstrap and a full bootstrap.
|
55
|
+
#
|
56
|
+
# Partial Bootstrap: a node will be partially bootstrapped if it has
|
57
|
+
# 1. Chef installed by omnibus
|
58
|
+
# 2. Ruby installed by omnibus
|
59
|
+
# 3. A Chef client registered with the Chef server.
|
60
|
+
# note: the name of the client is the "node_name" of the node. This obtained by running the
|
61
|
+
# ruby node name script "script/node_name.rb" on the node.
|
62
|
+
#
|
63
|
+
# Full Bootstrap: a node will be fully bootstrapped if it does not satisfy all of the criteria for a
|
64
|
+
# partial bootstrap.
|
65
|
+
#
|
66
|
+
# @example
|
67
|
+
# worker = Worker.new
|
68
|
+
# worker.run("cloud-1.riotgames.com") #=> {
|
69
|
+
# node: "cloud-1.riotgames.com",
|
70
|
+
# status: :ok,
|
71
|
+
# message: "",
|
72
|
+
# bootstrap_type: :full
|
73
|
+
# }
|
74
|
+
#
|
75
|
+
# @param [String] address
|
76
|
+
# a hostname or ipaddress to bootstrap
|
77
|
+
#
|
78
|
+
# @option options [String] :environment
|
79
|
+
# environment to join the node to (default: '_default')
|
80
|
+
# @option options [Hash] :attributes (Hash.new)
|
81
|
+
# a hash of attributes to use in the first Chef run
|
82
|
+
# @option options [Array] :run_list (Array.new)
|
83
|
+
# an initial run list to bootstrap with
|
84
|
+
# @option options [String] :chef_version
|
85
|
+
# version of Chef to install on the node
|
86
|
+
# @option options [Hash] :hints (Hash.new)
|
87
|
+
# a hash of Ohai hints to place on the bootstrapped node
|
88
|
+
# @option options [Boolean] :sudo (true)
|
89
|
+
# bootstrap with sudo
|
90
|
+
# @option options [String] :bootstrap_proxy (nil)
|
91
|
+
# URL to a proxy server to bootstrap through
|
92
|
+
#
|
93
|
+
# @return [Hash]
|
94
|
+
def run(address, options = {})
|
95
|
+
host = Host.new(address)
|
96
|
+
if host.full_bootstrap?
|
97
|
+
full_bootstrap(host, options)
|
98
|
+
else
|
99
|
+
partial_bootstrap(host, options.slice(:attributes, :run_list))
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Perform a bootstrap on a group of nodes which have not yet been registered with the Chef server
|
104
|
+
# and may not have Ruby, Chef, or other requirements installed.
|
105
|
+
#
|
106
|
+
# @example
|
107
|
+
# host = Host.new("cloud-1.riotgames.com")
|
108
|
+
#
|
109
|
+
# worker.full_bootstrap(host) #=> {
|
110
|
+
# node_name: "cloud-1",
|
111
|
+
# hostname: "cloud-1.riotgames.com",
|
112
|
+
# status: :ok
|
113
|
+
# message: "",
|
114
|
+
# bootstrap_type: :full
|
115
|
+
# }
|
116
|
+
#
|
117
|
+
# @param [Worker::Host] host
|
118
|
+
# a host to bootstrap
|
119
|
+
#
|
120
|
+
# @option options [String] :environment
|
121
|
+
# environment to join the node to (default: '_default')
|
122
|
+
# @option options [String] :chef_version
|
123
|
+
# version of Chef to install on the node
|
124
|
+
# @option options [Hash] :attributes (Hash.new)
|
125
|
+
# a hash of attributes to use in the first Chef run
|
126
|
+
# @option options [Array] :run_list (Array.new)
|
127
|
+
# an initial run list to bootstrap with
|
128
|
+
# @option options [Hash] :hints (Hash.new)
|
129
|
+
# a hash of Ohai hints to place on the bootstrapped node
|
130
|
+
# @option options [Boolean] :sudo (true)
|
131
|
+
# bootstrap with sudo
|
132
|
+
#
|
133
|
+
# @return [Hash]
|
134
|
+
def full_bootstrap(host, options = {})
|
135
|
+
options = options.reverse_merge(
|
136
|
+
run_list: Array.new,
|
137
|
+
attributes: Hash.new,
|
138
|
+
hints: Hash.new,
|
139
|
+
sudo: true
|
140
|
+
)
|
141
|
+
|
142
|
+
begin
|
143
|
+
options[:template] = MB::Bootstrap::Template.find(options[:template])
|
144
|
+
rescue MB::BootstrapTemplateNotFound => e
|
145
|
+
abort e
|
146
|
+
end
|
147
|
+
options.delete(:template) if options[:template].nil?
|
148
|
+
|
149
|
+
ssh_response = chef_connection.node.bootstrap(host.hostname, options)
|
150
|
+
|
151
|
+
{}.tap do |response|
|
152
|
+
response[:node_name] = nil
|
153
|
+
response[:hostname] = ssh_response.host
|
154
|
+
response[:bootstrap_type] = :full
|
155
|
+
response[:message] = ""
|
156
|
+
response[:status] = :ok
|
157
|
+
|
158
|
+
if ssh_response.error?
|
159
|
+
response[:status] = :error
|
160
|
+
response[:message] = ssh_response.stderr.chomp
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# Perform a bootstrap on a group of nodes which have already been registered with the Chef server.
|
166
|
+
#
|
167
|
+
# Partial bootstrap steps:
|
168
|
+
# 1. The given values given for the run_list and attributes options will be merged with the existing
|
169
|
+
# node data
|
170
|
+
# 2. Your organization's secret key will be placed on the node
|
171
|
+
# 3. Perform a chef client run on the target node
|
172
|
+
#
|
173
|
+
# @example
|
174
|
+
# host = Host.new("cloud-1.riotgames.com")
|
175
|
+
# worker.partial_bootstrap(host) #=> {
|
176
|
+
# node_name: "cloud-1",
|
177
|
+
# hostname: "cloud-1.riotgames.com",
|
178
|
+
# status: :ok
|
179
|
+
# message: "",
|
180
|
+
# bootstrap_type: :partial
|
181
|
+
# }
|
182
|
+
#
|
183
|
+
# @param [Worker::Host] host
|
184
|
+
# a host to bootstrap
|
185
|
+
#
|
186
|
+
# @option options [Array] :run_list
|
187
|
+
# a chef run list
|
188
|
+
# @option options [Hash] :attributes
|
189
|
+
# attributes to set on the node at normal precedence
|
190
|
+
#
|
191
|
+
# @return [Hash]
|
192
|
+
def partial_bootstrap(host, options = {})
|
193
|
+
log.info "#{host} is already registered with Chef. Performing a partial bootstrap."
|
194
|
+
|
195
|
+
{}.tap do |response|
|
196
|
+
response[:node_name] = host.node_name
|
197
|
+
response[:hostname] = host.hostname
|
198
|
+
response[:bootstrap_type] = :partial
|
199
|
+
response[:message] = ""
|
200
|
+
response[:status] = :ok
|
201
|
+
|
202
|
+
begin
|
203
|
+
chef_connection.node.merge_data(host.node_name, options.slice(:run_list, :attributes))
|
204
|
+
node_querier.put_secret(host.hostname)
|
205
|
+
node_querier.chef_run(host.hostname)
|
206
|
+
rescue Ridley::Errors::ResourceNotFound => ex
|
207
|
+
response[:status] = :error
|
208
|
+
response[:message] = "Host #{host} has a client on the node and a matching client on the Chef server, " +
|
209
|
+
"but there is no matching node object on the Chef server for the client. This should not happen. " +
|
210
|
+
"Run `mb purge #{host}` or manually destroy the client on the node itself and the node object on " +
|
211
|
+
"the Chef server and re-run your bootstrap."
|
212
|
+
# JW TODO: We can recover here by creating the node object. However, we can't do this right
|
213
|
+
# now because OHC/OPC have ACLs associated with the node object. When we create the object the
|
214
|
+
# API user that we are acting as will be the only user with full permissions to the node
|
215
|
+
# object resulting in a 403 error when the node attempts to save itself back to the Chef server.
|
216
|
+
#
|
217
|
+
# TLDR; The undocumented ACL endpoints need to be implemented in Ridley to support graceful
|
218
|
+
# recovery. https://github.com/RiotGames/ridley/issues/147
|
219
|
+
rescue Exception => ex
|
220
|
+
response[:status] = :error
|
221
|
+
response[:message] = ex.to_s
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
data/lib/mb/chef.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Chef
|
3
|
+
# Handles loading configuration values from a Chef config file
|
4
|
+
class Config < Hash
|
5
|
+
DEFAULT_PATHS = %w[
|
6
|
+
./.chef/knife.rb
|
7
|
+
~/.chef/knife.rb
|
8
|
+
/etc/chef/solo.rb
|
9
|
+
/etc/chef/client.rb
|
10
|
+
]
|
11
|
+
|
12
|
+
# @param [String] path
|
13
|
+
def initialize(path = nil)
|
14
|
+
@path = path
|
15
|
+
end
|
16
|
+
|
17
|
+
# Parse the file for the path and store symbolicated keys for knife
|
18
|
+
# configuration options.
|
19
|
+
#
|
20
|
+
# @return [Knife] self
|
21
|
+
def parse
|
22
|
+
parse_file
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def parse_file
|
29
|
+
lines.each { |line| parse_line line }
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse_line(line)
|
33
|
+
eval line, binding
|
34
|
+
rescue
|
35
|
+
end
|
36
|
+
|
37
|
+
def method_missing(key, value = nil)
|
38
|
+
store key.to_sym, value
|
39
|
+
end
|
40
|
+
|
41
|
+
def lines
|
42
|
+
file_contents.lines.to_a
|
43
|
+
end
|
44
|
+
|
45
|
+
def file_contents
|
46
|
+
File.read(file_path)
|
47
|
+
rescue
|
48
|
+
String.new
|
49
|
+
end
|
50
|
+
|
51
|
+
def file_path
|
52
|
+
File.expand_path(path)
|
53
|
+
end
|
54
|
+
|
55
|
+
def path
|
56
|
+
@path ||= DEFAULT_PATHS.find { |path|
|
57
|
+
File.exist?(File.expand_path(path))
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
# Because it's common to set the local variable current_dir in a knife.rb
|
62
|
+
# and then interpolate that into strings, set it here because that's hard
|
63
|
+
# to parse.
|
64
|
+
def current_dir
|
65
|
+
File.dirname(file_path)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Daniel DeLeo (<dan@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2010 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
# Retrieved on 1/28/2014 @ 24dc69a9a97e82a6e4207de68d6dcc664178249b
|
19
|
+
# Source: https://github.com/opscode/chef/blob/24dc69a9a97e82a6e4207de68d6dcc664178249b/lib/chef/run_list/run_list_item.rb
|
20
|
+
|
21
|
+
module MotherBrain
|
22
|
+
module Chef
|
23
|
+
class RunListItem
|
24
|
+
# MB Specific
|
25
|
+
RECIPE_SEPARATOR = "::"
|
26
|
+
# /MB Specific
|
27
|
+
|
28
|
+
QUALIFIED_RECIPE = %r{^recipe\[([^\]@]+)(@([0-9]+(\.[0-9]+){1,2}))?\]$}
|
29
|
+
QUALIFIED_ROLE = %r{^role\[([^\]]+)\]$}
|
30
|
+
VERSIONED_UNQUALIFIED_RECIPE = %r{^([^@]+)(@([0-9]+(\.[0-9]+){1,2}))$}
|
31
|
+
FALSE_FRIEND = %r{[\[\]]}
|
32
|
+
|
33
|
+
attr_reader :name, :type, :version
|
34
|
+
|
35
|
+
|
36
|
+
def initialize(item)
|
37
|
+
@version = nil
|
38
|
+
case item
|
39
|
+
when Hash
|
40
|
+
assert_hash_is_valid_run_list_item!(item)
|
41
|
+
@type = (item['type'] || item[:type]).to_sym
|
42
|
+
@name = item['name'] || item[:name]
|
43
|
+
if (item.has_key?('version') || item.has_key?(:version))
|
44
|
+
@version = item['version'] || item[:version]
|
45
|
+
end
|
46
|
+
when String
|
47
|
+
if match = QUALIFIED_RECIPE.match(item)
|
48
|
+
# recipe[recipe_name]
|
49
|
+
# recipe[recipe_name@1.0.0]
|
50
|
+
@type = :recipe
|
51
|
+
@name = match[1]
|
52
|
+
@version = match[3] if match[3]
|
53
|
+
elsif match = QUALIFIED_ROLE.match(item)
|
54
|
+
# role[role_name]
|
55
|
+
@type = :role
|
56
|
+
@name = match[1]
|
57
|
+
elsif match = VERSIONED_UNQUALIFIED_RECIPE.match(item)
|
58
|
+
# recipe_name@1.0.0
|
59
|
+
@type = :recipe
|
60
|
+
@name = match[1]
|
61
|
+
@version = match[3] if match[3]
|
62
|
+
elsif match = FALSE_FRIEND.match(item)
|
63
|
+
# Recipe[recipe_name]
|
64
|
+
# roles[role_name]
|
65
|
+
name = match[1]
|
66
|
+
raise ArgumentError, "Unable to create #{self.class} from #{item.class}:#{item.inspect}: must be recipe[#{name}] or role[#{name}]"
|
67
|
+
|
68
|
+
else
|
69
|
+
# recipe_name
|
70
|
+
@type = :recipe
|
71
|
+
@name = item
|
72
|
+
end
|
73
|
+
else
|
74
|
+
raise ArgumentError, "Unable to create #{self.class} from #{item.class}:#{item.inspect}: must be a Hash or String"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_s
|
79
|
+
"#{@type}[#{@name}#{@version ? "@#{@version}" :""}]"
|
80
|
+
end
|
81
|
+
|
82
|
+
def role?
|
83
|
+
@type == :role
|
84
|
+
end
|
85
|
+
|
86
|
+
def recipe?
|
87
|
+
@type == :recipe
|
88
|
+
end
|
89
|
+
|
90
|
+
def ==(other)
|
91
|
+
if other.kind_of?(String)
|
92
|
+
self.to_s == other.to_s
|
93
|
+
else
|
94
|
+
other.respond_to?(:type) && other.respond_to?(:name) && other.respond_to?(:version) && other.type == @type && other.name == @name && other.version == @version
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def assert_hash_is_valid_run_list_item!(item)
|
99
|
+
unless (item.key?('type')|| item.key?(:type)) && (item.key?('name') || item.key?(:name))
|
100
|
+
raise ArgumentError, "Initializing a #{self.class} from a hash requires that it have a 'type' and 'name' key"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# MB specific
|
105
|
+
def cookbook_name
|
106
|
+
if self.recipe?
|
107
|
+
cookbook, _ = self.name.split(RECIPE_SEPARATOR)
|
108
|
+
return cookbook
|
109
|
+
end
|
110
|
+
return nil
|
111
|
+
end
|
112
|
+
# /MB specific
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|