Class: Kuby::Plugins::RailsApp::Plugin
- Inherits:
-
Kuby::Plugin
show all
- Extended by:
- KubeDSL::ValueFields
- Defined in:
- lib/kuby/plugins/rails_app/plugin.rb
Constant Summary
collapse
- WEB_ROLE =
'web'.freeze
- DEFAULT_HOSTNAME =
'localhost'.freeze
- MASTER_KEY_VAR =
'RAILS_MASTER_KEY'.freeze
- ENV_SECRETS =
[MASTER_KEY_VAR].freeze
- ENV_EXCLUDE =
['RAILS_ENV'].freeze
- DEFAULT_ASSET_URL =
'/assets'.freeze
- DEFAULT_PACKS_URL =
'/packs'.freeze
- DEFAULT_ASSET_PATH =
'./public'.freeze
Instance Attribute Summary
Attributes inherited from Kuby::Plugin
#environment
Instance Method Summary
collapse
#after_deploy, #after_setup, #before_setup, #dockerfiles, #setup
Constructor Details
#initialize(environment) ⇒ Plugin
Returns a new instance of Plugin.
Instance Method Details
#after_configuration ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 42
def after_configuration
context = self
if manage_database? && @database = Database.get(self)
@database.plugin.instance_eval(&@database_block) if @database_block
environment.kubernetes.plugins[@database.plugin_name] = @database.plugin
environment.kubernetes.add_plugin(:kube_db)
unless environment.development?
environment.docker do
insert :rewrite_db_config, RewriteDbConfig.new, after: :copy_phase
end
end
end
unless environment.development?
environment.kubernetes.add_plugin(:nginx_ingress)
environment.kubernetes.add_plugin(:rails_assets) do
asset_url context.asset_url
packs_url context.packs_url
asset_path context.asset_path
end
if @tls_enabled
context = self
environment.kubernetes.add_plugin(:cert_manager) do
email context.environment.docker.credentials.email
end
end
end
end
|
#app_secrets(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 205
def app_secrets(&block)
spec = self
@app_secrets ||= KubeDSL.secret do
metadata do
name "#{spec.selector_app}-secrets"
namespace spec.namespace.metadata.name
end
type 'Opaque'
data do
if master_key = ENV[MASTER_KEY_VAR]
add MASTER_KEY_VAR.to_sym, master_key
else
master_key_path = File.join(spec.root, 'config', 'master.key')
if File.exist?(master_key_path)
add MASTER_KEY_VAR.to_sym, File.read(master_key_path).strip
end
end
end
end
@app_secrets.instance_eval(&block) if block
@app_secrets
end
|
#before_deploy(manifest) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 79
def before_deploy(manifest)
if cert_manager = environment.kubernetes.plugin(:cert_manager)
cert_manager.annotate_ingress(ingress)
end
image_with_tag = "#{docker.metadata.image_url}:#{kubernetes.tag}"
if assets = environment.kubernetes.plugin(:rails_assets)
assets.configure_ingress(ingress, hostname)
assets.configure_deployment(deployment, image_with_tag)
end
spec = self
deployment do
spec do
template do
spec do
container(:web) do
image image_with_tag
end
unless spec.environment.development?
init_container(:create_db) do
image image_with_tag
end
init_container(:migrate_db) do
image image_with_tag
end
end
end
end
end
end
end
|
#bootsnap_volume_claim(&block) ⇒ Object
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 532
def bootsnap_volume_claim(&block)
spec = self
if environment.development?
@bootsnap_volume_claim ||= KubeDSL.persistent_volume_claim do
metadata do
name "#{spec.selector_app}-bootsnap"
namespace spec.namespace.metadata.name
end
spec do
access_modes ['ReadWriteMany']
storage_class_name 'hostpath'
resources do
requests do
add :storage, '2Gi'
end
end
end
end
@bootsnap_volume_claim.instance_eval(&block) if block
@bootsnap_volume_claim
end
end
|
#bundle_volume_claim(&block) ⇒ Object
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 505
def bundle_volume_claim(&block)
spec = self
if environment.development?
@bundle_volume_claim ||= KubeDSL.persistent_volume_claim do
metadata do
name "#{spec.selector_app}-bundle"
namespace spec.namespace.metadata.name
end
spec do
access_modes ['ReadWriteMany']
storage_class_name 'hostpath'
resources do
requests do
add :storage, '2Gi'
end
end
end
end
@bundle_volume_claim.instance_eval(&block) if block
@bundle_volume_claim
end
end
|
#code_volume(&block) ⇒ Object
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 447
def code_volume(&block)
spec = self
if environment.development?
@code_volume ||= KubeDSL.persistent_volume do
metadata do
name "#{spec.selector_app}-code"
end
spec do
access_modes ['ReadWriteMany']
capacity do
add :storage, '1Mi'
end
host_path do
path File.expand_path(spec.root)
end
storage_class_name 'hostpath'
end
end
@code_volume.instance_eval(&block) if block
@code_volume
end
end
|
#code_volume_claim(&block) ⇒ Object
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 476
def code_volume_claim(&block)
spec = self
if environment.development?
@code_volume_claim ||= KubeDSL.persistent_volume_claim do
metadata do
name "#{spec.selector_app}-code"
namespace spec.namespace.metadata.name
end
spec do
access_modes ['ReadWriteMany']
resources do
requests do
add :storage, '1Mi'
end
end
storage_class_name 'hostpath'
volume_name spec.code_volume.metadata.name
end
end
@code_volume_claim.instance_eval(&block) if block
@code_volume_claim
end
end
|
#config_map(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 179
def config_map(&block)
spec = self
@config_map ||= KubeDSL.config_map do
metadata do
name "#{spec.selector_app}-config"
namespace spec.namespace.metadata.name
end
data do
ENV.each_pair do |key, val|
include_key = key.start_with?('RAILS_') &&
!ENV_SECRETS.include?(key) &&
!ENV_EXCLUDE.include?(key)
if include_key
add key.to_sym, val
end
end
end
end
@config_map.instance_eval(&block) if block
@config_map
end
|
38
39
40
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 38
def configure(&block)
instance_eval(&block) if block
end
|
#database(&block) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 117
def database(&block)
if block
@database_block = block
elsif environment.configured?
@database
end
end
|
#database_host ⇒ Object
75
76
77
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 75
def database_host
database.plugin.host
end
|
#deployment(&block) ⇒ Object
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 233
def deployment(&block)
kube_spec = self
@deployment ||= KubeDSL.deployment do
metadata do
name "#{kube_spec.selector_app}-#{kube_spec.role}"
namespace kube_spec.namespace.metadata.name
labels do
add :app, kube_spec.selector_app
add :role, kube_spec.role
end
annotations do
add(
'getkuby.io/dockerfile-checksum',
kube_spec.environment.docker.to_dockerfile.checksum
)
end
end
spec do
replicas kube_spec.replicas
selector do
match_labels do
add :app, kube_spec.selector_app
add :role, kube_spec.role
end
end
strategy do
type 'RollingUpdate'
rolling_update do
max_surge '25%'
max_unavailable 0
end
end
template do
metadata do
labels do
add :app, kube_spec.selector_app
add :role, kube_spec.role
end
end
spec do
container(:web) do
name "#{kube_spec.selector_app}-#{kube_spec.role}"
image_pull_policy 'IfNotPresent'
port do
container_port kube_spec.docker.webserver_phase.port
name 'http'
protocol 'TCP'
end
env_from do
config_map_ref do
name kube_spec.config_map.metadata.name
end
end
env_from do
secret_ref do
name kube_spec.app_secrets.metadata.name
end
end
unless kube_spec.environment.development?
readiness_probe do
success_threshold 1
failure_threshold 2
initial_delay_seconds 15
period_seconds 3
timeout_seconds 1
http_get do
path '/healthz'
port kube_spec.docker.webserver_phase.port
scheme 'HTTP'
end
end
end
if kube_spec.environment.development?
env do
name 'BUNDLE_PATH'
value '/bundle'
end
env do
name 'GEM_HOME'
value '/bundle'
end
env do
name 'BOOTSNAP_CACHE_DIR'
value '/usr/src/bootsnap'
end
volume_mount do
name "#{kube_spec.selector_app}-code"
mount_path '/usr/src/app'
end
volume_mount do
name "#{kube_spec.selector_app}-bundle"
mount_path '/bundle'
end
volume_mount do
name "#{kube_spec.selector_app}-bootsnap"
mount_path '/usr/src/bootsnap'
end
end
end
if kube_spec.environment.development?
volume do
name "#{kube_spec.selector_app}-code"
persistent_volume_claim do
claim_name kube_spec.code_volume_claim.metadata.name
end
end
volume do
name "#{kube_spec.selector_app}-bundle"
persistent_volume_claim do
claim_name kube_spec.bundle_volume_claim.metadata.name
end
end
volume do
name "#{kube_spec.selector_app}-bootsnap"
persistent_volume_claim do
claim_name kube_spec.bootsnap_volume_claim.metadata.name
end
end
else
init_container(:create_db) do
name "#{kube_spec.selector_app}-create-db"
command %w(bundle exec rake kuby:rails_app:db:create_unless_exists)
end
init_container(:migrate_db) do
name "#{kube_spec.selector_app}-migrate-db"
command %w(bundle exec rake db:migrate)
end
image_pull_secret do
name kube_spec.environment.kubernetes.registry_secret.metadata.name
end
end
restart_policy 'Always'
service_account_name kube_spec.service_account.metadata.name
end
end
end
end
@deployment.instance_eval(&block) if block
@deployment
end
|
#docker ⇒ Object
583
584
585
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 583
def docker
environment.docker
end
|
#ingress(&block) ⇒ Object
404
405
406
407
408
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 404
def ingress(&block)
spec = self
tls_enabled = @tls_enabled
@ingress ||= KubeDSL::DSL::Extensions::V1beta1::Ingress.new do
metadata do
name "#{spec.selector_app}-ingress"
namespace spec.namespace.metadata.name
annotations do
add :'kubernetes.io/ingress.class', 'nginx'
end
end
spec do
rule do
host spec.hostname
http do
path do
path '/'
backend do
service_name spec.service.metadata.name
service_port spec.service.spec.ports.first.port
end
end
end
end
if tls_enabled
tls do
secret_name "#{spec.selector_app}-tls"
hosts [spec.hostname]
end
end
end
end
@ingress.instance_eval(&block) if block
@ingress
end
|
#kubernetes ⇒ Object
587
588
589
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 587
def kubernetes
environment.kubernetes
end
|
#namespace ⇒ Object
591
592
593
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 591
def namespace
environment.kubernetes.namespace
end
|
#resources ⇒ Object
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 559
def resources
@resources ||= [
service,
service_account,
config_map,
app_secrets,
deployment,
ingress,
code_volume,
code_volume_claim,
bundle_volume_claim,
bootsnap_volume_claim,
*database&.plugin&.resources
]
end
|
#role ⇒ Object
579
580
581
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 579
def role
WEB_ROLE
end
|
#selector_app ⇒ Object
575
576
577
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 575
def selector_app
environment.kubernetes.selector_app
end
|
#service(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 125
def service(&block)
spec = self
@service ||= KubeDSL.service do
metadata do
name "#{spec.selector_app}-svc"
namespace spec.namespace.metadata.name
labels do
add :app, spec.selector_app
add :role, spec.role
end
end
spec do
type 'NodePort'
selector do
add :app, spec.selector_app
add :role, spec.role
end
port do
name 'http'
port spec.docker.webserver_phase.port
protocol 'TCP'
target_port 'http'
end
end
end
@service.instance_eval(&block) if block
@service
end
|
#service_account(&block) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 160
def service_account(&block)
spec = self
@service_account ||= KubeDSL.service_account do
metadata do
name "#{spec.selector_app}-sa"
namespace spec.namespace.metadata.name
labels do
add :app, spec.selector_app
add :role, spec.role
end
end
end
@service_account.instance_eval(&block) if block
@service_account
end
|