-
Notifications
You must be signed in to change notification settings - Fork 0
/
communitygarden-cli
executable file
·286 lines (258 loc) · 6.93 KB
/
communitygarden-cli
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
#!/bin/bash
# Author: An Pham, Myriam
# Filename: communnity-garden.sh
# Version: testing
## declaring modules function
clean () {
sudo_check
apt-get clean
apt-get autoclean -y
apt-get autoremove -y
}
print_status(){
local message="$1"
local status="$2"
# Get the number of columns, but subtact 8 to leave space for the status.
local columns=$((COLUMNS-8))
# Print left-aligned message and right-aligned status.
printf "%-*s [%s] \n" "$columns" "$message" "$status"
}
install_docker () {
# Add Docker's official GPG key:
apt-get update
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
usermod -aG docker pi
}
install_docker_compose () {
sudo_check
apt-get update
apt-get install python3
python3 -m pip install docker-compose
}
install_essential () {
apt install python3-configobj python3-pil python3-serial python3-usb python3-pip python-pip -y
# pacakges require for network wifi
apt install network-manager -y
# package for AWS IOT
pip install awsiotsdk
}
sudo_check () {
if [ "$EUID" -ne 0 ]
then echo "Please run as root or with sudo"
exit
fi
}
checking_weather_station_connection () {
ls /dev/ttyUSB0
[ "$?" = 1 ] && print_status "Finding Davis Weather station..." "Ok" || print_status "Finding Davis weather station ..." "Fail"
}
install_weewx_dependency () {
# tested
sudo_check
apt install \
python3-configobj \
python3-pil \
python3-serial \
python3-usb \
python3-pip -y
pip3 install Cheetah3
}
install_home_assistant () {
apt-get install -y python3 python3-dev python3-venv python3-pip bluez libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5 libturbojpeg0-dev tzdata ffmpeg liblapack3 liblapack-dev libatlas-base-dev
useradd -rm homeassistant -G dialout,gpio,i2c
mkdir /srv/homeassistant
chown homeassistant:homeassistant /srv/homeassistant
echo '###'
sudo -u homeassistant -H -s
cd /srv/homeassistant
python3 -m venv .
source bin/activate
echo '###'
python3 -m pip install wheel
pip3 install homeassistant==2023.8.4
echo 'Home Assitant is listened on port 8123'
}
install_weewx () {
install_weewx_dependency
download_and_install_weewx
install_database
}
enable_weewx () {
cd /home/weewx
cp util/systemd/weewx.service /etc/systemd/system
systemctl enable weewx
systemctl start weewx
}
status_weewx () {
systemctl status weewx
}
uninstall_weewx () {
systemctl stop weewx
systemctl daemon-reload
rm -r /home/weewx
rm /etc/systemd/system/weewx.service
}
print_line () {
echo "============================"
}
download_and_install_weewx () {
local weewx_url='https://weewx.com/downloads/weewx-4.10.2.tar.gz'
local weewx_dest='/var/temp/weewx/'
mkdir -p "${weewx_dest}"
print_line
echo "Downloading weewx file..."
wget -o "${weewx_dest}weex-4.10.2" "${weewx_url}"
[ "$?" = 1 ] && print_status "Downloading weewx file..." "Ok" || print_status "Downloading weewx file.." "Fail"
cp -v weewx-4.10.2.tar.gz "${weewx_dest}"
cd "${weewx_dest}"
print_line
echo "Extracting downloaded file"
tar xvzf weewx-4.10.2.tar.gz
cd weewx-4.10.2
python3 ./setup.py build
python3 ./setup.py install
[ "$?" = 0 ] && echo "Success. The application has been installed at /home/weewx " || echo "Failed to install weewix"
print_line
enable_weewx
}
wifi_check () {
# check if wifi connection is available
ping -q -c1 1.1.1.1 &>/dev/null && print_status "Checking ping request..." "Ok" || print_status "Checking ping request..." "Fail"
nslookup google.com &>/dev/null && print_status "Checking dns service..." "OK" || print_status "Checking dns Service..." "Fail"
}
install_wifi_private () {
local username=${1}
local password=${2}
# WARNING: do not remove the line below,. This is used as backup for comfinguration.
cp -v /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.bak
cat << EOF > /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="eduroam"
scan_ssid=1
key_mgmt=WPA-EAP
eap=PEAP
identity="$username@cpp.edu"
# anonymous_identity="*****@cpp.edu"
password="$password"
ca_cert="/usr/share/ca-certificates/eduroam.pem"
phase1="peaplabel=auto peapver=auto"
phase2="auth=MSCHAPV2"
}
EOF
wpa_cli -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
}
install_database () {
# Required if you are using MySQL (MariaDB):
apt install mariadb-client python3-mysqldb sqlite3 -y
}
## Main ##
main () {
while [ -n "$1" ]
do
case "$1" in
--check-connection )
wifi_check
shift
;;
--install-dependency )
install_essential
shift
;;
--install-weewx )
install_weewx
shift
;;
--enable-weewx)
# tested
enable_weewx
shift
;;
--database-sql )
# tested
install_database
shift
;;
--clean|-c )
clean
shift
;;
--install-wifi-eduroam )
shift
# school username and password without @edu
install_wifi_private "$1" "$2"
shift
;;
--download-weewx )
download_and_install_weewx
shift
;;
--uninstall-weewx )
uninstall_weewx
shift
;;
--check-weather-station )
checking_weather_station_connection
shift
;;
--status-weewx )
status_weewx
shift
;;
--version | -v)
echo 'Community Garden v1.1'
shift
;;
--start-pump)
echo 'Starting pump...'
shift
;;
--install-docker)
echo 'Installing docker ...'
install_docker
shift
;;
--install-docker-compose)
echo "Installing docker-compose ..."
install_docker_compose
shift
;;
--install-home-assistant)
echo "Installing home assistant ..."
install_home_assistant
shift
;;
* | -*)
echo 'Invalid argument'
help
exit 1
break
;;
esac
done
}
quick_start () {
pass
}
##
help () {
echo "Usuage: $(basename $0) <--install-dependency|-v>. First, edit the variable to match your bluetooth interface MAC address. "
echo ''
echo 'Where:'
echo ' --install-dependency install essential dependency'
echo ' --install-weewx download and install weewx'
echo ' --install-docker download and install docker'
echo ' --install-docker-compose download and install docker-compose '
echo ' --install-home-assistant download and install home-assistant'
echo ' --download-weewx download weewx only'
echo ' --download-weewx download weewx only'
echo ' --uninstall-weewx uninstall weewx'
echo ' --status-weex check status of weewx'
echo ' --clean | -c clean unused packages'
echo ' --check-connection check internet connection'
echo ' --start-pump start the pump valve'
echo ''
echo 'Example:'
echo " $(basename $0) -v | --version Check the version of the program "
}
main "$@"