cfndk 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/bin/cfndk +15 -12
- data/lib/cfndk.rb +1 -0
- data/lib/cfndk/key_pair.rb +7 -7
- data/lib/cfndk/logger.rb +14 -0
- data/lib/cfndk/stack.rb +229 -1
- data/lib/cfndk/stacks.rb +44 -247
- data/lib/cfndk/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f083e8a85ab15d77d5de878cfdbe04449b5e4c63
|
4
|
+
data.tar.gz: 5baddc25f36a208cc9040fcf40f0d9fc314da6ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25585042c39f20242014e99ea37bff5e9638621aca5d730d8328c2a09d287a9f70b5dd77795a62e3a1cf80301ca75ff964c3a9c6712e07a6fe829f1f54031e43
|
7
|
+
data.tar.gz: 46872932ad3ca28c12ec6238514f0eb068529a682338796fad391b9b6000c2e9d5278b1eb764e2b85f9fd1b73005c29d701ce9c8a296ba266c769a4bd5fc6f6b
|
data/README.md
CHANGED
data/bin/cfndk
CHANGED
@@ -12,6 +12,7 @@ require 'json'
|
|
12
12
|
require 'aws-sdk'
|
13
13
|
require 'terminal-table'
|
14
14
|
require 'securerandom'
|
15
|
+
require 'logger'
|
15
16
|
|
16
17
|
require 'cfndk.rb'
|
17
18
|
|
@@ -82,6 +83,8 @@ opt = OptionParser.new do |o|
|
|
82
83
|
o.permute!(ARGV)
|
83
84
|
end
|
84
85
|
|
86
|
+
logger = CFnDK::Logger.new(option)
|
87
|
+
|
85
88
|
if ARGV.length != 1
|
86
89
|
puts opt.help
|
87
90
|
exit 1
|
@@ -90,17 +93,17 @@ elsif ARGV[0] == 'generate-uuid'
|
|
90
93
|
exit 0
|
91
94
|
elsif ARGV[0] == 'init'
|
92
95
|
if File.file?(option[:config_path])
|
93
|
-
|
96
|
+
logger.error "File exist. #{option[:config_path]}".color(:red)
|
94
97
|
exit 1
|
95
98
|
end
|
96
|
-
|
99
|
+
logger.info 'init...'.color(:green)
|
97
100
|
FileUtils.cp_r(Dir.glob(File.dirname(__FILE__) + '/../skel/*'), './')
|
98
|
-
|
101
|
+
logger.info "create #{option[:config_path]}".color(:green)
|
99
102
|
exit 0
|
100
103
|
end
|
101
104
|
|
102
105
|
unless File.file?(option[:config_path])
|
103
|
-
|
106
|
+
logger.error "File does not exist. #{option[:config_path]}".color(:red)
|
104
107
|
exit 1
|
105
108
|
end
|
106
109
|
|
@@ -110,35 +113,35 @@ stacks = CFnDK::Stacks.new(data, option, credentials)
|
|
110
113
|
keypairs = CFnDK::KeyPairs.new(data, option, credentials)
|
111
114
|
|
112
115
|
if ARGV[0] == 'create'
|
113
|
-
|
116
|
+
logger.info 'create...'.color(:green)
|
114
117
|
stacks.validate
|
115
118
|
keypairs.create
|
116
119
|
stacks.create
|
117
120
|
elsif ARGV[0] == 'update'
|
118
|
-
|
121
|
+
logger.info 'update...'.color(:green)
|
119
122
|
stacks.validate
|
120
123
|
stacks.update
|
121
124
|
elsif ARGV[0] == 'create-or-changeset'
|
122
|
-
|
125
|
+
logger.info 'create or changeset...'.color(:green)
|
123
126
|
stacks.validate
|
124
127
|
stacks.create_or_changeset
|
125
128
|
elsif ARGV[0] == 'destroy'
|
126
|
-
|
129
|
+
logger.info 'destroy...'.color(:green)
|
127
130
|
if do_destroy(option)
|
128
131
|
stacks.destroy
|
129
132
|
keypairs.destroy
|
130
133
|
end
|
131
134
|
elsif ARGV[0] == 'validate'
|
132
|
-
|
135
|
+
logger.info 'validate...'.color(:green)
|
133
136
|
stacks.validate
|
134
137
|
elsif ARGV[0] == 'report-event'
|
135
|
-
|
138
|
+
logger.info 'report event...'.color(:green)
|
136
139
|
stacks.report_event
|
137
140
|
elsif ARGV[0] == 'report-stack'
|
138
|
-
|
141
|
+
logger.info 'report stack...'.color(:green)
|
139
142
|
stacks.report_stack
|
140
143
|
elsif ARGV[0] == 'report-stack-resource'
|
141
|
-
|
144
|
+
logger.info 'report stack resource...'.color(:green)
|
142
145
|
stacks.report_stack_resource
|
143
146
|
else
|
144
147
|
puts opt.help
|
data/lib/cfndk.rb
CHANGED
data/lib/cfndk/key_pair.rb
CHANGED
@@ -6,25 +6,26 @@ module CFnDK
|
|
6
6
|
@key_file = nil
|
7
7
|
@key_file = data['key_file'] || nil if data
|
8
8
|
@option = option
|
9
|
+
@logger = CFnDK::Logger.new(option)
|
9
10
|
@client = Aws::EC2::Client.new(credentials: credentials)
|
10
11
|
end
|
11
12
|
|
12
13
|
def create
|
13
|
-
|
14
|
+
@logger.info(('creating keypair: ' + name).color(:green))
|
14
15
|
key_pair = @client.create_key_pair(
|
15
16
|
key_name: name
|
16
17
|
)
|
17
|
-
|
18
|
+
@logger.info(('created keypair: ' + name).color(:green))
|
18
19
|
|
19
20
|
create_key_file(key_pair)
|
20
21
|
end
|
21
22
|
|
22
23
|
def destroy
|
23
|
-
|
24
|
+
@logger.info(('deleting keypair: ' + name).color(:green))
|
24
25
|
@client.delete_key_pair(
|
25
26
|
key_name: name
|
26
27
|
)
|
27
|
-
|
28
|
+
@logger.info(('deleted keypair: ' + name).color(:green))
|
28
29
|
end
|
29
30
|
|
30
31
|
def name
|
@@ -33,12 +34,11 @@ module CFnDK
|
|
33
34
|
|
34
35
|
private
|
35
36
|
|
36
|
-
def create_key_file
|
37
|
+
def create_key_file(key_pair)
|
37
38
|
return unless @key_file
|
38
39
|
key_file = CFnDK::ErbString.new(@key_file, @option).value
|
39
|
-
|
40
|
+
@logger.info(('create key file: ' + key_file).color(:green))
|
40
41
|
File.write(key_file, key_pair.key_material)
|
41
42
|
end
|
42
|
-
|
43
43
|
end
|
44
44
|
end
|
data/lib/cfndk/logger.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module CFnDK
|
2
|
+
class Logger < Logger
|
3
|
+
def initialize(option)
|
4
|
+
super(STDOUT)
|
5
|
+
self.level = Logger::INFO unless option[:v]
|
6
|
+
self.formatter = proc { |severity, datetime, progname, message|
|
7
|
+
message.to_s.split(/\n/).map do |line|
|
8
|
+
"#{datetime} #{severity} #{line}\n"
|
9
|
+
end.join
|
10
|
+
}
|
11
|
+
self.datetime_format = '%Y-%m-%dT%H:%M:%S'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/cfndk/stack.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module CFnDK
|
2
2
|
class Stack
|
3
3
|
attr_reader :template_file, :parameter_input, :capabilities, :depends, :timeout_in_minutes
|
4
|
-
def initialize(name, data, option)
|
4
|
+
def initialize(name, data, option, credentials)
|
5
5
|
@name = name
|
6
6
|
@template_file = data['template_file'] || ''
|
7
7
|
@parameter_input = data['parameter_input'] || ''
|
@@ -10,6 +10,217 @@ module CFnDK
|
|
10
10
|
@timeout_in_minutes = data['timeout_in_minutes'] || 1
|
11
11
|
@override_parameters = data['parameters'] || {}
|
12
12
|
@option = option
|
13
|
+
@client = Aws::CloudFormation::Client.new(credentials: credentials)
|
14
|
+
@logger = CFnDK::Logger.new(option)
|
15
|
+
end
|
16
|
+
|
17
|
+
def create
|
18
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
19
|
+
@logger.info(('creating stack: ' + @name).color(:green))
|
20
|
+
@logger.debug('Name :' + name)
|
21
|
+
@logger.debug('Parametres :' + parameters.inspect)
|
22
|
+
@logger.debug('Capabilities:' + capabilities.inspect)
|
23
|
+
@logger.debug('Timeout :' + timeout_in_minutes.to_s)
|
24
|
+
@client.create_stack(
|
25
|
+
stack_name: name,
|
26
|
+
template_body: template_body,
|
27
|
+
parameters: parameters,
|
28
|
+
capabilities: capabilities,
|
29
|
+
timeout_in_minutes: timeout_in_minutes
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def wait_until_create
|
34
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
35
|
+
begin
|
36
|
+
@client.wait_until(
|
37
|
+
:stack_create_complete,
|
38
|
+
stack_name: name
|
39
|
+
)
|
40
|
+
@logger.info(('created stack: ' + @name).color(:green))
|
41
|
+
rescue Aws::Waiters::Errors::FailureStateError => ex
|
42
|
+
@logger.error ex.message
|
43
|
+
report_event
|
44
|
+
raise ex
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def update
|
49
|
+
return false if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
50
|
+
@logger.info(('updating stack: ' + @name).color(:green))
|
51
|
+
@logger.debug('Name :' + name)
|
52
|
+
@logger.debug('Parametres :' + parameters.inspect)
|
53
|
+
@logger.debug('Capabilities:' + capabilities.inspect)
|
54
|
+
@logger.debug('Timeout :' + timeout_in_minutes.to_s)
|
55
|
+
begin
|
56
|
+
@client.update_stack(
|
57
|
+
stack_name: name,
|
58
|
+
template_body: template_body,
|
59
|
+
parameters: parameters,
|
60
|
+
capabilities: capabilities
|
61
|
+
)
|
62
|
+
true
|
63
|
+
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
64
|
+
@logger.error ex.message.color(:red)
|
65
|
+
false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def wait_until_update
|
70
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
71
|
+
@client.wait_until(
|
72
|
+
:stack_update_complete,
|
73
|
+
stack_name: name
|
74
|
+
)
|
75
|
+
@logger.info(('updated stack: ' + @name).color(:green))
|
76
|
+
end
|
77
|
+
|
78
|
+
def destroy
|
79
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
80
|
+
@logger.info(('deleting stack: ' + @name).color(:green))
|
81
|
+
@logger.debug('Name :' + name)
|
82
|
+
@client.delete_stack(
|
83
|
+
stack_name: name
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
def wait_until_destroy
|
88
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
89
|
+
@client.wait_until(
|
90
|
+
:stack_delete_complete,
|
91
|
+
stack_name: name
|
92
|
+
)
|
93
|
+
@logger.info(('deleted stack: ' + @name).color(:green))
|
94
|
+
end
|
95
|
+
|
96
|
+
def create_change_set
|
97
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
98
|
+
@logger.info(('creating change set: ' + @name).color(:green))
|
99
|
+
@logger.debug('Name :' + name)
|
100
|
+
@logger.debug('Parametres :' + parameters.inspect)
|
101
|
+
@logger.debug('Capabilities:' + capabilities.inspect)
|
102
|
+
@client.create_change_set(
|
103
|
+
stack_name: name,
|
104
|
+
template_body: template_body,
|
105
|
+
parameters: parameters,
|
106
|
+
capabilities: capabilities,
|
107
|
+
change_set_name: name
|
108
|
+
)
|
109
|
+
end
|
110
|
+
|
111
|
+
def wait_until_create_change_set
|
112
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
113
|
+
begin
|
114
|
+
@client.wait_until(
|
115
|
+
:change_set_create_complete,
|
116
|
+
stack_name: name,
|
117
|
+
change_set_name: name
|
118
|
+
)
|
119
|
+
@logger.info(('created chnage set: ' + name).color(:green))
|
120
|
+
rescue Aws::Waiters::Errors::FailureStateError => ex
|
121
|
+
resp = @client.describe_change_set(
|
122
|
+
change_set_name: name,
|
123
|
+
stack_name: name
|
124
|
+
)
|
125
|
+
if resp.status_reason != "The submitted information didn't contain changes. Submit different information to create a change set."
|
126
|
+
@logger.error ex.message.color(:red)
|
127
|
+
raise ex
|
128
|
+
else
|
129
|
+
@logger.error(('failed create change set: ' + name).color(:red))
|
130
|
+
@logger.error resp.status_reason
|
131
|
+
@client.delete_change_set(
|
132
|
+
change_set_name: name,
|
133
|
+
stack_name: name
|
134
|
+
)
|
135
|
+
@logger.info(('deleted change set: ' + name).color(:red))
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def validate
|
141
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
142
|
+
@logger.info(('validate stack: ' + @name).color(:green))
|
143
|
+
@logger.debug('Name :' + name)
|
144
|
+
@client.validate_template(
|
145
|
+
template_body: template_body
|
146
|
+
)
|
147
|
+
end
|
148
|
+
|
149
|
+
def exits?
|
150
|
+
@client.describe_stacks(
|
151
|
+
stack_name: name
|
152
|
+
)
|
153
|
+
true
|
154
|
+
rescue Aws::CloudFormation::Errors::ValidationError
|
155
|
+
false
|
156
|
+
end
|
157
|
+
|
158
|
+
def report_stack
|
159
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
160
|
+
@logger.info(('stack: ' + @name).color(:green))
|
161
|
+
@logger.debug('Name :' + name)
|
162
|
+
begin
|
163
|
+
rows = @client.describe_stacks(
|
164
|
+
stack_name: name
|
165
|
+
).stacks.map do |item|
|
166
|
+
[
|
167
|
+
item.stack_name,
|
168
|
+
item.creation_time,
|
169
|
+
item.deletion_time,
|
170
|
+
coloerd_status(item.stack_status),
|
171
|
+
item.stack_status_reason]
|
172
|
+
end
|
173
|
+
table = Terminal::Table.new headings: %w(Name Creation Deletion Status Reason), rows: rows
|
174
|
+
@logger.info table
|
175
|
+
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
176
|
+
@logger.warn ex.message
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def report_event
|
181
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
182
|
+
@logger.info(('stack: ' + @name).color(:green))
|
183
|
+
@logger.debug('Name :' + name)
|
184
|
+
begin
|
185
|
+
rows = @client.describe_stack_events(
|
186
|
+
stack_name: name
|
187
|
+
).stack_events.map do |item|
|
188
|
+
[
|
189
|
+
item.resource_type,
|
190
|
+
item.timestamp,
|
191
|
+
coloerd_status(item.resource_status),
|
192
|
+
item.resource_status_reason]
|
193
|
+
end
|
194
|
+
table = Terminal::Table.new headings: %w(Type Time Status Reason), rows: rows
|
195
|
+
@logger.info table
|
196
|
+
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
197
|
+
@logger.warn ex.message
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def report_stack_resource
|
202
|
+
return if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(@name)
|
203
|
+
@logger.info(('stack: ' + @name).color(:green))
|
204
|
+
@logger.debug('Name :' + name)
|
205
|
+
begin
|
206
|
+
rows = @client.describe_stack_resources(
|
207
|
+
stack_name: name
|
208
|
+
).stack_resources.map do |item|
|
209
|
+
[
|
210
|
+
item.logical_resource_id,
|
211
|
+
item.physical_resource_id,
|
212
|
+
item.resource_type,
|
213
|
+
item.timestamp,
|
214
|
+
coloerd_status(item.resource_status),
|
215
|
+
item.resource_status_reason,
|
216
|
+
item.description,
|
217
|
+
]
|
218
|
+
end
|
219
|
+
table = Terminal::Table.new headings: %w(L-name P-name Type Timestamp Status Reason Desc), rows: rows
|
220
|
+
@logger.info table
|
221
|
+
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
222
|
+
@logger.warn ex.message
|
223
|
+
end
|
13
224
|
end
|
14
225
|
|
15
226
|
def name
|
@@ -33,6 +244,23 @@ module CFnDK
|
|
33
244
|
|
34
245
|
private
|
35
246
|
|
247
|
+
def coloerd_status(str)
|
248
|
+
case str
|
249
|
+
when 'CREATE_FAILED' then
|
250
|
+
item.resource_status.color :red
|
251
|
+
when 'ROLLBACK_IN_PROGRESS' then
|
252
|
+
item.resource_status.color :red
|
253
|
+
when 'ROLLBACK_COMPLETE' then
|
254
|
+
item.resource_status.color :red
|
255
|
+
when 'CREATE_COMPLETE' then
|
256
|
+
item.resource_status.color :green
|
257
|
+
when 'DELETE_COMPLETE' then
|
258
|
+
item.resource_status.color :gray
|
259
|
+
else
|
260
|
+
item.resource_status.color :orange
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
36
264
|
def eval_override_parameter(k, v)
|
37
265
|
if @override_parameters[k]
|
38
266
|
CFnDK::ErbString.new(@override_parameters[k], @option).value
|
data/lib/cfndk/stacks.rb
CHANGED
@@ -2,41 +2,20 @@ module CFnDK
|
|
2
2
|
class Stacks
|
3
3
|
def initialize(data, option, credentials)
|
4
4
|
@option = option
|
5
|
-
@
|
6
|
-
|
7
|
-
|
5
|
+
@credentials = credentials
|
6
|
+
@logger = CFnDK::Logger.new(option)
|
7
|
+
|
8
|
+
prepare_stack(data)
|
9
|
+
prepare_sequence
|
8
10
|
end
|
9
11
|
|
10
12
|
def create
|
11
13
|
@sequence.each do |stacks|
|
12
14
|
stacks.each do |name|
|
13
|
-
|
14
|
-
puts(('creating ' + name).color(:green))
|
15
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
16
|
-
puts('Parametres :' + @stacks[name].parameters.inspect) if @option[:v]
|
17
|
-
puts('Capabilities:' + @stacks[name].capabilities.inspect) if @option[:v]
|
18
|
-
puts('timeout :' + @stacks[name].timeout_in_minutes.to_s) if @option[:v]
|
19
|
-
@cfn_client.create_stack(
|
20
|
-
stack_name: @stacks[name].name,
|
21
|
-
template_body: @stacks[name].template_body,
|
22
|
-
parameters: @stacks[name].parameters,
|
23
|
-
capabilities: @stacks[name].capabilities,
|
24
|
-
timeout_in_minutes: @stacks[name].timeout_in_minutes
|
25
|
-
)
|
15
|
+
@stacks[name].create
|
26
16
|
end
|
27
17
|
stacks.each do |name|
|
28
|
-
|
29
|
-
begin
|
30
|
-
@cfn_client.wait_until(
|
31
|
-
:stack_create_complete,
|
32
|
-
stack_name: @stacks[name].name
|
33
|
-
)
|
34
|
-
puts(('created ' + name).color(:green))
|
35
|
-
rescue Aws::Waiters::Errors::FailureStateError => ex
|
36
|
-
puts ex.message
|
37
|
-
report_event
|
38
|
-
raise ex
|
39
|
-
end
|
18
|
+
@stacks[name].wait_until_create
|
40
19
|
end
|
41
20
|
end
|
42
21
|
end
|
@@ -45,31 +24,30 @@ module CFnDK
|
|
45
24
|
@sequence.each do |stacks|
|
46
25
|
updating_stacks = []
|
47
26
|
stacks.each do |name|
|
48
|
-
|
49
|
-
|
50
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
51
|
-
puts('Parametres :' + @stacks[name].parameters.inspect) if @option[:v]
|
52
|
-
puts('Capabilities:' + @stacks[name].capabilities.inspect) if @option[:v]
|
53
|
-
puts('timeout :' + @stacks[name].timeout_in_minutes.to_s) if @option[:v]
|
54
|
-
begin
|
55
|
-
@cfn_client.update_stack(
|
56
|
-
stack_name: @stacks[name].name,
|
57
|
-
template_body: @stacks[name].template_body,
|
58
|
-
parameters: @stacks[name].parameters,
|
59
|
-
capabilities: @stacks[name].capabilities
|
60
|
-
)
|
61
|
-
updating_stacks.push name
|
62
|
-
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
63
|
-
puts ex.message.color :red
|
64
|
-
end
|
27
|
+
result = @stacks[name].update
|
28
|
+
updating_stacks.push name if result
|
65
29
|
end
|
66
30
|
updating_stacks.each do |name|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
31
|
+
@stacks[name].wait_until_update
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy
|
37
|
+
@sequence.reverse_each do |stacks|
|
38
|
+
stacks.each do |name|
|
39
|
+
@stacks[name].destroy
|
40
|
+
end
|
41
|
+
stacks.each do |name|
|
42
|
+
@stacks[name].wait_until_destroy
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def validate
|
48
|
+
@sequence.each do |stacks|
|
49
|
+
stacks.each do |name|
|
50
|
+
@stacks[name].validate
|
73
51
|
end
|
74
52
|
end
|
75
53
|
end
|
@@ -79,238 +57,57 @@ module CFnDK
|
|
79
57
|
create_stacks = []
|
80
58
|
changeset_stacks = []
|
81
59
|
stacks.each do |name|
|
82
|
-
|
83
|
-
|
84
|
-
@cfn_client.describe_stacks(
|
85
|
-
stack_name: @stacks[name].name
|
86
|
-
)
|
87
|
-
puts(('creating ' + name).color(:green))
|
88
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
89
|
-
puts('Parametres :' + @stacks[name].parameters.inspect) if @option[:v]
|
90
|
-
puts('Capabilities:' + @stacks[name].capabilities.inspect) if @option[:v]
|
91
|
-
@cfn_client.create_change_set(
|
92
|
-
stack_name: @stacks[name].name,
|
93
|
-
template_body: @stacks[name].template_body,
|
94
|
-
parameters: @stacks[name].parameters,
|
95
|
-
capabilities: @stacks[name].capabilities,
|
96
|
-
change_set_name: @stacks[name].name,
|
97
|
-
)
|
60
|
+
if @stacks[name].exits?
|
61
|
+
@stacks[name].create_change_set
|
98
62
|
changeset_stacks.push name
|
99
|
-
|
100
|
-
|
101
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
102
|
-
puts('Parametres :' + @stacks[name].parameters.inspect) if @option[:v]
|
103
|
-
puts('Capabilities:' + @stacks[name].capabilities.inspect) if @option[:v]
|
104
|
-
puts('timeout :' + @stacks[name].timeout_in_minutes.to_s) if @option[:v]
|
105
|
-
@cfn_client.create_stack(
|
106
|
-
stack_name: @stacks[name].name,
|
107
|
-
template_body: @stacks[name].template_body,
|
108
|
-
parameters: @stacks[name].parameters,
|
109
|
-
capabilities: @stacks[name].capabilities,
|
110
|
-
timeout_in_minutes: @stacks[name].timeout_in_minutes
|
111
|
-
)
|
63
|
+
else
|
64
|
+
@stacks[name].create
|
112
65
|
create_stacks.push name
|
113
66
|
end
|
114
67
|
end
|
115
68
|
create_stacks.each do |name|
|
116
|
-
|
117
|
-
@cfn_client.wait_until(
|
118
|
-
:stack_create_complete,
|
119
|
-
stack_name: @stacks[name].name
|
120
|
-
)
|
121
|
-
puts(('created ' + name).color(:green))
|
69
|
+
@stacks[name].wait_until_create
|
122
70
|
end
|
123
71
|
changeset_stacks.each do |name|
|
124
|
-
|
125
|
-
begin
|
126
|
-
@cfn_client.wait_until(
|
127
|
-
:change_set_create_complete,
|
128
|
-
stack_name: @stacks[name].name,
|
129
|
-
change_set_name: @stacks[name].name
|
130
|
-
)
|
131
|
-
puts(('created ' + @stacks[name].name).color(:green))
|
132
|
-
rescue Aws::Waiters::Errors::FailureStateError => ex
|
133
|
-
resp = @cfn_client.describe_change_set(
|
134
|
-
change_set_name: @stacks[name].name,
|
135
|
-
stack_name: @stacks[name].name,
|
136
|
-
)
|
137
|
-
if resp.status_reason != "The submitted information didn't contain changes. Submit different information to create a change set."
|
138
|
-
puts ex.message.color :red
|
139
|
-
raise ex
|
140
|
-
else
|
141
|
-
puts(('failed ' + @stacks[name].name).color(:red))
|
142
|
-
puts resp.status_reason
|
143
|
-
@cfn_client.delete_change_set(
|
144
|
-
change_set_name: @stacks[name].name,
|
145
|
-
stack_name: @stacks[name].name,
|
146
|
-
)
|
147
|
-
puts(('deleted ' + @stacks[name].name).color(:red))
|
148
|
-
end
|
149
|
-
end
|
72
|
+
@stacks[name].wait_until_create_change_set
|
150
73
|
end
|
151
74
|
end
|
152
75
|
end
|
153
76
|
|
154
77
|
def report_stack
|
155
|
-
rows = @sequence.flat_map do |stacks|
|
156
|
-
stacks.flat_map do |name|
|
157
|
-
rows = []
|
158
|
-
begin
|
159
|
-
rows = @cfn_client.describe_stacks(
|
160
|
-
stack_name: @stacks[name].name
|
161
|
-
).stacks.map do |item|
|
162
|
-
[
|
163
|
-
item.stack_name,
|
164
|
-
item.creation_time,
|
165
|
-
item.deletion_time,
|
166
|
-
case item.stack_status
|
167
|
-
when 'CREATE_FAILED' then
|
168
|
-
item.stack_status.color :red
|
169
|
-
when 'ROLLBACK_IN_PROGRESS' then
|
170
|
-
item.stack_status.color :red
|
171
|
-
when 'ROLLBACK_COMPLETE' then
|
172
|
-
item.stack_status.color :red
|
173
|
-
when 'CREATE_COMPLETE' then
|
174
|
-
item.stack_status.color :green
|
175
|
-
when 'DELETE_COMPLETE' then
|
176
|
-
item.stack_status.color :gray
|
177
|
-
else
|
178
|
-
item.stack_status.color :orange
|
179
|
-
end,
|
180
|
-
item.stack_status_reason]
|
181
|
-
end
|
182
|
-
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
183
|
-
puts ex.message
|
184
|
-
end
|
185
|
-
rows
|
186
|
-
end
|
187
|
-
end
|
188
|
-
table = Terminal::Table.new headings: %w(Name Creation Deletion Status Reason), rows: rows
|
189
|
-
puts table
|
190
|
-
end
|
191
|
-
|
192
|
-
def report_stack_resource
|
193
78
|
@sequence.each do |stacks|
|
194
79
|
stacks.each do |name|
|
195
|
-
|
196
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
197
|
-
begin
|
198
|
-
rows = @cfn_client.describe_stack_resources(
|
199
|
-
stack_name: @stacks[name].name
|
200
|
-
).stack_resources.map do |item|
|
201
|
-
[
|
202
|
-
item.logical_resource_id,
|
203
|
-
item.physical_resource_id,
|
204
|
-
item.resource_type,
|
205
|
-
item.timestamp,
|
206
|
-
case item.resource_status
|
207
|
-
when 'CREATE_FAILED' then
|
208
|
-
item.resource_status.color :red
|
209
|
-
when 'ROLLBACK_IN_PROGRESS' then
|
210
|
-
item.resource_status.color :red
|
211
|
-
when 'ROLLBACK_COMPLETE' then
|
212
|
-
item.resource_status.color :red
|
213
|
-
when 'CREATE_COMPLETE' then
|
214
|
-
item.resource_status.color :green
|
215
|
-
when 'DELETE_COMPLETE' then
|
216
|
-
item.resource_status.color :gray
|
217
|
-
else
|
218
|
-
item.resource_status.color :orange
|
219
|
-
end,
|
220
|
-
item.resource_status_reason,
|
221
|
-
item.description,
|
222
|
-
]
|
223
|
-
end
|
224
|
-
table = Terminal::Table.new headings: %w(L-name P-name Type Timestamp Status Reason Desc), rows: rows
|
225
|
-
puts table
|
226
|
-
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
227
|
-
puts ex.message
|
228
|
-
end
|
80
|
+
@stacks[name].report_stack
|
229
81
|
end
|
230
82
|
end
|
231
83
|
end
|
232
84
|
|
233
|
-
def
|
85
|
+
def report_stack_resource
|
234
86
|
@sequence.each do |stacks|
|
235
87
|
stacks.each do |name|
|
236
|
-
|
237
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
238
|
-
begin
|
239
|
-
rows = @cfn_client.describe_stack_events(
|
240
|
-
stack_name: @stacks[name].name
|
241
|
-
).stack_events.map do |item|
|
242
|
-
[
|
243
|
-
item.resource_type,
|
244
|
-
item.timestamp,
|
245
|
-
case item.resource_status
|
246
|
-
when 'CREATE_FAILED' then
|
247
|
-
item.resource_status.color :red
|
248
|
-
when 'ROLLBACK_IN_PROGRESS' then
|
249
|
-
item.resource_status.color :red
|
250
|
-
when 'ROLLBACK_COMPLETE' then
|
251
|
-
item.resource_status.color :red
|
252
|
-
when 'CREATE_COMPLETE' then
|
253
|
-
item.resource_status.color :green
|
254
|
-
when 'DELETE_COMPLETE' then
|
255
|
-
item.resource_status.color :gray
|
256
|
-
else
|
257
|
-
item.resource_status.color :orange
|
258
|
-
end,
|
259
|
-
item.resource_status_reason]
|
260
|
-
end
|
261
|
-
table = Terminal::Table.new headings: %w(Type Time Status Reason), rows: rows
|
262
|
-
puts table
|
263
|
-
rescue Aws::CloudFormation::Errors::ValidationError => ex
|
264
|
-
puts ex.message
|
265
|
-
end
|
266
|
-
end
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
|
-
def destroy
|
271
|
-
@sequence.reverse_each do |stacks|
|
272
|
-
stacks.each do |name|
|
273
|
-
next if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(name)
|
274
|
-
puts(('deleting ' + name).color(:green))
|
275
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
276
|
-
@cfn_client.delete_stack(
|
277
|
-
stack_name: @stacks[name].name
|
278
|
-
)
|
279
|
-
end
|
280
|
-
stacks.each do |name|
|
281
|
-
next if @option[:stack_names].instance_of?(Array) && !@option[:stack_names].include?(name)
|
282
|
-
@cfn_client.wait_until(
|
283
|
-
:stack_delete_complete,
|
284
|
-
stack_name: @stacks[name].name
|
285
|
-
)
|
286
|
-
puts(('deleted ' + name).color(:green))
|
88
|
+
@stacks[name].report_stack_resource
|
287
89
|
end
|
288
90
|
end
|
289
91
|
end
|
290
92
|
|
291
|
-
def
|
93
|
+
def report_event
|
292
94
|
@sequence.each do |stacks|
|
293
95
|
stacks.each do |name|
|
294
|
-
|
295
|
-
puts(('validate ' + name).color(:green))
|
296
|
-
puts('Name :' + @stacks[name].name) if @option[:v]
|
297
|
-
@cfn_client.validate_template(
|
298
|
-
template_body: @stacks[name].template_body
|
299
|
-
)
|
96
|
+
@stacks[name].report_event
|
300
97
|
end
|
301
98
|
end
|
302
99
|
end
|
303
100
|
|
304
101
|
private
|
305
102
|
|
306
|
-
def
|
103
|
+
def prepare_stack(data)
|
307
104
|
@stacks = {}
|
308
105
|
data['stacks'].each do |name, properties|
|
309
|
-
@stacks[name] = Stack.new(name, properties, @option)
|
106
|
+
@stacks[name] = Stack.new(name, properties, @option, @credentials)
|
310
107
|
end
|
311
108
|
end
|
312
109
|
|
313
|
-
def
|
110
|
+
def prepare_sequence
|
314
111
|
@sequence = []
|
315
112
|
names_of_upprocessed_stack = @stacks.keys
|
316
113
|
names_of_processed_stack = []
|
@@ -320,7 +117,7 @@ module CFnDK
|
|
320
117
|
names_of_processed_stack.include? depend_name
|
321
118
|
end
|
322
119
|
end
|
323
|
-
raise 'There are cyclic dependency.' if names.empty?
|
120
|
+
raise 'There are cyclic dependency or stack is not exit. unprocessed_stack: ' + names_of_upprocessed_stack.join(',') if names.empty?
|
324
121
|
names_of_processed_stack += names
|
325
122
|
names_of_upprocessed_stack -= names
|
326
123
|
@sequence.push names
|
data/lib/cfndk/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cfndk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshihisa AMAKATA
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- lib/cfndk/erb_string.rb
|
116
116
|
- lib/cfndk/key_pair.rb
|
117
117
|
- lib/cfndk/key_pairs.rb
|
118
|
+
- lib/cfndk/logger.rb
|
118
119
|
- lib/cfndk/stack.rb
|
119
120
|
- lib/cfndk/stacks.rb
|
120
121
|
- lib/cfndk/version.rb
|