class Opsup::StackOperator::CommandDeployer

Public Class Methods

create(config:, opsworks:, logger:) click to toggle source
# File lib/opsup/stack_operator/command_deployer.rb, line 24
def self.create(config:, opsworks:, logger:)
  new(config: config, opsworks: opsworks, logger: logger)
end
new(config:, opsworks:, logger:) click to toggle source
# File lib/opsup/stack_operator/command_deployer.rb, line 35
def initialize(config:, opsworks:, logger:)
  @config = T.let(config, Config)
  @opsworks = T.let(opsworks, Aws::OpsWorks::Client)
  @logger = T.let(logger, ::Logger)
end

Public Instance Methods

run_command(command) click to toggle source
# File lib/opsup/stack_operator/command_deployer.rb, line 42
def run_command(command)
  mode = @config.mode
  instance_ids = @config.instance_ids
  dryrun = @config.dryrun

  @logger.info("Running #{command} command in #{mode} mode...")

  case mode
  when :parallel
    @logger.info("Creating single deployment for the #{instance_ids.size} instances...")
    create_deployment(command, instance_ids) unless dryrun
  when :serial
    instance_ids.each.with_index do |id, i|
      @logger.info("Creating deployment for instances[#{i}] (#{id})...")
      create_deployment(command, [id]) unless dryrun
    end
  when :one_then_all
    @logger.info("Creating deployment for the first instance (#{instance_ids[0]})...")
    create_deployment(command, [T.must(instance_ids[0])]) unless dryrun

    rest = T.must(instance_ids[1..-1])
    if !rest.empty?
      @logger.info("Creating deployment for the other #{rest.size} instances...")
      create_deployment(command, rest) unless dryrun
    else
      @logger.info('No other instances exist.')
    end
  else
    raise "Unknown running mode: #{mode}"
  end
end

Private Instance Methods

create_deployment(command, instance_ids) click to toggle source
# File lib/opsup/stack_operator/command_deployer.rb, line 80
        def create_deployment(command, instance_ids)
  res = @opsworks.create_deployment(
    stack_id: @config.stack.stack_id,
    app_id: @config.app.app_id,
    instance_ids: instance_ids,
    command: { name: command, args: {} },
  )

  @logger.info("Waiting deployment #{res.deployment_id}...")
  @opsworks.wait_until(:deployment_successful, {
    deployment_ids: [res.deployment_id],
  })

  nil
end