-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathinstall.sh
executable file
·268 lines (232 loc) · 7.35 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
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
#!/bin/bash
find_python(){
if [[ $(python3 --version) ]]; then
python=$(which python3)
pip="python3-pip"
pip_run='pip3'
else
python=$(which python)
pip="python-pip"
pip_run='pip'
fi
if [[ "$python" == *"python"* ]]; then
print_green "+ Found: $python"
else
print_yellow "Python not found!\n Exiting\n"
exit
fi
}
printm(){
length=$(expr length "$1")
length=$(($length + 4))
printf "\n"
printf ":: $1 \n\n"
}
print_green(){
tput setaf 2; echo "$1"
tput sgr 0
}
print_yellow(){
tput setaf 3; printf "$1"
tput sgr 0
}
check_and_install_pip(){
pip_ver=$(${python} -m pip --version 2>&1);
if [[ "$pip_ver" == *"No"* ]]; then
echo "- Pip is not installed, installing it."
sudo apt install $pip
else
print_green "+ Found: $pip"
fi
}
create_venv(){
printm "Creating virtual environment"
# Check if python3-venv is installed
if ! dpkg -l | grep -q python3-venv; then
echo "python3-venv is not installed. Installing..."
sudo apt-get install -y python3-venv
else
print_green "+ Found: python3-venv"
fi
# Create a virtual environment
${python} -m venv rpi_mon_env
print_green "+ Virtual environment created"
# Activate the virtual environment
source rpi_mon_env/bin/activate
if [[ $(python3 --version) ]]; then
python=$(which python3)
else
python=$(which python)
fi
print_green "+ Activated virtual environment"
}
install_requirements(){
printm "Installing requirements"
$pip_run install -r requirements.txt
# Deactivate the virtual environment
print_green "+ Requirements installed"
print_green "+ Deactivating virtual environment"
deactivate
}
mqtt_configuration(){
printm "MQTT settings"
printf "Enter mqtt_host: "
read HOST
sed -i "s/ip address or host/${HOST}/" src/config.py
printf "Enter mqtt_user: "
read USER
sed -i "s/username/${USER}/" src/config.py
printf "Enter mqtt_password: "
read PASS
sed -i "s/\"password/\"${PASS}/" src/config.py
printf "Enter mqtt_port (default is 1883): "
read PORT
if [ -z "$PORT" ]; then
PORT=1883
fi
sed -i "s/1883/${PORT}/" src/config.py
printf "Enter mqtt_topic_prefix (default is rpi-MQTT-monitor): "
read TOPIC
if [ -z "$TOPIC" ]; then
TOPIC=rpi-MQTT-monitor
fi
sed -i "s/rpi-MQTT-monitor/${TOPIC}/" src/config.py
printf "Do you need to control your monitors? (default is No): "
read CONTROL
if [[ "$CONTROL" =~ ^([yY][eE][sS]|[yY])$ ]]; then
sed -i "s/display_control = False/display_control = True/g" src/config.py
fi
finish_message="MQTT broker"
}
hass_api_configuration(){
printf "Enter Home Assistant API URL (defalut is http://localhost:8123): "
read HA_URL
if [ -z "$HA_URL" ]; then
HA_URL="http://localhost:8123"
fi
sed -i "s|your_hass_host|${HA_URL}|" src/config.py
printf "Enter Home Assistant API Token: "
read HA_TOKEN
sed -i "s|your_hass_token|${HA_TOKEN}|" src/config.py
hass_api=" --hass_api"
finish_message="Home Assistant API"
}
update_config(){
if [ -f src/config.py ]; then
read -p "src/config.py already exists Do you want to remove it? (y/n) " yn
case $yn in
[Yy]* ) echo "replacing config file";;
[Nn]* ) return;;
* ) echo "Please answer y for yes or n for no.";;
esac
fi
print_green "+ Copy config.py.example to config.py"
cp src/config.py.example src/config.py
user=$(whoami)
sed -i "s/os_user_to_be_replaced/${user}/" src/config.py
echo "Do you want to use Home Assistant API or MQTT?"
echo "1) Home Assistant API"
echo "2) MQTT (default)"
read -p "Enter your choice [1 or 2]: " choice
# Run the appropriate configuration function based on the user's choice
case $choice in
1)
hass_api_configuration
;;
2 | "")
mqtt_configuration
;;
*)
echo "Invalid choice. Defaulting to MQTT configuration."
mqtt_configuration
;;
esac
print_green "+ config.py is updated with provided settings"
# Get the local version
local_version=$(git describe --tags)
# Update the version in config.py
sed -i "s/version = .*/version = '${local_version}'/" src/config.py
}
set_cron(){
printm "Setting Cronjob"
cwd=$(pwd)
crontab -l > tempcron
if grep -q rpi-cpu2mqtt.py tempcron; then
cronfound=$(grep rpi-cpu2mqtt.py tempcron)
print_yellow " There is already a cronjob running rpi-cpu2mqtt.py - skipping cronjob creation.\n"
print_yellow " If you want the cronjob to be automatically created remove the line below from your\n cronjobs list and run the installer again.\n\n"
echo " ${cronfound}"
else
printf "How often do you want the script to run in minutes? (default is 2): "
read MIN
if [ -z "$MIN" ]; then
MIN=2
fi
echo "Adding the line below to your crontab"
echo "*/${MIN} * * * * cd ${cwd}; ${python} ${cwd}/src/rpi-cpu2mqtt.py${hass_api}"
echo "*/${MIN} * * * * cd ${cwd}; ${python} ${cwd}/src/rpi-cpu2mqtt.py${hass_api}" >> tempcron
crontab tempcron
fi
rm tempcron
}
set_service(){
printm "Setting systemd service"
if [ -f /etc/systemd/system/rpi-mqtt-monitor.service ]; then
read -p "Service file already exists. Do you want to remove it? (y/n) " yn
case $yn in
[Yy]* ) sudo rm /etc/systemd/system/rpi-mqtt-monitor.service;;
[Nn]* ) return;;
* ) echo "Please answer y for yes or n for no.";;
esac
fi
printf "How often do you want the script to run in seconds? (default is 120): "
read MIN
if [ -z "$MIN" ]; then
MIN=120
fi
sed -i "s/120/${MIN}/" src/config.py
cwd=$(pwd)
user=$(whoami)
exec_start="${python} ${cwd}/src/rpi-cpu2mqtt.py --service${hass_api}"
print_green "+ Copy rpi-mqtt-monitor.service to /etc/systemd/system/"
sudo cp ${cwd}/rpi-mqtt-monitor.service /etc/systemd/system/
sudo sed -i "s|WorkingDirectory=.*|WorkingDirectory=${cwd}|" /etc/systemd/system/rpi-mqtt-monitor.service
sudo sed -i "s|User=YOUR_USER|User=root|" /etc/systemd/system/rpi-mqtt-monitor.service
sudo sed -i "s|ExecStart=.*|ExecStart=${exec_start}|" /etc/systemd/system/rpi-mqtt-monitor.service
home_dir=$(eval echo ~$user)
sudo sed -i "s|Environment=\"HOME=/home/username\"|Environment=\"HOME=${home_dir}\"|" /etc/systemd/system/rpi-mqtt-monitor.service
sudo systemctl daemon-reload
sudo systemctl enable rpi-mqtt-monitor.service
sudo systemctl start rpi-mqtt-monitor.service
sudo service rpi-mqtt-monitor restart
sudo service rpi-mqtt-monitor status
print_green "+ Service is enabled and started"
git config --global --add safe.directory ${cwd}
}
create_shortcut(){
printm "Creating shortcut rpi-mqtt-monitor"
cwd=$(pwd)
echo "${python} ${cwd}/src/rpi-cpu2mqtt.py \$@" > rpi-mqtt-monitor
sudo mv rpi-mqtt-monitor /usr/local/bin/
sudo chmod +x /usr/local/bin/rpi-mqtt-monitor
}
main(){
find_python
check_and_install_pip
create_venv
install_requirements
update_config
create_shortcut
while true; do
read -p "Do you want to set up a (c)ron job or a (s)ervice? " cs
case $cs in
[Cc]* ) set_cron; break;;
[Ss]* ) set_service; break;;
* ) echo "Please answer c for cron or s for service.";;
esac
done
printm "Installation is complete."
echo "rpi-mqtt-monitor is now running and sending information to your ${finish_message}."
echo "To see all available options run: rpi-mqtt-monitor -h in the terminal."
}
main