Module: Rails::Gke::Initialize

Defined in:
lib/rails/gke/initialize.rb,
lib/rails/gke/initialize/initialize.rb

Class Method Summary collapse

Class Method Details

.configObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rails/gke/initialize/initialize.rb', line 4

def config
  FileUtils.mkdir_p "config/initializers" unless File.directory? "config"
  path = "config/initializers/rails-gke.rb"
  File.open(path, "w") do |f|
    f.write <<~EOS
      Rails::Gke.configure do |config|
        config.project_id = "PROJECT_ID"
        config.app = "APP-name"
        config.network = "default"
        config.machine_type = "custom-1-6656"
        config.zone = "asia-northeast1"
        config.domain = "DOMAIN"
        config.channel = "stable"
        config.google_application_credentials = "GOOGLE_APPLICATION_CREDENTIALS"
      end
    EOS
  end
end

.create_rails_taskObject



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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/rails/gke/initialize/initialize.rb', line 225

def create_rails_task
  FileUtils.mkdir_p "lib/tasks" unless File.directory? "lib/tasks"
  path = "lib/tasks/gke.rake"
  File.open(path, "w") do |f|
    f.write <<~EOS
      namespace :gke do
        task create_cluster: :environment do
          Rails::Gke.create_cluster
        end

        task add_backend_service: :environment do
          neg_name = Rails::Gke.set_network_group_list_env
          Rails::Gke.add_backend_service neg_name: neg_name
        end


        task create_td_default: :environment do
          Rails::Gke.create_health_check
          Rails::Gke.create_firewall_rule
          Rails::Gke.create_backend_service
          neg_name = Rails::Gke.set_network_group_list_env
          Rails::Gke.add_backend_service neg_name: neg_name
          Rails::Gke.create_url_map
          Rails::Gke.create_path_matcher
          Rails::Gke.create_target_grpc_proxy
          Rails::Gke.create_forwarding_rule
        end

        task create_td: :environment do
          app = "blog-service"
          health_check_name = app.to_s "-hc"
          Rails::Gke.create_health_check health_check_name: health_check_name
          firewall_rule_name = app.to_s "-allow-health-checks"
          Rails::Gke.create_firewall_rule firewall_rule_name: firewall_rule_name
          service_name = app.to_s ""
          zone = Rails::Gke.configuration.zone

          Rails::Gke.create_backend_service service_name: service_name, health_check_name: health_check_name
          neg_name = "k8s1-87bf55a7-default-blog-service-8080-c7e834de"
          Rails::Gke.add_backend_service service_name: service_name, neg_name: neg_name, zone: zone

          url_map_name = app.to_s "-url-map"
          Rails::Gke.create_url_map url_map_name: url_map_name, service_name: service_name
          path_matcher_name = app.to_s "-path-mathcher"
          hostname = app.to_s ""
          port = "5000"
          Rails::Gke.create_path_matcher url_map_name: url_map_name, service_name: service_name, path_matcher_name: path_matcher_name, hostname: hostname, port: port
          proxy_name = app.to_s "-proxy"
          Rails::Gke.create_target_grpc_proxy proxy_name: proxy_name, url_map_name: url_map_name
          forwarding_rule_name = app.to_s "-forwarding-rule"
          Rails::Gke.create_forwarding_rule forwarding_rule_name: forwarding_rule_name, proxy_name: proxy_name, port: port
        end

        task delete_td: :environment do
          app = "blog-service"
          health_check_name = app.to_s "-hc"
          firewall_rule_name = app.to_s "-allow-health-checks"
          service_name = app.to_s ""
          url_map_name = app.to_s "-url-map"
          proxy_name = app.to_s "-proxy"
          forwarding_rule_name = app.to_s "-forwarding-rule"

          Rails::Gke.delete_forwarding_rule forwarding_rule_name: forwarding_rule_name
          Rails::Gke.delete_target_grpc_proxy proxy_name: proxy_name
          Rails::Gke.delete_url_map url_map_name: url_map_name
          Rails::Gke.delete_backend_service service_name: service_name
          Rails::Gke.delete_health_check health_check_name: health_check_name
          Rails::Gke.delete_firewall_rule firewall_rule_name: firewall_rule_name
        end

        task get_neg_name: :environment do
          Rails::Gke.get_network_group_list
        end

        task set_neg_name: :environment do
          Rails::Gke.set_network_group_list_env
        end
      
        task delete_td_default: :environment do
          Rails::Gke.delete_forwarding_rule
          Rails::Gke.delete_target_grpc_proxy
          Rails::Gke.delete_url_map
          Rails::Gke.delete_backend_service
          Rails::Gke.delete_health_check
          Rails::Gke.delete_firewall_rule
          Rails::Gke.delete_cluster
          neg_name = Rails::Gke.set_network_group_list_env
          Rails::Gke.delete_network_group_list neg_name: neg_name
        end
      
        task :resize_cluster, [:pool_name, :node_num] => :environment do |_, args|
          pool_name = "default-pool" || args[:pool_name]
          node_num = 1 || args[:node_num]
          Rails::Gke.resize_cluster pool_name: pool_name, node_num: node_num
        end
      
        task create_namespace: :environment do
          Rails::Gke.create_namespace
        end
      
        task create_ip: :environment do
          Rails::Gke.create_ip
        end
      
        task apply_deployment: :environment do
          Rails::Gke.apply_deployment
        end
      
        task apply_service: :environment do
          Rails::Gke.apply_service
        end
      
        task apply_secret: :environment do
          Rails::Gke.apply_secret
        end
      
        task apply_ingress: :environment do
          Rails::Gke.apply_ingress
        end
      
        task delete_deployment: :environment do
          Rails::Gke.delete_deployment
        end
      
        task delete_service: :environment do
          Rails::Gke.delete_service
        end
      
        task delete_secret: :environment do
          Rails::Gke.delete_secret
        end
      
        task delete_ingress: :environment do
          Rails::Gke.delete_ingress
        end
      
        task test: :environment do
          Rails::Gke.run_test
        end
      
        task :update, [:version] => :environment do |_, args|
          Rails::Gke.update_container version: args[:version]
        end
      
        task apply_all: :environment do
          Rails::Gke.apply_deployment
          Rails::Gke.apply_service
          Rails::Gke.apply_secret
          Rails::Gke.apply_ingress
        end
      
        task delete_all: :environment do
          Rails::Gke.delete_deployment
          Rails::Gke.delete_service
          Rails::Gke.delete_secret
          Rails::Gke.delete_ingress
        end
      
        task get_pods: :environment do
          Rails::Gke.get_pods
        end
      
        task get_svc: :environment do
          Rails::Gke.get_svc
        end
      
        task get_ingress: :environment do
          Rails::Gke.get_ingress
        end
      
        task get_clusters: :environment do
          Rails::Gke.get_clusters
        end
      
        task get_current_cluster: :environment do
          Rails::Gke.get_current_cluster
        end
      
        task :use_context, [:cluster] => :environment do |_, args|
          Rails::Gke.use_context cluster: args[:cluster]
        end
      
        task get_credentials: :environment do
          Rails::Gke.get_credentials
        end
      end          
    EOS
  end
  true
