This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·254 lines (225 loc) · 11.3 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
#!/usr/bin/env bash
# SchoolConnect Server-Installer
# © 2019 - 2021 Johannes Kreutz.
version='1.2.0'
# Check for root rights
if [[ $EUID > 0 ]]; then
echo "To install the SchoolConnect Server-Manager, you have to run this as root (sudo is fine)."
exit
fi
# Check if the servermanager is already installed
if [ -d "servermanager" ]; then
if [ -f "servermanager/.SchoolConnectSetupDone" ]; then
whiptail --title "ALREADY INSTALLED" --msgbox "It seems like the SchoolConnect Server-Manager is already installed. Please delete 'servermanager/.SchoolConnectSetupDone' if you are shure it is not installed yet." 10 80
exit
fi
fi
# Check for minimal dependencies (graphical install)
if ! command -v whiptail > /dev/null; then
echo "Installing some dependencies first..."
apt install -y whiptail sudo
fi
# Begin graphical install
whiptail --title "WELCOME TO THE SCHOOLCONNECT SERVER-MANAGER INSTALLER!" --msgbox "This script will install the SchoolConnect Server-Manager on your machine. Press Return to begin!" 10 80
# Ask and store required settings
until ! [ -z "$SLAPD_PASSWORD" ]; do
SLAPD_PASSWORD=$(whiptail --title "SLAPD ROOT PASSWORD" --inputbox "First, please enter a root password for the slapd-service (OpenLDAP). WARNING! Anyone with this password can read and change all user data. Keep this password safe, you will need it if something fails!" 10 80 "" 3>&1 1>&2 2>&3)
exitstatus=$?
if ! [ $exitstatus = 0 ]; then
echo "User canceled setup. Nothing has been installed yet, so it's safe to start again."
exit
fi
if [ -z "$SLAPD_PASSWORD" ]; then
whiptail --title "INPUT VERIFICATION ERROR" --msgbox "Empty passwords are not allowed. Please enter a password." 10 80
fi
done
until ! [ -z "$MYSQL_PASSWORD" ]; do
MYSQL_PASSWORD=$(whiptail --title "MYSQL ROOT PASSWORD" --inputbox "Now, please enter a root password for the mysqld-service (MySQL Database). WARNING! Anyone with this password can read and change all configuration data, as well as the credentials of the emergency admin account. Keep this password safe, you will need it if something fails!" 10 80 "" 3>&1 1>&2 2>&3)
exitstatus=$?
if ! [ $exitstatus = 0 ]; then
echo "User canceled setup. Nothing has been installed yet, so it's safe to start again."
exit
fi
if [ -z "$MYSQL_PASSWORD" ]; then
whiptail --title "INPUT VERIFICATION ERROR" --msgbox "Empty passwords are not allowed. Please enter a password." 10 80
fi
done
until ! [ -z "$SLAPD_ORGANIZATION" ]; do
SLAPD_ORGANIZATION=$(whiptail --title "SLAPD ORGANIZATION" --inputbox "Slapd needs the name of your organization. We recommend you using the name of your school." 10 80 "" 3>&1 1>&2 2>&3)
exitstatus=$?
if ! [ $exitstatus = 0 ]; then
echo "User canceled setup. Nothing has been installed yet, so it's safe to start again."
exit
fi
if [ -z "$SLAPD_ORGANIZATION" ]; then
whiptail --title "INPUT VERIFICATION ERROR" --msgbox "This value is required." 10 80
fi
done
PREV_USERDATA=""
until ! [ -z "$USERDATA" ]; do
USERDATA=$(whiptail --title "USER DATA STORAGE" --inputbox "Where do you want to store user data? Please type an absolute path, for example to the mount point of your data disks. We highly recommend to create regular backups of this location!" 10 80 "$PREV_USERDATA" 3>&1 1>&2 2>&3)
exitstatus=$?
if ! [ $exitstatus = 0 ]; then
echo "User canceled setup. Nothing has been installed yet, so it's safe to start again."
exit
fi
if [ -z "$USERDATA" ]; then
whiptail --title "INPUT VERIFICATION ERROR" --msgbox "This value is required." 10 80
fi
if [ ! -d "$USERDATA" ]; then
if (whiptail --title "DIRECTORY NOT FOUND" --yesno "The directory you entered does not exist. Should we create it?." 10 80); then
if mkdir -p "$USERDATA" ; then
whiptail --title "DIRECTORY CREATION" --msgbox "User data directory successfully created." 10 80
else
whiptail --title "DIRECTORY CREATION FAILED" --msgbox "Unable to create the user data directory. Check permissions." 10 80
PREV_USERDATA=$USERDATA
unset USERDATA
fi
else
PREV_USERDATA=$USERDATA
unset USERDATA
fi
fi
done
# Remove trailing slash if existing
USERDATA=$(echo $USERDATA | sed 's:/*$::')
# Explain optional settings
whiptail --title "OPTIONAL SETTINGS" --msgbox "The following settings already have good default values. You can change them, but we don't recommend it unless you know what you are doing." 10 80
# Ask and store optional settings
until ! [ -z "$SLAPD_DOMAIN0" ]; do
SLAPD_DOMAIN0=$(whiptail --title "SLAPD TOPLEVEL DOMAIN" --inputbox "Slapd needs a top level domain. Everything will be stored under this key, but the user doesn't see it." 10 80 "local" 3>&1 1>&2 2>&3)
exitstatus=$?
if ! [ $exitstatus = 0 ]; then
echo "User canceled setup. Nothing has been installed yet, so it's safe to start again."
exit
fi
if [ -z "$SLAPD_DOMAIN0" ]; then
if (whiptail --title "INPUT VERIFICATION ERROR" --yesno "Even if changing is optional, this value is required. Do you want to use the default? (No takes you back to the input screen.)" 10 80) then
SLAPD_DOMAIN0="local"
fi
fi
done
until ! [ -z "$SLAPD_DOMAIN1" ]; do
SLAPD_DOMAIN1=$(whiptail --title "SLAPD SECOND LEVEL DOMAIN" --inputbox "Slapd also needs a second level domain. A lot will be stored under this key, but the user doesn't see it." 10 80 "schoolconnect" 3>&1 1>&2 2>&3)
exitstatus=$?
if ! [ $exitstatus = 0 ]; then
echo "User canceled setup. Nothing has been installed yet, so it's safe to start again."
exit
fi
if [ -z "$SLAPD_DOMAIN1" ]; then
if (whiptail --title "INPUT VERIFICATION ERROR" --yesno "Even if changing is optional, this value is required. Do you want to use the default? (No takes you back to the input screen.)" 10 80) then
SLAPD_DOMAIN1="schoolconnect"
fi
fi
done
whiptail --title "FINISH INSTALLATION" --msgbox "All configuration values are set. Now I'll install the required dependencies, download the server manager files and install them. Relax for some minutes." 10 80
# Create a user for the server manager
adduser --system --shell /bin/bash --gecos "User running the SchoolConnect Server-Manager" --group --disabled-password servermanager
# Allow the servermanager user to start and stop its service
if [ -f "/tmp/sudoers.sctmp" ]; then
rm /tmp/sudoers.sctmp
fi
cp /etc/sudoers /tmp/sudoers.sctmp
if ! grep -qxF "Cmnd_Alias SERVERMANAGER_CONTROL = /bin/systemctl restart servermanager.service" /tmp/sudoers.sctmp; then
echo "Cmnd_Alias SERVERMANAGER_CONTROL = /bin/systemctl restart servermanager.service" >> /tmp/sudoers.sctmp
fi
if ! grep -qxF "servermanager ALL=(ALL) NOPASSWD: SERVERMANAGER_CONTROL" /tmp/sudoers.sctmp; then
echo "servermanager ALL=(ALL) NOPASSWD: SERVERMANAGER_CONTROL" >> /tmp/sudoers.sctmp
fi
if ! grep -qxF "Cmnd_Alias SERVERMANAGER_VOLUMEBACKUP = /usr/local/bin/servermanager/volumebackup.py" /tmp/sudoers.sctmp; then
echo "Cmnd_Alias SERVERMANAGER_VOLUMEBACKUP = /usr/local/bin/servermanager/volumebackup.py" >> /tmp/sudoers.sctmp
fi
if ! grep -qxF "servermanager ALL=(ALL) NOPASSWD: SERVERMANAGER_VOLUMEBACKUP" /tmp/sudoers.sctmp; then
echo "servermanager ALL=(ALL) NOPASSWD: SERVERMANAGER_VOLUMEBACKUP" >> /tmp/sudoers.sctmp
fi
visudo -c -f /tmp/sudoers.sctmp
if [ "$?" -eq "0" ]; then
cp /tmp/sudoers.sctmp /etc/sudoers
else
deluser servermanager
echo "Unable to modify sudoers file. Exiting."
exit 1
fi
rm /tmp/sudoers.sctmp
# Install dependencies
apt install -y python3 git docker docker.io python3-pip python3-flask python3-docker python3-yaml
# Allow servermanager to control docker
usermod -a -G docker servermanager
# Download the server manager files and extract them
wget -O servermanager_latest.tar.gz 'https://github.com/philleconnect/ServerManager/releases/download/'$version'/servermanager.tar.gz'
tar -zxf servermanager_latest.tar.gz --directory /usr/local/bin/
rm servermanager_latest.tar.gz
# Create configuration folder
mkdir /etc/servermanager
# Create container storage
mkdir /var/lib/servermanager
mkdir /var/lib/servermanager/services
mkdir /var/lib/servermanager/services/buildcache
# Install python modules for servermanager user
#su servermanager -c "pip3 install docker"
#su servermanager -c "pip3 install flask"
# Grant privileges for config and storage folders
chown -R servermanager:servermanager /etc/servermanager
chown -R servermanager:servermanager /var/lib/servermanager
chown -R servermanager:servermanager /usr/local/bin/servermanager
chown root:root /usr/local/bin/servermanager/volumebackup.py
chmod 755 /usr/local/bin/servermanager/volumebackup.py
# Install a systemd init script for the server manager
cat > /etc/systemd/system/servermanager.service <<EOF
[Unit]
Description=SchoolConnect Server Manager
After=syslog.target
[Service]
Type=simple
User=servermanager
Group=servermanager
WorkingDirectory=/usr/local/bin/servermanager
ExecStart=/usr/local/bin/servermanager/servermanager.py
SyslogIdentifier=servermanager
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# Write user settings to file
ENVVARS="{\"SLAPD_PASSWORD\":{\"value\":\"$SLAPD_PASSWORD\",\"description\":\"Root-Passwort für den LDAP-Server.\",\"mutable\":false},\"MYSQL_ROOT_PASSWORD\":{\"value\":\"$MYSQL_PASSWORD\",\"description\":\"Root-Passwort für den MySQL-Server.\",\"mutable\":false},\"SLAPD_ORGANIZATION\":{\"value\":\"$SLAPD_ORGANIZATION\",\"description\":\"Organisations-Kennung in der LDAP-Struktur.\",\"mutable\":false},\"USERDATA\":{\"value\":\"$USERDATA\",\"description\":\"Pfad zum Speicherort für Benutzerdaten.\",\"mutable\":true},\"SLAPD_DOMAIN0\":{\"value\":\"$SLAPD_DOMAIN0\",\"description\":\"Top-Level-Domain für den LDAP-Server.\",\"mutable\":false},\"SLAPD_DOMAIN1\":{\"value\":\"$SLAPD_DOMAIN1\",\"description\":\"Second-Level-Domain für den LDAP-Server.\",\"mutable\":false}}"
echo "$ENVVARS" > /etc/servermanager/env.json
chown servermanager:servermanager /etc/servermanager/env.json
# Create folder structure if not existing in userdata directory
mkdir -p $USERDATA/deleted
mkdir -p $USERDATA/users
mkdir -p $USERDATA/shares
mkdir -p $USERDATA/images
mkdir -p $USERDATA/updates
mkdir -p $USERDATA/updates/drivers
mkdir -p $USERDATA/updates/images
mkdir -p $USERDATA/shares/roomExchange
mkdir -p $USERDATA/shares/schoolExchange
mkdir -p $USERDATA/shares/schoolTemplate
mkdir -p $USERDATA/shares/teacherExchange
mkdir -p $USERDATA/shares/teacherTemplate
mkdir -p $USERDATA/PreviousVersions
# Start the server manager manually with firstsetup parameter (takes some time, will build containers here)
echo "Building containers. This might take a while..."
su servermanager -c "python3 /usr/local/bin/servermanager/servermanager.py firstsetup"
# Create loopback IP so the pc_admin container can connect to the servermanager
cat << EOF >> /etc/netplan/40-servermanager-loopback.yaml
network:
version: 2
renderer: networkd
ethernets:
lo:
match:
name: lo
addresses: [ 192.168.255.255/32 ]
EOF
netplan generate
netplan apply
# Start the server manager via systemd
systemctl enable servermanager
systemctl start servermanager
# Finalize
touch /etc/servermanager/.ServerManagerSetupDone
whiptail --title "INSTALLATION SUCCEDED" --msgbox "The SchoolConnect Server-Manager has been successfully installed on your server. We will now start it, so you can finish the setup in your web browser. Simply go to http://YOUR_IP:84" 10 80