Class: Ridoku::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/ridoku/base.rb
Direct Known Subclasses
Backup, Cook, Create, Cron, Db, Deploy, Domain, Dump, Env, List, Log, Maintenance, Packages, Run, Service, Worker
Constant Summary
collapse
- POSTGRES_GROUP_NAME =
'Ridoku-PostgreSQL-Server'
Class Attribute Summary collapse
Class Method Summary
collapse
-
.base_command(app_id, instance_ids, comment) ⇒ Object
-
.color_code_logs(logs) ⇒ Object
-
.configure_ec2_client ⇒ Object
-
.configure_iam_client ⇒ Object
-
.configure_instance_roles ⇒ Object
-
.configure_opsworks_client ⇒ Object
-
.configure_roles ⇒ Object
-
.configure_service_roles ⇒ Object
-
.create_app(conf) ⇒ Object
-
.create_role(conf) ⇒ Object
-
.deploy(app_id, instance_ids, comment, custom_json = nil) ⇒ Object
-
.execute_recipes(app_id, instance_ids, comment, recipes, custom_json = nil) ⇒ Object
-
.extract_instance_ids(layers = nil) ⇒ Object
-
.fetch_account(options = {}) ⇒ Object
-
.fetch_app(options = {}) ⇒ Object
-
.fetch_instance(shortname = :all, options = {}) ⇒ Object
‘lb’ - load balancing layers ‘rails-app’ ‘custom’.
-
.fetch_layer(shortname = :all, options = {}) ⇒ Object
-
.fetch_permissions(options = {}) ⇒ Object
-
.fetch_roles ⇒ Object
-
.fetch_stack(options = {}) ⇒ Object
-
.get_instances_for_layer(layer) ⇒ Object
-
.get_layer_ids(shortname) ⇒ Object
-
.if_debug?(&block) ⇒ Boolean
-
.initialize_app_environment(conf) ⇒ Object
-
.instance_by_id(id) ⇒ Object
-
.instance_role_configured? ⇒ Boolean
-
.load_config(path) ⇒ Object
-
.monitor_deployment(dep_ids) ⇒ Object
-
.postgresql_group_exists?(region = 'us-west-1') ⇒ Boolean
-
.pretty_instances(io) ⇒ Object
-
.roles_configured? ⇒ Boolean
-
.rollback(app_id, instance_ids, comment, custom_json = nil) ⇒ Object
-
.run_command(deployment) ⇒ Object
-
.save_app(values) ⇒ Object
-
.save_config(path, limit = [:app, :stack, :ssh_key, :local_init, :shell_user, :service_arn, :instance_arn, :backup_bucket]) ⇒ Object
-
.save_layer(layer, values) ⇒ Object
-
.save_stack ⇒ Object
-
.select_instances(args) ⇒ Object
-
.service_role_configured? ⇒ Boolean
-
.standard_deploy(layer = :all, custom_json = nil) ⇒ Object
-
.update_cookbooks(instance_ids) ⇒ Object
-
.update_pg_security_group(client = self.ec2_client) ⇒ Object
-
.update_pg_security_groups_in_all_regions ⇒ Object
-
.valid_instances?(args) ⇒ Boolean
Class Attribute Details
.account ⇒ Object
Returns the value of attribute account.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def account
@account
end
|
.account_id ⇒ Object
Returns the value of attribute account_id.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def account_id
@account_id
end
|
.app ⇒ Object
Returns the value of attribute app.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def app
@app
end
|
.app_list ⇒ Object
Returns the value of attribute app_list.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def app_list
@app_list
end
|
.aws_client ⇒ Object
Returns the value of attribute aws_client.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def aws_client
@aws_client
end
|
.config ⇒ Object
Returns the value of attribute config.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def config
@config
end
|
.custom_json ⇒ Object
Returns the value of attribute custom_json.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def custom_json
@custom_json
end
|
.ec2_client ⇒ Object
Returns the value of attribute ec2_client.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def ec2_client
@ec2_client
end
|
.iam_client ⇒ Object
Returns the value of attribute iam_client.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def iam_client
@iam_client
end
|
.instance_list ⇒ Object
Returns the value of attribute instance_list.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def instance_list
@instance_list
end
|
.instances ⇒ Object
Returns the value of attribute instances.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def instances
@instances
end
|
.layer_list ⇒ Object
Returns the value of attribute layer_list.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def layer_list
@layer_list
end
|
.layers ⇒ Object
Returns the value of attribute layers.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def layers
@layers
end
|
.permissions ⇒ Object
Returns the value of attribute permissions.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def permissions
@permissions
end
|
.stack ⇒ Object
Returns the value of attribute stack.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def stack
@stack
end
|
.stack_list ⇒ Object
Returns the value of attribute stack_list.
23
24
25
|
# File 'lib/ridoku/base.rb', line 23
def stack_list
@stack_list
end
|
Class Method Details
.base_command(app_id, instance_ids, comment) ⇒ Object
571
572
573
574
575
576
577
578
579
580
|
# File 'lib/ridoku/base.rb', line 571
def base_command(app_id, instance_ids, )
fail ArgumentError.new('[ERROR] No instances selected.') if
!instance_ids.is_a?(Array) || instance_ids.empty?
{}.tap do |cmd|
cmd[:instance_ids] = instance_ids
cmd[:app_id] = app_id if app_id
cmd[:comment] = if
end
end
|
.color_code_logs(logs) ⇒ Object
642
643
644
|
# File 'lib/ridoku/base.rb', line 642
def color_code_logs(logs)
$stderr.puts(logs.gsub(%r((?<color>\[[0-9]{1,2}m)),"\e\\k<color>"))
end
|
236
237
238
239
240
|
# File 'lib/ridoku/base.rb', line 236
def configure_ec2_client
return if self.ec2_client
self.ec2_client = AWS::EC2.new
end
|
229
230
231
232
233
234
|
# File 'lib/ridoku/base.rb', line 229
def configure_iam_client
return if self.iam_client
iam = AWS::IAM.new
self.iam_client = iam.client
end
|
353
354
355
356
357
358
359
360
|
# File 'lib/ridoku/base.rb', line 353
def configure_instance_roles
return true if instance_role_configured?
fetch_account
instance_role = "%7B%22Version%22%3A%222008-10-17%22%2C%22Statement%22%3A%5B%7B%22Sid%22%3A%22%22%2C%22Effect%22%3A%22Allow%22%2C%22Principal%22%3A%7B%22Service%22%3A%22ec2.amazonaws.com%22%7D%2C%22Action%22%3A%22sts%3AAssumeRole%22%7D%5D%7D"
instance_resource = 'role/aws-opsworks-ec2-role'
instance_role_arn = "arn:aws:iam::#{account_id}:#{instance_resource}"
end
|
58
59
60
61
|
# File 'lib/ridoku/base.rb', line 58
def configure_opsworks_client
opsworks = AWS::OpsWorks.new
self.aws_client = opsworks.client
end
|
348
349
350
351
|
# File 'lib/ridoku/base.rb', line 348
def configure_roles
configure_service_roles
configure_instance_roles
end
|
362
363
364
365
366
367
368
369
|
# File 'lib/ridoku/base.rb', line 362
def configure_service_roles
return true if service_role_configured?
fetch_account
opsworks_role = "%7B%22Version%22%3A%222008-10-17%22%2C%22Statement%22%3A%5B%7B%22Sid%22%3A%22%22%2C%22Effect%22%3A%22Allow%22%2C%22Principal%22%3A%7B%22Service%22%3A%22opsworks.amazonaws.com%22%7D%2C%22Action%22%3A%22sts%3AAssumeRole%22%7D%5D%7D"
opsworks_resource = 'role/aws-opsworks-service-role'
opsworks_role_arn = "arn:aws:iam::#{account_id}:#{opsworks_resource}"
end
|
.create_app(conf) ⇒ Object
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
# File 'lib/ridoku/base.rb', line 379
def create_app(conf)
conf[:stack_id] = stack[:stack_id]
key_file = conf[:app_source][:ssh_key]
fail ArgumentError.new('Key file doesn\'t exist.') unless
File.exists?(key_file)
File.open(key_file, 'r') { |f| conf[:app_source][:ssh_key] = f.read }
conf[:attributes].tap do |opt|
opt.keys.each do |k|
opt[k.to_s.camelize] = opt.delete(k).to_s unless k.is_a?(String)
end
end
fail ArgumentError.new('attribute:rails_env must be specified.') unless
conf[:attributes]['RailsEnv'].length > 0
if config[:practice]
$stdout.puts conf.to_s
else
aws_client.create_app(conf)
initialize_app_environment(conf)
end
end
|
.create_role(conf) ⇒ Object
371
372
373
374
375
376
377
|
# File 'lib/ridoku/base.rb', line 371
def create_role(conf)
if config[:practice]
puts conf.to_s
else
iam_client.create_role(conf)
end
end
|
.deploy(app_id, instance_ids, comment, custom_json = nil) ⇒ Object
600
601
602
603
604
605
606
607
|
# File 'lib/ridoku/base.rb', line 600
def deploy(app_id, instance_ids, , custom_json = nil)
base_command(app_id, instance_ids, ).tap do |cmd|
cmd[:command] = {
name: 'deploy'
}
cmd[:custom_json] = custom_json if custom_json
end
end
|
.execute_recipes(app_id, instance_ids, comment, recipes, custom_json = nil) ⇒ Object
589
590
591
592
593
594
595
596
597
598
|
# File 'lib/ridoku/base.rb', line 589
def execute_recipes(app_id, instance_ids, , recipes,
custom_json = nil)
base_command(app_id, instance_ids, ).tap do |cmd|
cmd[:command] = {
name: 'execute_recipes',
args: { 'recipes' => [recipes].flatten }
}
cmd[:custom_json] = custom_json if custom_json
end
end
|
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
# File 'lib/ridoku/base.rb', line 554
def (layers = nil)
Base.fetch_instance(layers || Base.config[:layers] || :all, force: true)
names = Base.config[:instances] || []
instances = Base.instances.select do |inst|
if names.length > 0
names.index(inst[:hostname]) != nil && inst[:status] != 'offline'
else
inst[:status] == 'online'
end
end
instances.map do |inst|
inst[:instance_id]
end
end
|
.fetch_account(options = {}) ⇒ Object
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# File 'lib/ridoku/base.rb', line 285
def fetch_account(options = {})
return account if account && !options[:force]
configure_iam_client
self.account = iam_client.get_user
self.account_id = nil
account[:user][:arn].match(/.*:.*:.*:.*:([0-9]+)/) do |m|
self.account_id = m[1]
end
fail StandardError.new('Failed to determine account ID from user info (it was me not you!)!') unless
account_id
account
end
|
.fetch_app(options = {}) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/ridoku/base.rb', line 96
def fetch_app(options = {})
return app if app && !options[:force]
fetch_stack
app_name = config[:app]
fail InvalidConfig.new(:app, :none) unless app_name
self.app_list = aws_client.describe_apps(stack_id: stack[:stack_id])[:apps]
self.app = nil
app_list.each do |sapp|
self.app = sapp if app_name == sapp[:name]
end
fail InvalidConfig.new(:app, :invalid) unless app
return app
end
|
.fetch_instance(shortname = :all, options = {}) ⇒ Object
‘lb’ - load balancing layers ‘rails-app’ ‘custom’
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/ridoku/base.rb', line 193
def fetch_instance(shortname = :all, options = {})
return instances if instances && !options[:force]
fetch_stack
unless instance_list
self.instance_list = self.instances =
aws_client.describe_instances(stack_id: stack[:stack_id])[:instances]
end
if shortname != :all
fetch_layer(shortname, force: true)
self.instances = []
layers.each do |layer|
instance = aws_client.describe_instances(
layer_id: layer[:layer_id])
self.instances << instance[:instances]
end
self.instances.flatten!
end
end
|
.fetch_layer(shortname = :all, options = {}) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/ridoku/base.rb', line 137
def fetch_layer(shortname = :all, options = {})
return layers if layers && !options[:force]
fetch_stack
unless self.layer_list
self.layers = self.layer_list = aws_client.describe_layers(
stack_id: stack[:stack_id])[:layers]
end
if shortname != :all
shortname = [shortname] unless shortname.is_a?(Array)
self.layers = []
shortname.each do |short|
self.layers << self.layer_list.select do |layer|
layer[:shortname] == short
end
end
self.layers.flatten!
end
end
|
.fetch_permissions(options = {}) ⇒ Object
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/ridoku/base.rb', line 305
def fetch_permissions(options = {})
fetch_stack
fetch_account
return permissions if permissions && !options[:force]
self.permissions = aws_client.describe_permissions(
iam_user_arn: account[:user][:arn],
stack_id: stack[:stack_id]
)
end
|
.fetch_roles ⇒ Object
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/ridoku/base.rb', line 317
def fetch_roles
configure_iam_client
service = 'aws-opsworks-service-role'
instance = 'aws-opsworks-ec2-role'
iam_client.list_roles[:roles].each do |role|
config[:instance_arn] = role[:arn] if role[:role_name] == instance && !config.key?(:instance_arn)
config[:service_arn] = role[:arn] if role[:role_name] == service && !config.key?(:service_arn)
end
end
|
.fetch_stack(options = {}) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/ridoku/base.rb', line 63
def fetch_stack(options = {})
return stack if stack && !options[:force]
configure_opsworks_client
stack_name = config[:stack]
fail InvalidConfig.new(:stack, :none) unless stack_name ||
!options[:force]
self.stack_list = aws_client.describe_stacks[:stacks]
self.stack = nil
stack_list.each do |stck|
self.stack = stck if stack_name == stck[:name]
end
fail InvalidConfig.new(:stack, :invalid) if !stack &&
!options[:force]
self.custom_json = JSON.parse(stack[:custom_json]) if stack
return stack
end
|
.get_instances_for_layer(layer) ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/ridoku/base.rb', line 216
def get_instances_for_layer(layer)
layer_ids = get_layer_ids(layer)
instances = aws_client
.describe_instances(stack_id: stack[:stack_id])[:instances]
ret = []
layer_ids.each do |id|
instances.each do |inst|
ret << inst if inst[:layer_ids].include?(id)
end
end
ret
end
|
.get_layer_ids(shortname) ⇒ Object
160
161
162
163
164
165
|
# File 'lib/ridoku/base.rb', line 160
def get_layer_ids(shortname)
fetch_stack
layers = aws_client.describe_layers(stack_id: stack[:stack_id])[:layers]
layers.select { |l| l[:shortname] == shortname }
.map { |l| l[:layer_id] }
end
|
.if_debug?(&block) ⇒ Boolean
329
330
331
|
# File 'lib/ridoku/base.rb', line 329
def if_debug?(&block)
yield if config[:debug]
end
|
.initialize_app_environment(conf) ⇒ Object
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
# File 'lib/ridoku/base.rb', line 409
def initialize_app_environment(conf)
fetch_stack
fetch_layer
fetch_instance
app_layer = layer_list.select do |lyr|
lyr[:shortname] == 'rails-app'
end.first
db_layer = layer_list.select do |lyr|
lyr[:shortname] == 'postgresql'
end.first
deploy_info = custom_json['deploy']
app = conf[:shortname]
instance = instances.select do |inst|
inst[:status] == 'online' &&
inst[:layer_ids].index(app_layer[:layer_id]) != nil
end.first
db_instance = instances.select do |inst|
inst[:layer_ids].index(db_layer[:layer_id]) != nil
end.first
dbase_info = {
database: app,
username: SecureRandom.hex(12),
user_password: SecureRandom.hex(12)
}
((custom_json['postgresql'] ||= {})['databases'] ||= []) << dbase_info
deploy_info[app] = {
auto_assets_precompile_on_deploy: true,
assetmaster: instance[:hostname],
app_env: {
'RAILS_ENV' => conf[:attributes]['RailsEnv']
},
database: {
adapter: 'postgresql',
username: dbase_info[:username],
database: dbase_info[:database],
host: db_instance[:public_ip],
password: dbase_info[:user_password],
port: custom_json['postgresql']['config']['port']
}
}
save_stack
run_command({
instance_ids: [db_instance[:instance_id]],
command: {
name: 'execute_recipes',
args: { 'recipes' => 'postgresql::create_databases' }
}
})
end
|
.instance_by_id(id) ⇒ Object
185
186
187
188
|
# File 'lib/ridoku/base.rb', line 185
def instance_by_id(id)
fetch_instance
instance_list.select { |is| is[:instance_id] == id }.first
end
|
343
344
345
346
|
# File 'lib/ridoku/base.rb', line 343
def instance_role_configured?
fetch_roles
config.key?(:instance_arn) && config[:instance_arn] != nil
end
|
.load_config(path) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/ridoku/base.rb', line 31
def load_config(path)
if File.exists?(path)
File.open(path, 'r') do |file|
self.config = JSON.parse(file.read, symbolize_names: true)
end
end
(self.config ||= {}).tap do |default|
default[:wait] = true
end
end
|
.monitor_deployment(dep_ids) ⇒ Object
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
# File 'lib/ridoku/base.rb', line 646
def monitor_deployment(dep_ids)
cmds = aws_client.describe_commands(deployment_id: dep_ids)
commands = cmds[:commands].map do |cmd|
{ command: cmd, instance: instance_by_id(cmd[:instance_id]) }
end
$stdout.puts "Command issued to #{commands.length} instances:"
commands.each do |cmd|
$stdout.puts " #{$stdout.colorize(cmd[:instance][:hostname],
:green)}"
end
20.times do |time|
cmds = aws_client.describe_commands(deployment_id: dep_ids)
success = cmds[:commands].select do |cmd|
cmd[:status] == 'successful'
end
case time % 4
when 0
print "\\\r"
when 1
print "|\r"
when 2
print "/\r"
when 3
print "-\r"
end
if cmds.length == success.length
$stdout.puts 'Command executed successfully.'
return
end
not_ok = cmds[:commands].select do |cmd|
['running', 'pending', 'successful'].index(cmd[:status]) == nil
end.map do |cmd|
{
command: cmd,
instance: instance_by_id(cmd[:instance_id])
}
end
not_ok.each do |item|
$stderr.puts "#{item[:instance][:hostname]}"
$stderr.puts " Status: " +
$stderr.colorize(item[:command][:status], :red)
$stderr.puts " Url: " + item[:command][:log_url]
color_code_logs(RestClient.get(item[:command][:log_url]))
exit 1
end
sleep 5
end
end
|
.postgresql_group_exists?(region = 'us-west-1') ⇒ Boolean
242
243
244
245
246
|
# File 'lib/ridoku/base.rb', line 242
def postgresql_group_exists?(region = 'us-west-1')
configure_ec2_client
ec2_client.security_groups.filter('group-name', POSTGRES_GROUP_NAME).length > 0
end
|
.pretty_instances(io) ⇒ Object
506
507
508
509
510
511
512
513
514
515
516
|
# File 'lib/ridoku/base.rb', line 506
def pretty_instances(io)
inststr = []
instances.each do |inst|
val = "#{inst[:hostname]} [#{inst[:status]}]"
inststr << io.colorize(val,
[:bold, inst[:status] == 'online' ? :green : :red])
end
inststr
end
|
333
334
335
336
|
# File 'lib/ridoku/base.rb', line 333
def roles_configured?
fetch_roles
service_role_configured? && instance_role_configured?
end
|
.rollback(app_id, instance_ids, comment, custom_json = nil) ⇒ Object
609
610
611
612
613
614
|
# File 'lib/ridoku/base.rb', line 609
def rollback(app_id, instance_ids, , custom_json = nil)
dep = deploy(app_id, instance_ids, , custom_json)
dep[:command] = { name: 'rollback' }
dep
end
|
.run_command(deployment) ⇒ Object
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
# File 'lib/ridoku/base.rb', line 518
def run_command(deployment)
fetch_stack
fetch_app
deployment[:stack_id] = stack[:stack_id]
if config[:practice]
$stdout.puts "Would run command: #{deployment[:command][:name]}"
$stdout.puts 'On instances:'
instances.each do |inst|
next unless
deployment[:instance_ids].index(inst[:instance_id]) != nil
$stdout.puts " #{inst[:hostname]}: #{$stdout.colorize(
inst[:status], inst[:status] == 'online' ? :green : :red)}"
end
if deployment.key?(:custom_json)
$stdout.puts 'With custom_json:'
$stdout.puts JSON.pretty_generate(deployment[:custom_json])
end
else
if deployment.key?(:custom_json)
deployment[:custom_json] = JSON.generate(deployment[:custom_json])
end
depid = aws_client.create_deployment(deployment)[:deployment_id]
$stdout.puts $stdout.colorize('Command Sent', :green) if
config[:verbose]
monitor_deployment(depid) if config[:wait]
end
end
|
.save_app(values) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/ridoku/base.rb', line 116
def save_app(values)
values = [values] unless values.is_a?(Array)
unless app
$stderr.puts "Unable to save information because no app is " +
"specified."
return
end
save_info = {
app_id: app[:app_id]
}
save_info.tap do |info|
values.each do |val|
info[val] = app[val]
end
end
aws_client.update_app(save_info)
end
|
.save_config(path, limit = [:app, :stack, :ssh_key, :local_init,
:shell_user, :service_arn, :instance_arn, :backup_bucket]) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ridoku/base.rb', line 43
def save_config(path, limit = [:app, :stack, :ssh_key, :local_init,
:shell_user, :service_arn, :instance_arn, :backup_bucket])
save = {}
if limit.length
limit.each do |lc|
save[lc] = config[lc]
end
else
save = config
end
File.open(path, 'w') do |file|
file.write(save.to_json)
end
end
|
.save_layer(layer, values) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/ridoku/base.rb', line 167
def save_layer(layer, values)
values = [values] unless values.is_a?(Array)
return unless values.length > 0
save_info = {
layer_id: layer[:layer_id]
}
save_info.tap do |info|
values.each do |val|
info[val] = layer[val]
end
end
aws_client.update_layer(save_info)
end
|
.save_stack ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/ridoku/base.rb', line 88
def save_stack
aws_client.update_stack(
stack_id: stack[:stack_id],
custom_json: custom_json.to_json,
service_role_arn: stack[:service_role_arn]
) if stack
end
|
.select_instances(args) ⇒ Object
494
495
496
497
498
499
500
501
502
503
504
|
# File 'lib/ridoku/base.rb', line 494
def select_instances(args)
fetch_instance
return instance_list unless args
args = [args] unless args.is_a?(Array)
return nil if args.length == 0
self.instances = instance_list.select do |inst|
args.index(inst[:hostname]) != nil
end
end
|
338
339
340
341
|
# File 'lib/ridoku/base.rb', line 338
def service_role_configured?
fetch_roles
config.key?(:service_arn) && config[:service_arn] != nil
end
|
.standard_deploy(layer = :all, custom_json = nil) ⇒ Object
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
# File 'lib/ridoku/base.rb', line 616
def standard_deploy(layer = :all, custom_json = nil)
fetch_instance(layer, force: true)
fetch_app
instances.select! { |inst| inst[:status] == 'online' }
instance_ids = instances.map { |inst| inst[:instance_id] }
unless config[:quiet]
$stdout.puts "Application:"
$stdout.puts " #{$stdout.colorize(app[:name], :bold)}"
$stdout.puts "#{instances.length} instance(s):"
pretty_instances($stdout).each do |inst|
$stdout.puts " #{inst}"
end
$stdout.puts "Repository:"
$stdout.puts " #{$stdout.colorize(app[:app_source][:url], :bold)}"\
" @ #{$stdout.colorize(app[:app_source][:revision], :bold)}"
end
run_command(deploy(app[:app_id], instance_ids, config[:comment],
custom_json))
end
|
.update_cookbooks(instance_ids) ⇒ Object
582
583
584
585
586
587
|
# File 'lib/ridoku/base.rb', line 582
def update_cookbooks(instance_ids)
command = Base.base_command(nil, instance_ids,
Base.config[:comment])
command[:command] = { name: 'update_custom_cookbooks' }
command
end
|
.update_pg_security_group(client = self.ec2_client) ⇒ Object
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
|
# File 'lib/ridoku/base.rb', line 255
def update_pg_security_group(client = self.ec2_client)
fetch_stack
port = 5432
if custom_json.key?('postgresql') &&
custom_json['postgresql'].key?('config')
custom_json['postgresql']['config'].key?('port')
port = custom_json['postgresql']['config']['port']
end
perm_match = false
group = client.security_groups.filter('group-name', POSTGRES_GROUP_NAME).first
unless group
$stdout.puts "Creating security group: #{POSTGRES_GROUP_NAME} in #{client.regions.first.name}"
group = client.security_groups.create(POSTGRES_GROUP_NAME)
else
group.ingress_ip_permissions.each do |ipperm|
if ipperm.protocol == :tcp && ipperm.port_range == port..port
perm_match = true
else
ipperm.revoke
end
end
end
group.authorize_ingress(:tcp, port) unless perm_match
end
|
.update_pg_security_groups_in_all_regions ⇒ Object
248
249
250
251
252
253
|
# File 'lib/ridoku/base.rb', line 248
def update_pg_security_groups_in_all_regions
AWS.regions.each do |region|
$stdout.puts "Checking region: #{region.name}"
update_pg_security_group(region.ec2)
end
end
|
.valid_instances?(args) ⇒ Boolean
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
# File 'lib/ridoku/base.rb', line 471
def valid_instances?(args)
args = [args] unless args.is_a?(Array)
return false if args.length == 0
fetch_instance
inst_names = instances.map do |inst|
return false if args.index(inst[:hostname]) != nil &&
inst[:status] == 'stopped'
inst[:hostname]
end
args.each do |arg|
return false if inst_names.index(arg) == nil
end
true
end
|