-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_adm.sh
263 lines (240 loc) · 5.56 KB
/
node_adm.sh
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
cmd=$1
cname=$2
container_name=$3
id=
config_file='docker_cid.cnf'
foo='bar'
# config
deploy_folder='deploy/*'
re_read_config() {
docker_log='/var/lib/docker/containers/'$id'*/'$id'*-json.log'
}
usage() {
echo
echo " Usage: $(basename ${0}) <command> <cname>"
echo
echo " h, help show help usage"
echo
exit 1
}
help() {
echo
echo " Shell for maintenance nodejs-api: optimize for Docker"
echo " Usage: $(basename ${0}) <command> <cname>"
echo
echo " <command>:"
echo " deploy do cp, build, restart the container"
echo " start start container"
echo " stop stop container"
echo " build build source code in src/ to build/ in container"
echo " restart restart container"
echo " cp copy all files in deploy/ folder to app/ in docker container process"
echo " log tail log docker container"
echo " cplog copy logs/ folder from docker container to current directory"
echo " create <version> create new docker container with settings from env_new_container.sh"
echo " <version> - nodejs-api image version ex. 19.01.DB.06"
echo " config <cid> create config file with container id for default use"
echo
echo " <cname>:"
echo " \$name target container name (if no file docker_cid.cnf provide)"
echo
echo " Docker console:"
echo " checklog check docker console log file size in MB (Local)"
echo " truncate truncate/clean docker console log file (when is too large)"
echo
}
get_docker_cid_from_name() {
echo 'finding container name ['$1']..';
id=$(docker ps -a | grep $1 | awk '{print $1}')
}
read_config() {
if [ -z "$cname" ]
then
if [ -e "$config_file" ] && [ -f "$config_file" ]
then
echo 'Get container id from '$config_file
id=$(<$config_file)
else
echo 'No file '$config_file', please create config or pass container name instead'
exit 1
fi
else
echo 'Get container id from name ['$cname']';
get_docker_cid_from_name $cname
fi
if [ -z "$id" ]
then
echo 'Cannot get id from container name..'
exit 1
else
echo 'Current container id ['$id']'
re_read_config
fi
}
cp() {
echo 'Copy deploy/. to app/ '
docker cp $deploy_folder $id:app/
echo 'Restarting Container...'
docker restart $id
}
log() {
echo 'tail console log container..'
docker logs -f $id
}
cplog() {
echo 'Copy app/logs/ from container to current dir'
docker cp $id:app/logs/ .
}
checklog() {
ls -l $docker_log
if [ -e "$docker_log" ] && [ -f "$docker_log" ]
then
echo '-> File size..'
du -msh $docker_log
echo -e '-----------------------------';
else
echo 'No file in '$docker_log'..'
fi
}
trun_fnc() {
echo 'truncate -s 0 /var/lib/docker/containers/'$id'/'$id'-json.log'
if [ -e "$docker_log" ] && [ -f "$docker_log" ]
then
truncate -s 0 $docker_log
else
echo 'No file in '$docker_log'..'
fi
}
start() {
docker start $id
}
stop() {
docker stop $id
}
build() {
docker exec -it $id npm run build
}
restart() {
docker restart $id
}
create_container() {
if [ -z "$cname" ]
then VERSION=''
else VERSION=':'$cname
fi
if [ -z "$container_name" ]
then container_name='nodejs-api'
fi
echo
echo "Create container name [$container_name]"
# Set ENV
echo 'Exporting environment for create container...'
chmod +x env_new_container.sh
. env_new_container.sh
echo 'Test echo $RB_NODE_ENV => ['$RB_NODE_ENV']'
# Run Container
docker run -it -d \
-e "NODE_ENV=$RB_NODE_ENV" \
-e "WRITEDATALOGCONSOLE=$RB_WRITEDATALOGCONSOLE" \
-e "WRITEDBLOGCONSOLE=$RB_WRITEDBLOGCONSOLE" \
-e "WRITENODELOGCONSOLE=$RB_WRITENODELOGCONSOLE" \
-e "ENABLEDB2=$RB_ENABLEDB2" \
-e "REPLYWAITTIME=$RB_REPLYWAITTIME" \
-e "PAGESIZE=$RB_PAGESIZE" \
-e "AMQPURI=$RB_AMQPURI" \
-e "RPCQUEUE=$RB_RPCQUEUE" \
-e "DBCODEPAGE=$RB_DBCODEPAGE" \
-e "DBHOST=$RB_DBHOST" \
-e "DBPORT=$RB_DBPORT" \
-e "DBNAME=$RB_DBNAME" \
-e "DBTABLE=$RB_DBTABLE" \
-e "DBUSER=$RB_DBUSER" \
-e "DBPWD=$RB_DBPWD" \
-e "RBHOSTNAME=$RB_RBHOSTNAME" \
-e "RBPORT=$RB_RBPORT" \
-e "SELFQUEUE=$RB_SELFQUEUE" \
-e "DIRECTQUEUE=$RB_DIRECTQUEUE" \
--name $container_name \
-p 15673:15673 \
fwg/nodejs-api$VERSION \
npm start
}
create_config() {
if [ -e "$config_file" ] && [ -f "$config_file" ]
then
echo 'replace new container id to file '$config_file'..'
#truncate -s 0 $config_file
echo $cname > $config_file
else
echo 'create new config file '$config_file'..'
#touch $config_file
echo $cname > $config_file
fi
echo 'save config file done..'
}
if [ -z "$foo" ]
then
echo 'Not start shell'
else
# for those not sent parameter $2 as container id
case $cmd in
h|help)
help
exit 1
;;
config|cnf)
create_config
exit 1
;;
create)
create_container
exit 1
;;
'')
usage
exit 1
;;
esac
echo
# for those who want use default container id or pass param $2
read_config
case $cmd in
d|deploy)
cp
build
restart
;;
s|start)
start
;;
st|stop)
stop
;;
b|build)
build
;;
r|restart)
restart
;;
cp)
cp
;;
log)
log
;;
cplog)
cplog
;;
checklog)
checklog
;;
trun|truncate)
trun_fnc
;;
*)
usage
exit 1
;;
esac
echo 'Exit shell..'
fi