Repository navigation
Expand file tree
/
Copy pathamumu
More file actions
executable file
·462 lines (374 loc) · 9.66 KB
/
Copy pathamumu
File metadata and controls
executable file
·462 lines (374 loc) · 9.66 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/bash
# Copyright (c) 2012 Yuki KAN and Chinachu Project Contributors
# https://chinachu.moe/
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Amumu-client CLI
# ------------
#
# Usage:
#
# ./amumu <cmd> ...
#
NODE_VER=8.1.1
NAVE_VER=2.0.0
main () {
local SELF_PATH DIR SYM
# get the absolute path of the executable
SELF_PATH="$0"
if [ "${SELF_PATH:0:1}" != "." ] && [ "${SELF_PATH:0:1}" != "/" ]; then
SELF_PATH=./"$SELF_PATH"
fi
SELF_PATH=$( cd -P -- "$(dirname -- "$SELF_PATH")" \
&& pwd -P \
) && SELF_PATH=$SELF_PATH/$(basename -- "$0")
# resolve symlinks
while [ -h "$SELF_PATH" ]; do
DIR=$(dirname -- "$SELF_PATH")
SYM=$(readlink -- "$SELF_PATH")
SELF_PATH=$( cd -- "$DIR" \
&& cd -- $(dirname -- "$SYM") \
&& pwd \
)/$(basename -- "$SYM")
done
# path
AMUMU_DIR=$(dirname -- "$SELF_PATH")
NAVE_DIR=${AMUMU_DIR}/.nave
NODE_PATH=${NAVE_DIR}/installed/${NODE_VER}/bin/node
NPM_PATH=${NAVE_DIR}/installed/${NODE_VER}/bin/npm
MONGO_DIR=${AMUMU_DIR}/.mongo
DB_PATH=${AMUMU_DIR}/data
export PATH=${AMUMU_DIR}/bin:${MONGO_DIR}/bin:$NAVE_DIR:${NAVE_DIR}/installed/${NODE_VER}/bin:$PATH
cd $AMUMU_DIR
export AMUMU_DIR
export NAVE_DIR
local cmd="$1"
shift
case $cmd in
client | service | installer | setup)
cmd="amumu_$cmd"
;;
* )
cmd="amumu_help"
;;
esac
$cmd "$@" && exit 0 || fail "failed somehow"
}
amumu_client () {
if ! [ -f "client_config.json" ]; then
cp -v client/client_config.sample.json client_config.json
fi
ensure_dir log
node client/amumu_client.js "$1" > /dev/stdout 2>&1 | tee -a ./log/client
return ${PIPESTATUS[0]}
}
amumu_setup () {
local cmd="$1"
shift
case $cmd in
encode | db)
cmd="amumu_setup_$cmd"
;;
* )
cmd="amumu_setup_help"
;;
esac
$cmd "$@" && return 0
}
amumu_setup_encode () {
recorded="$1"
if [ -z ${recorded} ] || ! [ -f ${recorded} ]; then
echo "file ${recorded} not found"
return 1
fi
count=`ps aux | grep mongod | grep -v grep | wc -l`
if [ ${count} = 0 ]; then
amumu_db_start
fi
tmp=${AMUMU_DIR}/tmp.json
cat ${recorded} | jq 'map(select( .recorded | contains(".m2ts")))' > ${tmp}
len=$(cat ${tmp} | jq length)
for i in $( seq 0 $(($len - 1)) ); do
raw=$(cat ${tmp} | jq .[$i])
recorded=$(echo ${raw} | jq '.recorded')
if eval [ -f "${recorded}" ]; then
amumu_client "${raw}"
fi
done
return 0
}
amumu_setup_db () {
echo "Starting Mongodb..."
ensure_dir "${DB_PATH}"
mongod --fork --logpath ${DB_PATH}/mongod.log --dbpath ${DB_PATH} --port 10782 --bind_ip_all
echo "done"
return 0
}
amumu_service () {
local cmd name action
name="$1"
action="$2"
case $action in
initscript | execute )
case $name in
proxy | encoder )
cmd="amumu_service_$action"
;;
esac
;;
* )
cmd="amumu_service_help"
;;
esac
$cmd "$name" && return 0
}
amumu_service_initscript () {
local name="$1"
cat <<EOF
#!/bin/bash
# /etc/
### BEGIN INIT INFO
# Provides: amumu-${name}
# Required-Start: \$local_fs \$remote_fs \$network \$syslog
# Required-Stop: \$local_fs \$remote_fs \$network \$syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the amumu-${name}
# Description: starts the amumu-${name} (USER=$USER)
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=${AMUMU_DIR}/amumu
DAEMON_OPTS="service ${name} execute"
NAME=amumu-${name}
USER=$USER
PIDFILE=/var/run/\${NAME}.pid
test -x \$DAEMON || exit 0
start () {
echo -n "Starting \${NAME}: "
if [ -f \$PIDFILE ]; then
PID=\`cat \$PIDFILE\`
if [ -z "\`ps axf | grep \${PID} | grep -v grep\`" ]; then
rm -f \$PIDFILE
else
echo "\${NAME} is already running? (pid=\${PID})"
exit
fi
fi
PID=\$(su \$USER -c "exec \$DAEMON \$DAEMON_OPTS < /dev/null > /dev/null 2>&1 & echo \\\$!")
if [ -z \$PID ]; then
echo "Failed!"
exit
else
echo \$PID > \$PIDFILE
fi
echo "OK."
}
stop () {
echo -n "Stopping \${NAME}: "
if [ -f \$PIDFILE ]; then
PID=\`cat \$PIDFILE\`
PGID=\`ps -p \$PID -o pgrp | grep -v PGRP\`
kill -QUIT -\`echo \$PGID\`
echo "OK."
rm -f \$PIDFILE
else
echo "\${NAME} is not running? (\${PIDFILE} not found)."
fi
}
status () {
if [ -f \$PIDFILE ]; then
PID=\`cat \$PIDFILE\`
if [ -z "\`ps axf | grep \${PID} | grep -v grep\`" ]; then
echo "\${NAME} is dead but \${PIDFILE} exists."
else
echo "\${NAME} is running."
fi
else
echo "\${NAME} is NOT running."
fi
}
case "\$1" in
start )
start "\$@"
;;
stop )
stop "\$@"
;;
restart )
stop "\$@"
sleep 3
start "\$@"
;;
status )
status "\$@"
;;
*)
echo "Usage: \$NAME {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
EOF
return 0
}
amumu_service_execute () {
local name="$1"
if ! [ -f "${name}_config.json" ]; then
cp -v ${name}/${name}_config.sample.json ${name}_config.json
fi
ensure_dir log
node ${name}/amumu_${name}.js > /dev/stdout 2>&1 | tee -a ./log/${name}
return ${PIPESTATUS[0]}
}
amumu_service_help () {
cat <<EOF
Usage: ./amumu service <name> <action>
Names:
proxy amumu Proxy Server Service
encoder amumu Encode Server Service
Actions:
initscript Output a LSB init script
execute Execute
Examples:
# Create a LSB init script and Install to system
./amumu service proxy initscript > /tmp/amumu-proxy
./amumu service encoder initscript > /tmp/amumu-encoder
sudo chown root:root /tmp/amumu-proxy /tmp/amumu-encoder
sudo chmod +x /tmp/amumu-proxy /tmp/amumu-encoder
sudo mv /tmp/amumu-proxy /tmp/amumu-encoder /etc/init.d/
EOF
return 0
}
amumu_installer () {
echo "Amumu Installer Menu:"
echo "[!] These are installed under all ${AMUMU_DIR}/..."
echo "[!] Recommend the Auto installation."
local PS3 item cmd
PS3="what do you install? > "
select item in \
"Auto (full)" "Node.js Environment and Modules" "mongodb"
do
echo "selected: ${item}"
case $item in
"Auto (full)" )
cmd="amumu_installer_auto_full"
;;
"Node.js Environment and Modules" )
cmd="amumu_installer_node_full"
;;
"mongodb" )
cmd="amumu_installer_mongodb"
;;
* )
cmd="amumu_installer"
;;
esac
$cmd && break
done
return 0
}
amumu_installer_auto_full () {
amumu_installer_node
amumu_installer_node_modules
amumu_installer_mongodb
return 0
}
amumu_installer_node_full () {
amumu_installer_node
amumu_installer_node_modules
return 0
}
amumu_installer_node () {
remove_dir "$NAVE_DIR"
ensure_dir "$NAVE_DIR"
echo "Installing Node using Nave..."
if [ -f "/etc/alpine-release" ]; then
nave-alpine.sh install $NODE_VER
else
wget -O - https://github.com/isaacs/nave/archive/v${NAVE_VER}.tar.gz | tar zxvf - -C $NAVE_DIR nave-${NAVE_VER}/nave.sh
mv ${NAVE_DIR}/nave-${NAVE_VER}/nave.sh ${NAVE_DIR}/
rm -rfv $NAVE_DIR/nave-${NAVE_VER}
${NAVE_DIR}/nave.sh install $NODE_VER
fi
rm -fv ${NAVE_DIR}/node
ln -sv $NODE_PATH ${NAVE_DIR}/node
ln -sv $NPM_PATH ${NAVE_DIR}/npm
echo "done."
return 0
}
amumu_installer_node_modules () {
echo "Installing Node Modules using NPM..."
npm install
npm update
echo "done."
return 0
}
amumu_installer_mongodb () {
remove_dir "$MONGO_DIR"
ensure_dir "$MONGO_DIR"
echo "Installing mongodb"
wget -O - https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz | tar zxvf -
cp -R -n mongodb-linux-x86_64-3.4.6/* ${MONGO_DIR}
rm -rfv mongodb-linux-x86_64-3.4.6/
echo "done."
return 0
}
ensure_dir () {
if ! [ -d "$1" ]; then
mkdir -p -- "$1" || fail "couldn't create $1"
fi
}
remove_dir () {
if [ -d "$1" ]; then
rm -rf -- "$1" || fail "couldn't remove $1"
echo "removed \`$1'"
fi
}
amumu_help () {
cat <<EOF
Usage: ./amumu <cmd> ...
Commands:
installer Install binaries
client <json> Run a client.
service <name> <action> Service-utility.
setup <req> Setup-utility.
help Output this information.
json:
amumu need keys 'id' and 'recorded' on chinachu.
amumu do not use other keys.
exemple:
{
"id": "0000id",
"recorded": "/usr/local/chinachu/recorded/hoge.m2ts",
}
EOF
return 0
}
amumu_setup_help () {
cat <<EOF
Usage: ./amumu setup <cmd> ...
Commands:
encode <path/to/recorded.json> Queue encode and run a mongodb if not run.
db Run a mongodb.
help Output this information.
EOF
return 0
}
main "$@"