-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
153 lines (134 loc) · 4.26 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
## for Ubuntu
# setup variables
## keep the same domain name, project name and root folder name
DOMAIN = arknodejs.com
## keep the same subproject name and its pm2 process name
DIR_APP = app # a.k.a pm2 name in pm2-*.json
DIR_API = api
DIR_ADMIN = admin
DIR_PROFILE = profile
DIR_APP_ROOT = /var/www/$(DOMAIN)
DIR_NGINX = /etc/nginx
PM = apt-get
COLORS:=$(shell tput colors 2> /dev/null)
ifeq ($(COLORS), 256)
C_RESET=\033[0;39;49m
C_GREEN=\033[38;5;118m
C_BLUE=\033[38;5;81m
C_RED=\033[38;5;161m
C_PURPLE=\033[38;5;135m
C_ORANGE=\033[38;5;208m
C_YELLOW=\033[38;5;227m
C_GREY=\033[38;5;245m
C_WHITE=\033[38;5;15m
else ifeq ($(COLORS), 16)
C_RESET=\033[0;39;49m
C_GREEN=\033[0;32m
C_BLUE=\033[0;34m
C_RED=\033[0;31m
C_PURPLE=\033[0;35m
C_ORANGE=\033[1;31m
C_YELLOW=\033[0;33m
C_GREY=\033[1;30m
C_WHITE=\033[1;37m
endif
all: prep service install service-start start
prep: prep-nginx prep-certbot prep-mongo prep-redis prep-nodejs prep-pm2 prep-yarn
prep-nginx:
ifeq (, $(shell which nginx))
@echo "$(C_BLUE)nginx installing...$(C_RESET)"
@apt-get update
@apt-get install nginx
endif
@echo "$(C_GREEN)nginx installed$(C_RESET)"
@echo "$(C_BLUE)copy nginx configuration ...$(C_RESET)"
@cp -b $(DIR_APP_ROOT)/conf/$(DOMAIN) $(DIR_NGINX)/sites-available/
@ln -s $(DIR_NGINX)/sites-available/$(DOMAIN) $(DIR_NGINX)/sites-enabled/$(DOMAIN)
@echo "$(C_GREEN)nginx configured$(C_RESET)"
prep-certbot:
ifeq (, $(shell which certbot))
@echo "$(C_BLUE)certbot installing...$(C_RESET)"
@apt-get install software-properties-common
@add-apt-repository ppa:certbot/certbot
@apt-get update && apt-get install certbot
endif
@echo "$(C_GREEN)certbot installed$(C_RESET)"
@certbot certonly --standalone -d $(DOMAIN) -d api.$(DOMAIN) -d admin.$(DOMAIN) -d www.$(DOMAIN) -d profile.$(DOMAIN) -d deploy.$(DOMAIN)
@echo "$(C_GREEN)certbot certificate generated$(C_RESET)"
prep-mongo:
ifeq (, $(shell which mongod))
@echo "$(C_BLUE)mongodb installing...$(C_RESET)"
@apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
@echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
@apt-get update && apt-get install -y mongodb-org
endif
@echo "$(C_GREEN)mongodb installed$(C_RESET)"
prep-redis:
# REDIS := $(shell redis-server)
ifeq (, $(shell which redis-server))
@echo "$(C_BLUE)redis installing...$(C_RESET)"
@apt-get install redis-server
endif
@echo "$(C_GREEN)redis installed$(C_RESET)"
prep-nodejs:
ifeq (, $(shell which node))
@echo "$(C_BLUE)nodejs installing...$(C_RESET)"
@curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
@apt-get install -y nodejs
endif
@echo "$(C_GREEN)nodejs installed$(C_RESET)"
prep-pm2:
ifeq (, $(shell which pm2))
@echo "$(C_BLUE)pm2 installing...$(C_RESET)"
@npm i -g pm2
endif
@echo "$(C_GREEN)pm2 installed$(C_RESET)"
prep-yarn:
ifeq (, $(shell which yarn))
@echo "$(C_BLUE)yarn installing...$(C_RESET)"
@npm i -g yarn
endif
@echo "$(C_GREEN)yarn installed$(C_RESET)"
conf-redis:
@nano /etc/redis/redis.conf
#add:
# maxmemory 128mb
# maxmemory-policy allkeys-lru
service-start:
@nginx
@service mongod start
@systemctl restart redis-server.service
@systemctl enable redis-server.service
service-end:
@service mongod stop
@nginx -s stop
test:
@echo "$(C_BLUE)<-- nginx test start -->$(C_RESET)"
@systemctl status nginx
@nginx -t
@echo "$(C_GREEN)<-- nginx test end -->$(C_RESET)"
### exploring parallel solutions
### https://stackoverflow.com/questions/23814510/multi-threaded-make
clean:
@rm -rf $(DIR_APP_ROOT)/$(DIR_API)/node_modules
@rm -rf $(DIR_APP_ROOT)/$(DIR_APP)/node_modules
@rm -rf $(DIR_APP_ROOT)/$(DIR_ADMIN)/node_modules
@rm -rf $(DIR_APP_ROOT)/$(DIR_PROFILE)/node_modules
@echo "node_modules clean complete"
# No Build from server side, in case of node_modules version mismatch
start: start-api start-app
start-api:
@echo "start $(DIR_API)"
@cd $(DIR_APP_ROOT)/$(DIR_API) && pm2 start pm2-prod.json
start-app:
@echo "start $(DIR_APP)"
@cd $(DIR_APP_ROOT)/$(DIR_APP) && pm2 start pm2-prod.json
end:
@pm2 stop api app
kill:
@pm2 kill
version:
@nginx -v
@echo "node" && node -v
@echo "npm" && npm -v
@echo "yarn" && yarn -v