Class: Lono::Sets::Status::Instances

Inherits:
Object
  • Object
show all
Includes:
AwsServices, TimeSpent
Defined in:
lib/lono/sets/status/instances.rb

Constant Summary collapse

@@aws_cli_command_shown =
false

Instance Method Summary collapse

Methods included from TimeSpent

#show_time_spent

Methods included from Utils::PrettyTime

#pretty_time

Methods included from AwsServices

#cfn, #ec2, #iam, #s3, #s3_presigner, #s3_resource, #sts

Methods included from AwsServices::Helper

#rollback_complete?, #testing_update?

Methods included from AwsServices::StackSet

#find_stack_set, #stack_set_exists?

Methods included from AwsServices::Stack

#find_stack, #stack_exists?

Constructor Details

#initialize(options = {}) ⇒ Instances

Returns a new instance of Instances.



6
7
8
9
10
# File 'lib/lono/sets/status/instances.rb', line 6

def initialize(options={})
  @options = options
  @stack, @operation_id = options[:stack], options[:operation_id]
  @show_time_spent = options[:show_time_spent].nil? ? true : options[:show_time_spent]
end

Instance Method Details

#completed?(status) ⇒ Boolean

describe_stack_set_operation stack_set_operation.status is one of RUNNING, SUCCEEDED, FAILED, STOPPING, STOPPED

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/lono/sets/status/instances.rb', line 92

def completed?(status)
  completed_statuses = %w[SUCCEEDED FAILED STOPPED]
  completed_statuses.include?(status)
end

#instancesObject



106
107
108
# File 'lib/lono/sets/status/instances.rb', line 106

def instances
  stack_instances.map { |stack_instance| Instance.new(stack_instance) }
end

#latest_operation_idObject



128
129
130
131
132
133
134
# File 'lib/lono/sets/status/instances.rb', line 128

def latest_operation_id
  resp = cfn.list_stack_set_operations(
    stack_set_name: @stack,
    max_results: 1,
  )
  resp.summaries.first.operation_id
end

#operation_idObject



124
125
126
# File 'lib/lono/sets/status/instances.rb', line 124

def operation_id
  @operation_id ||= latest_operation_id
end

#showObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lono/sets/status/instances.rb', line 22

def show
  if stack_instances.empty?
    # Note: no access to @blueprint here
    puts <<~EOL
      There are 0 stack instances associated with the #{@stack} stack set.
      Use `lono set_instances deploy` to add stack instances. Example:

          lono set_instances deploy #{@stack} --accounts 111 --regions us-west-2 us-east-2

    EOL
    return
  end

  threads = start_wait_for_instances_threads
  wait_until_stack_set_operation_complete
  threads.map(&:join)
end

#show_aws_cli_command(operation_id) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lono/sets/status/instances.rb', line 77

def show_aws_cli_command(operation_id)
  return if @@aws_cli_command_shown

  command = "aws cloudformation describe-stack-set-operation --stack-set-name #{@stack} --operation-id #{operation_id}"
  puts <<~EOL
    Here is also the cli command to check:

        #{command}

  EOL
  @@aws_cli_command_shown = true
end

#stack_instancesObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lono/sets/status/instances.rb', line 110

def stack_instances
  resp = cfn.list_stack_instances(stack_set_name: @stack)
  summaries = resp.summaries
  # filter is really only used internally. So it's fine to keep it as complex data structure since that's what we
  # build it up as in Lono::SetInstances::Deploy
  filter = @options[:filter] # [["112233445566", "us-west-1"],["112233445566", "us-west-2"]]
  return summaries unless filter

  summaries.reject do |s|
    intersect = [[s., s.region]] & filter
    intersect.empty?
  end
end

#start_wait_for_instances_threadsObject



40
41
42
43
44
45
46
# File 'lib/lono/sets/status/instances.rb', line 40

def start_wait_for_instances_threads
  # Tricky: extra sleep so that the show_aws_cli_command in wait_until_stack_set_operation_complete
  # shows up first. Quickest way to implement.
  with_instances do |instance|
    Thread.new { sleep 5;  instance.show }
  end
end

#wait(to = "completed") ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/lono/sets/status/instances.rb', line 12

def wait(to="completed")
  puts "Stack Instance statuses... (takes a while)"
  puts "You can also check with StackSets console at the Operations Tab."
  wait_until_outdated if @options[:start_on_outdated]

  threads = start_wait_for_instances_threads
  wait_until_stack_set_operation_complete # start the the tailer here so the show_aws_cli_command shows up
  threads.map(&:join)
end

#wait_until_outdatedObject

If we dont wait until OUTDATED, during a ‘lono sets deploy` it’ll immediately think that the instance statuses are done



98
99
100
101
102
103
104
# File 'lib/lono/sets/status/instances.rb', line 98

def wait_until_outdated
  outdated = false
  until outdated
    outdated = stack_instances.detect { |stack_instance| stack_instance.status == "OUTDATED" }
    sleep 5
  end
end

#wait_until_stack_set_operation_completeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lono/sets/status/instances.rb', line 55

def wait_until_stack_set_operation_complete
  status, stack_set_operation = nil, nil
  until completed?(status)
    resp = cfn.describe_stack_set_operation(
      stack_set_name: @stack,
      operation_id: operation_id,
    )
    stack_set_operation = resp.stack_set_operation
    status = stack_set_operation.status
    show_aws_cli_command(stack_set_operation.operation_id)
    # puts "DEBUG: wait_until_stack_set_operation_complete"
    unless completed?(status)
      sleep 5
    end
  end
  if @show_time_spent # or else it double shows from `lono sets deploy`. Do want it to show for `lono set_instances sync` though
    show_time_spent(stack_set_operation)
    puts "Stack set operation completed."
  end
end

#with_instancesObject



48
49
50
51
52
53
# File 'lib/lono/sets/status/instances.rb', line 48

def with_instances
  stack_instances.map do |stack_instance|
    instance = Instance.new(stack_instance)
    yield(instance)
  end
end