class Stax::Cmd::Codepipeline

Constants

COLORS

Public Instance Methods

approvals() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 97
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
history() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 57
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
open() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 218
def open
  my.stack_pipeline_names.each do |name|
    os_open(pipeline_link(name))
  end
end
stages() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 45
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) click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 123
def start(name = nil)
  name ||= my.stack_pipeline_names.first
  debug("Starting execution for #{name}")
  puts Aws::Codepipeline.start(name)
  tail name
end
state() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 78
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
status() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 71
def status
  s = my.stack_pipeline_status
  puts(s)
  exit(1) if s == 'Failed'
end
stop(name = nil) click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 133
def stop(name = nil)
  name ||= my.stack_pipeline_names.first
  id = Aws::Codepipeline.state(name).stage_states.first.latest_execution.pipeline_execution_id
  debug("Stopping #{name} #{id}")
  puts Aws::Codepipeline.client.stop_pipeline_execution(
    pipeline_name: name,
    pipeline_execution_id: id,
    abandon: options[:abandon],
    reason: options[:reason],
  ).pipeline_execution_id
rescue ::Aws::CodePipeline::Errors::ServiceError => e
  fail_task(e.message)
end
tail(name = nil) click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 148
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
transitions() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 169
def transitions
  my.stack_pipeline_names.each do |name|
    if options[:enable]
      debug("Enable stage transition for #{name} #{options[:enable]}")
      Aws::Codepipeline.client.enable_stage_transition(
        pipeline_name: name,
        stage_name: options[:enable],
        transition_type: :Inbound,
      )
    elsif options[:disable]
      debug("Disable stage transition for #{name} #{options[:disable]}")
      Aws::Codepipeline.client.disable_stage_transition(
        pipeline_name: name,
        stage_name: options[:disable],
        transition_type: :Inbound,
        reason: ask('reason for disable?')
      )
    else
      debug("Stage transitions for #{name}")
      state = Aws::Codepipeline.state(name)
      print_table state.stage_states.map { |s|
        t = s.inbound_transition_state
        [ s.stage_name, color(t.enabled ? :enabled : :disabled, COLORS), t.disabled_reason, t.last_changed_at ]
      }
    end
  end
end
webhooks() click to toggle source
# File lib/stax/mixin/codepipeline.rb, line 198
def webhooks
  webhooks = Aws::Codepipeline.client.list_webhooks.map(&:webhooks).flatten
  my.stack_pipeline_names.each do |pipeline|
    debug("Webhooks for pipeline #{pipeline}")
    print_table webhooks.select { |w|
      w.definition.target_pipeline == pipeline
    }.map { |w|
      [ w.definition.name, w.definition.target_pipeline, w.error_message, w.last_triggered ]
    }
  end
end