-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbot.sh
More file actions
executable file
·167 lines (96 loc) · 2.53 KB
/
bot.sh
File metadata and controls
executable file
·167 lines (96 loc) · 2.53 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
#!/bin/bash
# --------------------------- #
cd $(dirname "$(readlink -f "$0")")
# --------------------------- #
ARTIFACT="td-pm-bot"
MODULE="bot"
SERVICE_NAME="td-pm"
JAVA_ARGS=""
ARGS=""
CONFIG_TEMPLATE="_pm.yml"
CONFIG_FILE="pm.yml"
[ -f "bot.conf" ] && source "bot.conf"
# --------------------------- #
info() { echo "I: $*"; }
error() {
echo "E: $*"
exit 1
}
if [ ! "$1" ]; then
echo "bash $0 [ init | update | run | log | start | stop | ... ]"
exit
fi
if [ "$1" == "init" ]; then
echo ">> 写入服务"
cat >/etc/systemd/system/$SERVICE_NAME.service <<EOF
[Unit]
Description=Telegram Bot ($SERVICE_NAME)
After=network.target
Wants=network.target
[Service]
Type=simple
WorkingDirectory=$(readlink -e ./)
ExecStart=/bin/bash $0 run
Restart=on-failure
RestartPreventExitStatus=100
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
echo ">> 写入启动项"
systemctl enable $SERVICE_NAME &>/dev/null
echo "<< 完毕."
exit
elif [ "$1" == "config" ]; then
[ -f $CONFIG_FILE ] || cp $CONFIG_TEMPLATE $CONFIG_FILE
editor $CONFIG_FILE
elif [ "$1" == "run" ]; then
shift
EXEC="$MODULE/build/install/main/bin/main"
[ -x "$EXEC" ] || bash $0 rebuild || exit 1
if [ ! -x "$*" ]; then
exec $EXEC $@
else
exec $EXEC $ARGS
fi
elif [ "$1" == "start" ]; then
systemctl start $SERVICE_NAME
bash $0 log
elif [ "$1" == "restart" ]; then
systemctl restart $SERVICE_NAME &
bash $0 log
elif [ "$1" == "force-update" ]; then
git fetch &>/dev/null
git reset --hard FETCH_HEAD
elif [ "$1" == "rebuild" ]; then
git submodule update --init --force --recursive
./gradlew ${MODULE}:installDist || exit 1
elif [ "$1" == "update" ]; then
git fetch &>/dev/null
if [ "$(git rev-parse HEAD)" = "$(git rev-parse FETCH_HEAD)" ]; then
echo "<< 没有更新"
exit 1
fi
echo ">> 检出更新 $(git rev-parse FETCH_HEAD)"
git reset --hard FETCH_HEAD
shift
./gradlew ${MODULE}:clean
bash $0 rebuild $@
exit $?
elif [ "$1" == "upgrade" ]; then
git fetch &>/dev/null
if [ "$(git rev-parse HEAD)" = "$(git rev-parse FETCH_HEAD)" ]; then
echo "<< 没有更新"
exit 1
fi
echo ">> 检出更新 $(git rev-parse FETCH_HEAD)"
git reset --hard FETCH_HEAD
shift
bash $0 rebuild $@ && bash $0 restart
elif [ "$1" == "log" ]; then
exec journalctl -u $SERVICE_NAME -o short --no-hostname -f -n 40
elif [ "$1" == "logs" ]; then
exec journalctl -u $SERVICE_NAME -o short --no-hostname --no-tail -e
else
systemctl "$1" $SERVICE_NAME
fi