rescue StandardError => error
  puts error
  false
end

.create_souls_taskObject



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
61
62
63
64
65
66
67
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
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
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
# File 'lib/rails/gke/initialize/initialize.rb', line 31

def create_souls_task
  FileUtils.mkdir_p "app/tasks" unless File.directory? "app/tasks"
  path = "app/tasks/gke.rake"
  File.open(path, "w") do |f|
    f.write <<~EOS
      namespace :gke do
        task create_cluster: :environment do
          Rails::Gke.create_cluster
        end

        task add_backend_service: :environment do
          neg_name = Rails::Gke.set_network_group_list_env
          Rails::Gke.add_backend_service neg_name: neg_name
        end


        task create_td_default: :environment do
          Rails::Gke.create_health_check
          Rails::Gke.create_firewall_rule
          Rails::Gke.create_backend_service
          neg_name = Rails::Gke.set_network_group_list_env
          Rails::Gke.add_backend_service neg_name: neg_name
          Rails::Gke.create_url_map
          Rails::Gke.create_path_matcher
          Rails::Gke.create_target_grpc_proxy
          Rails::Gke.create_forwarding_rule
        end

        task create_td: :environment do
          app = "blog-service"
          health_check_name = app.to_s + "-hc"
          Rails::Gke.create_health_check health_check_name: health_check_name
          firewall_rule_name = app.to_s + "-allow-health-checks"
          Rails::Gke.create_firewall_rule firewall_rule_name: firewall_rule_name
          service_name = app.to_s + ""
          zone = Rails::Gke.configuration.zone

          Rails::Gke.create_backend_service service_name: service_name, health_check_name: health_check_name
          neg_name = "k8s1-87bf55a7-default-blog-service-8080-c7e834de"
          Rails::Gke.add_backend_service service_name: service_name, neg_name: neg_name, zone: zone

          url_map_name = app.to_s + "-url-map"
          Rails::Gke.create_url_map url_map_name: url_map_name, service_name: service_name
          path_matcher_name = app.to_s + "-path-mathcher"
          hostname = app.to_s + ""
          port = "5000"
          Rails::Gke.create_path_matcher url_map_name: url_map_name, service_name: service_name, path_matcher_name: path_matcher_name, hostname: hostname, port: port
          proxy_name = app.to_s + "-proxy"
          Rails::Gke.create_target_grpc_proxy proxy_name: proxy_name, url_map_name: url_map_name
          forwarding_rule_name = app.to_s + "-forwarding-rule"
          Rails::Gke.create_forwarding_rule forwarding_rule_name: forwarding_rule_name, proxy_name: proxy_name, port: port
        end

        task delete_td: :environment do
          app = "blog-service"
          health_check_name = app.to_s + "-hc"
          firewall_rule_name = app.to_s + "-allow-health-checks"
          service_name = app.to_s + ""
          url_map_name = app.to_s + "-url-map"
          proxy_name = app.to_s + "-proxy"
          forwarding_rule_name = app.to_s + "-forwarding-rule"

          Rails::Gke.delete_forwarding_rule forwarding_rule_name: forwarding_rule_name
          Rails::Gke.delete_target_grpc_proxy proxy_name: proxy_name
          Rails::Gke.delete_url_map url_map_name: url_map_name
          Rails::Gke.delete_backend_service service_name: service_name
          Rails::Gke.delete_health_check health_check_name: health_check_name
          Rails::Gke.delete_firewall_rule firewall_rule_name: firewall_rule_name
        end

        task get_neg_name: :environment do
          Rails::Gke.get_network_group_list
        end

        task set_neg_name: :environment do
          Rails::Gke.set_network_group_list_env
        end
      
        task delete_td_default: :environment do
          Rails::Gke.delete_forwarding_rule
          Rails::Gke.delete_target_grpc_proxy
          Rails::Gke.delete_url_map
          Rails::Gke.delete_backend_service
          Rails::Gke.delete_health_check
          Rails::Gke.delete_firewall_rule
          Rails::Gke.delete_cluster
          neg_name = Rails::Gke.set_network_group_list_env
          Rails::Gke.delete_network_group_list neg_name: neg_name
        end
      
        task :resize_cluster, [:pool_name, :node_num] => :environment do |_, args|
          pool_name = "default-pool" || args[:pool_name]
          node_num = 1 || args[:node_num]
          Rails::Gke.resize_cluster pool_name: pool_name, node_num: node_num
        end
      
        task create_namespace: :environment do
          Rails::Gke.create_namespace
        end
      
        task create_ip: :environment do
          Rails::Gke.create_ip
        end
      
        task apply_deployment: :environment do
          Rails::Gke.apply_deployment
        end
      
        task apply_service: :environment do
          Rails::Gke.apply_service
        end
      
        task apply_secret: :environment do
          Rails::Gke.apply_secret
        end
      
        task apply_ingress: :environment do
          Rails::Gke.apply_ingress
        end
      
        task delete_deployment: :environment do
          Rails::Gke.delete_deployment
        end
      
        task delete_service: :environment do
          Rails::Gke.delete_service
        end
      
        task delete_secret: :environment do
          Rails::Gke.delete_secret
        end
      
        task delete_ingress: :environment do
          Rails::Gke.delete_ingress
        end
      
        task test: :environment do
          Rails::Gke.run_test
        end
      
        task :update, [:version] => :environment do |_, args|
          Rails::Gke.update_container version: args[:version]
        end
      
        task apply_all: :environment do
          Rails::Gke.apply_deployment
          Rails::Gke.apply_service
          Rails::Gke.apply_secret
          Rails::Gke.apply_ingress
        end
      
        task delete_all: :environment do
          Rails::Gke.delete_deployment
          Rails::Gke.delete_service
          Rails::Gke.delete_secret
          Rails::Gke.delete_ingress
        end
      
        task get_pods: :environment do
          Rails::Gke.get_pods
        end
      
        task get_svc: :environment do
          Rails::Gke.get_svc
        end
      
        task get_ingress: :environment do
          Rails::Gke.get_ingress
        end
      
        task get_clusters: :environment do
          Rails::Gke.get_clusters
        end
      
        task get_current_cluster: :environment do
          Rails::Gke.get_current_cluster
        end
      
        task :use_context, [:cluster] => :environment do |_, args|
          Rails::Gke.use_context cluster: args[:cluster]
        end
      
        task get_credentials: :environment do
          Rails::Gke.get_credentials
        end
      end
    EOS
  end
  true
