forked from pyscada/PyScada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·260 lines (224 loc) · 7.14 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
#!/bin/bash
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
INSTALL_ROOT=/var/www/pyscada
# todo : add inputs for mysql root pwd, db name, username, user pwd
function validate_url(){
if [[ `wget_proxy -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
return 0
else
return 1
fi
}
function add_line_if_not_exist(){
grep -qF "$1" "$2" || echo "$1" | tee --append "$2"
}
function pip3_proxy(){
if [[ "$answer_proxy" == "n" ]]; then
pip3 $*
else
echo "pip3 using" $answer_proxy "for" $* > /dev/tty
pip3 --proxy=$answer_proxy $*
fi
}
function pip3_proxy_not_rust(){
if [[ "$answer_proxy" == "n" ]]; then
CRYPTOGRAPHY_DONT_BUILD_RUST=1 pip3 install cryptography==3.4.6 --no-cache-dir
pip3 $*
else
echo "pip3 using" $answer_proxy "for" $* > /dev/tty
CRYPTOGRAPHY_DONT_BUILD_RUST=1 pip3 --proxy=$answer_proxy install cryptography==3.4.6 --no-cache-dir
pip3 --proxy=$answer_proxy $*
fi
}
function apt_proxy(){
if [[ "$answer_proxy" == "n" ]]; then
apt-get $*
else
echo "apt using" $answer_proxy "for" $* > /dev/tty
export http_proxy=$answer_proxy
apt-get $*
unset http_proxy
fi
}
function wget_proxy(){
if [[ "$answer_proxy" == "n" ]]; then
echo "wget no proxy" $* > /dev/tty
wget --no-proxy $*
else
echo "wget using" $answer_proxy "for" $* > /dev/tty
http_proxy=$answer_proxy https_proxy=$answer_proxy ftp_proxy=$answer_proxy wget $*
fi
}
echo 'date :'
echo $(date)
read -p "Is the date and time correct ? [y/n]: " answer_date
if [[ "$answer_date" == "n" ]]; then
exit
fi
read -p "Use proxy ? [http://proxy:port or n]: " answer_proxy
apt_proxy install -y python3-pip
echo 'Some python3 packages installed:'
pip3 list | grep -i -E 'pyscada|channels|asgiref'
read -p "Update only (don't create db, user, copy services, settings and urls...) ? [y/n]: " answer_update
read -p "Install channels and redis ? [y/n]: " answer_channels
if [[ "$answer_update" == "n" ]]; then
read -p "DB name ? [PyScada_db]: " answer_db_name
fi
if [[ "$answer_db_name" == "" ]]; then
answer_db_name="PyScada_db"
fi
echo $answer_db_name
echo "Stopping PyScada"
systemctl stop pyscada gunicorn gunicorn.socket
sleep 1 # Give systemd time to shutdown
systemctl --quiet is-active pyscada
if [ $? == 0 ] ; then
echo "Can't stop pyscada systemd service. Aborting." > /dev/stderr
exit 1
fi
echo "PyScada stopped"
# Install prerequisites
DEB_TO_INSTALL="
libatlas-base-dev
libffi-dev
libhdf5-103
libhdf5-dev
libjpeg-dev
libmariadb-dev
libopenjp2-7
mariadb-server
nginx
python3-dev
python3-mysqldb
python3-pip
zlib1g-dev
"
apt_proxy install -y $DEB_TO_INSTALL
PIP_TO_INSTALL="
cffi
Cython
docutils
gunicorn
lxml
mysqlclient
numpy
"
pip3_proxy install --upgrade $PIP_TO_INSTALL
# Install PyScada
pip3_proxy install --upgrade .
if [[ "$answer_channels" == "y" ]]; then
apt_proxy -y install redis-server
if grep -R "Raspberry Pi 3" "/proc/device-tree/model" ; then
echo "Don't install Rust for RPI3"
pip3_proxy_not_rust install --upgrade channels channels-redis asgiref
else
pip3_proxy install --upgrade cryptography==3.4.6 channels channels-redis asgiref
fi
fi
if [[ "$answer_update" == "n" ]]; then
# Create pyscada user
echo "Creating system user pyscada..."
adduser pyscada
mkdir -p $INSTALL_ROOT/http
chown -R pyscada:pyscada $INSTALL_ROOT
touch /var/log/pyscada_{daemon,debug}.log
chown pyscada:pyscada /var/log/pyscada_{daemon,debug}.log
# Add rights for usb, i2c and serial
add_line_if_not_exist 'SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0664", GROUP="pyscada"' /etc/udev/rules.d/10-usb.rules
add_line_if_not_exist 'KERNEL=="ttyS[0-9]", GROUP="dialout", MODE="0777"' /etc/udev/rules.d/10-usb.rules
adduser www-data pyscada
adduser pyscada dialout
adduser pyscada i2c
fi
SERVER_ROOT=$INSTALL_ROOT/PyScadaServer
if [[ "$answer_update" == "n" ]]; then
# Create DB
mysql <<-EOF
CREATE DATABASE IF NOT EXISTS ${answer_db_name}
CHARACTER SET utf8;
GRANT ALL PRIVILEGES ON ${answer_db_name}.*
TO 'PyScada-user'@'localhost'
IDENTIFIED BY 'PyScada-user-password';
EOF
sudo -u pyscada mkdir -p $SERVER_ROOT
sudo -u pyscada django-admin startproject PyScadaServer $SERVER_ROOT
# Copy settings.py and urls.py
var1=$(grep SECRET_KEY $SERVER_ROOT/PyScadaServer/settings.py)
echo $var1
printf -v var2 '%q' "$var1"
cp extras/settings.py $SERVER_ROOT/PyScadaServer
(
cd $SERVER_ROOT/PyScadaServer
sed -i "s/SECRET_KEY.*/$var2/g" settings.py
sed -i "s/PyScada_db'/${answer_db_name}'/g" settings.py
)
cp extras/urls.py $SERVER_ROOT/PyScadaServer
fi
(
cd $SERVER_ROOT
# Migration and static files
sudo -u pyscada python3 manage.py migrate
sudo -u pyscada python3 manage.py collectstatic --noinput
# Load fixtures with default configuration for chart lin colors and units
sudo -u pyscada python3 manage.py loaddata color
sudo -u pyscada python3 manage.py loaddata units
# Initialize the background service system of pyscada
sudo -u pyscada python3 manage.py pyscada_daemon init
)
if [[ "$answer_update" == "n" ]]; then
(
cd $SERVER_ROOT
python3 manage.py shell <<-EOF
try:
from django.contrib.auth import get_user_model
from django.db.utils import IntegrityError
User = get_user_model()
User.objects.create_superuser('pyscada',
'password')
except IntegrityError:
print('User pyscada already exist')
EOF
)
# Nginx
cp extras/nginx_sample.conf /etc/nginx/sites-available/pyscada.conf
ln -sf ../sites-available/pyscada.conf /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
mkdir -p /etc/nginx/ssl
# the certificate will be valid for 5 Years,
openssl req -x509 -nodes -days 1780 -newkey rsa:2048 -keyout /etc/nginx/ssl/pyscada_server.key -out /etc/nginx/ssl/pyscada_server.crt -subj '/CN=www.mydom.com/O=My Company Name LTD./C=US'
systemctl enable nginx.service # enable autostart on boot
systemctl restart nginx
# Gunicorn and PyScada as systemd units
cp extras/service/systemd/{gunicorn.{socket,service},pyscada_daemon.service} /etc/systemd/system
# Rename PyScada service file
mv /etc/systemd/system/pyscada_daemon.service /etc/systemd/system/pyscada.service
# Fix if gunicorn installed in /usr/bin and not /usr/local/bin -> create symbolic link
if [[ $(which gunicorn) != /usr/local/bin/gunicorn ]] && [[ ! -f /usr/local/bin/gunicorn ]] && [[ -f /usr/bin/gunicorn ]]; then
echo "Creating symcolic link to gunicorn";
ln -s /usr/bin/gunicorn /usr/local/bin/gunicorn;
fi
fi
# enable the services for autostart
systemctl enable gunicorn
systemctl restart gunicorn
systemctl enable pyscada
systemctl restart pyscada
sleep 1
systemctl --quiet is-active pyscada
if [ $? != 0 ] ; then
echo "Can't start pyscada systemd service." > /dev/stderr
exit 1
fi
if [[ "$answer_update" == "n" ]]; then
echo "PyScada installed"
echo "Connect to http://127.0.0.1 using :"
echo "username : pyscada"
echo "password : password"
else
echo "PyScada updated"
fi