forked from ThreeDotsLabs/wild-workouts-go-ddd-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
760 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
steps: | ||
- id: trainer-lint | ||
name: golang | ||
entrypoint: ./scripts/lint.sh | ||
args: [trainer] | ||
- id: trainings-lint | ||
name: golang | ||
entrypoint: ./scripts/lint.sh | ||
args: [trainings] | ||
- id: users-lint | ||
name: golang | ||
entrypoint: ./scripts/lint.sh | ||
args: [users] | ||
|
||
- id: trainer-docker | ||
name: gcr.io/cloud-builders/docker | ||
entrypoint: ./scripts/build-docker.sh | ||
args: ["trainer", "$PROJECT_ID"] | ||
waitFor: [trainer-lint] | ||
- id: trainings-docker | ||
name: gcr.io/cloud-builders/docker | ||
entrypoint: ./scripts/build-docker.sh | ||
args: ["trainings", "$PROJECT_ID"] | ||
waitFor: [trainings-lint] | ||
- id: users-docker | ||
name: gcr.io/cloud-builders/docker | ||
entrypoint: ./scripts/build-docker.sh | ||
args: ["users", "$PROJECT_ID"] | ||
waitFor: [users-lint] | ||
|
||
- id: trainer-http-deploy | ||
name: gcr.io/cloud-builders/gcloud | ||
entrypoint: ./scripts/deploy.sh | ||
args: [trainer, http, "$PROJECT_ID"] | ||
waitFor: [trainer-docker] | ||
- id: trainer-grpc-deploy | ||
name: gcr.io/cloud-builders/gcloud | ||
entrypoint: ./scripts/deploy.sh | ||
args: [trainer, grpc, "$PROJECT_ID"] | ||
waitFor: [trainer-docker] | ||
- id: trainings-http-deploy | ||
name: gcr.io/cloud-builders/gcloud | ||
entrypoint: ./scripts/deploy.sh | ||
args: [trainings, http, "$PROJECT_ID"] | ||
waitFor: [trainings-docker] | ||
- id: users-http-deploy | ||
name: gcr.io/cloud-builders/gcloud | ||
entrypoint: ./scripts/deploy.sh | ||
args: [users, http, "$PROJECT_ID"] | ||
waitFor: [users-docker] | ||
- id: users-grpc-deploy | ||
name: gcr.io/cloud-builders/gcloud | ||
entrypoint: ./scripts/deploy.sh | ||
args: [users, grpc, "$PROJECT_ID"] | ||
waitFor: [users-docker] | ||
|
||
- id: web-deps | ||
name: node:12.16.2 | ||
entrypoint: yarn | ||
args: [install] | ||
dir: web | ||
waitFor: ['-'] | ||
- id: openapi-js | ||
name: openapitools/openapi-generator-cli:v4.3.0 | ||
entrypoint: "./scripts/openapi-js.sh" | ||
waitFor: ['-'] | ||
- id: web-build | ||
name: node:12.16.2 | ||
entrypoint: yarn | ||
args: [build] | ||
dir: web | ||
waitFor: [web-deps, openapi-js] | ||
- name: gcr.io/$PROJECT_ID/firebase | ||
args: ['deploy', '--project=$PROJECT_ID'] | ||
dir: web | ||
waitFor: [web-build] | ||
|
||
options: | ||
env: | ||
- 'GO111MODULE=on' | ||
machineType: 'N1_HIGHCPU_8' | ||
|
||
images: | ||
- 'gcr.io/$PROJECT_ID/trainer' | ||
- 'gcr.io/$PROJECT_ID/trainings' | ||
- 'gcr.io/$PROJECT_ID/users' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM golang:1.14 | ||
|
||
ARG SERVICE | ||
|
||
RUN mkdir /internal | ||
COPY . /internal | ||
WORKDIR /internal/$SERVICE | ||
RUN go build -o /app . | ||
|
||
CMD ["/app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
readonly service="$1" | ||
readonly project_id="$2" | ||
|
||
docker build -t "gcr.io/$project_id/$service" "./internal" -f "./docker/app-prod/Dockerfile" --build-arg "SERVICE=$service" | ||
docker push "gcr.io/$project_id/$service" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
readonly service="$1" | ||
readonly server_to_run="$2" | ||
readonly project_id="$3" | ||
|
||
gcloud run deploy "$service-$server_to_run" \ | ||
--image "gcr.io/$project_id/$service" \ | ||
--region europe-west1 \ | ||
--platform managed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ set -e | |
|
||
readonly service="$1" | ||
|
||
cd "./pkg/$service" | ||
cd "./internal/$service" | ||
go vet . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#!/bin/bash | ||
for service in trainer trainings users; do | ||
/usr/local/bin/docker-entrypoint.sh generate \ | ||
-i "./openapi/$service.yml" \ | ||
-i "./api/openapi/$service.yml" \ | ||
-g javascript \ | ||
-o "./web/src/repositories/clients/$service" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export TF_VAR_project=wild-workouts-project | ||
export TF_VAR_user=user@company.com | ||
export TF_VAR_billing_account=My billing account | ||
export TF_VAR_region=europe-west1 | ||
export TF_VAR_firebase_location=europe-west |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
base_image:=gcr.io/cloudrun/hello | ||
load_envs:=source ./.env | ||
|
||
all: check init apply push_repo done | ||
|
||
.PHONY: check | ||
check: | ||
@command -v terraform >/dev/null || ( echo "terraform is not installed"; exit 1 ) | ||
@command -v gcloud >/dev/null || ( echo "gcloud is not installed"; exit 1 ) | ||
|
||
.PHONY: init | ||
init: | ||
@./set-envs.sh | ||
terraform init | ||
|
||
.PHONY: apply | ||
apply: | ||
${load_envs} && terraform apply | ||
|
||
.PHONY: push_repo | ||
push_repo: | ||
git config --global credential.https://source.developers.google.com.helper gcloud.sh | ||
repo_url=$(shell terraform output -no-color | grep repo_url | awk '{print $$3}') && \ | ||
if [ -z "$(shell git remote | grep google)" ]; then \ | ||
git remote add google "$$repo_url"; \ | ||
else \ | ||
git remote set-url google "$$repo_url"; \ | ||
fi | ||
git push google master | ||
|
||
.PHONY: done | ||
done: | ||
@echo | ||
@printf "=%.0s" {1..80} | ||
@echo | ||
@echo | ||
@printf "\e[93mThe setup is almost done!\e[0m\n" | ||
@echo | ||
@printf "Now you need to enable \e[1mEmail/Password\e[0m provider in the Firebase console.\n" | ||
@${load_envs} && echo "To do this, visit https://console.firebase.google.com/u/0/project/$$TF_VAR_project/authentication/providers" | ||
@echo | ||
@printf "You can also downgrade the subscription plan to \e[1mSpark\e[0m (it's set to Blaze by default).\n" | ||
@echo "The Spark plan is completely free and has all features needed for running this project." | ||
@echo | ||
@${load_envs} && printf "\e[92mCongratulations!\e[0m Your project should be available at: https://$$TF_VAR_project.web.app \n" | ||
@echo | ||
@${load_envs} && echo "If it's not, check if the build finished successfully: https://console.cloud.google.com/cloud-build/builds?project=$$TF_VAR_project" | ||
@echo | ||
@echo "If you need help, feel free to contact us at https://threedots.tech" | ||
|
||
.PHONY: set_project | ||
set_project: | ||
${load_envs} && gcloud config set project "$$TF_VAR_project" | ||
|
||
.PHONY: firestore | ||
firestore: set_project | ||
${load_envs} && gcloud alpha firestore databases create "--region=$$TF_VAR_firebase_location" | ||
|
||
.PHONY: firebase_builder | ||
firebase_builder: set_project | ||
[ -d cloud-builders-community ] || git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git | ||
cd cloud-builders-community/firebase; gcloud builds submit . | ||
|
||
.PHONY: docker_images | ||
docker_images: | ||
docker pull "${base_image}" | ||
|
||
${load_envs} && for service in trainer trainings users; do \ | ||
tag="gcr.io/$$TF_VAR_project/$$service"; \ | ||
docker tag "${base_image}" "$$tag"; \ | ||
docker push "$$tag"; \ | ||
done | ||
|
||
.PHONY: destroy | ||
destroy: | ||
terraform state rm "google_project_iam_member.owner" | ||
terraform state rm "google_project_service.container_registry" | ||
terraform state rm "google_project_service.cloud_run" | ||
${load_envs} && terraform destroy | ||
git remote rm google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## Required software | ||
|
||
* Terraform (tested on v0.12.24) | ||
* gcloud CLI | ||
* Docker (with daemon running) | ||
|
||
``` | ||
Terraform v0.12.24 | ||
Google Cloud SDK 290.0.1 | ||
alpha 2019.05.17 | ||
beta 2019.05.17 | ||
core 2020.04.24 | ||
``` | ||
|
||
## Setup | ||
|
||
1. Authorize in gcloud CLI. | ||
|
||
This projects aims to allow as easy as possible setup. Default application login is not recommended for production use. | ||
|
||
``` | ||
gcloud auth login | ||
gcloud config set account | ||
gcloud auth application-default login | ||
``` | ||
|
||
2. Run make. While terraform is running, you will be asked to confirm applying changes. Answer wih `yes`. | ||
|
||
```bash | ||
make | ||
``` | ||
|
||
3. Make sure you enable `Email/Password` authentication provider in Firebase as described in the `make` output. | ||
|
||
a. Open FireBase console: https://console.firebase.google.com | ||
b. Choose `Wild Workouts` project | ||
c. Go to `Authentication` | ||
d. Choose `Sign-in method` tab | ||
e. Click on `Email/Password`, switch to `Enabled` and click `Save`. | ||
|
||
## Cloud builds | ||
|
||
Go to https://console.cloud.google.com/cloud-build/builds to see your recent builds. | ||
|
||
## Destroy | ||
|
||
If you want to tear down the project, run `make destroy`. | ||
|
||
If you want to create it again, make sure to: | ||
* Use different project name. | ||
* Remove `terraform.tfstate` file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module cloud_run_trainer_grpc { | ||
source = "./service" | ||
|
||
project = var.project | ||
location = var.region | ||
dependency = null_resource.init_docker_images | ||
|
||
name = "trainer" | ||
protocol = "grpc" | ||
} | ||
|
||
module cloud_run_trainer_http { | ||
source = "./service" | ||
|
||
project = var.project | ||
location = var.region | ||
dependency = null_resource.init_docker_images | ||
|
||
name = "trainer" | ||
protocol = "http" | ||
auth = false | ||
|
||
envs = [ | ||
{ | ||
name = "TRAINER_GRPC_ADDR" | ||
value = module.cloud_run_trainer_grpc.endpoint | ||
} | ||
] | ||
} | ||
|
||
module cloud_run_trainings_http { | ||
source = "./service" | ||
|
||
project = var.project | ||
location = var.region | ||
dependency = null_resource.init_docker_images | ||
|
||
name = "trainings" | ||
protocol = "http" | ||
auth = false | ||
|
||
envs = [ | ||
{ | ||
name = "TRAINER_GRPC_ADDR" | ||
value = module.cloud_run_trainer_grpc.endpoint | ||
}, | ||
{ | ||
name = "USERS_GRPC_ADDR" | ||
value = module.cloud_run_users_grpc.endpoint | ||
} | ||
] | ||
} | ||
|
||
module cloud_run_users_grpc { | ||
source = "./service" | ||
|
||
project = var.project | ||
location = var.region | ||
dependency = null_resource.init_docker_images | ||
|
||
name = "users" | ||
protocol = "grpc" | ||
} | ||
|
||
module cloud_run_users_http { | ||
source = "./service" | ||
|
||
project = var.project | ||
location = var.region | ||
dependency = null_resource.init_docker_images | ||
|
||
name = "users" | ||
protocol = "http" | ||
auth = false | ||
|
||
envs = [ | ||
{ | ||
name = "USERS_GRPC_ADDR" | ||
value = module.cloud_run_users_grpc.endpoint | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
resource "null_resource" "init_docker_images" { | ||
triggers = { | ||
project = google_project.project.number | ||
} | ||
|
||
provisioner "local-exec" { | ||
command = "make docker_images" | ||
} | ||
|
||
depends_on = [google_project_service.container_registry] | ||
} |
Oops, something went wrong.