rescue StandardError => error
  puts error
  false
end

.create_ymlObject



23
24
25
26
27
28
29
# File 'lib/rails/gke/initialize/initialize.rb', line 23

def create_yml
  return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
  puts "created deployment.yml" if self.deployment
  puts "created service.yml" if self.service
  puts "created secret.yml" if self.secret
  puts "created ingress.yml" if self.ingress
end

.deploymentObject



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
470
471
472
473
474
475
# File 'lib/rails/gke/initialize/initialize.rb', line 419

def deployment
  return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
  return "Error: Already Exsit deployment.yml" if File.file? "deployment.yml"
  File.open("deployment.yml", "w") do |f|
    yml = <<~EOS
      apiVersion: extensions/v1beta1
      kind: Deployment
      metadata:
        name: #{Rails::Gke.configuration.app}-deployment
      spec:
        replicas: 3
        template:
          metadata:
            labels:
              app: #{Rails::Gke.configuration.app}
          spec:
            containers:
              - name: #{Rails::Gke.configuration.app}
                image: asia.gcr.io/#{Rails::Gke.configuration.project_id}/#{Rails::Gke.configuration.app}:0.0.1
                ports:
                  - containerPort: 3000
                    protocol: TCP
                livenessProbe:
                  httpGet:
                    path: /
                    port: 3000
                  initialDelaySeconds: 30
                  timeoutSeconds: 1
                readinessProbe:
                  httpGet:
                    path: /
                    port: 3000
                  initialDelaySeconds: 30
                  timeoutSeconds: 1
                env:
                  - name: DB_HOST
                    valueFrom:
                      secretKeyRef:
                        name: #{Rails::Gke.configuration.app}-secret
                        key: db_host
                  - name: DB_USER
                    valueFrom:
                      secretKeyRef:
                        name: #{Rails::Gke.configuration.app}-secret
                        key: db_user
                  - name: DB_PW
                    valueFrom:
                      secretKeyRef:
                        name: #{Rails::Gke.configuration.app}-secret
                        key: db_pw
    EOS
    f.write(yml)
  end
  true
