-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
208 lines (153 loc) · 6.68 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
# main app phonies
.PHONY: deploy build run prep-do clear clean
# -------------------------------------------------------------------------
# deploy from local with production config to remote
# just select ip address from digital ocean droplet
# first run in cli to omit ssh questions $ "ssh-add"
# call like: $ make deploy dir=$(pwd) ip=64.225.104.7
deploy: prep-do
ssh root@$(ip) "cd /webgrinch; make build run"
# build container image from container manifest file
build:
docker build -t webgrinch-alpha -f ./build/container-image/Dockerfile .
# run container based on an image
run:
docker run --restart=always -d \
-v $(shell readlink -f /etc/letsencrypt/live/webgrinch.8qfx1ai5.de/cert.pem):/certs/cert.pem \
-v $(shell readlink -f /etc/letsencrypt/live/webgrinch.8qfx1ai5.de/privkey.pem):/certs/privkey.pem \
-p "80:80" \
-p "443:443" \
webgrinch-alpha
# prepare droplet on digital ocean from remote
# call like: make prep-do dir=$(pwd) ip=64.225.104.7
prep-do: clean
ssh root@$(ip) "apt-get update; apt-get -y upgrade; apt-get -y install docker.io; systemctl start docker; systemctl enable docker"
#ssh root@$(ip) "mkdir webgrinch"
rsync -v --archive --delete --exclude=.git* --compress $(dir) root@$(ip):/
#scp -r $(dir) root@$(ip):/
ssh root@$(ip) "apt-get -y install make; cd /webgrinch; make clear"
# stop and remove all docker container
# call like: make deploy dir=$(pwd) ip=64.225.104.7
clear:
-docker stop `docker ps -a -q` 2>/dev/null
-docker rm `docker ps -a -q` 2>/dev/null
#docker system prune -f
# remove temp files
clean:
find . -type f -name '*.tmp.*' -print0 | xargs -0 rm
find . -type f -name 'webgrinch*' -print0 | xargs -0 rm
find . -type f -name '*.test' -print0 | xargs -0 rm
rm -rf tmp
# main app development phonies
.PHONY: rundev serve login loginforce access ps runtapid
# -------------------------------------------------------------------------
# run container for dev local based on an image
rundev:
docker run --rm -d --name api \
-v $(shell pwd)/tmp/certs:/certs \
-p "80:80" \
-p "443:443" \
webgrinch-alpha
# run local api tests
# call like: make runt url=http://api/api
# docker run --rm --link api -t postman/newman run
# docker run --rm --link api newman run /Contract_Tests.postman_collection.json --env-var baseUrl=http://api/api
runtapid:
docker build -t newman -f ./build/container-image-newman/Dockerfile .
docker run --rm --link api newman run /Contract_Tests.postman_collection.json --env-var baseUrl=http://api/api
# run service on local docker env for development
serve: clean clear tls build rundev itestd
# ssh to digital ocean
# call like: make access ip=64.225.104.7
access:
ssh -t root@$(ip) "cd /webgrinch ; bash"
# get shell inside of the first running docker container
# call like: make login
login:
docker exec -it `docker ps -a -q | head -n 1` /bin/sh
# get shell inside of a stoped docker container
# call like: make loginforce
loginforce:
docker run -it --env TLSCERT --env TLSCERTKEY --entrypoint /bin/sh webgrinch-alpha -s
# list all docker containers
ps:
docker ps -a
# test phonies
.PHONY: utest utestd itestd itest btest test
# -------------------------------------------------------------------------
# run go unit tests
utest:
docker build -t utest -f build/container-image-utest/Dockerfile .
docker run utest
# run go unit tests during deploy
utestd:
@echo "RUN go unit tests..."
@go test -count=1 ./internal/... | egrep "^(FAIL.|ok)" | sed ''/ok/s//`printf "\033[32mok\033[0m"`/'' | sed ''/FAIL/s//`printf "\033[31mFAIL\033[0m"`/'' | sed ''/?/s//`printf "\033[33m?\033[0m"`/''
# run go integration tests during deploy
itestd:
@echo "RUN go integration tests..."
@sleep 2 # starting the server is to slow
@go test -count=1 ./test/... | egrep "^(FAIL.|ok)" | sed ''/ok/s//`printf "\033[32mok\033[0m"`/'' | sed ''/FAIL/s//`printf "\033[31mFAIL\033[0m"`/'' | sed ''/?/s//`printf "\033[33m?\033[0m"`/''
# run go integration tests during deploy
itest:
@echo "RUN go integration tests..."
@go test -v -count=1 ./test/... | sed ''/PASS/s//`printf "\033[32mPASS\033[0m"`/'' | sed ''/ok/s//`printf "\033[32mok\033[0m"`/'' | sed ''/FAIL/s//`printf "\033[31mFAIL\033[0m"`/'' | sed ''/?/s//`printf "\033[33m?\033[0m"`/''
# run go benchmark tests
btest:
cd internal/encode; go test -v -count=1 -bench=Encoding -cpuprofile=cpu.tmp.out
cd internal/encode; go tool pprof cpu.tmp.out
#web > ../../test/results/web_result.tmp.svg
#top50 > ../../test/results/top50_result.tmp.txt
# run all tests
test:
go test -count=1 ./... | egrep "^(FAIL.|ok|?)" | sed ''/ok/s//`printf "\033[32mok\033[0m"`/'' | sed ''/FAIL/s//`printf "\033[31mFAIL\033[0m"`/'' | sed ''/?/s//`printf "\033[33m?\033[0m"`/''
# service phonies
.PHONY: swagger sass tls cirunner le-list le-create le-renew
# -------------------------------------------------------------------------
# recreate swagger static content from the files
swagger:
go get github.com/rakyll/statik
cd third_party/swagger-ui; statik -src=`pwd`/dist
# run sass conversion
sass:
cd build/container-image-sass; docker build -t sass .
cat web/static/example/scss/main.scss | docker run -i sass > web/static/example/css/main.css
# create self signed certificate for local development
tls:
cd build/container-image-tls; docker build -t tls .
mkdir -p tmp/certs
docker run -i tls cert.pem > tmp/certs/cert.pem
docker run -i tls privkey.pem > tmp/certs/privkey.pem
# list all the registered certificates on the system from letsencrypt
# run on remote host
le-list:
sudo docker run -it --rm --name certbot \
-v "/etc/letsencrypt:/etc/letsencrypt" \
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
certbot/certbot certificates
# create a new letsencrypt certificate (only if you do not have an old one)
# run on remote host
le-create:
sudo docker run -it --rm --name certbot \
-p 80:80 \
-p 443:443 \
-v "/etc/letsencrypt:/etc/letsencrypt" \
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
certbot/certbot certonly --standalone
# renew a letsencrypt certificate specified by name (also see list certificates)
# run on remote host
le-renew:
# stop the current running server (downtime)
-docker stop `docker ps -a -q`
# renew the cert
sudo docker run -it --rm --name certcdbot \
-p "80:80" \
-p "443:443" \
-v "/etc/letsencrypt:/etc/letsencrypt" \
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
certbot/certbot certonly --standalone --force-renew --cert-name webgrinch.8qfx1ai5.de
# restart the server
# -make run
## if you want to deploy to the server and for ssh access, you maybe need to update your .ssh/known_hosts file
#
# auto renew is triggerd by cron "sudo crontab -e"