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,72 @@
|
|
1
|
+
require 'reel/rack'
|
2
|
+
|
3
|
+
module MotherBrain
|
4
|
+
class RestGateway < Reel::Rack::Server
|
5
|
+
class << self
|
6
|
+
# @raise [Celluloid::DeadActorError] if rest gateway has not been started
|
7
|
+
#
|
8
|
+
# @return [Celluloid::Actor(Gateway)]
|
9
|
+
def instance
|
10
|
+
MB::Application[:rest_gateway] or raise Celluloid::DeadActorError, "REST Gateway not running"
|
11
|
+
end
|
12
|
+
|
13
|
+
# Start the REST Gateway and add it to the application's registry.
|
14
|
+
#
|
15
|
+
# @note you probably don't want to manually start the REST Gateway unless you are testing. Start
|
16
|
+
# the entire application with {MB::Application.run}
|
17
|
+
def start(options = {})
|
18
|
+
MB::Application[:rest_gateway] = new(options)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Stop the currently running REST Gateway
|
22
|
+
#
|
23
|
+
# @note you probably don't want to manually stop the REST Gateway unless you are testing. Stop
|
24
|
+
# the entire application with {MB::Application.stop}
|
25
|
+
def stop
|
26
|
+
instance.shutdown
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
include MB::Logging
|
31
|
+
|
32
|
+
DEFAULT_PORT = ENV["PORT"].to_i || 26100
|
33
|
+
|
34
|
+
DEFAULT_OPTIONS = {
|
35
|
+
host: '0.0.0.0',
|
36
|
+
port: DEFAULT_PORT,
|
37
|
+
quiet: false
|
38
|
+
}.freeze
|
39
|
+
|
40
|
+
VALID_OPTIONS = [
|
41
|
+
:host,
|
42
|
+
:port,
|
43
|
+
:quiet
|
44
|
+
].freeze
|
45
|
+
|
46
|
+
finalizer :finalize_callback
|
47
|
+
|
48
|
+
# @option options [String] :host ('0.0.0.0')
|
49
|
+
# @option options [Integer] :port (26100)
|
50
|
+
# @option options [Boolean] :quiet (false)
|
51
|
+
def initialize(options = {})
|
52
|
+
log.debug { "REST Gateway starting..." }
|
53
|
+
|
54
|
+
options = DEFAULT_OPTIONS.merge(options.slice(*VALID_OPTIONS))
|
55
|
+
app = MB::API::Application.new
|
56
|
+
|
57
|
+
# reel-rack uses Rack standard capitalizations in > 0.0.2
|
58
|
+
options[:Host] = options[:host]
|
59
|
+
options[:Port] = options[:port]
|
60
|
+
|
61
|
+
log.info { "REST Gateway listening on #{options[:host]}:#{options[:port]}" }
|
62
|
+
super(app, options)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def finalize_callback
|
68
|
+
log.debug { "REST Gateway stopping..." }
|
69
|
+
self.shutdown
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Ridley
|
2
|
+
class CookbookObject < Ridley::ChefObject
|
3
|
+
# Check if the cookbook has the required files to be a motherbrain plugin
|
4
|
+
#
|
5
|
+
# @return [Boolean]
|
6
|
+
def has_motherbrain_plugin?
|
7
|
+
plugin_file = root_files.find { |file| file[:name] == MB::Plugin::PLUGIN_FILENAME }
|
8
|
+
metadata_file = root_files.find do |file|
|
9
|
+
file[:name] == MB::CookbookMetadata::RUBY_FILENAME || file[:name] == MB::CookbookMetadata::JSON_FILENAME
|
10
|
+
end
|
11
|
+
|
12
|
+
plugin_file && metadata_file
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/mb/srv_ctl.rb
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module MotherBrain
|
4
|
+
class SrvCtl
|
5
|
+
class << self
|
6
|
+
include MB::Mixin::CodedExit
|
7
|
+
|
8
|
+
def default_options
|
9
|
+
{
|
10
|
+
config: MB::Config.default_path
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param [Array] args
|
15
|
+
# @param [#to_s] filename
|
16
|
+
#
|
17
|
+
# @return [Hash]
|
18
|
+
def parse(args, filename)
|
19
|
+
options = Hash.new
|
20
|
+
|
21
|
+
OptionParser.new("Usage: #{filename} [options]") do |opts|
|
22
|
+
opts.on("-c", "--config PATH", "path to configuration file", "(default: '#{default_options[:config]}')") do |v|
|
23
|
+
options[:config] = File.expand_path(v)
|
24
|
+
end
|
25
|
+
|
26
|
+
opts.on("-v", "--verbose", "run with verbose output") do
|
27
|
+
options[:log_level] = Logger::DEBUG
|
28
|
+
end
|
29
|
+
|
30
|
+
opts.on("-d", "--daemonize", "run in daemon mode") do
|
31
|
+
options[:daemonize] = true
|
32
|
+
|
33
|
+
unless options[:log_location]
|
34
|
+
options[:log_location] = FileSystem.logs.join('application.log').to_s
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
opts.on("-P", "--pid PATH", String, "pid file to read/write from") do |v|
|
39
|
+
options[:pid_file] = File.expand_path(v)
|
40
|
+
end
|
41
|
+
|
42
|
+
opts.on("-l", "--log PATH", String, "path to log file") do |v|
|
43
|
+
options[:log_location] = v
|
44
|
+
end
|
45
|
+
|
46
|
+
opts.on("-k", "--kill", "kill mbsrv if running") do |v|
|
47
|
+
options[:kill] = v
|
48
|
+
end
|
49
|
+
|
50
|
+
opts.on_tail("-h", "--help", "show this message") do
|
51
|
+
ui.say opts
|
52
|
+
exit
|
53
|
+
end
|
54
|
+
end.parse!(args)
|
55
|
+
|
56
|
+
options
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param [Array] args
|
60
|
+
# @param [String] filename
|
61
|
+
def run(args, filename)
|
62
|
+
MB::Logging.setup
|
63
|
+
|
64
|
+
options = parse(args, filename)
|
65
|
+
ctl = new(options)
|
66
|
+
|
67
|
+
options[:kill] ? ctl.stop : ctl.start
|
68
|
+
rescue MB::MBError => ex
|
69
|
+
ui.error ex
|
70
|
+
exit_with(ex)
|
71
|
+
end
|
72
|
+
|
73
|
+
# @return [MB::Cli::Shell::Color, MB::Cli::Shell::Basic]
|
74
|
+
def ui
|
75
|
+
@ui ||= MB::Cli::Shell.shell.new
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
attr_reader :config
|
80
|
+
|
81
|
+
# @option options [String] :config (MB::Config.default_path)
|
82
|
+
# @option options [Boolean] :daemonize
|
83
|
+
# @option options [String] :pid_file
|
84
|
+
# @option options [Integer] :log_level
|
85
|
+
# @option options [String] :log_location
|
86
|
+
def initialize(options = {})
|
87
|
+
options = self.class.default_options.merge(options)
|
88
|
+
@config = MB::Config.from_file(options[:config])
|
89
|
+
|
90
|
+
unless options[:log_level].nil?
|
91
|
+
@config.log.level = options[:log_level]
|
92
|
+
end
|
93
|
+
|
94
|
+
unless options[:log_location].nil?
|
95
|
+
@config.log.location = options[:log_location]
|
96
|
+
end
|
97
|
+
|
98
|
+
unless options[:daemonize].nil?
|
99
|
+
@config.server.daemonize = options[:daemonize]
|
100
|
+
end
|
101
|
+
|
102
|
+
unless options[:pid_file].nil?
|
103
|
+
@config.server.pid = options[:pid_file]
|
104
|
+
end
|
105
|
+
|
106
|
+
MB::Logging.setup(@config.to_logger)
|
107
|
+
end
|
108
|
+
|
109
|
+
def start
|
110
|
+
if config.server.daemonize
|
111
|
+
if pid
|
112
|
+
ui.say "mbsrv already started"
|
113
|
+
exit 1
|
114
|
+
end
|
115
|
+
|
116
|
+
daemonize
|
117
|
+
end
|
118
|
+
|
119
|
+
MB::Application.run(config)
|
120
|
+
end
|
121
|
+
|
122
|
+
def stop
|
123
|
+
unless pid
|
124
|
+
ui.say "mbsrv not started"
|
125
|
+
exit 0
|
126
|
+
end
|
127
|
+
|
128
|
+
Process.kill('TERM', pid)
|
129
|
+
destroy_pid
|
130
|
+
rescue Errno::ESRCH
|
131
|
+
destroy_pid
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def daemonize
|
137
|
+
unless File.exist?(pid_directory)
|
138
|
+
begin
|
139
|
+
ui.say "creating pid directory at #{pid_directory}"
|
140
|
+
FileUtils.mkdir_p(pid_directory)
|
141
|
+
rescue Errno::EACCES => ex
|
142
|
+
ui.error ex
|
143
|
+
self.class.exit_with(ex)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
unless File.writable?(pid_directory)
|
148
|
+
ui.say "startup failed: couldn't write pid to #{config.server.pid}"
|
149
|
+
exit 1
|
150
|
+
end
|
151
|
+
|
152
|
+
Process.daemon
|
153
|
+
create_pid
|
154
|
+
end
|
155
|
+
|
156
|
+
def create_pid
|
157
|
+
File.open(config.server.pid, 'w') { |f| f.write Process.pid }
|
158
|
+
end
|
159
|
+
|
160
|
+
def pid_directory
|
161
|
+
File.dirname(config.server.pid)
|
162
|
+
end
|
163
|
+
|
164
|
+
def destroy_pid
|
165
|
+
FileUtils.rm(config.server.pid)
|
166
|
+
end
|
167
|
+
|
168
|
+
def pid
|
169
|
+
return nil unless pid_file?
|
170
|
+
pid = File.read(config.server.pid).chomp.to_i
|
171
|
+
return nil unless pid > 0
|
172
|
+
pid
|
173
|
+
end
|
174
|
+
|
175
|
+
def pid_file?
|
176
|
+
File.exists?(config.server.pid)
|
177
|
+
end
|
178
|
+
|
179
|
+
def ui
|
180
|
+
self.class.ui
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
data/lib/mb/test.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'rspec/mocks'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'webmock'
|
4
|
+
|
5
|
+
module MotherBrain
|
6
|
+
module Test
|
7
|
+
def self.mock(type)
|
8
|
+
type = type.to_s
|
9
|
+
klass = begin
|
10
|
+
"::MB::Test::#{type.camelize}".constantize
|
11
|
+
rescue NameError
|
12
|
+
MB.log.warn { "Couldn't find test class for #{type}" }
|
13
|
+
return false
|
14
|
+
end
|
15
|
+
klass.mock!
|
16
|
+
end
|
17
|
+
|
18
|
+
class Base
|
19
|
+
def self.mock!
|
20
|
+
new.register_mocks
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
::RSpec::Mocks.setup(self)
|
25
|
+
WebMock.disable_net_connect!(allow_localhost: true)
|
26
|
+
end
|
27
|
+
|
28
|
+
def type
|
29
|
+
self.class.to_s.sub(/.*::/, '').upcase
|
30
|
+
end
|
31
|
+
|
32
|
+
def available_mocks
|
33
|
+
[]
|
34
|
+
end
|
35
|
+
|
36
|
+
def register_mocks
|
37
|
+
return unless MB.testing?
|
38
|
+
available_mocks.each do |mock|
|
39
|
+
env = ENV["MB_TEST_#{type}_#{mock.upcase}"]
|
40
|
+
self.send(mock, env) if env
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Init < Base
|
46
|
+
include WebMock::API
|
47
|
+
|
48
|
+
def available_mocks
|
49
|
+
[:env, :cookbook, :bootstrap, :template, :template_url]
|
50
|
+
end
|
51
|
+
|
52
|
+
def ridley
|
53
|
+
return @ridley if @ridley
|
54
|
+
@ridley = double('ridley')
|
55
|
+
Application.ridley.wrapped_object.should_receive(:connection).and_return(@ridley)
|
56
|
+
@ridley.should_receive(:alive?).and_return(true)
|
57
|
+
@ridley.should_receive(:terminate).and_return(true)
|
58
|
+
@ridley.should_receive(:url_prefix).and_return("http://chef.example.com")
|
59
|
+
@ridley
|
60
|
+
end
|
61
|
+
|
62
|
+
def env(name)
|
63
|
+
ridley.should_receive(:get).with("environments/#{name}").
|
64
|
+
and_return(double(:response, :body => {}))
|
65
|
+
end
|
66
|
+
|
67
|
+
def cookbook(name, version = nil)
|
68
|
+
ridley.should_receive(:get).with("cookbooks").and_return(double(:response, :body => {}))
|
69
|
+
ridley.should_receive(:get).with("cookbooks/#{name}").and_return(double(:response, :body => {}))
|
70
|
+
plugin = MB::Application.plugin_manager.find(name, version)
|
71
|
+
MB::Application.plugin_manager.should_receive(:for_environment).and_return(plugin)
|
72
|
+
end
|
73
|
+
|
74
|
+
def bootstrap(x)
|
75
|
+
Application.bootstrap_manager.wrapped_object.should_receive(:bootstrap) do |job, environment, manifest, plugin, options|
|
76
|
+
job.report_running
|
77
|
+
job.report_success
|
78
|
+
job.terminate if job.alive?
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def template(name)
|
83
|
+
ridley.should_receive(:get).with("nodes").and_return(double(:response, :body => []))
|
84
|
+
node = double('node')
|
85
|
+
Application.ridley.wrapped_object.should_receive(:node).and_return(node)
|
86
|
+
ssh = double('ssh')
|
87
|
+
node.should_receive(:bootstrap) do |hostnames, options|
|
88
|
+
raise "Template not set!" unless options[:template]
|
89
|
+
raise "Template not right!" unless options[:template] =~ /#{name}/
|
90
|
+
[ssh]
|
91
|
+
end
|
92
|
+
node.should_receive(:all).and_return([])
|
93
|
+
ssh.should_receive(:host).and_return("foo.example.com")
|
94
|
+
ssh.should_receive(:error?).and_return(false)
|
95
|
+
Application.node_querier.wrapped_object.should_receive(:node_name).and_return("foo.example.com")
|
96
|
+
end
|
97
|
+
|
98
|
+
def template_url(name_and_url)
|
99
|
+
name, url = name_and_url.split("##")
|
100
|
+
stub_request(:get, url).to_return(:body => name)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/mb/thor_ext.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Code from https://github.com/wycats/thor/pull/317
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
module Shell
|
5
|
+
class Basic
|
6
|
+
def ask(statement, *args)
|
7
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
8
|
+
color = args.first
|
9
|
+
|
10
|
+
if options[:limited_to]
|
11
|
+
ask_filtered(statement, color, options)
|
12
|
+
else
|
13
|
+
ask_simply(statement, color, options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def ask_simply(statement, color, options)
|
20
|
+
default = options[:default]
|
21
|
+
message = [statement, ("(#{default.inspect})" if default), nil].uniq.join(" ")
|
22
|
+
say(message, color)
|
23
|
+
result = stdin.gets
|
24
|
+
|
25
|
+
return unless result
|
26
|
+
|
27
|
+
result.strip!
|
28
|
+
|
29
|
+
if default && result == ""
|
30
|
+
default
|
31
|
+
else
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def ask_filtered(statement, color, options)
|
37
|
+
answer_set = options[:limited_to]
|
38
|
+
correct_answer = nil
|
39
|
+
until correct_answer
|
40
|
+
answer = ask_simply("#{statement} #{answer_set.inspect}", color, options)
|
41
|
+
correct_answer = answer_set.include?(answer) ? answer : nil
|
42
|
+
answers = answer_set.map(&:inspect).join(", ")
|
43
|
+
say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
|
44
|
+
end
|
45
|
+
correct_answer
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/mb/upgrade.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
module MotherBrain
|
2
|
+
module Upgrade
|
3
|
+
# Handles upgrading nodes in already environments.
|
4
|
+
class Manager
|
5
|
+
class << self
|
6
|
+
# @raise [Celluloid::DeadActorError] if Upgrade Manager has not been started
|
7
|
+
#
|
8
|
+
# @return [Celluloid::Actor(Upgrade::Manager)]
|
9
|
+
def instance
|
10
|
+
MB::Application[:upgrade_manager] or raise Celluloid::DeadActorError, "upgrade manager not running"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
include Celluloid
|
15
|
+
include MB::Logging
|
16
|
+
|
17
|
+
finalizer :finalize_callback
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
log.debug { "Upgrade Manager starting..." }
|
21
|
+
end
|
22
|
+
|
23
|
+
# Asynchronously upgrade an environment
|
24
|
+
#
|
25
|
+
# @param [String] environment
|
26
|
+
# name of the environment to upgrade
|
27
|
+
# @param [MB::Plugin] plugin
|
28
|
+
# plugin to use for performing the upgrade on the environment
|
29
|
+
#
|
30
|
+
# @option options [Hash] component_versions
|
31
|
+
# Hash of components and the versions to set them to
|
32
|
+
# @option options [Hash] cookbook_versions
|
33
|
+
# Hash of cookbooks and the versions to set them to
|
34
|
+
# @option options [Hash] environment_attributes
|
35
|
+
# any additional attributes to set on the environment
|
36
|
+
# @option options [String] environment_attributes_file
|
37
|
+
# any additional attributes to set on the environment via a json file
|
38
|
+
# @option options [Boolean] :force
|
39
|
+
# Force any locks to be overwritten
|
40
|
+
#
|
41
|
+
# @return [JobTicket]
|
42
|
+
def async_upgrade(environment, plugin, options = {})
|
43
|
+
job = Job.new(:upgrade)
|
44
|
+
|
45
|
+
async(:upgrade, job, environment, plugin, options)
|
46
|
+
|
47
|
+
job.ticket
|
48
|
+
end
|
49
|
+
|
50
|
+
# Synchronously upgrade an environment
|
51
|
+
#
|
52
|
+
# @param [MB::Job] job
|
53
|
+
# @param [String] environment
|
54
|
+
# name of the environment to upgrade
|
55
|
+
# @param [MB::Plugin] plugin
|
56
|
+
# plugin to use for performing the upgrade on the environment
|
57
|
+
#
|
58
|
+
# @option options [Hash] component_versions
|
59
|
+
# Hash of components and the versions to set them to
|
60
|
+
# @option options [Hash] cookbook_versions
|
61
|
+
# Hash of cookbooks and the versions to set them to
|
62
|
+
# @option options [Hash] environment_attributes
|
63
|
+
# any additional attributes to set on the environment
|
64
|
+
# @option options [String] environment_attributes_file
|
65
|
+
# any additional attributes to set on the environment via a json file
|
66
|
+
# @option options [Boolean] :force
|
67
|
+
# Force any locks to be overwritten
|
68
|
+
#
|
69
|
+
# @return [Job]
|
70
|
+
def upgrade(job, environment, plugin, options = {})
|
71
|
+
worker = Worker.new(job, environment.freeze, plugin.freeze, options.freeze)
|
72
|
+
worker.run
|
73
|
+
ensure
|
74
|
+
worker.terminate if worker && worker.alive?
|
75
|
+
job.terminate if job && job.alive?
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def finalize_callback
|
81
|
+
log.debug { "Upgrade Manager stopping..." }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|