rescue
  false
end

.grpc_service_deploymentObject



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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/rails/gke/initialize/initialize.rb', line 532

def grpc_service_deployment
  return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
  return "Error: Already Exsit deployment.yml" if File.file? "deployment.yml"
  path = "secret.yml"
  File.open(path, "w") do |f|
    f.write <<~EOS
      apiVersion: v1
      kind: Service
      metadata:
        name: #{Rails::Gke.configuration.service1}
        annotations:
          cloud.google.com/neg: '{"exposed_ports":{"8080":{}}}'
      spec:
        ports:
        - port: 8080
          name: #{Rails::Gke.configuration.service1}
          protocol: TCP
          targetPort: 50051
        selector:
          run: #{Rails::Gke.configuration.service1}
        type: ClusterIP

      ---
      apiVersion: extensions/v1beta1
      kind: Deployment
      metadata:
        labels:
          run: #{Rails::Gke.configuration.service1}
        name: #{Rails::Gke.configuration.service1}
      spec:
        replicas: 2
        template:
          metadata:
            labels:
              run: #{Rails::Gke.configuration.service1}
          spec:
            containers:
            - image: asia.gcr.io/#{Rails::Gke.configuration.project_id}/#{Rails::Gke.configuration.service1}:0.0.1
              name: #{Rails::Gke.configuration.service1}
              ports:
              - protocol: TCP
                containerPort: 50051
    EOS
  end
  true
rescue
  false
end

.ingressObject



503
504
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/rails/gke/initialize/initialize.rb', line 503

def ingress
  return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
  return "Error: Already Exsit ingress.yml" if File.file? "ingress.yml"
  File.open("ingress.yml", "w") do |f|
    yml = <<~EOS
      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: https-#{Rails::Gke.configuration.app}
        annotations:
          kubernetes.io/ingress.global-static-ip-name: https-#{Rails::Gke.configuration.app}
          networking.gke.io/managed-certificates: #{Rails::Gke.configuration.app}-secret
      spec:
        rules:
        - host: #{Rails::Gke.configuration.domain}
          http:
            paths:
            - backend:
                serviceName: #{Rails::Gke.configuration.app}-service
                servicePort: 80

    EOS
    f.write(yml)
  end
  true
rescue
  false
end

.secretObject



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/rails/gke/initialize/initialize.rb', line 581

def secret
  return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
  return "Error: Already Exsit secret.yml" if File.file? "secret.yml"
  File.open("secret.yml", "w") do |f|
    yml = <<~EOS
      apiVersion: v1
      kind: Secret
      metadata:
        name: #{Rails::Gke.configuration.app}-secret
      type: Opaque
      data:
        db_user: dXNlcg==
        db_pw: cGFzc3dvcmQ=
        db_host: bG9jYWxob3N0
    EOS
    f.write(yml)
  end
  true
rescue
  false
end

.serviceObject



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
# File 'lib/rails/gke/initialize/initialize.rb', line 477

def service
  return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
  return "Error: Already Exsit service.yml" if File.file? "service.yml"
  File.open("service.yml", "w") do |f|
    yml = <<~EOS
      kind: Service
      apiVersion: v1
      metadata:
        name: #{Rails::Gke.configuration.app}-service
      spec:
        selector:
          app: #{Rails::Gke.configuration.app}
        type: LoadBalancer
        ports:
          - name: http
            protocol: TCP
            port: 80
            targetPort: 3000
    EOS
    f.write(yml)
  end
  true
rescue
  false
end