-
Notifications
You must be signed in to change notification settings - Fork 1
/
sage-community.sh
executable file
·381 lines (323 loc) · 12.3 KB
/
sage-community.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash -e
usage () {
cat << EOF
usage: $0 <cmd>
Available commands:
- check: check you system's conformance to minimal requirements
- install: install Sage Community
-c CONFIG, --config CONFIG Specify Sage config
-h HOSTNAME, --hostname HOSTNAME Bind to HOSTNAME instead of default <ip>.nip.io.
Note that HOSTNAME cannot be IP address, since
it is used to generate certificates.
- group-add: add new group to Sage Community
- wipe: delete Sage Community and remove created data
To install Sage Community, run commands:
\$ $0 install
To uninstall Sage Community, run command:
\$ $0 wipe
To add groups to Sage Community, run commands:
\$ $0 group-add <groups.yml>
EOF
}
case "$1" in
check | install | group-add | wipe )
SUBCOMMAND="$1"
shift
;;
*)
usage >&2
exit 1
;;
esac
TEMP=$(getopt -o 'c:h:' --long 'config:,hostname:' -n "$0" -- "$@")
if [ $? -ne 0 ]; then
echo 'Terminating...' >&2
exit 1
fi
eval set -- "$TEMP"
unset TEMP
while true; do
case "$1" in
'-h'|'--hostname')
hostname="$2"
shift 2
continue
;;
'-c'|'--config')
config="$2"
if [ ! -e "$config" ]; then
echo "File '$config' does not exist" >&2
exit 1
fi
shift 2
continue
;;
'--')
shift
break
;;
*)
echo 'Internal error!' >&2
exit 1
;;
esac
done
# Making variables visible in inner bash sessions
make_vars_pre () {
export vault_pass='demo'
export vault_file="$(pwd)/sage_vault_pass.txt"
export ssh_dir="${HOME}/.ssh/sage"
export ansible_user="$(id -un)"
export ansible_host="$(hostnamectl --static)"
export ssh_port='2222'
export ssh_bind='127.0.0.1'
if [ -n "${hostname}" ]; then
export ansible_host="$hostname"
export docker_args=("${docker_args[@]}" -e EXTERNAL_HOSTNAME=${ansible_host})
fi
}
version_at_least () {
dpkg --compare-versions "$1" 'ge' "$2"
}
check () {
if ! (groups | grep -q -e sudo -e root); then
user=$(id -un)
echo "Use are running as user ${user}, which does not belong to group sudo."
echo "Please, add user ${user} to sudo group or run as a different user."
fail=1
fi
if ! command -v sshd >/dev/null; then
echo "Your system does not have sshd installed."
echo "Please, try running 'apt update && apt install openssh-server'."
fail=1
fi
if ! command -v docker >/dev/null; then
echo "Your system does not have docker installed."
echo "Please, follow instructions on 'https://docs.docker.com/engine/install/ubuntu/'" \
"and install docker."
fail=1
fi
local docker_ver_req="20.10"
local docker_ver="$(docker -v | grep -o '[0-9]\+\.[0-9]\+' | head -1)"
if ! version_at_least "${docker_ver}" "${docker_ver_req}"; then
echo "You have docker version ${docker_ver} installed, but minimum ${docker_ver_req} required."
echo "Please, follow instructions on 'https://docs.docker.com/engine/install/ubuntu/'" \
"and install latest version of docker."
fail=1
fi
if ! (docker system info 2>/dev/null | grep -q compose); then
echo "You do not have docker Compose plugin installed."
echo "Please, follow instructions on 'https://docs.docker.com/compose/install/linux/'" \
"and install latest version of plugin."
fail=1
fi
if ! command -v python3 >/dev/null; then
echo "Your system does not have python3 installed."
echo "Please, install python3."
fail=1
fi
local python_ver_req="3.8"
local python_ver="$(python3 -V | grep -o '[0-9]\+\.[0-9]\+' | head -1)"
if ! version_at_least "${python_ver}" "${python_ver_req}"; then
echo "You have python version ${python_ver} installed, but minimum ${python_ver_req} required."
echo "Please, try running 'apt update && apt install python3.10'."
fail=1
fi
if [ "$(df -l --output=avail . | tail -1)" -lt '30000000' ]; then
echo "You are running Sage Community on a disk with only $(df -lh . --output=avail | tail -1)" \
"space available (30GB required)."
echo "Please, try expanding the disk or move $0 to a different disk."
fail=1
fi
if ! (compgen -b | grep -iq chrom); then
echo "Warning: could not locate Chrome browser."
echo "This is not a problem; though other browsers are not supported explicitly."
fi
if [ -n "${fail}" ]; then
exit 1
fi
echo 'OK'
}
config () {
echo "${vault_pass}" > "${vault_file}"
# if [ ! -e '.external_hostname' ] && ! grep -q ${ansible_host:-placeholder} /etc/hosts ; then
# echo '--> Create hosts record'
# echo "127.0.0.1 ${ansible_host}" | sudo tee -a /etc/hosts
# fi
echo '--> Login to Tinkoff container registry'
cat <<EOF | sudo docker login --username json_key --password-stdin cr.yandex
{
"created_at": "2023-04-18T15:28:49.752643522Z",
"id": "ajej1bpt8jrps5cctvm9",
"key_algorithm": "RSA_2048",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDR4gBffVVzUOLJ\nNqYcU5jD6BAHuBfJhV8sPMgVcuos3NTT9gFdMmguuVB6sb7k8Hzc8Xmyg+MLZZeo\nUrtDAFWMVAb+Uuh9P0Ss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6CeI4R/6RhweaShf49\noaR8+l1INjNeSkJ3IkM6PVF89zufnImLWuYg1CBS8Li8iAuML+Pktt9GtwKBgQCe\nVNPS6GwnGzIcT+juj3c7Qu6QkLCRV95gMYzxu751DSz4VgQOJCganA68O93ZoiTn\nJZD9D6YcyNE46Gt1k/5RH3aZx6rkngVg3YLD09T8z+LxLXvTPNyzvzFy0o7rZJa2\nFrvAlwJHQM36d+EfSVOV+/ABSYVCSGuq2TF+8DeajQKBgHXsIN0OsD+Y4b/+yRlO\n1kIZTYekjy49K32DM0GlltnzV9kDdtceh1IaWqI34T9NusiFAtldFIU9xRmgdZOR\nlabsYvZXeyOixsAGCF7+jWaffx3jVGHkdtuoI/lEX474H9BjEWQPSF+UVlcxWzVY\njrJ9+qfMVmcHTRyxsAQJHphH\n-----END PRIVATE KEY-----\n",
"public_key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0eIAX31Vc1DiyTamHFOY\nw+gQB7gXyYVfLDzIFXLqLNzU0/YBXTJoLrlQerG+5PB83PF5soPjC2WXqFK7QwBV\njFQG/lLofT9ErCSXZ1wimwrd2ucAiOTwwex3L0pWH8qxyOUPWMYyJAj2R4rEHIZQ\nMtC8rIXPP0dkOaSmmHdsHGqQQQSlDYakyYbHcIdR3pxiJfOdawakh6w6tb7PNyk7\nKV49ZA8rks8miyI2OSWFBSoUEQYKKohnpB0yksXUiu3+cx7K2Fmx9gFJ3yggg/vX\n/DEgICEVeuWk6536N+SNxeZanRlHb6gOAUx4iA2fXnZaoG7KAqkf/EslkxQ5DyNo\nhQIDAQAB\n-----END PUBLIC KEY-----\n",
"service_account_id": "ajeo1o7m5pbfmqq2mlud"
}
EOF
echo '--> Generate sudoers.d config'
echo 'Please, enter sudo password, if requested:'
sudo tee /etc/sudoers.d/sage-community >/dev/null <<EOF
# This file is needed for the Sage Community installation only
# User privilege specification
$(id -un) ALL=(ALL) NOPASSWD: ALL
EOF
echo '--> Make directories'
mkdir -p /tmp/sage "${ssh_dir}"
chmod 700 "${ssh_dir}"
echo '--> Generate temporary ssh key for the installation'
ssh-keygen -t rsa -N '' -f "${ssh_dir}/sage-community" <<<y >/dev/null
ssh-keygen -t rsa -N '' -f "${ssh_dir}/sage_host_key" <<<y >/dev/null
echo '--> Generate temporary sshd configuration'
cat "${ssh_dir}/sage-community.pub" > "${ssh_dir}/sage_authorized_keys"
chmod 600 "${ssh_dir}/sage_authorized_keys"
cat > "${ssh_dir}/sage-config" <<EOF
Port ${ssh_port}
ListenAddress ${ssh_bind}
AuthorizedKeysFile ${ssh_dir}/sage_authorized_keys
HostKey ${ssh_dir}/sage_host_key
KbdInteractiveAuthentication no
UsePAM yes
AllowAgentForwarding yes
AllowTcpForwarding yes
GatewayPorts no
PermitTTY yes
PrintMotd no
PrintLastLog no
TCPKeepAlive yes
PermitTunnel yes
PidFile ${ssh_dir}/sshd.pid
Banner none
AcceptEnv LANG LC_*
EOF
}
unconfig () {
echo '--> Remove temporary files'
rm -rf /tmp/sage "${ssh_dir}"
rm -f "${vault_file}"
echo 'Please, enter sudo password, if requested:'
sudo rm -f /etc/sudoers.d/sage-community
echo '--> Logout from Tinkoff container registry'
sudo docker logout cr.yandex
}
# Must be called in subshell due to running ssh-agent
sshd_up () {
# sshd requires absolute path to start
$(which sshd) -f "${ssh_dir}/sage-config"
eval $(ssh-agent)
ssh-add "${ssh_dir}/sage-community"
}
sshd_down () {
if [ -e "${SSH_AUTH_SOCK}" -a -e "${ssh_dir}/sage-config" ]; then
kill $(cat ${ssh_dir}/sshd.pid)
if [ -n "${SSH_AGENT_PID}" ]; then
kill ${SSH_AGENT_PID}
fi
fi
}
make_vars_post () {
docker_args=(
"${docker_args[@]}"
-it
--pull always
--name sage-installer
--network host
--add-host ${ansible_host}:127.0.0.1
--add-host 10.102.0.1.nip.io:127.0.0.1
-v $(pwd)/sage_vault_pass.txt:/root/ansible/.vault_pass.txt
-v $(readlink -f ${SSH_AUTH_SOCK}):/ssh-agent
-e SSH_AUTH_SOCK=/ssh-agent
-e ANSIBLE_SSH_PORT=${ssh_port}
)
if [ -n "$config" ]; then
docker_args=(
"${docker_args[@]}"
-v "$(pwd)/$config:/root/ansible/ext_config.yml"
-e ANSIBLE_EXTRA_ARGS='--extra-vars @/root/ansible/ext_config.yml'
)
# Map address if specified
if grep -q sagenet "$config"; then
docker_args=(
"${docker_args[@]}"
--add-host $(grep gateway "$config" | grep -o '[0-9.]*' | head -1).nip.io:127.0.0.1
)
fi
fi
export docker_args=(
"${docker_args[@]}"
cr.yandex/crprr2k9fm05ldok4bht/sage-trukk-demo:latest
/root/ansible/setup-community.sh
)
}
abs_filename () {
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}
docker_rm () {
sudo docker rm -f sage-installer > /dev/null 2>&1
}
docker_run () {
docker_rm
sudo docker run "${docker_args[@]}" $SUBCOMMAND ${ansible_user} ${ansible_host}
}
case "$SUBCOMMAND" in
"check")
check
;;
"install")
(
make_vars_pre
config
sshd_up
make_vars_post
docker_run $*
sudo docker cp sage-installer:/root/ansible/sage_hostname.txt .
sudo docker cp sage-installer:/root/ansible/files/certificates/demo_local_localhost.tgz.gpg .
docker_rm
sshd_down
unconfig
echo "Please, navigate to $(cat sage_hostname.txt)"
rm -f sage_hostname.txt
)
;;
"group-add")
(
if [ ! -e "$2" ]; then
usage 1>&2
exit 1
fi
make_vars_pre
config
sshd_up
export docker_args=(
${docker_args[@]}
--rm
-e ACCEPT_EULA=yes
-v "$(abs_filename $2)":/root/ansible/new_groups.yml
-v $(pwd)/demo_local_localhost.tgz.gpg:/root/ansible/files/certificates/demo_local_localhost.tgz.gpg
)
make_vars_post
docker_run $*
sshd_down
unconfig
)
;;
"wipe")
(
make_vars_pre
config
sshd_up
export docker_args=(
${docker_args[@]}
--rm
-e ACCEPT_EULA=yes
)
make_vars_post
docker_run $*
sshd_down
unconfig
)
;;
*)
usage 1>&2
;;
esac