sumomo 0.8.4 → 0.8.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +169 -0
- data/Gemfile +2 -0
- data/Rakefile +5 -3
- data/bin/console +4 -3
- data/data/sumomo/api_modules/node_modules/.bin/uuid +1 -1
- data/data/sumomo/custom_resources/TempS3Bucket.js +208 -0
- data/exe/sumomo +42 -41
- data/lib/sumomo.rb +235 -233
- data/lib/sumomo/api.rb +148 -151
- data/lib/sumomo/cdn.rb +119 -113
- data/lib/sumomo/dns.rb +20 -20
- data/lib/sumomo/ec2.rb +490 -491
- data/lib/sumomo/ecs.rb +256 -262
- data/lib/sumomo/irregular.rb +9 -0
- data/lib/sumomo/momo_extensions/resource.rb +8 -7
- data/lib/sumomo/momo_extensions/stack.rb +4 -3
- data/lib/sumomo/network.rb +109 -106
- data/lib/sumomo/stack.rb +191 -189
- data/lib/sumomo/version.rb +3 -1
- data/sumomo.gemspec +23 -22
- metadata +25 -22
data/lib/sumomo.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'momo'
|
2
4
|
require 's3cabinet'
|
3
5
|
require 'aws-sdk'
|
4
6
|
require 'zip'
|
5
7
|
require 'yaml'
|
6
8
|
|
7
|
-
require
|
9
|
+
require 'sumomo/version'
|
8
10
|
require 'sumomo/api'
|
9
11
|
require 'sumomo/cdn'
|
10
12
|
require 'sumomo/dns'
|
@@ -16,236 +18,236 @@ require 'sumomo/momo_extensions/resource'
|
|
16
18
|
require 'sumomo/momo_extensions/stack'
|
17
19
|
|
18
20
|
module Sumomo
|
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
|
-
|
21
|
+
def self.make_master_key_name(name:)
|
22
|
+
"#{name}_master_key"
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.make_master_key_key(name:)
|
26
|
+
"cloudformation/#{make_master_key_name(name: name)}.pem"
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.update_stack(name:, region:, sns_arn: nil, &block)
|
30
|
+
cf = Aws::CloudFormation::Client.new(region: region)
|
31
|
+
s3 = Aws::S3::Client.new(region: region)
|
32
|
+
ec2 = Aws::EC2::Client.new(region: region)
|
33
|
+
|
34
|
+
begin
|
35
|
+
s3.head_bucket(bucket: name)
|
36
|
+
rescue Aws::S3::Errors::NotFound => e
|
37
|
+
s3.create_bucket(bucket: name)
|
38
|
+
end
|
39
|
+
|
40
|
+
store = S3Cabinet::S3Cabinet.new(nil, nil, name, region)
|
41
|
+
|
42
|
+
master_key_name = make_master_key_name(name: name)
|
43
|
+
master_key_key = make_master_key_key(name: name)
|
44
|
+
|
45
|
+
unless store.get(master_key_key)
|
46
|
+
|
47
|
+
resp = nil
|
48
|
+
begin
|
49
|
+
puts 'No master key found, creating...'
|
50
|
+
resp = ec2.create_key_pair(key_name: master_key_name)
|
51
|
+
rescue StandardError
|
52
|
+
puts 'Master key conflict! Deleting old one'
|
53
|
+
ec2.delete_key_pair(key_name: master_key_name)
|
54
|
+
resp = ec2.create_key_pair(key_name: master_key_name)
|
55
|
+
end
|
56
|
+
|
57
|
+
store.set(master_key_key, resp.key_material)
|
58
|
+
store.set("#{master_key_key}.fingerprint", resp.key_fingerprint)
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
dummy_number = store.get('cloudformation/dummy_number')
|
63
|
+
dummy_number = 0 if dummy_number.nil?
|
64
|
+
dummy_number += 1
|
65
|
+
store.set('cloudformation/dummy_number', dummy_number)
|
66
|
+
|
67
|
+
hidden_values = []
|
68
|
+
|
69
|
+
template = Momo.cfl do
|
70
|
+
inject Sumomo::Stack
|
71
|
+
|
72
|
+
@region = region
|
73
|
+
@version_number = dummy_number
|
74
|
+
@custom_resources = {}
|
75
|
+
@bucket_name = name
|
76
|
+
@store = store
|
77
|
+
@master_key_name = master_key_name
|
78
|
+
@ec2 = ec2
|
79
|
+
@cf = cf
|
80
|
+
@s3 = s3
|
81
|
+
@has_dummy = true
|
82
|
+
@dummy_vpc = nil
|
83
|
+
|
84
|
+
instance_eval(&block)
|
85
|
+
|
86
|
+
dummy_vpc = @dummy_vpc
|
87
|
+
|
88
|
+
if @has_dummy
|
89
|
+
make 'AWS::EC2::SecurityGroup', name: 'DummyResource' do
|
90
|
+
GroupDescription 'Dummy resource for tracking Cloudformation Deployment.'
|
91
|
+
VpcId dummy_vpc unless dummy_vpc.nil?
|
92
|
+
Tags [{ 'Key' => 'Name', 'Value' => "dummyfordeploy#{dummy_number}" }]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
hidden_values = @hidden_values
|
97
|
+
end.templatize
|
98
|
+
|
99
|
+
# TODO: if the template is too big, split it into nested templates
|
100
|
+
|
101
|
+
# puts JSON.parse(template).to_yaml
|
102
|
+
|
103
|
+
store.set_raw('cloudformation/template', template)
|
104
|
+
|
105
|
+
update_options = {
|
106
|
+
stack_name: name,
|
107
|
+
template_url: store.url('cloudformation/template'),
|
108
|
+
parameters: hidden_values,
|
109
|
+
capabilities: ['CAPABILITY_IAM']
|
110
|
+
}
|
111
|
+
|
112
|
+
begin
|
113
|
+
cf.update_stack(update_options)
|
114
|
+
rescue StandardError => e
|
115
|
+
if e.message.end_with? 'does not exist'
|
116
|
+
update_options[:timeout_in_minutes] = 30
|
117
|
+
update_options[:notification_arns] = sns_arn if sns_arn
|
118
|
+
cf.create_stack(update_options)
|
119
|
+
else
|
120
|
+
p e
|
121
|
+
puts "Error: #{e.message}"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.wait_for_stack(name:, region:)
|
127
|
+
cf = Aws::CloudFormation::Client.new(region: region)
|
128
|
+
|
129
|
+
stack_id = name
|
130
|
+
|
131
|
+
begin
|
132
|
+
resp = cf.describe_stack_events(stack_name: stack_id)
|
133
|
+
top_event = resp.stack_events[0]
|
134
|
+
top_event_id = top_event.event_id
|
135
|
+
puts "#{top_event.logical_resource_id} #{top_event.resource_status} #{top_event.resource_status_reason}"
|
136
|
+
rescue StandardError => e
|
137
|
+
puts "describe_stack_events: #{e.message}"
|
138
|
+
end
|
139
|
+
|
140
|
+
failure_count = 0
|
141
|
+
loop do
|
142
|
+
begin
|
143
|
+
unless /^arn\:/.match(stack_id)
|
144
|
+
stack_id = cf.describe_stacks(stack_name: stack_id).stacks[0].stack_id
|
145
|
+
# puts "Unique Stack ID: #{stack_id}"
|
146
|
+
end
|
147
|
+
|
148
|
+
resp = cf.describe_stack_events(stack_name: stack_id)
|
149
|
+
curr = 0
|
150
|
+
lines = []
|
151
|
+
loop do
|
152
|
+
curr_event = resp.stack_events[curr]
|
153
|
+
break if curr_event.event_id == top_event_id
|
154
|
+
|
155
|
+
lines << "#{curr_event.logical_resource_id} #{curr_event.resource_status} #{curr_event.resource_status_reason}"
|
156
|
+
break if curr == resp.stack_events.length - 1
|
157
|
+
|
158
|
+
curr += 1
|
159
|
+
end
|
160
|
+
|
161
|
+
lines.reverse.each { |x| puts x }
|
162
|
+
|
163
|
+
top_event_id = resp.stack_events[0].event_id
|
164
|
+
rescue StandardError => e
|
165
|
+
puts "describe_stack_events: #{e.message}"
|
166
|
+
failure_count += 1
|
167
|
+
break if failure_count > 5
|
168
|
+
end
|
169
|
+
|
170
|
+
sleep 1
|
171
|
+
begin
|
172
|
+
resp = cf.describe_stacks(stack_name: stack_id)
|
173
|
+
|
174
|
+
break if /(COMPLETE$)|(FAILED$)/.match(resp.stacks[0].stack_status)
|
175
|
+
rescue StandardError => e
|
176
|
+
puts "describe_stacks: #{e.message}"
|
177
|
+
break
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
class APITester
|
183
|
+
attr_accessor :apis
|
184
|
+
def initialize(&block)
|
185
|
+
@apis = {}
|
186
|
+
instance_eval(&block)
|
187
|
+
end
|
188
|
+
|
189
|
+
def make_api(_domain_name, name:, script: nil, dns: nil, cert: nil, with_statements: [], &block)
|
190
|
+
@apis[name] = block
|
191
|
+
end
|
192
|
+
|
193
|
+
def method_missing(name, *args, &block); end
|
194
|
+
end
|
195
|
+
|
196
|
+
def self.test_api(apiname, pretty_print, &block)
|
197
|
+
tester = APITester.new(&block)
|
198
|
+
test_name = nil
|
199
|
+
if tester.apis.length == 1
|
200
|
+
test_name = tester.apis.keys.first
|
201
|
+
elsif apiname
|
202
|
+
if tester.apis.key? apiname
|
203
|
+
test_name = apiname
|
204
|
+
else
|
205
|
+
puts "Unknown API name. Please choose from one of the APIs: #{tester.apis.keys.inspect}"
|
206
|
+
end
|
207
|
+
else
|
208
|
+
puts "Please choose from one of the APIs: #{tester.apis.keys.inspect}"
|
209
|
+
end
|
210
|
+
|
211
|
+
if test_name
|
212
|
+
puts "Testing API #{test_name}"
|
213
|
+
apigen = Stack::APIGenerator.new(pretty_print: pretty_print, &tester.apis[test_name])
|
214
|
+
|
215
|
+
script = File.read(File.join(Gem.loaded_specs['sumomo'].full_gem_path, 'data', 'sumomo', 'api_modules', 'test_script.js'))
|
216
|
+
script.sub!('// {{ ROUTES }}', apigen.generate)
|
217
|
+
script.gsub!('{{ SCRIPT }}', apigen.init_script)
|
218
|
+
|
219
|
+
File.write('.test.js', script)
|
220
|
+
Stack::APIGenerator.combine_modules('.test_modules')
|
221
|
+
|
222
|
+
exec 'NODE_PATH=.test_modules node .test.js'
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def self.delete_stack(name:, region:, retain_bucket: false)
|
227
|
+
cf = Aws::CloudFormation::Client.new(region: region)
|
228
|
+
ec2 = Aws::EC2::Client.new(region: region)
|
229
|
+
|
230
|
+
cf.delete_stack(stack_name: name)
|
231
|
+
ec2.delete_key_pair(key_name: make_master_key_name(name: name))
|
232
|
+
|
233
|
+
unless retain_bucket
|
234
|
+
wait_for_stack(name: name, region: region)
|
235
|
+
s3 = Aws::S3::Resource.new(region: region)
|
236
|
+
bucket = s3.bucket(name)
|
237
|
+
bucket.delete!
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def self.get_stack_outputs(name:, region:)
|
242
|
+
cf = Aws::CloudFormation::Client.new(region: region)
|
243
|
+
|
244
|
+
map = {}
|
245
|
+
cf.describe_stacks(stack_name: name).stacks[0].outputs.each do |x|
|
246
|
+
map[x.output_key] = x.output_value
|
247
|
+
end
|
248
|
+
|
249
|
+
map
|
250
|
+
end
|
251
|
+
|
252
|
+
singleton_class.send(:alias_method, :create_stack, :update_stack)
|
251
253
|
end
|