souls 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ Apache License
2
+
3
+ Version 2.0, January 2004
4
+
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
16
+
17
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
18
+
19
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
20
+
21
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
22
+
23
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
24
+
25
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
26
+
27
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
28
+
29
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
30
+
31
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
32
+
33
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
34
+
35
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
36
+
37
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
38
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
39
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
40
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
41
+
42
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
43
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
44
+
45
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
46
+
47
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
48
+
49
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
50
+
51
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
52
+
53
+ END OF TERMS AND CONDITIONS
54
+
55
+ Copyright 2020 ELSOUL LABO B.V.
56
+
57
+ Licensed under the Apache License, Version 2.0 (the "License");
58
+ you may not use this file except in compliance with the License.
59
+ You may obtain a copy of the License at
60
+
61
+ http://www.apache.org/licenses/LICENSE-2.0
62
+
63
+ Unless required by applicable law or agreed to in writing, software
64
+ distributed under the License is distributed on an "AS IS" BASIS,
65
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
66
+ See the License for the specific language governing permissions and
67
+ limitations under the License.
@@ -0,0 +1,122 @@
1
+ [![SOULs](https://firebasestorage.googleapis.com/v0/b/el-quest.appspot.com/o/mediaLibrary%2F1605615287402_souls-ogp.jpg?alt=media&token=1115aa76-6863-469d-acc8-9815ca7fac37)](https://rubygems.org/gems/souls)
2
+
3
+ <p align="center">
4
+ <a aria-label="Ruby logo" href="https://el-soul.com">
5
+ <img src="https://badgen.net/badge/icon/Made%20by%20ELSOUL?icon=ruby&label&color=black&labelColor=black">
6
+ </a>
7
+ <br/>
8
+
9
+ <a aria-label="Ruby Gem version" href="https://rubygems.org/gems/souls">
10
+ <img alt="" src="https://badgen.net/rubygems/v/souls/latest">
11
+ </a>
12
+ <a aria-label="Downloads Number" href="https://rubygems.org/gems/souls">
13
+ <img alt="" src="https://badgen.net/rubygems/dt/souls">
14
+ </a>
15
+ <a aria-label="License" href="https://github.com/elsoul/souls/blob/master/LICENSE">
16
+ <img alt="" src="https://badgen.net/badge/license/Apache/blue">
17
+ </a>
18
+ </p>
19
+
20
+ ## What's SOULs?
21
+
22
+ Welcome to SOULs Framework!
23
+
24
+ SOULS is a Web Application Framework for Microservices on Multi Cloud Platform such as Google Cloud Platform, Amazon Web Services, and Alibaba Cloud. Auto deploy with scalable condition. You can focus on business logic. No more infra problems.
25
+
26
+ SOULs creates 4 types of framework.
27
+
28
+ 1. Service - gRPC Serverless Scalable Service (Ruby)
29
+ 2. API - GraphQL to call gRPC (Ruby)
30
+ 3. Media Web Client - Media web client with SSG (TypeScript)
31
+ 4. Admin Web Client - Admin Console and CMS (TypeScript)
32
+
33
+ ## Dependency
34
+
35
+ - [Google SDK](https://cloud.google.com/sdk/docs)
36
+ - [Docker](https://www.docker.com/)
37
+ - [Firebase CLI](https://firebase.google.com/docs/cli)
38
+
39
+ ## Cloud Infrastructure
40
+
41
+ - [Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine)
42
+ - [Google Traffic Director](https://cloud.google.com/traffic-director)
43
+ - [Google Cloud Run](https://cloud.google.com/run)
44
+ - [Google Firebase](https://firebase.google.com/)
45
+ - [Google Cloud Scheduler](https://cloud.google.com/scheduler)
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'souls'
53
+ ```
54
+
55
+ And then execute:
56
+
57
+ $ bundle install
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install souls
62
+
63
+ And Create Your APP
64
+
65
+ $ souls new app_name
66
+
67
+ ### Choose SOULs Type:
68
+
69
+ 1. Service
70
+ 2. API
71
+ 3. Media Web
72
+ 4. Admin Web
73
+
74
+ ## Usage
75
+
76
+ Version Check
77
+
78
+ $ souls -v
79
+
80
+ Init Proto Files
81
+
82
+ $ souls p `service_name`
83
+
84
+ Run Server
85
+
86
+ $ souls s
87
+
88
+ Run Console
89
+
90
+ $ souls c
91
+
92
+ Run Infra Command
93
+
94
+ $ souls i `method_name`
95
+
96
+ Deploy
97
+
98
+ $ souls i deploy
99
+
100
+ ※Only Initial
101
+
102
+ Update
103
+
104
+ $ souls i update
105
+
106
+ ## Development
107
+
108
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
109
+
110
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org/gems/souls).
111
+
112
+ ## Contributing
113
+
114
+ Bug reports and pull requests are welcome on GitHub at https://github.com/elsoul/souls. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
115
+
116
+ ## License
117
+
118
+ The gem is available as open source under the terms of the [Apache-2.0 License](https://www.apache.org/licenses/LICENSE-2.0).
119
+
120
+ ## Code of Conduct
121
+
122
+ Everyone interacting in the HotelPrice project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/elsoul/souls/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "souls"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ Souls.configure do |config|
2
+ config.project_id = "el-quest"
3
+ config.app = "el-quest"
4
+ config.namespace = "souls"
5
+ config.service_name = "blog-service"
6
+ config.network = "elsoul"
7
+ config.machine_type = "elsoul"
8
+ config.zone = "elsoul"
9
+ config.domain = "elsoul"
10
+ config.google_application_credentials = "./config/credentials.json"
11
+ config.strain = "service"
12
+ config.proto_package_name = "souls"
13
+ end
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+ require "souls"
3
+
4
+ begin
5
+ require "./config/initializers/souls" unless ARGV[0] == "new"
6
+ case ARGV[0]
7
+ when "new"
8
+ if ARGV[1].nil?
9
+ puts "you need to specify your app name \n `souls new app_name`"
10
+ exit
11
+ end
12
+ puts "Which framework: \n 1. SOULS gRPC Service \n 2. SOULS GraphQL API \n 3. SOULS Media Web \n 4. SOULS Admin Web \n Enter Number: "
13
+ strain = STDIN.gets.chomp.to_i
14
+ (1..4).include?(strain) ? puts("Generating Souls.. \n") : raise(StandardError, "Choose Number 1..4")
15
+ Souls::Init.create_souls strain: strain, app_name: ARGV[1]
16
+ when "s", "server"
17
+ strain = Souls.configuration.strain
18
+ case strain
19
+ when "api"
20
+ system "bundle exec puma -p 3000"
21
+ when "service"
22
+ system "bundle exec rake run_server"
23
+ else
24
+ system "yarn dev"
25
+ end
26
+ when "c", "console"
27
+ strain = Souls.configuration.strain
28
+ case strain
29
+ when "api"
30
+ system "bundle exec irb"
31
+ when "service"
32
+ system "bundle exec irb"
33
+ else
34
+ system "yarn dev"
35
+ end
36
+ when "i", "infra"
37
+ Souls.send ARGV[1]
38
+ when "p", "proto"
39
+ Souls::Init.proto proto_package_name: "souls", service: ARGV[1]
40
+ when "init"
41
+ Souls::Init.config
42
+ when "-v", "--version"
43
+ puts Souls::VERSION
44
+ when "test"
45
+ system "ruby greeter_client.rb 'SOULS TEST RUN!'"
46
+ else
47
+ puts "Welcome to Souls!Yeah!"
48
+ end
49
+ rescue StandardError => error
50
+ puts error
51
+ puts "Thank you!Souls!"
52
+ end
@@ -0,0 +1,427 @@
1
+ require "souls/version"
2
+ require "souls/init"
3
+ require "google/cloud/firestore"
4
+
5
+ module Souls
6
+ class Error < StandardError; end
7
+ class << self
8
+ attr_accessor :configuration
9
+
10
+ def delete_forwarding_rule forwarding_rule_name: "grpc-gke-forwarding-rule"
11
+ system "gcloud compute -q forwarding-rules delete #{forwarding_rule_name} --global"
12
+ end
13
+
14
+ def create_forwarding_rule forwarding_rule_name: "grpc-gke-forwarding-rule", proxy_name: "grpc-gke-proxy", port: 8000
15
+ system "gcloud compute -q forwarding-rules create #{forwarding_rule_name} \
16
+ --global \
17
+ --load-balancing-scheme=INTERNAL_SELF_MANAGED \
18
+ --address=0.0.0.0 \
19
+ --target-grpc-proxy=#{proxy_name} \
20
+ --ports #{port} \
21
+ --network #{Souls.configuration.network}"
22
+ end
23
+
24
+ def delete_target_grpc_proxy proxy_name: "grpc-gke-proxy"
25
+ system "gcloud compute -q target-grpc-proxies delete #{proxy_name}"
26
+ end
27
+
28
+ def create_target_grpc_proxy proxy_name: "grpc-gke-proxy", url_map_name: "grpc-gke-url-map"
29
+ system "gcloud compute -q target-grpc-proxies create #{proxy_name} \
30
+ --url-map #{url_map_name} \
31
+ --validate-for-proxyless"
32
+ end
33
+
34
+ def create_path_matcher url_map_name: "grpc-gke-url-map", service_name: "grpc-gke-helloworld-service", path_matcher_name: "grpc-gke-path-matcher", hostname: "helloworld-gke", port: "8000"
35
+ system "gcloud compute -q url-maps add-path-matcher #{url_map_name} \
36
+ --default-service #{service_name} \
37
+ --path-matcher-name #{path_matcher_name} \
38
+ --new-hosts #{hostname}:#{port}"
39
+ end
40
+
41
+ def delete_url_map url_map_name: "grpc-gke-url-map"
42
+ system "gcloud compute -q url-maps delete #{url_map_name}"
43
+ end
44
+
45
+ def create_url_map url_map_name: "grpc-gke-url-map", service_name: "grpc-gke-helloworld-service"
46
+ system "gcloud compute -q url-maps create #{url_map_name} \
47
+ --default-service #{service_name}"
48
+ end
49
+
50
+ def add_backend_service service_name: "grpc-gke-helloworld-service", zone: "us-central1-a", neg_name: ""
51
+ system "gcloud compute -q backend-services add-backend #{service_name} \
52
+ --global \
53
+ --network-endpoint-group #{neg_name} \
54
+ --network-endpoint-group-zone #{zone} \
55
+ --balancing-mode RATE \
56
+ --max-rate-per-endpoint 5"
57
+ end
58
+
59
+ def delete_backend_service service_name: "grpc-gke-helloworld-service"
60
+ system "gcloud compute -q backend-services delete #{service_name} --global"
61
+ end
62
+
63
+ def create_backend_service service_name: "grpc-gke-helloworld-service", health_check_name: "grpc-gke-helloworld-hc"
64
+ system "gcloud compute -q backend-services create #{service_name} \
65
+ --global \
66
+ --load-balancing-scheme=INTERNAL_SELF_MANAGED \
67
+ --protocol=GRPC \
68
+ --health-checks #{health_check_name}"
69
+ end
70
+
71
+ def delete_firewall_rule firewall_rule_name: "grpc-gke-allow-health-checks"
72
+ system "gcloud compute -q firewall-rules delete #{firewall_rule_name}"
73
+ end
74
+
75
+ def create_firewall_rule firewall_rule_name: "grpc-gke-allow-health-checks"
76
+ network = Souls.configuration.network
77
+ system "gcloud compute -q firewall-rules create #{firewall_rule_name} \
78
+ --network #{network} \
79
+ --action allow \
80
+ --direction INGRESS \
81
+ --source-ranges 35.191.0.0/16,130.211.0.0/22 \
82
+ --target-tags allow-health-checks \
83
+ --rules tcp:50051"
84
+ end
85
+
86
+ def delete_health_check health_check_name: "grpc-gke-helloworld-hc"
87
+ system "gcloud compute -q health-checks delete #{health_check_name}"
88
+ end
89
+
90
+ def create_health_check health_check_name: "grpc-gke-helloworld-hc"
91
+ system "gcloud compute -q health-checks create grpc #{health_check_name} --use-serving-port"
92
+ end
93
+
94
+ def create_network
95
+ return "Error: Please Set Souls.configuration" if Souls.configuration.nil?
96
+ network = Souls.configuration.network
97
+ system "gcloud compute networks create #{network}"
98
+ end
99
+
100
+ def create_firewall_tcp ip_range:
101
+ network = Souls.configuration.network
102
+ `gcloud compute firewall-rules create #{network} --network #{network} --allow tcp,udp,icmp --source-ranges #{ip_range}`
103
+ end
104
+
105
+ def create_firewall_ssh
106
+ network = Souls.configuration.network
107
+ `gcloud compute firewall-rules create #{network}-ssh --network #{network} --allow tcp:22,tcp:3389,icmp`
108
+ end
109
+
110
+ def get_network_group_list
111
+ system "gcloud compute network-endpoint-groups list"
112
+ end
113
+
114
+ def create_network_group
115
+ app = Souls.configuration.app
116
+ network = Souls.configuration.network
117
+ sub_network = Souls.configuration.network
118
+ system("gcloud compute network-endpoint-groups create #{app} \
119
+ --default-port=0 \
120
+ --network #{network} \
121
+ --subnet #{sub_network} \
122
+ --global")
123
+ end
124
+
125
+ def export_network_group
126
+ app = Souls.configuration.app
127
+ system "NEG_NAME=$(gcloud compute network-endpoint-groups list | grep #{app} | awk '{print $1}')"
128
+ `echo $NEG_NAME > ./infra/config/neg_name`
129
+ end
130
+
131
+ def delete_network_group_list neg_name:
132
+ zone = Souls.configuration.zone
133
+ system "gcloud compute network-endpoint-groups delete #{neg_name} --zone #{zone} -q"
134
+ end
135
+
136
+ def delete_cluster
137
+ app = Souls.configuration.app
138
+ zone = Souls.configuration.zone
139
+ system "gcloud container clusters delete #{app} --zone #{zone} -q"
140
+ end
141
+
142
+ def config_set_main
143
+ project_id = Souls.configuration.main_project_id
144
+ system "gcloud config set project #{project_id}"
145
+ end
146
+
147
+ def config_set
148
+ project_id = Souls.configuration.project_id
149
+ system "gcloud config set project #{project_id}"
150
+ end
151
+
152
+ def create_cluster
153
+ app = Souls.configuration.app
154
+ network = Souls.configuration.network
155
+ sub_network = Souls.configuration.network
156
+ machine_type = Souls.configuration.machine_type
157
+ zone = Souls.configuration.zone
158
+ system("gcloud container clusters create #{app} \
159
+ --network #{network} \
160
+ --subnetwork #{sub_network} \
161
+ --zone #{zone} \
162
+ --scopes=https://www.googleapis.com/auth/cloud-platform \
163
+ --machine-type #{machine_type} \
164
+ --enable-autorepair \
165
+ --enable-ip-alias \
166
+ --num-nodes 2 \
167
+ --enable-autoscaling \
168
+ --min-nodes 1 \
169
+ --max-nodes 4 \
170
+ --tags=allow-health-checks")
171
+ end
172
+
173
+ def deploy
174
+ strain = Souls.configuration.strain
175
+ case strain
176
+ when "api"
177
+ Souls::Init.api_deploy
178
+ when "service"
179
+ Souls::Init.service_deploy
180
+ else
181
+ puts "coming soon..."
182
+ end
183
+ end
184
+
185
+ def update
186
+ `souls i apply_deployment`
187
+ end
188
+
189
+ def resize_cluster pool_name: "default-pool", node_num: 1
190
+ app = Souls.configuration.app
191
+ zone = Souls.configuration.zone
192
+ system "gcloud container clusters resize #{app} --node-pool #{pool_name} --num-nodes #{node_num} --zone #{zone}"
193
+ end
194
+
195
+ def create_namespace
196
+ app = Souls.configuration.app
197
+ system("kubectl create namespace #{app}")
198
+ end
199
+
200
+ def create_ip
201
+ ip_name = "#{Souls.configuration.app}-ip"
202
+ system("gcloud compute addresses create #{ip_name} --global")
203
+ end
204
+
205
+ def apply_deployment
206
+ namespace = Souls.configuration.namespace
207
+ system("kubectl apply -f ./infra/deployment.yml --namespace=#{namespace}")
208
+ end
209
+
210
+ def apply_secret
211
+ namespace = Souls.configuration.namespace
212
+ system("kubectl apply -f ./infra/secret.yml --namespace=#{namespace}")
213
+ end
214
+
215
+ def apply_service
216
+ namespace = Souls.configuration.namespace
217
+ system("kubectl apply -f ./infra/service.yml --namespace=#{namespace}")
218
+ end
219
+
220
+ def apply_ingress
221
+ namespace = Souls.configuration.namespace
222
+ system("kubectl apply -f ./infra/ingress.yml --namespace=#{namespace}")
223
+ end
224
+
225
+ def delete_deployment
226
+ namespace = Souls.configuration.namespace
227
+ system("kubectl delete -f ./infra/deployment.yml --namespace=#{namespace}")
228
+ end
229
+
230
+ def delete_secret
231
+ namespace = Souls.configuration.namespace
232
+ system("kubectl delete -f ./infra/secret.yml --namespace=#{namespace}")
233
+ end
234
+
235
+ def delete_service
236
+ namespace = Souls.configuration.namespace
237
+ system("kubectl delete -f ./infra/service.yml --namespace=#{namespace}")
238
+ end
239
+
240
+ def delete_ingress
241
+ namespace = Souls.configuration.namespace
242
+ system("kubectl delete -f ./infra/ingress.yml --namespace=#{namespace}")
243
+ end
244
+
245
+ def create_dns_conf
246
+ app = Souls.configuration.app
247
+ namespace = Souls.configuration.namespace
248
+ domain = Souls.configuration.domain
249
+ `echo "#{domain}. 300 IN A $(kubectl get ingress --namespace #{namespace} | grep #{app} | awk '{print $3}')" >> ./infra/config/dns_conf`
250
+ "created dns file!"
251
+ end
252
+
253
+ def set_dns
254
+ project_id = Souls.configuration.project_id
255
+ `gcloud dns record-sets import -z=#{project_id} --zone-file-format ./infra/config/dns_conf`
256
+ end
257
+
258
+ def update_container zone: :asia
259
+ project_id = Souls.configuration.project_id
260
+ firestore = Google::Cloud::Firestore.new
261
+ strain = Souls.configuration.strain
262
+ app = Souls.configuration.app
263
+ container = case strain
264
+ when "api"
265
+ app
266
+ else
267
+ Souls.configuration.service_name
268
+ end
269
+ zones = {
270
+ us: "gcr.io",
271
+ eu: "eu.gcr.io",
272
+ asia: "asia.gcr.io"
273
+ }
274
+ versions = firestore.doc "containers/#{container}/versions/1"
275
+ if versions.get.exists?
276
+ versions = firestore.col("containers").doc(container).col("versions")
277
+ query = versions.order("version_counter", "desc").limit 1
278
+ query.get do |v|
279
+ @next_version = v.data[:version_counter] + 1
280
+ end
281
+ else
282
+ @next_version = 1
283
+ end
284
+ version = firestore.col("containers").doc(container).col("versions").doc @next_version
285
+ version.set version: "v#{@next_version}", version_counter: @next_version, zone: zones[zone], created_at: Time.now.utc
286
+
287
+ system("docker build . -t #{app}:v#{@next_version}")
288
+ system("docker tag #{app}:v#{@next_version} #{zones[zone]}/#{project_id}/#{app}:v#{@next_version}")
289
+ system("docker push #{zones[zone]}/#{project_id}/#{app}:v#{@next_version}")
290
+ end
291
+
292
+ def create_service_account
293
+ service_account = Souls.configuration.app
294
+ `gcloud iam service-accounts create #{service_account} \
295
+ --description="Souls Service Account" \
296
+ --display-name="#{service_account}"`
297
+ end
298
+
299
+ def create_service_account_key
300
+ project_id = Souls.configuration.project_id
301
+ service_account = Souls.configuration.app
302
+ `gcloud iam service-accounts keys create ./config/keyfile.json \
303
+ --iam-account #{service_account}@#{project_id}.iam.gserviceaccount.com`
304
+ end
305
+
306
+ def add_service_account_role role: "roles/firebase.admin"
307
+ project_id = Souls.configuration.project_id
308
+ service_account = Souls.configuration.app
309
+ `gcloud projects add-iam-policy-binding #{project_id} \
310
+ --member="serviceAccount:#{service_account}@#{project_id}.iam.gserviceaccount.com" \
311
+ --role="#{role}"`
312
+ end
313
+
314
+ def get_pods
315
+ namespace = Souls.configuration.namespace
316
+ system("kubectl get pods --namespace=#{namespace}")
317
+ end
318
+
319
+ def get_svc
320
+ namespace = Souls.configuration.namespace
321
+ system("kubectl get svc --namespace=#{namespace}")
322
+ end
323
+
324
+ def get_ingress
325
+ namespace = Souls.configuration.namespace
326
+ system("kubectl get ingress --namespace=#{namespace}")
327
+ end
328
+
329
+ def run
330
+ app = Souls.configuration.app
331
+ system("docker rm -f web")
332
+ system("docker build . -t #{app}:latest")
333
+ system("docker run --name web -it --env-file $PWD/.env -p 3000:3000 #{app}:latest")
334
+ end
335
+
336
+ def get_clusters
337
+ system("kubectl config get-clusters")
338
+ end
339
+
340
+ def get_current_cluster
341
+ system("kubectl config current-context")
342
+ end
343
+
344
+ def use_context cluster:
345
+ system("kubectl config use-context #{cluster}")
346
+ end
347
+
348
+ def get_credentials
349
+ app = Souls.configuration.app
350
+ zone = Souls.configuration.zone
351
+ system "gcloud container clusters get-credentials #{app} --zone #{zone}"
352
+ end
353
+
354
+ def create_ssl
355
+ app = Souls.configuration.app
356
+ domain = Souls.configuration.domain
357
+ system "gcloud compute ssl-certificates create #{app}-ssl --domains=#{domain} --global"
358
+ end
359
+
360
+ def run_psql
361
+ `docker run --rm -d \
362
+ -p 5433:5432 \
363
+ -v postgres-tmp:/var/lib/postgresql/data \
364
+ -e POSTGRES_USER=postgres \
365
+ -e POSTGRES_PASSWORD=postgres \
366
+ -e POSTGRES_DB=souls_dev \
367
+ postgres:12-alpine`
368
+ system "docker ps"
369
+ end
370
+
371
+ def deploy_local
372
+ `docker network create --driver bridge shared`
373
+ `docker run -d --name proxy \
374
+ -p 80:80 -p 443:443 \
375
+ -v "/var/run/docker.sock:/tmp/docker.sock:ro" \
376
+ -v "$pwd/certs:/etc/nginx/certs:ro" \
377
+ -v "/etc/nginx/vhost.d" \
378
+ -v "/usr/share/nginx/html" \
379
+ --network shared \
380
+ --restart always \
381
+ jwilder/nginx-proxy`
382
+ `docker run -d --name letsencrypt \
383
+ -v "/home/certs:/etc/nginx/certs" \
384
+ -v "/var/run/docker.sock:/var/run/docker.sock:ro" \
385
+ --volumes-from proxy \
386
+ --network shared \
387
+ --restart always \
388
+ jrcs/letsencrypt-nginx-proxy-companion`
389
+ `docker run -d --name nginx \
390
+ -p 80:80 \
391
+ -e VIRTUAL_HOST=souls.el-soul.com \
392
+ -e LETSENCRYPT_HOST=souls.el-soul.com \
393
+ -e LETSENCRYPT_EMAIL=info@gmail.com \
394
+ --network shared \
395
+ --link web \
396
+ poppinfumi/nginx-http:latest`
397
+ `docker run -d --name web \
398
+ -p 3000:3000 \
399
+ --network shared \
400
+ asia.gcr.io/kaien-elixir/kaien:v2`
401
+ end
402
+ end
403
+
404
+ def self.configure
405
+ self.configuration ||= Configuration.new
406
+ yield(configuration)
407
+ end
408
+
409
+ class Configuration
410
+ attr_accessor :main_project_id, :project_id, :app, :network, :namespace, :service_name, :machine_type, :zone, :domain, :google_application_credentials, :strain, :proto_package_name
411
+
412
+ def initialize
413
+ @main_project_id = nil
414
+ @project_id = nil
415
+ @app = nil
416
+ @network = nil
417
+ @namespace = nil
418
+ @service_name = nil
419
+ @machine_type = nil
420
+ @zone = nil
421
+ @domain = nil
422
+ @google_application_credentials = nil
423
+ @strain = nil
424
+ @proto_package_name = nil
425
+ end
426
+ end
427
+ end