-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.sh
executable file
·481 lines (415 loc) · 14.1 KB
/
fix.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/bin/bash -eu
set -o pipefail
if [ -n "${FIX_SH_TIMING_LOG+x}" ]; then
rm -f "${FIX_SH_TIMING_LOG}"
if ! type gdate >/dev/null 2>&1; then sudo ln -sf /bin/date /bin/gdate; fi
fi
debug_timing() {
if [ -n "${FIX_SH_TIMING_LOG+x}" ]; then
# shellcheck disable=SC2034
_lastcmd=$(gdate +%s%3N)
last_command='start'
# shellcheck disable=SC2154
trap '_now=$(gdate +%s%3N); duration=$((_now - _lastcmd)); echo ${duration} ms: $last_command >> '"${FIX_SH_TIMING_LOG}"'; last_command="$BASH_COMMAND" >> '"${FIX_SH_TIMING_LOG}"'; _lastcmd=$_now' DEBUG
fi
}
# copy this into any function you want to debug further
debug_timing
set -o pipefail
install_rbenv() {
if [ "$(uname)" == "Darwin" ]
then
HOMEBREW_NO_AUTO_UPDATE=1 brew install rbenv || true
if ! type rbenv 2>/dev/null
then
# https://github.com/pyenv/pyenv-installer/blob/master/bin/pyenv-installer
>&2 cat <<EOF
WARNING: seems you still have not added 'rbenv' to the load path.
# Load rbenv automatically by adding
# the following to ~/.bashrc:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
EOF
fi
else
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
fi
}
set_rbenv_env_variables() {
export PATH="${HOME}/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
}
install_ruby_build() {
if [ "$(uname)" == "Darwin" ]
then
HOMEBREW_NO_AUTO_UPDATE=1 brew install ruby-build || true
else
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
fi
}
ensure_ruby_build() {
if ! type ruby-build >/dev/null 2>&1 && ! [ -d "${HOME}/.rbenv/plugins/ruby-build" ]
then
install_ruby_build
fi
}
ensure_rbenv() {
if ! type rbenv >/dev/null 2>&1 && ! [ -f "${HOME}/.rbenv/bin/rbenv" ]
then
install_rbenv
fi
set_rbenv_env_variables
ensure_ruby_build
}
latest_ruby_version() {
major_minor=${1}
# Double check that this command doesn't error out under -e
rbenv install --list >/dev/null 2>&1
# not sure why, but 'rbenv install --list' below exits with error code
# 1...after providing the same output the previous line gave when it
# exited with error code 0.
#
# https://github.com/rbenv/rbenv/issues/1441
set +e
rbenv install --list 2>/dev/null | cat | grep "^${major_minor}."
set -e
}
ensure_binary_library() {
library_base_name=${1:?library base name - like libfoo}
homebrew_package=${2:?homebrew package}
apt_package=${3:-${homebrew_package}}
if ! [ -f /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/"${library_base_name}*.dylib" ] && \
! [ -f /opt/homebrew/lib/"${library_base_name}*.dylib" ] && \
! [ -f /usr/lib/"${library_base_name}.so" ] && \
! [ -f /usr/lib/x86_64-linux-gnu/"${library_base_name}.so" ] && \
! [ -f /usr/local/lib/"${library_base_name}.so" ] && \
! [ -f /usr/local/opt/"${homebrew_package}/lib/${library_base_name}*.dylib" ]
then
if ! compgen -G "/opt/homebrew/Cellar/${homebrew_package}"*/*/"lib/${library_base_name}"*.dylib >/dev/null 2>&1
then
install_package "${homebrew_package}" "${apt_package}"
fi
fi
}
ensure_ruby_build_requirements() {
ensure_dev_library readline/readline.h readline libreadline-dev
ensure_dev_library zlib.h zlib zlib1g-dev
ensure_dev_library openssl/ssl.h openssl libssl-dev
ensure_dev_library yaml.h libyaml libyaml-dev
}
ensure_latest_ruby_build_definitions() {
ensure_rbenv
# last_pulled_unix_epoch="$(stat -f '%m' "$(rbenv root)"/plugins/ruby-build/.git/FETCH_HEAD)"
# # if not pulled in last 24 hours
# if [ $(( $(date +%s) - last_pulled_unix_epoch )) -gt $(( 24 * 60 * 60 )) ]
# then
git -C "$HOME"/.rbenv/plugins/ruby-build pull --force
# fi
}
# https://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-posix-sh
contains() {
string="$1"
substring="$2"
if [ "${string#*"$substring"}" != "$string" ]; then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
# You can find out which feature versions are still supported / have
# been release here: https://www.ruby-lang.org/en/downloads/
ensure_ruby_versions() {
ensure_latest_ruby_build_definitions
# You can find out which feature versions are still supported / have
# been release here: https://www.ruby-lang.org/en/downloads/
ruby_versions="$(latest_ruby_version 3.1)"
installed_ruby_versions="$(rbenv versions --bare --skip-aliases)"
echo "Latest Ruby versions: ${ruby_versions}"
for ver in $ruby_versions
do
if ! contains "${installed_ruby_versions}"$'\n' "${ver}"$'\n'; then
echo "Installing Ruby version $ver - existing versions: $installed_ruby_versions"
ensure_ruby_build_requirements
rbenv install -s "${ver}"
hash -r # ensure we are seeing latest bundler etc
else
echo "Found Ruby version $ver already installed"
fi
done
}
ensure_bundle() {
# Not sure why this is needed a second time, but it seems to be?
#
# https://app.circleci.com/pipelines/github/apiology/source_finder/21/workflows/88db659f-a4f4-4751-abc0-46f5929d8e58/jobs/107
set_rbenv_env_variables
bundler_version=$(ruby -e 'require "rubygems/bundler_version_finder"; puts Gem::BundlerVersionFinder.bundler_version')
# if bundler_version is empty
if [ -z "${bundler_version}" ]
then
bundler_version=$(bundle --version | cut -d ' ' -f 3)
fi
# if bundler_version is still empty
if [ -z "${bundler_version}" ]
then
gem install bundler:2.5.5
bundler_version=$(bundle --version | cut -d ' ' -f 3)
fi
echo "Bundler version: ${bundler_version}"
bundler_version_major=$(cut -d. -f1 <<< "${bundler_version}")
bundler_version_minor=$(cut -d. -f2 <<< "${bundler_version}")
bundler_version_patch=$(cut -d. -f3 <<< "${bundler_version}")
#
# Version 2.5.5 fixed an issue in 2.2.22 with the 'bump' gem:
#
# https://app.circleci.com/pipelines/github/apiology/checkoff/1281/workflows/f667f909-c3fc-4ae2-8593-dde2b588a7a7/jobs/2491
need_better_bundler=false
if [ "${bundler_version_major}" -lt 2 ]
then
need_better_bundler=true
elif [ "${bundler_version_major}" -eq 2 ]
then
if [ "${bundler_version_minor}" -lt 5 ]
then
need_better_bundler=true
elif [ "${bundler_version_minor}" -eq 5 ]
then
if [ "${bundler_version_patch}" -lt 5 ]
then
need_better_bundler=true
fi
fi
fi
if [ "${need_better_bundler}" = true ]
then
>&2 echo "Original bundler version: ${bundler_version}"
# need to do this first before 'bundle update --bundler' will work
make bundle_install
bundle update --bundler
gem install bundler:2.5.5
>&2 echo "Updated bundler version: $(bundle --version)"
# ensure next step installs fresh bundle
rm -f Gemfile.lock.installed
fi
# https://bundler.io/v2.0/bundle_lock.html#SUPPORTING-OTHER-PLATFORMS
#
# "If you want your bundle to support platforms other than the one
# you're running locally, you can run bundle lock --add-platform
# PLATFORM to add PLATFORM to the lockfile, force bundler to
# re-resolve and consider the new platform when picking gems, all
# without needing to have a machine that matches PLATFORM handy to
# install those platform-specific gems on.'
#
# This affects nokogiri, which will try to reinstall itself in
# Docker builds where it's already installed if this is not run.
make Gemfile.lock
PLATFORMS="arm64-darwin-24 x86_64-linux x86_64-linux-musl aarch64-linux aarch64-linux-musl"
for platform in ${PLATFORMS}
do
if ! grep -q "^ ${platform}$" Gemfile.lock
then
echo "Missing platform $platform in Gemfile.lock - adding all of ${PLATFORMS}"
bundle lock --add-platform "${PLATFORMS// /,}"
break
fi
done
ls -l
make bundle_install
}
set_ruby_local_version() {
latest_ruby_version="$(cut -d' ' -f1 <<< "${ruby_versions}")"
echo "${latest_ruby_version}" > .ruby-version
}
latest_python_version() {
major_minor=${1}
# https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
pyenv install --list | grep "^ ${major_minor}." | grep -v -- -dev | tail -1 | xargs
}
install_pyenv() {
if [ "$(uname)" == "Darwin" ]
then
HOMEBREW_NO_AUTO_UPDATE=1 brew install pyenv || true
if ! type pyenv 2>/dev/null
then
# https://github.com/pyenv/pyenv-installer/blob/master/bin/pyenv-installer
>&2 cat <<EOF
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PYENV_ROOT="${HOME}/.pyenv"
export PATH="${PYENV_ROOT}/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
EOF
fi
else
curl https://pyenv.run | bash
fi
}
set_pyenv_env_variables() {
# looks like pyenv scripts aren't -u clean:
#
# https://app.circleci.com/pipelines/github/apiology/cookiecutter-pypackage/15/workflows/10506069-7662-46bd-b915-2992db3f795b/jobs/15
set +u
export PYENV_ROOT="${HOME}/.pyenv"
export PATH="${PYENV_ROOT}/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
set -u
}
ensure_pyenv() {
if ! type pyenv >/dev/null 2>&1 && ! [ -f "${HOME}/.pyenv/bin/pyenv" ]
then
install_pyenv
fi
if ! type pyenv >/dev/null 2>&1
then
set_pyenv_env_variables
fi
}
update_package() {
homebrew_package=${1:?homebrew package}
apt_package=${2:-${homebrew_package}}
if [ "$(uname)" == "Darwin" ]
then
brew install "${homebrew_package}"
elif type apt-get >/dev/null 2>&1
then
make update_apt
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y "${apt_package}"
else
>&2 echo "Teach me how to install packages on this plaform"
exit 1
fi
}
ensure_python_build_requirements() {
ensure_dev_library zlib.h zlib zlib1g-dev
ensure_dev_library bzlib.h bzip2 libbz2-dev
ensure_dev_library openssl/ssl.h openssl libssl-dev
ensure_dev_library ffi/ffi.h libffi libffi-dev
ensure_dev_library sqlite3.h sqlite3 libsqlite3-dev
ensure_dev_library lzma.h xz liblzma-dev
ensure_dev_library readline/readline.h readline libreadline-dev
}
# You can find out which feature versions are still supported / have
# been release here: https://www.python.org/downloads/
ensure_python_versions() {
# You can find out which feature versions are still supported / have
# been release here: https://www.python.org/downloads/
python_versions="$(latest_python_version 3.12)"
echo "Latest Python versions: ${python_versions}"
installed_python_versions="$(pyenv versions --skip-envs --skip-aliases --bare)"
for ver in $python_versions
do
if ! contains "${installed_python_versions}"$'\n' "${ver}"$'\n'; then
echo "Installing Python version $ver - existing versions: $installed_python_versions"
ensure_python_build_requirements
if [ "$(uname)" == Darwin ]
then
if [ -z "${HOMEBREW_OPENSSL_PREFIX:-}" ]
then
HOMEBREW_OPENSSL_PREFIX="$(brew --prefix openssl)"
fi
pyenv_install() {
CFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/bzip2/include -I${HOMEBREW_OPENSSL_PREFIX}/include" LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib -L${HOMEBREW_OPENSSL_PREFIX}/lib" pyenv install --skip-existing "$@"
}
major_minor="$(cut -d. -f1-2 <<<"${ver}")"
pyenv_install "${ver}"
else
pyenv install -s "${ver}"
fi
hash -r
else
echo "Found Python version $ver already installed"
fi
done
}
ensure_pyenv_virtualenvs() {
latest_python_version="$(cut -d' ' -f1 <<< "${python_versions}")"
virtualenv_name="checkoff-${latest_python_version}"
if ! [ -d ~/".pyenv/versions/${virtualenv_name}" ]
then
pyenv virtualenv "${latest_python_version}" "${virtualenv_name}" || true
fi
# You can use this for your global stuff!
if ! [ -d ~/".pyenv/versions/mylibs" ]
then
pyenv virtualenv "${latest_python_version}" mylibs || true
fi
# shellcheck disable=SC2086
pyenv local "${virtualenv_name}" ${python_versions} mylibs
}
ensure_pip_and_wheel() {
# https://cve.mitre.org/cgi-bin/cvename.cgi?name=2023-5752
pip_version=$(python -c "import pip; print(pip.__version__)" | cut -d' ' -f2)
major_pip_version=$(cut -d '.' -f 1 <<< "${pip_version}")
minor_pip_version=$(cut -d '.' -f 2 <<< "${pip_version}")
if [[ major_pip_version -lt 23 ]]
then
pip install 'pip>=23.3'
elif [[ major_pip_version -eq 23 ]] && [[ minor_pip_version -lt 3 ]]
then
pip install 'pip>=23.3'
fi
# wheel is helpful for being able to cache long package builds
type wheel >/dev/null 2>&1 || pip install wheel
}
ensure_python_requirements() {
make pip_install
}
ensure_shellcheck() {
if ! type shellcheck >/dev/null 2>&1
then
install_package shellcheck
fi
}
ensure_overcommit() {
# don't run if we're in the middle of a cookiecutter child project
# test, or otherwise don't have a Git repo to install hooks into...
if [ -d .git ]
then
bundle exec overcommit --install
bundle exec overcommit --sign
bundle exec overcommit --sign pre-commit
else
>&2 echo 'Not in a git repo; not installing git hooks'
fi
}
ensure_rugged_packages_installed() {
# only needed if we don't already have rugged installed
if ! ls vendor/bundle/ruby/*/gems/rugged-* &>/dev/null
then
echo "Current directory"
pwd
echo "Vendor dir"
ls -l vendor || true
echo "List of vendor/bundle/gems:"
ls vendor/bundle/gems || true
echo "Did not find rugged gem installed; installing packages needed for rugged"
echo "Installed gems:"
gem list
echo "Gem environment:"
gem environment
echo "Bundle list:"
bundle list || true
ensure_binary_library libicuio icu4c libicu-dev # needed by rugged, needed by undercover
ensure_package pkg-config # needed by rugged, needed by undercover
ensure_package cmake # needed by rugged, needed by undercover
fi
}
ensure_rbenv
ensure_types_built() {
make build-typecheck
}
ensure_ruby_versions
set_ruby_local_version
ensure_rugged_packages_installed
ensure_bundle
ensure_pyenv
ensure_python_versions
ensure_pyenv_virtualenvs
ensure_pip_and_wheel
ensure_python_requirements
ensure_shellcheck
ensure_types_built
ensure_overcommit