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

Methods inherited from Kuby::Plugin

#after_configuration, #after_deploy, #after_setup, #before_deploy, #before_setup, dependencies, depends_on, #initialize, #remove, #setup, task_dirs

Constructor Details

This class inherits a constructor from Kuby::Plugin

Instance Method Details

#configure(&block) ⇒ Object



18
19
20
# File 'lib/kuby/plugins/rails_app/assets.rb', line 18

def configure(&block)
  instance_eval(&block)
end

#configure_ingress(ingress, hostname) ⇒ Object



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
# 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
        path_type 'Prefix'

        backend do
          service do
            name spec.service..name

            port do
              name spec.service.spec.ports.first.name
            end
          end
        end
      end

      path do
        path spec.packs_url
        path_type 'Prefix'

        backend do
          service do
            name spec.service..name

            port do
              name spec.service.spec.ports.first.name
            end
          end
        end
      end
    end
  end
end

#copy_taskObject



62
63
64
65
66
# File 'lib/kuby/plugins/rails_app/assets.rb', line 62

def copy_task
  @copy_task ||= AssetCopyTask.new(
    from: asset_path, to: RAILS_MOUNT_PATH
  )
end

#deployment(&block) ⇒ Object



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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/kuby/plugins/rails_app/assets.rb', line 176

def deployment(&block)
  kube_spec = self

  @deployment ||= KubeDSL.deployment do
     do
      name "#{kube_spec.selector_app}-#{kube_spec.role}"
      namespace kube_spec.namespace..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
         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 "#{kube_spec.image.image_url}:#{kube_spec.kubernetes.tag || Kuby::Docker::LATEST_TAG}-assets"

            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

            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

          image_pull_secret do
            name kube_spec.environment.kubernetes.registry_secret..name
          end

          volume do
            name 'nginx-config'

            config_map do
              name kube_spec.nginx_config..name
            end
          end

          restart_policy 'Always'
           kube_spec...name
        end
      end
    end
  end

  @deployment.instance_eval(&block) if block
  @deployment
end

#dockerObject



298
299
300
# File 'lib/kuby/plugins/rails_app/assets.rb', line 298

def docker
  environment.docker
end

#docker_imagesObject



282
283
284
# File 'lib/kuby/plugins/rails_app/assets.rb', line 282

def docker_images
  @docker_images ||= [docker_spec]
end

#imageObject



310
311
312
# File 'lib/kuby/plugins/rails_app/assets.rb', line 310

def image
  @image ||= RailsApp::AssetsImage.new(docker.image, -> { dockerfile }, docker.image.registry_index_url)
end

#kubernetesObject



302
303
304
# File 'lib/kuby/plugins/rails_app/assets.rb', line 302

def kubernetes
  environment.kubernetes
end

#namespaceObject



286
287
288
# File 'lib/kuby/plugins/rails_app/assets.rb', line 286

def namespace
  environment.kubernetes.namespace
end

#nginx_config(&block) ⇒ Object



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
# File 'lib/kuby/plugins/rails_app/assets.rb', line 122

def nginx_config(&block)
  spec = self

  @nginx_config ||= KubeDSL.config_map do
     do
      name "#{spec.selector_app}-#{spec.role}-nginx-config"
      namespace spec.namespace..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

#resourcesObject



273
274
275
276
277
278
279
280
# File 'lib/kuby/plugins/rails_app/assets.rb', line 273

def resources
  @resources ||= [
    service,
    ,
    nginx_config,
    deployment
  ]
end

#roleObject



294
295
296
# File 'lib/kuby/plugins/rails_app/assets.rb', line 294

def role
  ROLE
end

#selector_appObject



290
291
292
# File 'lib/kuby/plugins/rails_app/assets.rb', line 290

def selector_app
  environment.kubernetes.selector_app
end

#service(&block) ⇒ Object



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
# File 'lib/kuby/plugins/rails_app/assets.rb', line 68

def service(&block)
  spec = self

  @service ||= KubeDSL.service do
     do
      name "#{spec.selector_app}-#{spec.role}-svc"
      namespace spec.namespace..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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/kuby/plugins/rails_app/assets.rb', line 103

def (&block)
  spec = self

  @service_account ||= KubeDSL. do
     do
      name "#{spec.selector_app}-#{spec.role}-sa"
      namespace spec.namespace..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