-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathsetup.sh
291 lines (250 loc) · 7.71 KB
/
setup.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
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
#!/bin/bash
python_bin_path="$(which python3)"
venv_dir="venv"
project_path="$(pwd)"
service_name="tgbotyh"
source .env
google_gemini_api_key="${Google_Gemini_API_Key}"
telegram_bot_token="${Telegram_Bot_Token}"
anthropic_api_key="${Anthropic_API_Key}"
openai_api_key="${Openai_API_Key}"
yi_api_key="${Yi_API_Key}"
yi_base_url="${Yi_Base_Url}"
if [ -n "$Python_Bin_Path" ]; then
python_bin_path="$Python_Bin_Path"
fi
if [ -n "$Python_Venv_Path" ]; then
venv_dir="${Python_Venv_Path}"
fi
sudoCmd=""
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
sudoCmd="sudo"
fi
# Check Virtual Environment exist
if [ -d "$venv_dir" ]; then
echo "Virtual Environment already exist"
exit 1
fi
# created virtual environment
$python_bin_path -m venv "$venv_dir"
if [ $? -eq 0 ]; then
echo "Successfully created virtual environment."
else
echo "Failed to create virtual environment."
fi
source $venv_dir/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
osSystemMdPath="/lib/systemd/system/"
# Chcek OS release distribution
function getLinuxOSRelease(){
if [[ -f /etc/redhat-release ]]; then
osRelease="centos"
osSystemPackage="yum"
osSystemMdPath="/usr/lib/systemd/system/"
elif cat /etc/issue | grep -Eqi "debian|raspbian"; then
osRelease="debian"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
osRelease="ubuntu"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
osRelease="centos"
osSystemPackage="yum"
osSystemMdPath="/usr/lib/systemd/system/"
elif cat /proc/version | grep -Eqi "debian|raspbian"; then
osRelease="debian"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
elif cat /proc/version | grep -Eqi "ubuntu"; then
osRelease="ubuntu"
osSystemPackage="apt-get"
osSystemMdPath="/lib/systemd/system/"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
osRelease="centos"
osSystemPackage="yum"
osSystemMdPath="/usr/lib/systemd/system/"
fi
}
function installPythonVirtualEnv(){
echo
echo "=============================="
echo "Prapare to Install telegram bot"
echo
echo "Project path: $project_path"
echo "Python bin path: $python_bin_path"
echo "Google_Gemini_API_Key: $Google_Gemini_API_Key"
echo "Telegram Bot Token: $Telegram_Bot_Token"
echo "Anthropic API Key: $Anthropic_API_Key"
echo "Openai API Key: $Openai_API_Key"
echo "Yi API Key: $Yi_API_Key"
echo "Yi Base Url: $Yi_Base_Url"
echo "=============================="
echo
# Check Virtual Environment exist
if [ -d "$venv_dir" ]; then
echo "Virtual Environment already exist"
if [ -z "$1" ]; then
exit 1
else
source $venv_dir/bin/activate
fi
else
# created virtual environment
echo "Creating virtual environment..."
$python_bin_path -m venv "$venv_dir"
if [ $? -eq 0 ]; then
echo "Successfully created virtual environment."
source $venv_dir/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
else
echo "Failed to create virtual environment."
exit 1
fi
fi
}
function installSystemd(){
installPythonVirtualEnv
cat > ${osSystemMdPath}${service_name}.service <<-EOF
[Unit]
Description=$service_name service
After=network.target
[Service]
User=root
Group=root
Environment="GOOGLE_GEMINI_KEY=${google_gemini_api_key}"
Environment="ANTHROPIC_API_KEY=${anthropic_api_key}"
Environment="OPENAI_API_KEY=${openai_api_key}"
Environment="YI_API_KEY=${yi_api_key}"
Environment="YI_BASE_URL=${yi_base_url}"
WorkingDirectory=$project_path
ExecStart=$project_path/venv/bin/python $project_path/tg.py "${telegram_bot_token}"
Restart=on-failure
RestartSec=30
[Install]
WantedBy=multi-user.target
EOF
${sudoCmd} chmod +x ${osSystemMdPath}${service_name}.service
${sudoCmd} systemctl daemon-reload
${sudoCmd} systemctl enable ${service_name}.service
${sudoCmd} systemctl start ${service_name}.service
echo
echo "${service_name}.service running successfully"
echo
echo "Run following command to start / stop telegram bot"
echo "Start: systemctl start ${service_name}.service"
echo "Stop: systemctl stop ${service_name}.service"
echo "Check running status: systemctl status ${service_name}.service"
echo "=============================="
}
function uninstallSystemd(){
${sudoCmd} systemctl stop ${service_name}.service
${sudoCmd} systemctl disable ${service_name}.service
${sudoCmd} rm -rf ${osSystemMdPath}${service_name}.service
${sudoCmd} systemctl daemon-reload
}
function installCommandLine(){
installPythonVirtualEnv "noexit"
echo
echo "=============================="
export GOOGLE_GEMINI_KEY=$google_gemini_api_key
export ANTHROPIC_API_KEY=$anthropic_api_key
export OPENAI_API_KEY=$openai_api_key
export YI_API_KEY=$yi_api_key
export YI_BASE_URL=$yi_base_url
python tg.py "${telegram_bot_token}"
}
function runSystemd(){
echo
if [ "$1" == "start" ]; then
echo "systemctl start ${service_name}.service"
${sudoCmd} systemctl start ${service_name}.service
elif [ "$1" == "restart" ]; then
echo "systemctl restart ${service_name}.service"
${sudoCmd} systemctl restart ${service_name}.service
elif [ "$1" == "stop" ]; then
echo "systemctl stop ${service_name}.service"
${sudoCmd} systemctl stop ${service_name}.service
elif [ "$1" == "status" ]; then
echo "systemctl status ${service_name}.service"
${sudoCmd} systemctl status ${service_name}.service
else
echo "journalctl -n 30 -u ${service_name}.service "
${sudoCmd} journalctl -n 30 -u ${service_name}.service
fi
echo
}
function start_menu(){
clear
echo "=============================="
echo " 1. Install telegram bot and Run with Systemd Service"
echo " 2. Install and Run with Command Line"
echo
echo " 3. Uninstall telegram bot and Systemd Service"
echo
echo " 4. Restart ${service_name} Systemd Service"
echo " 5. Stop ${service_name} Systemd Service"
echo " 6. Check Status of ${service_name} Systemd Service"
echo " 7. Show Log of ${service_name} Systemd Service"
echo
echo " 0. exit"
echo
read -r -p "Please input number:" menuNumberInput
case "$menuNumberInput" in
1 )
installSystemd
;;
2 )
installCommandLine
;;
3 )
uninstallSystemd
;;
4 )
runSystemd "restart"
;;
5 )
runSystemd "stop"
;;
6 )
runSystemd "status"
;;
7 )
runSystemd
;;
0 )
exit 1
;;
* )
clear
echo "Please input correct number !"
sleep 2s
start_menu
;;
esac
}
function showMenu(){
if [ -z "$1" ]; then
start_menu
elif [ "$1" == "1" ]; then
installSystemd
elif [ "$1" == "2" ]; then
installCommandLine
elif [ "$1" == "3" ]; then
uninstallSystemd
elif [ "$1" == "4" ]; then
runSystemd "restart"
elif [ "$1" == "5" ]; then
runSystemd "stop"
elif [ "$1" == "6" ]; then
runSystemd "status"
elif [ "$1" == "7" ]; then
runSystemd
else
start_menu
fi
}
showMenu $1