-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.sh
executable file
·320 lines (284 loc) · 8.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
# Install Docker IaaS tools.
# Need to be run with root privileges.
#
# Requires:
# docker
# sshd
# apt-get
# groupadd
# socat
# jq
# sed
#
# Created by Peter Bryzgalov
# Copyright (C) 2015 RIKEN AICS. All rights reserved
version="0.45"
debug=1
# Set "$option yes" in $ssh_conf file.
# Uses sed.
# Works on OSX and Ubuntu. OSX sed is different: -E instead of -r for extended regexp,
# no regexp enhanced features like "\s", need '' after -i.
# Parameter: option name (AllowAgentForwarding, GatewayPorts, ...)
#
# Use global variables:
# $ssh_conf
# $format
permitOption() {
option=$1
if grep -qE "^\s*$option\s+(yes|no)+" "$ssh_conf"; then
ERROR=$( { sed -ri "s/\s*$option\s+(yes|no)+\s*/$option yes/" "$ssh_conf" > /dev/null; } 2>&1 )
if [ -n "$ERROR" ]; then
# OSX version
sed -E -i '' "s/[[:space:]]*$option[[:space:]]+(yes|no)+[[:space:]]*/$option yes/" "$ssh_conf"
fi
else
printf "\n%s\n" "$option yes" >> "$ssh_conf"
fi
printf "$format" "$ssh_conf" "$option permitted"
}
### Configuration section
diaasconfig="diaas_installed.conf"
##### These variables saved to the above file
forcecommand="/usr/local/bin/diaas.sh"
forcecommandlog="/var/log/diaas.log"
tablesfolder="/var/lib/diaas"
mountfile="$tablesfolder/mounttable.txt"
usersfile="$tablesfolder/userstable.txt"
dockerhost="localhost"
dockerport="4243"
dockercommand="docker"
diaasgroup="diaasgroup"
dockergroup="docker"
ssh_conf="/etc/ssh/sshd_config"
ssh_backup="${ssh_conf}.diaas_back"
sshd_pam="/etc/pam.d/sshd"
install_path="$(pwd)"
config_file="/etc/diias/config"
### Configuration section end
# Define output format
format="%-50s %-20s\n"
# Array for saving variables to configuration file
config_vars=(forcecommand forcecommandlog tablesfolder mountfile usersfile \
dockerhost dockerport dockercommand diaasgroup dockergroup ssh_conf \
ssh_backup sshd_pam format install_path config_file)
read -rd '' usage << EOF
Installation script for Docker IaaS tools v$version
Usage: \$ sudo $0 [-c]
Options:
-c print path to the file with configuration variables and exit.
Can be used with \"eval \$(./install.sh -c)\" command.
This option does not require root privileges.
-y assume answer "yes" to all questions to user during install.
Docker IaaS tools requirements: bash, Docker, socat, jq.
Required OS: Ubuntu, Debian.
EOF
# Check section
# Exit if something's wrong
if [ $# -gt 2 ]; then
printf "%s" "$usage"
exit 0
fi
if [[ "$1" == "-c" ]]; then
echo "export diaasconfig=\"$(pwd)/$diaasconfig\""
exit 0
elif [[ "$1" == "-y" ]]; then
AUTOINSTALL=1
elif [[ -n "$1" ]]; then
printf "%s" "$usage"
exit 1
fi
if [[ $(id -u) != "0" ]]; then
printf "Error: Must be root to use it.\n" 1>&2
exit 1
fi
# Check jq install
jq &>/dev/null
if [[ $? -gt 1 ]]; then
if [[ -z "$AUTOINSTALL" ]]; then
echo -n " jq is required for Docker IaaS Tools. Install jq? [y/n]"
read -n 1 install
printf "\n"
if [[ $install != "y" ]]; then
printf "Bye!\n"
exit 1
fi
fi
apt-get install -y jq
fi
# Check socat install
socat &>/dev/null
if [[ $? -gt 1 ]]; then
if [[ -z "$AUTOINSTALL" ]]; then
echo -n " socat is required for Docker IaaS Tools. Install socat? [y/n]"
read -n 1 install
printf "\n"
if [[ $install != "y" ]]; then
printf "Bye!\n"
exit 1
fi
fi
apt-get install -y socat
fi
# Check that port is not used
read pname pid junk <<< "$(sudo lsof -i TCP:$dockerport | grep -v COMMAND)"
if [[ -n "$pid" ]]; then
echo "Port $dockerport is used by $pname with PID $pid. Use another port number (dockerport var in install.sh)."
exit 1
fi
# Write variables to config file diaas_installed.conf
touch $diaasconfig
printf "" > $diaasconfig
for var in "${config_vars[@]}"; do
echo "$var=\"$(eval echo \$$var)\"" >> $diaasconfig
done
printf "$format" "$diaasconfig" "saved"
dockerimagesline=$($dockercommand images 2>/dev/null | grep IMAGE | wc -l)
if [[ $dockerimagesline -eq 0 ]]; then
printf "Error: Cannot connect to Docker with command:\n%s\n" "$dockercommand" 1>&2
exit 1
elif [[ $dockerimagesline -eq 1 ]]; then
printf "$format" "Connection to Docker" "OK"
else
printf "Somethings wrong :%s\n" "$dockerimagesline"
exit 1
fi
# Check section end
# Group diaasgroup - create if not exists
if [ -z "$(cat /etc/group | grep $diaasgroup:)" ]; then
if [[ -z "$AUTOINSTALL" ]]; then
echo -n " Create group $diaasgroup? [y/n]"
read -n 1 creategroup
printf "\n"
if [[ $creategroup != "y" ]]; then
echo "Bye!"
exit 0
fi
fi
groupadd "$diaasgroup"
printf "$format" "Group $diaasgroup" "created"
else
printf "$format" "Group $diaasgroup" "exists"
fi
# Copy files
cp docker.sh $forcecommand
if [[ $? -eq 1 ]]; then
echo "Error: Could not copy file $(pwd)/docker.sh to $forcecommand" 1>&2
exit 1
fi
printf "$format" "Copy $forcecommand" "OK"
# Add exec permission to diaas group
chown :$diaasgroup $forcecommand
chmod g+x $forcecommand
printf "$format" "$forcecommand permissions" "set"
# Replace filename with full path to config file in docker.sh
sed -ri "s#source diaasconfig#source $(pwd)/$diaasconfig#" "$forcecommand"
if [ ! -f "$forcecommandlog" ]; then
touch $forcecommandlog
chmod a+w $forcecommandlog
if [[ $? -eq 1 ]]; then
echo "Error: Could not create $forcecommandlog." 1>&2
exit 1
fi
printf "$format" "Create $forcecommandlog" "OK"
else
printf "$format" "$forcecommandlog" "exists"
fi
if [ ! -d "$tablesfolder" ]; then
if [ -f "$tablesfolder" ]; then
echo "Error: $tablesfolder exists, but is a regular file. Need directory." 1>&2
exit 1
fi
mkdir -p $tablesfolder
chmod a+w "$tablesfolder"
if [[ $? -eq 1 ]]; then
echo "Error: Could not create $tablesfolder." 1>&2
exit 1
fi
printf "$format" "Create $tablesfolder" "OK"
else
printf "$format" "$tablesfolder" "exists"
fi
if [ ! -f "$mountfile" ]; then
touch $mountfile
chmod a+w "$mountfile"
if [[ $? -eq 1 ]]; then
echo "Error: Could not create $mountfile." 1>&2
exit 1
fi
printf "$format" "Create $mountfile" "OK"
else
printf "$format" "$mountfile" "exists"
fi
if [ ! -f "$usersfile" ]; then
touch $usersfile
chmod +w "$usersfile"
if [[ $? -eq 1 ]]; then
echo "Error: Could not create $usersfile." 1>&2
exit 1
fi
printf "$format" "Create $usersfile" "OK"
else
printf "$format" "$usersfile" "exists"
fi
# Edit config files
if [ -f "$sshd_pam" ]; then
sed -ri 's/^session\s+required\s+pam_loginuid.so$/session optional pam_loginuid.so/' "$sshd_pam"
if [[ $? -eq 0 ]]; then
printf "$format" "$sshd_pam" "edited"
printf "%s" "sshd_pam_edited=\"edited\"" >> $diaasconfig
fi
fi
# Patch /etc/ssh/sshd_conf
if [ -f "$ssh_conf" ]; then
# Save original version
cp "$ssh_conf" "$ssh_backup"
# Permit options
permitOption "AllowAgentForwarding"
permitOption "GatewayPorts"
# Add ForceCommand for group $diaasgroup
if grep -qEi "match\s+group\+$diaasgroup" "$ssh_conf"; then
# do nothing
printf "$format" "$ssh_conf" "already patched"
elif grep -qi "forcecommand" "$ssh_conf"; then
# /etc/ssh/sshd_conf has differnet ForceCommand
# Need to edit manually
printf "$format" "$ssh_conf" "already has ForceCommand. Need manual editing."
echo "Check that it has the following:"
echo "AllowAgentForwarding yes"
echo "GatewayPorts yes"
echo "Match Group $diaasgroup"
echo " ForceCommand $forcecommand"
echo "----------"
echo "Fragment of your $ssh_conf file:"
grep -C 4 "$forcecommand" "$ssh_conf"
if [[ -z "$AUTOINSTALL" ]]; then
echo -n " Please confirm [press any key]"
read -n 1 foo
printf "\n"
fi
else
printf "\n%s\n\t%s\n" "Match Group $diaasgroup" "ForceCommand $forcecommand" >> "$ssh_conf"
printf "$format" "$ssh_conf" "ForceCommand added"
fi
if grep -q "AllowAgentForwarding\s+no" "$ssh_conf"; then
sed -ri 's/^\s*AllowAgentForwarding\.*$/AllowAgentForwarding yes/' "$ssh_conf"
printf "$format" "$ssh_conf" "AllowAgentForwarding permitted"
fi
else
echo "Error: SSH configuration file $ssh_conf not found." 1>&2
exit 1
fi
if [[ -z "$AUTOINSTALL" ]]; then
echo -n " Restart sshd? [y/n]"
read -n 1 restartssh
printf "\n"
if [[ $restartssh == "y" ]]; then
service ssh restart
else
printf "Please, restart sshd later with:\n\$ sudo service ssh restart\n"
fi
else
printf "Please, restart sshd with:\n\$ sudo service ssh restart\n"
fi
echo "Installation comlete."