-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·112 lines (93 loc) · 2.43 KB
/
install.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
#!/bin/bash
SCRIPT_PATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
ROOT_PATH="$( cd "${SCRIPT_PATH}/.." >/dev/null 2>&1; pwd -P )"
ENV_PATH="${HOME}/klipper-tg-bot-env"
Red='\033[0;31m'
Green='\033[0;32m'
Cyan='\033[0;36m'
Normal='\033[0m'
echo_text()
{
printf "${Normal}$1${Cyan}\n"
}
echo_error()
{
printf "${Red}$1${Normal}\n"
}
echo_ok()
{
printf "${Green}$1${Normal}\n"
}
install_packages()
{
python3 -m pip install --upgrade pip
if [ $? -eq 0 ]; then
echo_ok "Installed pip"
else
echo_error "Installation of pip failed"
fi
pip3 install virtualenv
if [ $? -eq 0 ]; then
echo_ok "Installed virtualenv"
else
echo_error "Installation of virtualenv failed"
fi
}
create_virtualenv()
{
echo_text "Creating virtual environment"
if [ ! -d "${ENV_PATH}" ]; then
virtualenv -p /usr/bin/python3 --system-site-packages "${ENV_PATH}"
fi
source "${ENV_PATH}/bin/activate"
while read requirements; do
pip --disable-pip-version-check --no-cache-dir install $requirements
if [ $? -gt 0 ]; then
echo "Error: pip install exited with status code $?"
echo "Unable to install dependencies, aborting install."
deactivate
exit 1
fi
done < ${ROOT_PATH}/requirements.txt
deactivate
echo_ok "Virtual enviroment created"
}
create_default_config()
{
echo_text "Create default config"
if [ ! -f "${HOME}/klipper-tg-bot.conf" ]; then
cp "${ROOT_PATH}/klipper-tg-bot.conf" "${HOME}/klipper-tg-bot.conf"
echo "Default config created (${HOME}/klipper-tg-bot.conf)"
else
echo "Config already exists (${HOME}/klipper-tg-bot.conf)"
fi
}
install_systemd_service()
{
echo_text "Installing systemd unit file"
mkdir -p ${HOME}/.config/systemd/user
cat > ${HOME}/.config/systemd/user/klipper-tg-bot.service <<EOF
[Unit]
Description=Telegram bot for Klipper
After=network.target
[Service]
Type=simple
WorkingDirectory=${ROOT_PATH}
ExecStart=${ENV_PATH}/bin/python3 -m app --config ${HOME}/klipper-tg-bot.conf
Restart=always
RestartSec=15
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
EOF
systemctl --user unmask klipper-tg-bot.service
systemctl --user daemon-reload
systemctl --user enable klipper-tg-bot.service
}
install_packages
create_virtualenv
create_default_config
install_systemd_service
echo_ok "Klipper telegram bot was installed"
echo_ok "Now edit ${HOME}/klipper-tg-bot.conf and start bot service"
echo_ok " systemctl --user start klipper-tg-bot.service"