Class: CfnEvents::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn-events/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
9
10
11
12
# File 'lib/cfn-events/runner.rb', line 5

def initialize(config)
  @config = config

  @config.cfn_client ||= begin
                           effective_options = CfnEvents::Client.core_v2_options.merge(config.client_options)
                           Aws::CloudFormation::Client.new(effective_options)
                         end
end

Instance Method Details

#all_eventsObject



26
27
28
# File 'lib/cfn-events/runner.rb', line 26

def all_events
  @config.cfn_client.describe_stack_events(stack_name: @stack_id).data.stack_events.reverse
end

#core_v2_optionsObject



14
15
16
# File 'lib/cfn-events/runner.rb', line 14

def core_v2_options
  i
end

#events_since_id(id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/cfn-events/runner.rb', line 35

def events_since_id(id)
  # There may be a more efficient algorithm
  events = all_events
  i = events.index {|e| e.event_id == id }
  if i < 0
    events
  else
    events[i+1..-1]
  end
end

#events_since_time(events, t) ⇒ Object



30
31
32
33
# File 'lib/cfn-events/runner.rb', line 30

def events_since_time(events, t)
  # There may be a more efficient algorithm
  events.select {|e| e.timestamp > t }
end

#resolve_stack(stack_name_or_id) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/cfn-events/runner.rb', line 18

def resolve_stack(stack_name_or_id)
  ans = @config.cfn_client.describe_stacks(stack_name: stack_name_or_id).data.stacks[0].stack_id
  if ans != stack_name_or_id
    $stderr.puts "Resolved #{stack_name_or_id} to #{ans}"
  end
  ans
end

#runObject

Calls $stdout.sync. Returns 0/1/2, like the command line exit code.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cfn-events/runner.rb', line 68

def run
  @stack_id = resolve_stack(@config.stack_name_or_id)

  events = all_events

  if @config.since
    show_events(events_since_time(events, @config.since))
  else
    show_events(events)
  end

  return 0 unless @config.forever or @config.wait

  while @config.forever or not steady_state?(events.last)
    $stdout.sync
    sleep @config.poll_seconds

    new_events = events_since_id(events.last.event_id)

    unless new_events.empty?
      show_events(new_events)
      events = new_events
    end
  end

  return 2 if events.last.resource_status.match /FAILED/
  return 1 if events.last.resource_status.match /ROLLBACK/
  return 0
end

#show_events(events) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cfn-events/runner.rb', line 46

def show_events(events)
  events.each do |e|
    if @config.output_json
      puts JSON.generate(e.to_h)
    else
      puts [
        e.timestamp.utc.strftime('%Y-%m-%dT%H:%M:%SZ'),
        e.resource_type,
        e.resource_status,
        e.logical_resource_id,
        e.physical_resource_id,
        e.resource_status_reason,
      ].join " "
    end
  end
end

#steady_state?(e) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/cfn-events/runner.rb', line 63

def steady_state?(e)
  e.resource_type == "AWS::CloudFormation::Stack" and not e.resource_status.match(/IN_PROGRESS/)
end