-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvirtual_workstation_setup.sh
executable file
·335 lines (286 loc) · 10.9 KB
/
virtual_workstation_setup.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
# Operations in this file should be idempotent-ish. At the very least, make
# sure your code doesn't do duplicate work/clobber existing files on rerun
set -euo pipefail
workdir=${1:-$HOME}
if [[ -z $(git config --get user.name) ]]; then
echo "Please ensure your git credentials are set up; see the onboarding wiki page for more info"
exit 1
fi
silent_grep() {
command grep -q > /dev/null 2>&1 "$@"
}
set +e
# we assume that if the user has non default SSH key filename they know about ssh-agent
# and can set it up themselves
for filename in ~/.ssh/id_*; do
[[ -e "$filename" && "$filename" != *".pub" ]] || continue
# check if SSH key has passphrase
ssh-keygen -y -P "" -f "$filename" > /dev/null 2>&1
[ $? -eq 0 ] && continue
# check if SSH agent is running and have any SSH keys added
# (we assume that the key with passphrase will be added, but not checking it explicitly)
ssh-add -l > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "SSH key with passphare found: $filename"
echo "Please do the following steps:"
echo " - Setup ssh-agent"
echo " - Add SSH key with passphrase to the ssh-agent"
echo " - Rerun the Evergreen project setup command"
echo "It will help to avoid entering passphrase during the setup script run"
exit 1
fi
done
set -e
# SSH into GitHub and check for the success message. The SSH command
# returns 1, so it can't be used alone
github_test=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -T [email protected] 2>&1 || true)
if ! (echo "${github_test}" | silent_grep "You've successfully authenticated, but GitHub does not provide shell access"); then
echo "Cannot login to GitHub via SSH"
echo "Please ensure your GitHub SSH keys have been set up; see the onboarding wiki page for more info"
echo "Your SSH Public Keys:"
cat ~/.ssh/id_*.pub || true
exit 1
fi
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
evg_user() {
local evg_username=$(grep -Po '(?<=user: ).*' "$HOME/.evergreen.yml")
if [ -z "$evg_username" ]; then
evg_username=$(grep -Po '(?<=user:).*' "$HOME/.evergreen.yml")
fi
echo "$evg_username"
}
# append a given block to a file. Surround it with the markers so
# it looks like this:
# # BEGIN Marker
# block goes here
# # END Marker
#
# Do not attempt to change the contents of a block in the future with this
# function. It doesn't support updating a block, so just make a block with a
# new marker, and yell at the user at the bottom of the script to fix it
#
# arg1: file
# arg2: a unique marker text for the block being appended
# arg3: block to inject
# arg4: if non empty, do not append if the file does not exist
idem_file_append() {
if [[ -z "$1" ]]; then
return 1
fi
if [[ ! -f "$1" && -n "${4-}" ]]; then
return
fi
if [[ -z "$2" ]]; then
return 2
fi
if [[ -z "$3" ]]; then
return 3
fi
local start_marker="# BEGIN $2"
local end_marker="# END $2"
if ! silent_grep "^$start_marker" "$1"; then
{
echo -e "\n$start_marker";
echo -e "$3";
echo -e "$end_marker";
} >> "$1"
fi
}
# Here's a quick explanation of what's going on here for the next soul here:
# If you like visuals instead: https://shreevatsa.files.wordpress.com/2008/03/bashstartupfiles1.png
# bash_profile is generally invoked once per login shell.
# bashrc is invoked for interactive, non-login shells
# setup_bash will make bash_profile source .bashrc (see the "may include" in
# the above diagram), and the .bashrc will source the server_bashrc.sh file
# in this repo.
# This ensures that no matter which kind of interactive shell you're using, the
# server_bashrc.sh has been sourced. While we technically could just source
# server_bashrc.sh in both .bash_profile, and .bashrc, this could break if
# something gets added to it that cannot be sourced more than once in the same
# session.
setup_bash() {
# Bash profile should source .bashrc
echo "################################################################################"
echo "Setting up bash..."
local block=$(cat <<BLOCK
if [[ -f ~/.bashrc ]]; then
source ~/.bashrc
fi
BLOCK
)
idem_file_append ~/.bash_profile "Source .bashrc" "$block"
idem_file_append ~/.bashrc "Source server_bashrc.sh" "source $HOME/mongodb-mongo-master/server-workflow-tool/server_bashrc.sh"
set +o nounset
source ~/.bash_profile
set -o nounset
}
setup_master() {
echo "################################################################################"
echo "Setting up the mongo repo..."
if [[ -d mongo ]]; then
echo "'mongo' dir exists; skipping setup"
return
fi
git clone [email protected]:10gen/mongo.git
pushd "$workdir/mongo"
/opt/mongodbtoolchain/v4/bin/python3 -m venv python3-venv
# virtualenv doesn't like nounset
set +o nounset
source python3-venv/bin/activate
set -o nounset
python -m pip install "pip==21.0.1"
python -m pip install 'poetry==1.5.1'
# PYTHON_KEYRING_BACKEND is needed to make poetry install work
# See guide https://wiki.corp.mongodb.com/display/KERNEL/Virtual+Workstation
export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
python -m poetry install --no-root --sync
python buildscripts/scons.py --variables-files=etc/scons/mongodbtoolchain_stable_clang.vars compiledb -j$(grep -c ^processor /proc/cpuinfo)
buildninjaic
set +o nounset
deactivate
set -o nounset
popd
echo "Finished setting up the mongo repo..."
}
setup_80() {
echo "################################################################################"
echo "Setting up the 8.0 branch..."
if [[ -d mongo-v80 ]]; then
echo "'mongo-v80' dir exists; skipping setup"
return
fi
pushd "$workdir/mongo"
git worktree add "$workdir/mongo-v80" v8.0
popd
pushd "$workdir/mongo-v80"
/opt/mongodbtoolchain/v4/bin/python3 -m venv python3-venv
# virtualenv doesn't like nounset
set +o nounset
source python3-venv/bin/activate
set -o nounset
python -m pip install "pip==21.0.1"
python -m pip install 'poetry==1.5.1'
# PYTHON_KEYRING_BACKEND is needed to make poetry install work
# See guide https://wiki.corp.mongodb.com/display/KERNEL/Virtual+Workstation
export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
python -m poetry install --no-root --sync
python buildscripts/scons.py --variables-files=etc/scons/mongodbtoolchain_stable_clang.vars compiledb -j$(grep -c ^processor /proc/cpuinfo)
buildninjaic
set +o nounset
deactivate
set -o nounset
popd
echo "Finished setting up the 8.0 branch"
}
setup_cr() {
echo "################################################################################"
echo "Setting up Rietveld Code Review tool..."
pushd "$workdir"
if [[ -d 'kernel-tools' ]]; then
echo "'kernel-tools' dir exists; skipping setup"
return
fi
git clone [email protected]:10gen/kernel-tools.git
popd
}
setup_gdb() {
echo "################################################################################"
echo "Setting up GDB..."
pushd "$workdir"
if [[ -d 'Boost-Pretty-Printer' ]]; then
echo "'Boost-Pretty-Printer' dir exists; skipping setup"
else
git clone [email protected]:mongodb-forks/Boost-Pretty-Printer.git
fi
# the original version of this script just appended this line, so we
# have to grep for it manually
if ! silent_grep "source $HOME/mongodb-mongo-master/server-workflow-tool/gdbinit" ~/.gdbinit; then
idem_file_append ~/.gdbinit "Server Workflow Tool gdbinit" "source $HOME/mongodb-mongo-master/server-workflow-tool/gdbinit"
fi
popd
}
setup_pipx() {
echo "################################################################################"
echo "Installing 'pipx' command..."
if command -v pipx &> /dev/null; then
echo "'pipx' command exists; skipping setup"
else
export PATH="$PATH:$HOME/.local/bin"
local venv_name="tmp-pipx-venv"
/opt/mongodbtoolchain/v4/bin/python3 -m venv $venv_name
# virtualenv doesn't like nounset
set +o nounset
source $venv_name/bin/activate
set -o nounset
python -m pip install --upgrade "pip<20.3"
python -m pip install pipx
pipx install pipx --python /opt/mongodbtoolchain/v4/bin/python3 --force
set +o nounset
deactivate
set -o nounset
rm -rf $venv_name
local marker="pipx config"
local block=$(cat <<BLOCK
# pipx will install binaries to "~/.local/bin"
export PATH="$PATH:$HOME/.local/bin"
BLOCK
)
idem_file_append ~/.bashrc "$marker" "$block"
idem_file_append ~/.zshrc "$marker" "$block" 1
fi
}
setup_evg_module_manager() {
echo "################################################################################"
echo "Installing 'evg-module-manager' command..."
export PATH="$PATH:$HOME/.local/bin"
if command -v evg-module-manager &> /dev/null; then
echo "'evg-module-manager' command exists; skipping setup"
else
pipx install evg-module-manager
fi
}
setup_db_contrib_tool() {
echo "################################################################################"
echo "Installing 'db-contrib-tool' command..."
export PATH="$PATH:$HOME/.local/bin"
if command -v db-contrib-tool &> /dev/null; then
echo "'db-contrib-tool' command exists; skipping setup"
else
pipx install db-contrib-tool
fi
}
pushd "$workdir"
set +o nounset
source ~/.bashrc
set -o nounset
sudo mkdir -p /data/db
sudo chown ubuntu /data/db
ssh-keyscan github.com >> ~/.ssh/known_hosts 2>&1
# Do setup_bash first because it affects the environment, so later steps may
# depend on having the right PATH or other settings.
setup_bash
setup_master
setup_80
setup_cr
setup_gdb
setup_pipx
setup_evg_module_manager # This step requires `setup_pipx` to have been run.
setup_db_contrib_tool # This step requires `setup_pipx` to have been run.
nag_user=1
if silent_grep server_bashrc ~/.bash_profile; then
echo "Please remove the line from your ~/.bash_profile that sources mongodb-mongo-master/server-workflow-tool/server_bashrc.sh"
nag_user=0
fi
if silent_grep "JIRA_USERNAME" ~/.bash_profile; then
echo "Please remove the line from your ~/.bash_profile that exports JIRA_USERNAME"
nag_user=0
fi
if [ $nag_user -eq 0 ]; then
echo "^^^^^^^^^^^^^^^ READ ABOVE ^^^^^^^^^^^^^^^"
fi
popd