Class: Stax::Cmd::Codepipeline
Constant Summary
collapse
- COLORS =
{
Succeeded: :green,
Failed: :red,
Stopped: :red,
Abandoned: :red,
enabled: :green,
disabled: :red,
}
Instance Method Summary
collapse
Methods inherited from SubCommand
#info, stax_info, stax_info_tasks
Instance Method Details
#approvals ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/stax/mixin/codepipeline.rb', line 77
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 ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/stax/mixin/codepipeline.rb', line 45
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
|
#stages ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/stax/mixin/codepipeline.rb', line 33
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
103
104
105
106
107
108
|
# File 'lib/stax/mixin/codepipeline.rb', line 103
def start(name = nil)
name ||= my.stack_pipeline_names.first
debug("Starting execution for #{name}")
puts Aws::Codepipeline.start(name)
tail name
end
|
#state ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/stax/mixin/codepipeline.rb', line 58
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
|
#stop(name = nil) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/stax/mixin/codepipeline.rb', line 113
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) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/stax/mixin/codepipeline.rb', line 128
def tail(name = nil)
trap('SIGINT', 'EXIT') 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 ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/stax/mixin/codepipeline.rb', line 149
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 ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/stax/mixin/codepipeline.rb', line 178
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
|