class Moonshot::Controller

The Controller coordinates and performs all Moonshot actions.

Attributes

config[RW]

Public Class Methods

new() { |config| ... } click to toggle source
# File lib/moonshot/controller.rb, line 13
def initialize
  @config = ControllerConfig.new
  yield @config if block_given?
end

Public Instance Methods

build_version(version_name) click to toggle source
# File lib/moonshot/controller.rb, line 140
def build_version(version_name)
  run_plugins(:pre_build)
  run_hook(:build, :pre_build, version_name)
  run_hook(:build, :build, version_name)
  run_hook(:build, :post_build, version_name)
  run_plugins(:post_build)
  run_hook(:repo, :store, @config.build_mechanism, version_name)
end
create() click to toggle source
# File lib/moonshot/controller.rb, line 22
def create # rubocop:disable AbcSize
  # Scan the template for all required parameters and configure
  # the ParameterCollection.
  @config.parameters = ParameterCollection.from_template(stack.template)

  # Import all Outputs from parent stacks as Parameters on this
  # stack.
  ParentStackParameterLoader.new(@config).load!

  # If there is an answer file, use it to populate parameters.
  if @config.answer_file
    YAML.load_file(@config.answer_file).each do |key, value|
      @config.parameters[key] = value
    end
  end

  # Apply any overrides configured, such as from the CLI -p option.
  @config.parameter_overrides.each do |key, value|
    @config.parameters[key] = value
  end

  # Interview the user for missing parameters, using the
  # appropriate prompts.
  @config.parameters.values.each do |sp|
    next if sp.set?

    parameter_source = @config.parameter_sources.fetch(sp.name,
                                                       @config.default_parameter_source)
    parameter_source.get(sp)
  end

  # Plugins get the final say on parameters before create,
  # allowing them to manipulate user supplied input and answers
  # file content.
  run_plugins(:pre_create)

  # Fail if any parameters are still missing without defaults.
  missing_parameters = @config.parameters.missing_for_create
  unless missing_parameters.empty?
    raise "The following parameters were not provided: #{missing_parameters.map(&:name).join(', ')}" # rubocop:disable LineLength
  end

  run_hook(:deploy, :pre_create)
  stack_ok = stack.create
  if stack_ok # rubocop:disable GuardClause
    run_hook(:deploy, :post_create)
    run_plugins(:post_create)
  else
    raise 'Stack creation failed!'
  end
end
delete() click to toggle source
# File lib/moonshot/controller.rb, line 155
def delete
  # Populate the current values of parameters, for use by plugins.
  @config.parameters = ParameterCollection.from_template(stack.template)
  stack.parameters.each do |key, value|
    @config.parameters[key].use_previous!(value) if @config.parameters.key?(key)
  end

  run_plugins(:pre_delete)
  run_hook(:deploy, :pre_delete)
  stack.delete
  run_hook(:deploy, :post_delete)
  run_plugins(:post_delete)
end
deploy_version(version_name) click to toggle source
# File lib/moonshot/controller.rb, line 149
def deploy_version(version_name)
  run_plugins(:pre_deploy)
  run_hook(:deploy, :deploy, @config.artifact_repository, version_name)
  run_plugins(:post_deploy)
end
doctor() click to toggle source
# File lib/moonshot/controller.rb, line 169
def doctor
  success = true
  success &&= stack.doctor_hook
  success &&= run_hook(:build, :doctor)
  success &&= run_hook(:repo, :doctor)
  success &&= run_hook(:deploy, :doctor)
  results = run_plugins(:doctor)

  success = false if results.value?(false)
  success
end
list() click to toggle source
# File lib/moonshot/controller.rb, line 18
def list
  Moonshot::StackLister.new(@config.app_name).list
end
push() click to toggle source
# File lib/moonshot/controller.rb, line 134
def push
  version = @config.dev_build_name_proc.call(@config)
  build_version(version)
  deploy_version(version)
end
ssh() click to toggle source
# File lib/moonshot/controller.rb, line 181
def ssh
  run_plugins(:pre_ssh)
  @config.ssh_instance ||= SSHTargetSelector.new(
    stack, asg_name: @config.ssh_auto_scaling_group_name).choose!
  cb = SSHCommandBuilder.new(@config.ssh_config, @config.ssh_instance)
  result = cb.build(@config.ssh_command)

  warn "Opening SSH connection to #{@config.ssh_instance} (#{result.ip})..."
  exec(result.cmd)
end
stack() click to toggle source
# File lib/moonshot/controller.rb, line 192
def stack
  @stack ||= Stack.new(@config)
end
status() click to toggle source
# File lib/moonshot/controller.rb, line 127
def status
  run_plugins(:pre_status)
  run_hook(:deploy, :status)
  stack.status
  run_plugins(:post_status)
end
update() click to toggle source
# File lib/moonshot/controller.rb, line 74
def update # rubocop:disable AbcSize
  # Scan the template for all required parameters and configure
  # the ParameterCollection.
  @config.parameters = ParameterCollection.from_template(stack.template)

  # Set all values already provided by the stack to UsePreviousValue.
  stack.parameters.each do |key, value|
    @config.parameters[key].use_previous!(value) if @config.parameters.key?(key)
  end

  # Import all Outputs from parent stacks as Parameters on this
  # stack.
  ParentStackParameterLoader.new(@config).load_missing_only!

  # If there is an answer file, use it to populate parameters.
  if @config.answer_file
    YAML.load_file(@config.answer_file).each do |key, value|
      @config.parameters[key] = value
    end
  end

  # Apply any overrides configured, such as from the CLI -p option.
  @config.parameter_overrides.each do |key, value|
    @config.parameters[key] = value
  end

  # Interview the user for missing parameters, using the
  # appropriate prompts.
  @config.parameters.values.each do |sp|
    next if sp.set?

    parameter_source = @config.parameter_sources.fetch(sp.name,
                                                       @config.default_parameter_source)
    parameter_source.get(sp)
  end

  # Plugins get the final say on parameters before create,
  # allowing them to manipulate user supplied input and answers
  # file content.
  run_plugins(:pre_update)

  # Fail if any parameters are still missing without defaults.
  missing_parameters = @config.parameters.missing_for_update
  unless missing_parameters.empty?
    raise "The following parameters were not provided: #{missing_parameters.map(&:name).join(', ')}" # rubocop:disable LineLength
  end

  run_hook(:deploy, :pre_update)
  stack.update
  run_hook(:deploy, :post_update)
  run_plugins(:post_update)
end

Private Instance Methods

get_mechanism(type) click to toggle source
# File lib/moonshot/controller.rb, line 223
def get_mechanism(type)
  case type
  when :build then @config.build_mechanism
  when :repo then @config.artifact_repository
  when :deploy then @config.deployment_mechanism
  else
    raise "Unknown hook type: #{type}"
  end
end
resources() click to toggle source
# File lib/moonshot/controller.rb, line 198
def resources
  @resources ||=
    Resources.new(stack: stack, ilog: @config.interactive_logger, controller: self)
end
run_hook(type, name, *args) click to toggle source
# File lib/moonshot/controller.rb, line 203
def run_hook(type, name, *args)
  mech = get_mechanism(type)
  name = name.to_s << '_hook'

  return unless mech && mech.respond_to?(name)

  mech.resources = resources
  mech.send(name, *args)
end
run_plugins(type) click to toggle source
# File lib/moonshot/controller.rb, line 213
def run_plugins(type)
  results = {}
  @config.plugins.each do |plugin|
    next unless plugin.respond_to?(type)
    results[plugin] = plugin.send(type, resources)
  end

  results
end