Class: Stax::Cmd::Codepipeline

Inherits:
SubCommand show all
Defined in:
lib/stax/mixin/codepipeline.rb

Constant Summary collapse

COLORS =
{
  Succeeded: :green,
  Failed:    :red,
}

Instance Method Summary collapse

Methods inherited from SubCommand

#info, stax_info, stax_info_tasks

Instance Method Details

#approvalsObject



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/stax/mixin/codepipeline.rb', line 73

def approvals
  my.stack_pipeline_names.each do |name|
    debug("Pending approvals for #{name}")
    Aws::Codepipeline.state(name).stage_states.each do |s|
      s.action_states.each do |a|
        next unless (a.latest_execution&.token && a.latest_execution&.status == 'InProgress')
        l = a.latest_execution
        ago = (t = l&.last_status_change) ? human_time_diff(Time.now - t, 1) : '?'
        puts "#{a.action_name} #{l&.token} #{ago} ago"
        resp = (options[:approved] && :approved) || (options[:rejected] && :rejected) || ask('approved,rejected,[skip]?', :yellow)
        status = resp.to_s.capitalize
        if (status == 'Rejected') || (status == 'Approved')
          Aws::Codepipeline.client.put_approval_result(
            pipeline_name: name,
            stage_name: s.stage_name,
            action_name: a.action_name,
            token: l.token,
            result: {status: status, summary: "#{status} by #{ENV['USER']}"},
          ).tap { |r| puts "#{status} at #{r&.approved_at}" }
        end
      end
    end
  end
end

#historyObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stax/mixin/codepipeline.rb', line 41

def history
  my.stack_pipeline_names.each do |name|
    debug("Execution history for #{name}")
    print_table Aws::Codepipeline.executions(name, options[:number]).map { |e|
      r = Aws::Codepipeline.execution(name, e.pipeline_execution_id)&.artifact_revisions&.first
      age = human_time_diff(Time.now - e.last_update_time, 1)
      duration = human_time_diff(e.last_update_time - e.start_time)
      [e.pipeline_execution_id, color(e.status, COLORS), "#{age} ago", duration, r&.revision_id&.slice(0,7) + ':' + r&.revision_summary]
    }
  end
end

#stagesObject



29
30
31
32
33
34
35
36
37
# File 'lib/stax/mixin/codepipeline.rb', line 29

def stages
  my.stack_pipeline_names.each do |name|
    debug("Stages for #{name}")
    print_table Aws::Codepipeline.stages(name).map { |s|
      actions = s.actions.map{ |a| a&.action_type_id&.provider }.join(' ')
      [s.name, actions]
    }
  end
end

#start(name = nil) ⇒ Object



99
100
101
102
103
104
# File 'lib/stax/mixin/codepipeline.rb', line 99

def start(name = nil)
  name ||= my.stack_pipeline_names.first
  debug("Starting execution for #{name}")
  puts Aws::Codepipeline.start(name)
  tail name
end

#stateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stax/mixin/codepipeline.rb', line 54

def state
  my.stack_pipeline_names.each do |name|
    state = Aws::Codepipeline.state(name)
    debug("State for #{name} at #{state.updated}")
    print_table state.stage_states.map { |s|
      s.action_states.map { |a|
        l = a.latest_execution
        percent = (l&.percent_complete || 100).to_s + '%'
        sha = a.current_revision&.revision_id&.slice(0,7)
        ago = (t = l&.last_status_change) ? human_time_diff(Time.now - t, 1) : '?'
        [s.stage_name, a.action_name, color(l&.status || '', COLORS), percent, "#{ago} ago", (sha || l&.token), l&.error_details&.message]
      }
    }.flatten(1)
  end
end

#tail(name = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/stax/mixin/codepipeline.rb', line 107

def tail(name = nil)
  trap('SIGINT', 'EXIT')    # clean exit with ctrl-c
  name ||= my.stack_pipeline_names.first
  last_seen = nil
  loop do
    state = Aws::Codepipeline.state(name)
    now = Time.now
    stages = state.stage_states.map do |s|
      last_change = s.action_states.map { |a| a&.latest_execution&.last_status_change }.compact.max
      revisions = s.action_states.map { |a| a.current_revision&.revision_id&.slice(0,7) }.join(' ')
      ago = last_change ? human_time_diff(now - last_change, 1) : '?'
      [s.stage_name, color(s&.latest_execution&.status || '', COLORS), "#{ago} ago", revisions].join(' ')
    end
    puts [set_color(now, :blue), stages].flatten.join('  ')
    sleep 5
  end
end