Skip to content

Commit

Permalink
Added infrastructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed May 12, 2020
1 parent 54ab4ea commit 227e540
Show file tree
Hide file tree
Showing 23 changed files with 760 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ vendor/

.terraform
*.tfstate
*.tfstate.backup
*.tfstate.*backup
terraform/.env
terraform/cloud-builders-community

Expand Down
86 changes: 86 additions & 0 deletions cloudbuild.yaml
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'
10 changes: 10 additions & 0 deletions docker/app-prod/Dockerfile
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"]
6 changes: 6 additions & 0 deletions scripts/build-docker.sh
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"
9 changes: 9 additions & 0 deletions scripts/deploy.sh
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
2 changes: 1 addition & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ set -e

readonly service="$1"

cd "./pkg/$service"
cd "./internal/$service"
go vet .
2 changes: 1 addition & 1 deletion scripts/openapi-js.sh
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
5 changes: 5 additions & 0 deletions terraform/.env.example
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
80 changes: 80 additions & 0 deletions terraform/Makefile
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
52 changes: 52 additions & 0 deletions terraform/README.md
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.
82 changes: 82 additions & 0 deletions terraform/cloud-run.tf
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
}
]
}
11 changes: 11 additions & 0 deletions terraform/docker-images.tf
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]
}
Loading

0 comments on commit 227e540

Please sign in to comment.