Class: Opsup::StackOperator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/opsup/stack_operator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opsworks:, logger:) ⇒ StackOperator

Returns a new instance of StackOperator.



17
18
19
20
# File 'lib/opsup/stack_operator.rb', line 17

def initialize(opsworks:, logger:)
  @opsworks = T.let(opsworks, Aws::OpsWorks::Client)
  @logger = T.let(logger, ::Logger)
end

Class Method Details

.create(opsworks:) ⇒ Object



9
10
11
12
13
14
# File 'lib/opsup/stack_operator.rb', line 9

def self.create(opsworks:)
  new(
    opsworks: opsworks,
    logger: Opsup::Logger.instance,
  )
end

Instance Method Details

#run_commands(commands, stack_name:, mode:, dryrun: false) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/opsup/stack_operator.rb', line 30

def run_commands(commands, stack_name:, mode:, dryrun: false)
  # Find the target stack.
  @logger.debug('Verifying the specified stack exists...')
  stacks = @opsworks.describe_stacks.stacks
  stack = stacks.find { |s| s.name == stack_name }
  raise Opsup::Error, "Stack #{stack_name} does not exist" if stack.nil?

  # Find the stack's apps.
  @logger.debug('Verifying the stack has at least one app...')
  apps = @opsworks.describe_apps(stack_id: stack.stack_id).apps
  raise Opsup::Error, "#{stack_name} has no apps" if apps.empty?

  # Find the instances to be updated.
  @logger.debug('Finding all working instances in the stack...')
  instances = @opsworks.describe_instances(stack_id: stack.stack_id).instances
  instances = instances.reject { |inst| inst.status == 'stopped' }

  raise Opsup::Error, 'No available instances found' if instances.empty?

  @logger.debug(
    "#{instances.size} #{instances.size == 1 ? 'instance is' : 'instances are'} found",
  )

  # Currently Opsup deploys only the first app by default.
  app = apps.first
  instance_ids = instances.map(&:instance_id)

  # Run the commands sequentially.
  commands.each do |command|
    @logger.info("Running #{command} command in #{mode} mode...")
    run_command(
      command,
      dryrun: dryrun,
      mode: mode,
      stack: stack,
      app: app,
      instance_ids: instance_ids,
    )
  end
end