-
Notifications
You must be signed in to change notification settings - Fork 33
/
stackup
executable file
·301 lines (228 loc) · 6.71 KB
/
stackup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env ruby
$LOAD_PATH << File.expand_path("../../lib", __FILE__)
require "clamp"
require "console_logger"
require "diffy"
require "multi_json"
require "stackup"
require "stackup/version"
require "yaml"
$stdout.sync = true
$stderr.sync = true
Clamp do
option ["-L", "--list"], :flag, "list stacks" do
list_stacks
exit 0
end
option ["-Y", "--yaml"], :flag, "output data in YAML format"
option "--debug", :flag, "enable debugging"
option ["--version"], :flag, "display version" do
puts "stackup v#{Stackup::VERSION}"
exit 0
end
parameter "NAME", "Name of stack", :attribute_name => :stack_name
def run(arguments)
super(arguments)
rescue Stackup::ServiceError => e
signal_error e.message
rescue Aws::Errors::MissingCredentialsError
signal_error "no credentials provided"
rescue Aws::CloudFormation::Errors::ExpiredToken => e
signal_error e.message
end
private
def logger
@logger ||= ConsoleLogger.new($stdout, debug?)
end
def format_data(data)
if yaml?
YAML.dump(data)
else
MultiJson.dump(data, :pretty => true)
end
end
def display_data(data)
puts format_data(data)
end
def stackup
Stackup(:logger => logger, :log_level => :debug)
end
def stack
stackup.stack(stack_name)
end
def list_stacks
stackup.stack_names.each do |name|
puts name
end
end
def report_change
final_status = yield
puts final_status unless final_status.nil?
end
def load_data(file)
YAML.load_file(file)
rescue Errno::ENOENT
signal_error "no such file: #{file.inspect}"
end
subcommand "status", "Print stack status." do
def execute
puts stack.status
end
end
subcommand "up", "Create/update the stack" do
option ["-t", "--template"], "FILE", "template file",
:attribute_name => :template_file
option ["-T", "--use-previous-template"], :flag,
"reuse the existing template"
option ["-p", "--parameters"], "FILE", "parameters file",
:attribute_name => :parameters_file
option "--policy", "FILE", "stack policy file",
:attribute_name => :policy_file
option "--on-failure", "ACTION",
"when stack creation fails: DO_NOTHING, ROLLBACK, or DELETE",
:default => "ROLLBACK"
def execute
unless template_file || use_previous_template?
signal_usage_error "Specify either --template or --use-previous-template"
end
options = {}
options[:template] = load_data(template_file) if template_file
options[:on_failure] = on_failure
options[:parameters] = load_data(parameters_file) if parameters_file
options[:stack_policy] = load_data(policy_file) if policy_file
options[:use_previous_template] = use_previous_template?
report_change do
stack.create_or_update(options)
end
end
end
subcommand "diff", "Compare template/params to current stack." do
option ["-t", "--template"], "FILE", "template file",
:attribute_name => :template_file
option ["-p", "--parameters"], "FILE", "parameters file",
:attribute_name => :parameters_file
option "--diff-format", "FORMAT", "'text', 'color', or 'html'", :default => "color"
def execute
# rubocop:disable Style/GuardClause
if template_file
template = load_data(template_file)
diff_data(stack.template, template)
end
if parameters_file
new_parameters = hashify_parameters(load_data(parameters_file))
existing_parameters = stack.parameters
new_parameters = existing_parameters.merge(new_parameters)
diff_data(existing_parameters.sort.to_h, new_parameters.sort.to_h)
end
end
private
def diff_data(existing_data, pending_data)
existing = format_data(existing_data) + "\n"
pending = format_data(pending_data) + "\n"
diff = Diffy::Diff.new(existing, pending).to_s(diff_format.to_sym)
return if diff =~ /\A\s*\Z/
puts diff
end
def hashify_parameters(parameters)
return parameters unless parameters.is_a?(Array)
{}.tap do |result|
parameters.each do |p|
key = p.fetch("ParameterKey") { p.fetch("parameter_key") }
value = p.fetch("ParameterValue") { p.fetch("parameter_value") }
result[key] = value
end
end
end
end
subcommand ["down", "delete"], "Remove the stack." do
def execute
report_change do
stack.delete
end
end
end
subcommand "cancel-update", "Cancel the update in-progress" do
def execute
report_change do
stack.cancel_update
end
end
end
subcommand "wait", "Wait until stack is stable" do
def execute
puts stack.wait
end
end
subcommand "events", "List stack events" do
option ["-f", "--follow"], :flag, "follow new events"
option ["--data"], :flag, "display events as data"
def execute
stack.watch(false) do |watcher|
loop do
watcher.each_new_event do |event|
display_event(event)
end
break unless follow?
sleep 5
end
end
end
private
def display_event(e)
if data?
display_data(event_data(e))
else
puts event_summary(e)
end
end
def event_data(e)
{
"timestamp" => e.timestamp.localtime,
"logical_resource_id" => e.logical_resource_id,
"physical_resource_id" => e.physical_resource_id,
"resource_status" => e.resource_status,
"resource_status_reason" => e.resource_status_reason
}.reject { |_k, v| blank?(v) }
end
def blank?(v)
v.nil? || v.respond_to?(:empty?) && v.empty?
end
def event_summary(e)
summary = "[#{e.timestamp.localtime.iso8601}] #{e.logical_resource_id}"
summary += " - #{e.resource_status}"
summary += " - #{e.resource_status_reason}" if e.resource_status_reason
summary
end
end
subcommand "template", "Display stack template." do
def execute
display_data(stack.template)
end
end
subcommand "parameters", "Display stack parameters." do
def execute
display_data(stack.parameters)
end
end
subcommand "resources", "Display stack resources." do
def execute
display_data(stack.resources)
end
end
subcommand "outputs", "Display stack outputs." do
def execute
display_data(stack.outputs)
end
end
subcommand "inspect", "status + parameters + resources + outputs" do
def execute
data = {
"Status" => stack.status,
"Parameters" => stack.parameters,
"Resources" => stack.resources,
"Outputs" => stack.outputs
}
display_data(data)
end
end
end