rails-gke 0.6.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ require_relative "./initialize/initialize.rb"
2
+
3
+ module Rails
4
+ module Gke
5
+ module Initialize
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,604 @@
1
+ module Rails
2
+ module Gke::Initialize
3
+ class << self
4
+ def config
5
+ FileUtils.mkdir_p "config/initializers" unless File.directory? "config"
6
+ path = "config/initializers/rails-gke.rb"
7
+ File.open(path, "w") do |f|
8
+ f.write <<~EOS
9
+ Rails::Gke.configure do |config|
10
+ config.project_id = "PROJECT_ID"
11
+ config.app = "APP-name"
12
+ config.network = "default"
13
+ config.machine_type = "custom-1-6656"
14
+ config.zone = "asia-northeast1"
15
+ config.domain = "DOMAIN"
16
+ config.channel = "stable"
17
+ config.google_application_credentials = "GOOGLE_APPLICATION_CREDENTIALS"
18
+ end
19
+ EOS
20
+ end
21
+ end
22
+
23
+ def create_yml
24
+ return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
25
+ puts "created deployment.yml" if self.deployment
26
+ puts "created service.yml" if self.service
27
+ puts "created secret.yml" if self.secret
28
+ puts "created ingress.yml" if self.ingress
29
+ end
30
+
31
+ def create_souls_task
32
+ FileUtils.mkdir_p "app/tasks" unless File.directory? "app/tasks"
33
+ path = "app/tasks/gke.rake"
34
+ File.open(path, "w") do |f|
35
+ f.write <<~EOS
36
+ namespace :gke do
37
+ task create_cluster: :environment do
38
+ Rails::Gke.create_cluster
39
+ end
40
+
41
+ task add_backend_service: :environment do
42
+ neg_name = Rails::Gke.set_network_group_list_env
43
+ Rails::Gke.add_backend_service neg_name: neg_name
44
+ end
45
+
46
+
47
+ task create_td_default: :environment do
48
+ Rails::Gke.create_health_check
49
+ Rails::Gke.create_firewall_rule
50
+ Rails::Gke.create_backend_service
51
+ neg_name = Rails::Gke.set_network_group_list_env
52
+ Rails::Gke.add_backend_service neg_name: neg_name
53
+ Rails::Gke.create_url_map
54
+ Rails::Gke.create_path_matcher
55
+ Rails::Gke.create_target_grpc_proxy
56
+ Rails::Gke.create_forwarding_rule
57
+ end
58
+
59
+ task create_td: :environment do
60
+ app = "blog-service"
61
+ health_check_name = app.to_s + "-hc"
62
+ Rails::Gke.create_health_check health_check_name: health_check_name
63
+ firewall_rule_name = app.to_s + "-allow-health-checks"
64
+ Rails::Gke.create_firewall_rule firewall_rule_name: firewall_rule_name
65
+ service_name = app.to_s + ""
66
+ zone = Rails::Gke.configuration.zone
67
+
68
+ Rails::Gke.create_backend_service service_name: service_name, health_check_name: health_check_name
69
+ neg_name = "k8s1-87bf55a7-default-blog-service-8080-c7e834de"
70
+ Rails::Gke.add_backend_service service_name: service_name, neg_name: neg_name, zone: zone
71
+
72
+ url_map_name = app.to_s + "-url-map"
73
+ Rails::Gke.create_url_map url_map_name: url_map_name, service_name: service_name
74
+ path_matcher_name = app.to_s + "-path-mathcher"
75
+ hostname = app.to_s + ""
76
+ port = "5000"
77
+ 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
78
+ proxy_name = app.to_s + "-proxy"
79
+ Rails::Gke.create_target_grpc_proxy proxy_name: proxy_name, url_map_name: url_map_name
80
+ forwarding_rule_name = app.to_s + "-forwarding-rule"
81
+ Rails::Gke.create_forwarding_rule forwarding_rule_name: forwarding_rule_name, proxy_name: proxy_name, port: port
82
+ end
83
+
84
+ task delete_td: :environment do
85
+ app = "blog-service"
86
+ health_check_name = app.to_s + "-hc"
87
+ firewall_rule_name = app.to_s + "-allow-health-checks"
88
+ service_name = app.to_s + ""
89
+ url_map_name = app.to_s + "-url-map"
90
+ proxy_name = app.to_s + "-proxy"
91
+ forwarding_rule_name = app.to_s + "-forwarding-rule"
92
+
93
+ Rails::Gke.delete_forwarding_rule forwarding_rule_name: forwarding_rule_name
94
+ Rails::Gke.delete_target_grpc_proxy proxy_name: proxy_name
95
+ Rails::Gke.delete_url_map url_map_name: url_map_name
96
+ Rails::Gke.delete_backend_service service_name: service_name
97
+ Rails::Gke.delete_health_check health_check_name: health_check_name
98
+ Rails::Gke.delete_firewall_rule firewall_rule_name: firewall_rule_name
99
+ end
100
+
101
+ task get_neg_name: :environment do
102
+ Rails::Gke.get_network_group_list
103
+ end
104
+
105
+ task set_neg_name: :environment do
106
+ Rails::Gke.set_network_group_list_env
107
+ end
108
+
109
+ task delete_td_default: :environment do
110
+ Rails::Gke.delete_forwarding_rule
111
+ Rails::Gke.delete_target_grpc_proxy
112
+ Rails::Gke.delete_url_map
113
+ Rails::Gke.delete_backend_service
114
+ Rails::Gke.delete_health_check
115
+ Rails::Gke.delete_firewall_rule
116
+ Rails::Gke.delete_cluster
117
+ neg_name = Rails::Gke.set_network_group_list_env
118
+ Rails::Gke.delete_network_group_list neg_name: neg_name
119
+ end
120
+
121
+ task :resize_cluster, [:pool_name, :node_num] => :environment do |_, args|
122
+ pool_name = "default-pool" || args[:pool_name]
123
+ node_num = 1 || args[:node_num]
124
+ Rails::Gke.resize_cluster pool_name: pool_name, node_num: node_num
125
+ end
126
+
127
+ task create_namespace: :environment do
128
+ Rails::Gke.create_namespace
129
+ end
130
+
131
+ task create_ip: :environment do
132
+ Rails::Gke.create_ip
133
+ end
134
+
135
+ task apply_deployment: :environment do
136
+ Rails::Gke.apply_deployment
137
+ end
138
+
139
+ task apply_service: :environment do
140
+ Rails::Gke.apply_service
141
+ end
142
+
143
+ task apply_secret: :environment do
144
+ Rails::Gke.apply_secret
145
+ end
146
+
147
+ task apply_ingress: :environment do
148
+ Rails::Gke.apply_ingress
149
+ end
150
+
151
+ task delete_deployment: :environment do
152
+ Rails::Gke.delete_deployment
153
+ end
154
+
155
+ task delete_service: :environment do
156
+ Rails::Gke.delete_service
157
+ end
158
+
159
+ task delete_secret: :environment do
160
+ Rails::Gke.delete_secret
161
+ end
162
+
163
+ task delete_ingress: :environment do
164
+ Rails::Gke.delete_ingress
165
+ end
166
+
167
+ task test: :environment do
168
+ Rails::Gke.run_test
169
+ end
170
+
171
+ task :update, [:version] => :environment do |_, args|
172
+ Rails::Gke.update_container version: args[:version]
173
+ end
174
+
175
+ task apply_all: :environment do
176
+ Rails::Gke.apply_deployment
177
+ Rails::Gke.apply_service
178
+ Rails::Gke.apply_secret
179
+ Rails::Gke.apply_ingress
180
+ end
181
+
182
+ task delete_all: :environment do
183
+ Rails::Gke.delete_deployment
184
+ Rails::Gke.delete_service
185
+ Rails::Gke.delete_secret
186
+ Rails::Gke.delete_ingress
187
+ end
188
+
189
+ task get_pods: :environment do
190
+ Rails::Gke.get_pods
191
+ end
192
+
193
+ task get_svc: :environment do
194
+ Rails::Gke.get_svc
195
+ end
196
+
197
+ task get_ingress: :environment do
198
+ Rails::Gke.get_ingress
199
+ end
200
+
201
+ task get_clusters: :environment do
202
+ Rails::Gke.get_clusters
203
+ end
204
+
205
+ task get_current_cluster: :environment do
206
+ Rails::Gke.get_current_cluster
207
+ end
208
+
209
+ task :use_context, [:cluster] => :environment do |_, args|
210
+ Rails::Gke.use_context cluster: args[:cluster]
211
+ end
212
+
213
+ task get_credentials: :environment do
214
+ Rails::Gke.get_credentials
215
+ end
216
+ end
217
+ EOS
218
+ end
219
+ true
220
+ rescue StandardError => error
221
+ puts error
222
+ false
223
+ end
224
+
225
+ def create_rails_task
226
+ FileUtils.mkdir_p "lib/tasks" unless File.directory? "lib/tasks"
227
+ path = "lib/tasks/gke.rake"
228
+ File.open(path, "w") do |f|
229
+ f.write <<~EOS
230
+ namespace :gke do
231
+ task create_cluster: :environment do
232
+ Rails::Gke.create_cluster
233
+ end
234
+
235
+ task add_backend_service: :environment do
236
+ neg_name = Rails::Gke.set_network_group_list_env
237
+ Rails::Gke.add_backend_service neg_name: neg_name
238
+ end
239
+
240
+
241
+ task create_td_default: :environment do
242
+ Rails::Gke.create_health_check
243
+ Rails::Gke.create_firewall_rule
244
+ Rails::Gke.create_backend_service
245
+ neg_name = Rails::Gke.set_network_group_list_env
246
+ Rails::Gke.add_backend_service neg_name: neg_name
247
+ Rails::Gke.create_url_map
248
+ Rails::Gke.create_path_matcher
249
+ Rails::Gke.create_target_grpc_proxy
250
+ Rails::Gke.create_forwarding_rule
251
+ end
252
+
253
+ task create_td: :environment do
254
+ app = "blog-service"
255
+ health_check_name = app.to_s "-hc"
256
+ Rails::Gke.create_health_check health_check_name: health_check_name
257
+ firewall_rule_name = app.to_s "-allow-health-checks"
258
+ Rails::Gke.create_firewall_rule firewall_rule_name: firewall_rule_name
259
+ service_name = app.to_s ""
260
+ zone = Rails::Gke.configuration.zone
261
+
262
+ Rails::Gke.create_backend_service service_name: service_name, health_check_name: health_check_name
263
+ neg_name = "k8s1-87bf55a7-default-blog-service-8080-c7e834de"
264
+ Rails::Gke.add_backend_service service_name: service_name, neg_name: neg_name, zone: zone
265
+
266
+ url_map_name = app.to_s "-url-map"
267
+ Rails::Gke.create_url_map url_map_name: url_map_name, service_name: service_name
268
+ path_matcher_name = app.to_s "-path-mathcher"
269
+ hostname = app.to_s ""
270
+ port = "5000"
271
+ 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
272
+ proxy_name = app.to_s "-proxy"
273
+ Rails::Gke.create_target_grpc_proxy proxy_name: proxy_name, url_map_name: url_map_name
274
+ forwarding_rule_name = app.to_s "-forwarding-rule"
275
+ Rails::Gke.create_forwarding_rule forwarding_rule_name: forwarding_rule_name, proxy_name: proxy_name, port: port
276
+ end
277
+
278
+ task delete_td: :environment do
279
+ app = "blog-service"
280
+ health_check_name = app.to_s "-hc"
281
+ firewall_rule_name = app.to_s "-allow-health-checks"
282
+ service_name = app.to_s ""
283
+ url_map_name = app.to_s "-url-map"
284
+ proxy_name = app.to_s "-proxy"
285
+ forwarding_rule_name = app.to_s "-forwarding-rule"
286
+
287
+ Rails::Gke.delete_forwarding_rule forwarding_rule_name: forwarding_rule_name
288
+ Rails::Gke.delete_target_grpc_proxy proxy_name: proxy_name
289
+ Rails::Gke.delete_url_map url_map_name: url_map_name
290
+ Rails::Gke.delete_backend_service service_name: service_name
291
+ Rails::Gke.delete_health_check health_check_name: health_check_name
292
+ Rails::Gke.delete_firewall_rule firewall_rule_name: firewall_rule_name
293
+ end
294
+
295
+ task get_neg_name: :environment do
296
+ Rails::Gke.get_network_group_list
297
+ end
298
+
299
+ task set_neg_name: :environment do
300
+ Rails::Gke.set_network_group_list_env
301
+ end
302
+
303
+ task delete_td_default: :environment do
304
+ Rails::Gke.delete_forwarding_rule
305
+ Rails::Gke.delete_target_grpc_proxy
306
+ Rails::Gke.delete_url_map
307
+ Rails::Gke.delete_backend_service
308
+ Rails::Gke.delete_health_check
309
+ Rails::Gke.delete_firewall_rule
310
+ Rails::Gke.delete_cluster
311
+ neg_name = Rails::Gke.set_network_group_list_env
312
+ Rails::Gke.delete_network_group_list neg_name: neg_name
313
+ end
314
+
315
+ task :resize_cluster, [:pool_name, :node_num] => :environment do |_, args|
316
+ pool_name = "default-pool" || args[:pool_name]
317
+ node_num = 1 || args[:node_num]
318
+ Rails::Gke.resize_cluster pool_name: pool_name, node_num: node_num
319
+ end
320
+
321
+ task create_namespace: :environment do
322
+ Rails::Gke.create_namespace
323
+ end
324
+
325
+ task create_ip: :environment do
326
+ Rails::Gke.create_ip
327
+ end
328
+
329
+ task apply_deployment: :environment do
330
+ Rails::Gke.apply_deployment
331
+ end
332
+
333
+ task apply_service: :environment do
334
+ Rails::Gke.apply_service
335
+ end
336
+
337
+ task apply_secret: :environment do
338
+ Rails::Gke.apply_secret
339
+ end
340
+
341
+ task apply_ingress: :environment do
342
+ Rails::Gke.apply_ingress
343
+ end
344
+
345
+ task delete_deployment: :environment do
346
+ Rails::Gke.delete_deployment
347
+ end
348
+
349
+ task delete_service: :environment do
350
+ Rails::Gke.delete_service
351
+ end
352
+
353
+ task delete_secret: :environment do
354
+ Rails::Gke.delete_secret
355
+ end
356
+
357
+ task delete_ingress: :environment do
358
+ Rails::Gke.delete_ingress
359
+ end
360
+
361
+ task test: :environment do
362
+ Rails::Gke.run_test
363
+ end
364
+
365
+ task :update, [:version] => :environment do |_, args|
366
+ Rails::Gke.update_container version: args[:version]
367
+ end
368
+
369
+ task apply_all: :environment do
370
+ Rails::Gke.apply_deployment
371
+ Rails::Gke.apply_service
372
+ Rails::Gke.apply_secret
373
+ Rails::Gke.apply_ingress
374
+ end
375
+
376
+ task delete_all: :environment do
377
+ Rails::Gke.delete_deployment
378
+ Rails::Gke.delete_service
379
+ Rails::Gke.delete_secret
380
+ Rails::Gke.delete_ingress
381
+ end
382
+
383
+ task get_pods: :environment do
384
+ Rails::Gke.get_pods
385
+ end
386
+
387
+ task get_svc: :environment do
388
+ Rails::Gke.get_svc
389
+ end
390
+
391
+ task get_ingress: :environment do
392
+ Rails::Gke.get_ingress
393
+ end
394
+
395
+ task get_clusters: :environment do
396
+ Rails::Gke.get_clusters
397
+ end
398
+
399
+ task get_current_cluster: :environment do
400
+ Rails::Gke.get_current_cluster
401
+ end
402
+
403
+ task :use_context, [:cluster] => :environment do |_, args|
404
+ Rails::Gke.use_context cluster: args[:cluster]
405
+ end
406
+
407
+ task get_credentials: :environment do
408
+ Rails::Gke.get_credentials
409
+ end
410
+ end
411
+ EOS
412
+ end
413
+ true
414
+ rescue StandardError => error
415
+ puts error
416
+ false
417
+ end
418
+
419
+ def deployment
420
+ return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
421
+ return "Error: Already Exsit deployment.yml" if File.file? "deployment.yml"
422
+ File.open("deployment.yml", "w") do |f|
423
+ yml = <<~EOS
424
+ apiVersion: extensions/v1beta1
425
+ kind: Deployment
426
+ metadata:
427
+ name: #{Rails::Gke.configuration.app}-deployment
428
+ spec:
429
+ replicas: 3
430
+ template:
431
+ metadata:
432
+ labels:
433
+ app: #{Rails::Gke.configuration.app}
434
+ spec:
435
+ containers:
436
+ - name: #{Rails::Gke.configuration.app}
437
+ image: asia.gcr.io/#{Rails::Gke.configuration.project_id}/#{Rails::Gke.configuration.app}:0.0.1
438
+ ports:
439
+ - containerPort: 3000
440
+ protocol: TCP
441
+ livenessProbe:
442
+ httpGet:
443
+ path: /
444
+ port: 3000
445
+ initialDelaySeconds: 30
446
+ timeoutSeconds: 1
447
+ readinessProbe:
448
+ httpGet:
449
+ path: /
450
+ port: 3000
451
+ initialDelaySeconds: 30
452
+ timeoutSeconds: 1
453
+ env:
454
+ - name: DB_HOST
455
+ valueFrom:
456
+ secretKeyRef:
457
+ name: #{Rails::Gke.configuration.app}-secret
458
+ key: db_host
459
+ - name: DB_USER
460
+ valueFrom:
461
+ secretKeyRef:
462
+ name: #{Rails::Gke.configuration.app}-secret
463
+ key: db_user
464
+ - name: DB_PW
465
+ valueFrom:
466
+ secretKeyRef:
467
+ name: #{Rails::Gke.configuration.app}-secret
468
+ key: db_pw
469
+ EOS
470
+ f.write(yml)
471
+ end
472
+ true
473
+ rescue
474
+ false
475
+ end
476
+
477
+ def service
478
+ return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
479
+ return "Error: Already Exsit service.yml" if File.file? "service.yml"
480
+ File.open("service.yml", "w") do |f|
481
+ yml = <<~EOS
482
+ kind: Service
483
+ apiVersion: v1
484
+ metadata:
485
+ name: #{Rails::Gke.configuration.app}-service
486
+ spec:
487
+ selector:
488
+ app: #{Rails::Gke.configuration.app}
489
+ type: LoadBalancer
490
+ ports:
491
+ - name: http
492
+ protocol: TCP
493
+ port: 80
494
+ targetPort: 3000
495
+ EOS
496
+ f.write(yml)
497
+ end
498
+ true
499
+ rescue
500
+ false
501
+ end
502
+
503
+ def ingress
504
+ return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
505
+ return "Error: Already Exsit ingress.yml" if File.file? "ingress.yml"
506
+ File.open("ingress.yml", "w") do |f|
507
+ yml = <<~EOS
508
+ apiVersion: extensions/v1beta1
509
+ kind: Ingress
510
+ metadata:
511
+ name: https-#{Rails::Gke.configuration.app}
512
+ annotations:
513
+ kubernetes.io/ingress.global-static-ip-name: https-#{Rails::Gke.configuration.app}
514
+ networking.gke.io/managed-certificates: #{Rails::Gke.configuration.app}-secret
515
+ spec:
516
+ rules:
517
+ - host: #{Rails::Gke.configuration.domain}
518
+ http:
519
+ paths:
520
+ - backend:
521
+ serviceName: #{Rails::Gke.configuration.app}-service
522
+ servicePort: 80
523
+
524
+ EOS
525
+ f.write(yml)
526
+ end
527
+ true
528
+ rescue
529
+ false
530
+ end
531
+
532
+ def grpc_service_deployment
533
+ return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
534
+ return "Error: Already Exsit deployment.yml" if File.file? "deployment.yml"
535
+ path = "secret.yml"
536
+ File.open(path, "w") do |f|
537
+ f.write <<~EOS
538
+ apiVersion: v1
539
+ kind: Service
540
+ metadata:
541
+ name: #{Rails::Gke.configuration.service1}
542
+ annotations:
543
+ cloud.google.com/neg: '{"exposed_ports":{"8080":{}}}'
544
+ spec:
545
+ ports:
546
+ - port: 8080
547
+ name: #{Rails::Gke.configuration.service1}
548
+ protocol: TCP
549
+ targetPort: 50051
550
+ selector:
551
+ run: #{Rails::Gke.configuration.service1}
552
+ type: ClusterIP
553
+
554
+ ---
555
+ apiVersion: extensions/v1beta1
556
+ kind: Deployment
557
+ metadata:
558
+ labels:
559
+ run: #{Rails::Gke.configuration.service1}
560
+ name: #{Rails::Gke.configuration.service1}
561
+ spec:
562
+ replicas: 2
563
+ template:
564
+ metadata:
565
+ labels:
566
+ run: #{Rails::Gke.configuration.service1}
567
+ spec:
568
+ containers:
569
+ - image: asia.gcr.io/#{Rails::Gke.configuration.project_id}/#{Rails::Gke.configuration.service1}:0.0.1
570
+ name: #{Rails::Gke.configuration.service1}
571
+ ports:
572
+ - protocol: TCP
573
+ containerPort: 50051
574
+ EOS
575
+ end
576
+ true
577
+ rescue
578
+ false
579
+ end
580
+
581
+ def secret
582
+ return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
583
+ return "Error: Already Exsit secret.yml" if File.file? "secret.yml"
584
+ File.open("secret.yml", "w") do |f|
585
+ yml = <<~EOS
586
+ apiVersion: v1
587
+ kind: Secret
588
+ metadata:
589
+ name: #{Rails::Gke.configuration.app}-secret
590
+ type: Opaque
591
+ data:
592
+ db_user: dXNlcg==
593
+ db_pw: cGFzc3dvcmQ=
594
+ db_host: bG9jYWxob3N0
595
+ EOS
596
+ f.write(yml)
597
+ end
598
+ true
599
+ rescue
600
+ false
601
+ end
602
+ end
603
+ end
604
+ end