forked from ardanlabs/service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
240 lines (181 loc) · 6.97 KB
/
makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
SHELL := /bin/bash
export PROJECT = ardan-starter-kit
# ==============================================================================
# Testing running system
# For testing a simple query on the system. Don't forget to `make seed` first.
# curl --user "admin@example.com:gophers" http://localhost:3000/v1/users/token/54bb2165-71e1-41a6-af3e-7da4a0e1e2c1
# export TOKEN="COPY TOKEN STRING FROM LAST CALL"
# curl -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/v1/users/1/2
# For testing load on the service.
# hey -m GET -c 100 -n 10000 -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/v1/users/1/2
# zipkin: http://localhost:9411
# expvarmon -ports=":4000" -vars="build,requests,goroutines,errors,mem:memstats.Alloc"
# Used to install expvarmon program for metrics dashboard.
# go install github.com/divan/expvarmon@latest
# // To generate a private/public key PEM file.
# openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
# openssl rsa -pubout -in private.pem -out public.pem
# ./sales-admin genkey
# ==============================================================================
# Building containers
all: sales metrics
sales:
docker build \
-f zarf/docker/dockerfile.sales-api \
-t sales-api-amd64:1.0 \
--build-arg VCS_REF=`git rev-parse HEAD` \
--build-arg BUILD_DATE=`date -u +”%Y-%m-%dT%H:%M:%SZ”` \
.
metrics:
docker build \
-f zarf/docker/dockerfile.metrics \
-t metrics-amd64:1.0 \
--build-arg VCS_REF=`git rev-parse HEAD` \
--build-arg BUILD_DATE=`date -u +”%Y-%m-%dT%H:%M:%SZ”` \
.
# ==============================================================================
# Running from within docker compose
run: up seed
up:
docker-compose -f zarf/compose/compose.yaml -f zarf/compose/compose-config.yaml up --detach --remove-orphans
down:
docker-compose -f zarf/compose/compose.yaml down --remove-orphans
logs:
docker-compose -f zarf/compose/compose.yaml logs -f
# ==============================================================================
# Running from within k8s/dev
kind-up:
kind create cluster --image kindest/node:v1.20.2 --name ardan-starter-cluster --config zarf/k8s/dev/kind-config.yaml
kind-down:
kind delete cluster --name ardan-starter-cluster
kind-load:
kind load docker-image sales-api-amd64:1.0 --name ardan-starter-cluster
kind load docker-image metrics-amd64:1.0 --name ardan-starter-cluster
kind-services:
kustomize build zarf/k8s/dev | kubectl apply -f -
kind-update: sales
kind load docker-image sales-api-amd64:1.0 --name ardan-starter-cluster
kubectl delete pods -lapp=sales-api
kind-metrics: metrics
kind load docker-image metrics-amd64:1.0 --name ardan-starter-cluster
kubectl delete pods -lapp=sales-api
kind-logs:
kubectl logs -lapp=sales-api --all-containers=true -f
kind-status:
kubectl get nodes
kubectl get pods --watch
kind-status-full:
kubectl describe pod -lapp=sales-api
kind-shell:
kubectl exec -it $(shell kubectl get pods | grep sales-api | cut -c1-26) --container app -- /bin/sh
kind-database:
# ./admin --db-disable-tls=1 migrate
# ./admin --db-disable-tls=1 seed
kind-delete:
kustomize build zarf/k8s/dev | kubectl delete -f -
# ==============================================================================
# Administration
migrate:
go run app/sales-admin/main.go migrate
seed: migrate
go run app/sales-admin/main.go seed
# ==============================================================================
# Running tests within the local computer
test:
go test ./... -count=1
staticcheck ./...
# ==============================================================================
# Modules support
deps-reset:
git checkout -- go.mod
go mod tidy
go mod vendor
tidy:
go mod tidy
go mod vendor
deps-upgrade:
# go get $(go list -f '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}' -m all)
go get -u -t -d -v ./...
go mod tidy
go mod vendor
deps-cleancache:
go clean -modcache
list:
go list -mod=mod all
# ==============================================================================
# Docker support
FILES := $(shell docker ps -aq)
down-local:
docker stop $(FILES)
docker rm $(FILES)
clean:
docker system prune -f
logs-local:
docker logs -f $(FILES)
# ==============================================================================
# GCP
export PROJECT = ardan-starter-kit
CLUSTER = ardan-starter-cluster
DATABASE = ardan-starter-db
ZONE = us-central1-b
gcp-config:
@echo Setting environment for $(PROJECT)
gcloud config set project $(PROJECT)
gcloud config set compute/zone $(ZONE)
gcloud auth configure-docker
gcp-project:
gcloud projects create $(PROJECT)
gcloud beta billing projects link $(PROJECT) --billing-account=$(ACCOUNT_ID)
gcloud services enable container.googleapis.com
gcp-cluster:
gcloud container clusters create $(CLUSTER) --enable-ip-alias --num-nodes=2 --machine-type=n1-standard-2
gcloud compute instances list
gcp-upload:
docker tag sales-api-amd64:1.0 gcr.io/$(PROJECT)/sales-api-amd64:1.0
docker tag metrics-amd64:1.0 gcr.io/$(PROJECT)/metrics-amd64:1.0
docker push gcr.io/$(PROJECT)/sales-api-amd64:1.0
docker push gcr.io/$(PROJECT)/metrics-amd64:1.0
gcp-database:
# Create User/Password
gcloud beta sql instances create $(DATABASE) --database-version=POSTGRES_9_6 --no-backup --tier=db-f1-micro --zone=$(ZONE) --no-assign-ip --network=default
gcloud sql instances describe $(DATABASE)
gcp-db-assign-ip:
gcloud sql instances patch $(DATABASE) --authorized-networks=[$(PUBLIC-IP)/32]
gcloud sql instances describe $(DATABASE)
gcp-db-private-ip:
# IMPORTANT: Make sure you run this command and get the private IP of the DB.
gcloud sql instances describe $(DATABASE)
gcp-services:
# The zarf/k8s/stg/stg-config.yaml file needs the private IP of the database.
kustomize build zarf/k8s/stg | kubectl apply -f -
# kubectl create -f deploy-sales-api.yaml
# kubectl expose -f expose-sales-api.yaml --type=LoadBalancer
gcp-status:
gcloud container clusters list
kubectl get nodes
kubectl get pods
kubectl get services sales-api
gcp-logs:
kubectl logs -lapp=sales-api --all-containers=true -f
gcp-shell:
kubectl exec -it $(shell kubectl get pods | grep sales-api | cut -c1-26 | head -1) --container app -- /bin/sh
# ./admin --db-disable-tls=1 migrate
# ./admin --db-disable-tls=1 seed
gcp-delete:
kubectl delete services sales-api
kubectl delete deployment sales-api
gcloud container clusters delete $(CLUSTER)
gcloud projects delete sales-api
gcloud container images delete gcr.io/$(PROJECT)/sales-api-amd64:1.0 --force-delete-tags
gcloud container images delete gcr.io/$(PROJECT)/metrics-amd64:1.0 --force-delete-tags
docker image remove gcr.io/sales-api/sales-api-amd64:1.0
docker image remove gcr.io/sales-api/metrics-amd64:1.0
#===============================================================================
# GKE Installation
#
# Install the Google Cloud SDK. This contains the gcloud client needed to perform
# some operatings
# https://cloud.google.com/sdk/
#
# Installing the K8s kubectl client.
# https://kubernetes.io/docs/tasks/tools/install-kubectl/