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, #docker_images, #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
|
# 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)
environment.docker do
insert :rewrite_db_config, RewriteDbConfig.new, after: :copy_phase
end
end
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
|
#app_secrets(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 200
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
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 75
def before_deploy(manifest)
if cert_manager = environment.kubernetes.plugin(:cert_manager)
cert_manager.annotate_ingress(ingress) if tls_enabled
end
image_with_tag = "#{docker.image.image_url}:#{kubernetes.tag || Kuby::Docker::LATEST_TAG}"
if assets = environment.kubernetes.plugin(:rails_assets)
assets.configure_ingress(ingress, hostname)
end
spec = self
deployment do
spec do
template do
spec do
container(:web) do
image image_with_tag
end
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
|
#config_map(&block) ⇒ Object
Also known as:
env
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 172
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
110
111
112
113
114
115
116
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 110
def database(&block)
if block
@database_block = block
elsif environment.configured?
@database
end
end
|
#database_host ⇒ Object
71
72
73
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 71
def database_host
database.plugin.host
end
|
#deployment(&block) ⇒ Object
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
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 228
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.image.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
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
init_container(:create_db) do
name "#{kube_spec.selector_app}-create-db"
command %w(bundle exec rake kuby:rails_app:db:create_unless_exists)
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
end
init_container(:migrate_db) do
name "#{kube_spec.selector_app}-migrate-db"
command %w(bundle exec rake db:migrate)
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
end
image_pull_secret do
name kube_spec.environment.kubernetes.registry_secret.metadata.name
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
426
427
428
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 426
def docker
environment.docker
end
|
#ingress(&block) ⇒ Object
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
403
404
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 363
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
430
431
432
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 430
def kubernetes
environment.kubernetes
end
|
#namespace ⇒ Object
434
435
436
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 434
def namespace
environment.kubernetes.namespace
end
|
#resources ⇒ Object
406
407
408
409
410
411
412
413
414
415
416
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 406
def resources
@resources ||= [
service,
service_account,
config_map,
app_secrets,
deployment,
ingress,
*database&.plugin&.resources
]
end
|
#role ⇒ Object
422
423
424
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 422
def role
WEB_ROLE
end
|
#selector_app ⇒ Object
418
419
420
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 418
def selector_app
environment.kubernetes.selector_app
end
|
#service(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 118
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/kuby/plugins/rails_app/plugin.rb', line 153
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
|