Class: Kuby::Plugins::RailsApp::Assets
- Inherits:
-
Kuby::Plugin
show all
- Extended by:
- KubeDSL::ValueFields
- Defined in:
- lib/kuby/plugins/rails_app/assets.rb
Constant Summary
collapse
- ROLE =
'assets'.freeze
- NGINX_IMAGE =
'nginx:1.9-alpine'.freeze
- NGINX_PORT =
8082
- NGINX_MOUNT_PATH =
'/usr/share/nginx/assets'.freeze
- RAILS_MOUNT_PATH =
'/usr/share/assets'.freeze
Instance Attribute Summary
Attributes inherited from Kuby::Plugin
#environment
Instance Method Summary
collapse
#after_configuration, #after_deploy, #after_setup, #before_deploy, #before_setup, #dockerfiles, #initialize, #setup
Constructor Details
This class inherits a constructor from Kuby::Plugin
Instance Method Details
18
19
20
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 18
def configure(&block)
instance_eval(&block)
end
|
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
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 50
def configure_deployment(deployment, docker_image)
spec = self
deployment.spec.template.spec do
init_container(:copy_assets) do
name "#{spec.selector_app}-copy-assets"
command %w(bundle exec rake kuby:rails_app:assets:copy)
image docker_image
volume_mount do
name 'assets'
mount_path RAILS_MOUNT_PATH
end
end
container(:web) do
volume_mount do
name 'assets'
mount_path NGINX_MOUNT_PATH
end
end
volume do
name 'assets'
persistent_volume_claim do
claim_name spec.volume_claim.metadata.name
end
end
end
end
|
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
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 22
def configure_ingress(ingress, hostname)
spec = self
ingress.spec.rule do
host hostname
http do
path do
path spec.asset_url
backend do
service_name spec.service.metadata.name
service_port spec.service.spec.ports.first.port
end
end
path do
path spec.packs_url
backend do
service_name spec.service.metadata.name
service_port spec.service.spec.ports.first.port
end
end
end
end
end
|
#copy_task ⇒ Object
82
83
84
85
86
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 82
def copy_task
@copy_task ||= AssetCopyTask.new(
from: asset_path, to: RAILS_MOUNT_PATH
)
end
|
#deployment(&block) ⇒ Object
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
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
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 196
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
end
spec do
replicas 1
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(:nginx) do
name "#{kube_spec.selector_app}-#{kube_spec.role}"
image_pull_policy 'IfNotPresent'
image NGINX_IMAGE
port do
container_port NGINX_PORT
name 'http'
protocol 'TCP'
end
volume_mount do
name 'nginx-config'
mount_path '/etc/nginx/nginx.conf'
sub_path 'nginx.conf'
end
volume_mount do
name 'assets'
mount_path NGINX_MOUNT_PATH
end
readiness_probe do
success_threshold 1
failure_threshold 2
initial_delay_seconds 5
period_seconds 3
timeout_seconds 1
http_get do
path '/500.html'
port NGINX_PORT
scheme 'HTTP'
end
end
end
volume do
name 'nginx-config'
config_map do
name kube_spec.nginx_config.metadata.name
end
end
volume do
name 'assets'
persistent_volume_claim do
claim_name kube_spec.volume_claim.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
|
#namespace ⇒ Object
334
335
336
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 334
def namespace
environment.kubernetes.namespace
end
|
#nginx_config(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 142
def nginx_config(&block)
spec = self
@nginx_config ||= KubeDSL.config_map do
metadata do
name "#{spec.selector_app}-#{spec.role}-nginx-config"
namespace spec.namespace.metadata.name
end
data do
add 'nginx.conf', <<~END
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen #{NGINX_PORT};
server_name localhost;
location / {
root #{File.join(NGINX_MOUNT_PATH, 'current')};
}
error_page 500 502 503 504 /500.html;
}
}
END
end
end
@nginx_config.instance_eval(&block) if block
@nginx_config
end
|
#resources ⇒ Object
324
325
326
327
328
329
330
331
332
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 324
def resources
@resources ||= [
service,
service_account,
nginx_config,
deployment,
volume_claim
]
end
|
#role ⇒ Object
342
343
344
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 342
def role
ROLE
end
|
#selector_app ⇒ Object
338
339
340
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 338
def selector_app
environment.kubernetes.selector_app
end
|
#service(&block) ⇒ Object
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
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 88
def service(&block)
spec = self
@service ||= KubeDSL.service do
metadata do
name "#{spec.selector_app}-#{spec.role}-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 NGINX_PORT
protocol 'TCP'
target_port 'http'
end
end
end
@service.instance_eval(&block) if block
@service
end
|
#service_account(&block) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 123
def service_account(&block)
spec = self
@service_account ||= KubeDSL.service_account do
metadata do
name "#{spec.selector_app}-#{spec.role}-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
|
#volume_claim ⇒ Object
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/kuby/plugins/rails_app/assets.rb', line 302
def volume_claim
spec = self
@volume_claim ||= KubeDSL.persistent_volume_claim do
metadata do
name "#{spec.selector_app}-#{spec.role}"
namespace spec.namespace.metadata.name
end
spec do
access_modes ['ReadWriteOnce']
storage_class_name spec.environment.kubernetes.provider.storage_class_name
resources do
requests do
add :storage, '10Gi'
end
end
end
end
end
|