Skip to content

Commit f97b708

Browse files
authored
Improve script and download robustness (#73)
* Improve robustness of bash scripts * Remove intel macos and make wget quiet * Removed quotes around patterns * Simplified sudoIfAvailable
1 parent 6c8b6f3 commit f97b708

File tree

5 files changed

+158
-73
lines changed

5 files changed

+158
-73
lines changed

.circleci/test-deploy.yml

+5-15
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ integration-tests: &integration-tests
1313
orb-tools/pack,
1414
integration-test-install,
1515
integration-test-install-release,
16-
integration-test-install-release-macos-intel-on-arm,
1716
integration-test-run-command,
1817
integration-test-run-tests
1918
]
@@ -23,10 +22,6 @@ executors:
2322
machine:
2423
image: ubuntu-2204:2024.01.1
2524
macos:
26-
macos:
27-
xcode: 15.1.0
28-
resource_class: macos.x86.medium.gen2
29-
macos-arm:
3025
macos:
3126
xcode: 15.3.0
3227
resource_class: macos.m1.medium.gen1
@@ -463,33 +458,28 @@ workflows:
463458
- integration-test-install:
464459
matrix:
465460
parameters:
466-
executor: [linux, windows, macos, macos-arm]
461+
executor: [linux, windows, macos]
467462

468463
- integration-test-install-release:
469464
matrix:
470465
parameters:
471-
executor: [linux, windows, macos, macos-arm]
466+
executor: [linux, windows, macos]
472467
release: [R2023bU1]
473468

474-
- integration-test-install-release:
475-
name: integration-test-install-release-macos-intel-on-arm
476-
executor: macos-arm
477-
release: R2023aU1
478-
479469
- integration-test-run-command:
480470
matrix:
481471
parameters:
482-
executor: [linux, windows, macos, macos-arm]
472+
executor: [linux, windows, macos]
483473

484474
- integration-test-run-tests:
485475
matrix:
486476
parameters:
487-
executor: [linux, windows, macos, macos-arm]
477+
executor: [linux, windows, macos]
488478

489479
- integration-test-run-build:
490480
matrix:
491481
parameters:
492-
executor: [linux, windows, macos, macos-arm]
482+
executor: [linux, windows, macos]
493483

494484
- orb-tools/pack:
495485
filters: *filters

src/scripts/install.sh

+43-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env bash
1+
#!/bin/bash
22

33
# Exit script if you try to use an uninitialized variable.
44
set -o nounset
@@ -10,23 +10,42 @@ set -o errexit
1010
set -o pipefail
1111

1212
sudoIfAvailable() {
13-
if [[ -x $(command -v sudo) ]]; then
13+
if command -v sudo >/dev/null 2>&1; then
1414
sudo -E bash "$@"
1515
else
1616
bash "$@"
1717
fi
1818
}
1919

20-
downloadAndRun() {
21-
url=$1
22-
shift
23-
curl -sfL $url | sudoIfAvailable -s -- "$@"
20+
stream() {
21+
local url="$1"
22+
if command -v wget >/dev/null 2>&1; then
23+
wget --retry-connrefused --waitretry=5 -qO- "$url"
24+
elif command -v curl >/dev/null 2>&1; then
25+
curl --retry 5 --retry-connrefused --retry-delay 5 -sSL "$url"
26+
else
27+
echo "Could not find wget or curl command" >&2
28+
return 1
29+
fi
30+
}
31+
32+
download() {
33+
local url="$1"
34+
local filename="$2"
35+
if command -v wget >/dev/null 2>&1; then
36+
wget --retry-connrefused --waitretry=5 -qO "$filename" "$url" 2>&1
37+
elif command -v curl >/dev/null 2>&1; then
38+
curl --retry 5 --retry-connrefused --retry-delay 5 -sSLo "$filename" "$url"
39+
else
40+
echo "Could not find wget or curl command" >&2
41+
return 1
42+
fi
2443
}
2544

2645
os=$(uname)
2746
arch=$(uname -m)
2847
binext=""
29-
tmpdir=$(dirname "$(mktemp -u)")
48+
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'install')
3049
rootdir="$tmpdir/matlab_root"
3150
batchdir="$tmpdir/matlab-batch"
3251
mpmdir="$tmpdir/mpm"
@@ -35,49 +54,49 @@ mpmbaseurl="https://www.mathworks.com/mpm"
3554

3655
# resolve release
3756
parsedrelease=$(echo "$PARAM_RELEASE" | tr '[:upper:]' '[:lower:]')
38-
if [[ $parsedrelease = "latest" ]]; then
39-
mpmrelease=$(curl https://ssd.mathworks.com/supportfiles/ci/matlab-release/v0/latest)
57+
if [[ "$parsedrelease" = "latest" ]]; then
58+
mpmrelease=$(stream https://ssd.mathworks.com/supportfiles/ci/matlab-release/v0/latest)
4059
else
41-
mpmrelease="${parsedrelease}"
60+
mpmrelease="$parsedrelease"
4261
fi
4362

4463
# validate release is supported
45-
if [[ $mpmrelease < "r2020b" ]]; then
64+
if [[ "$mpmrelease" < "r2020b" ]]; then
4665
echo "Release '${mpmrelease}' is not supported. Use 'R2020b' or a later release.">&2
4766
exit 1
4867
fi
4968

5069
# install system dependencies
51-
if [[ $os = Linux ]]; then
70+
if [[ "$os" = "Linux" ]]; then
5271
# install MATLAB dependencies
5372
release=$(echo "${mpmrelease}" | grep -ioE "(r[0-9]{4}[a-b])")
54-
downloadAndRun https://ssd.mathworks.com/supportfiles/ci/matlab-deps/v0/install.sh "$release"
73+
stream https://ssd.mathworks.com/supportfiles/ci/matlab-deps/v0/install.sh | sudoIfAvailable -s -- "$release"
5574
# install mpm depencencies
5675
sudoIfAvailable -c "apt-get install --no-install-recommends --no-upgrade --yes \
5776
wget \
5877
unzip \
5978
ca-certificates"
60-
elif [[ $os = Darwin && $arch = arm64 ]]; then
61-
if [[ $mpmrelease < "r2023b" ]]; then
79+
elif [[ "$os" = "Darwin" && "$arch" = "arm64" ]]; then
80+
if [[ "$mpmrelease" < "r2023b" ]]; then
6281
# install Rosetta 2
6382
sudoIfAvailable -c "softwareupdate --install-rosetta --agree-to-license"
6483
else
6584
# install Java runtime
6685
jdkpkg="$tmpdir/jdk.pkg"
67-
curl -sfL https://corretto.aws/downloads/latest/amazon-corretto-8-aarch64-macos-jdk.pkg -o $jdkpkg
68-
sudoIfAvailable -c "installer -pkg $jdkpkg -target /"
86+
download https://corretto.aws/downloads/latest/amazon-corretto-8-aarch64-macos-jdk.pkg "$jdkpkg"
87+
sudoIfAvailable -c "installer -pkg '$jdkpkg' -target /"
6988
fi
7089
fi
7190

7291
# set os specific options
73-
if [[ $os = CYGWIN* || $os = MINGW* || $os = MSYS* ]]; then
92+
if [[ "$os" = CYGWIN* || "$os" = MINGW* || "$os" = MSYS* ]]; then
7493
mwarch="win64"
7594
binext=".exe"
7695
rootdir=$(cygpath "$rootdir")
7796
mpmdir=$(cygpath "$mpmdir")
7897
batchdir=$(cygpath "$batchdir")
79-
elif [[ $os = Darwin ]]; then
80-
if [[ $arch = arm64 && ! $mpmrelease < "r2023b" ]]; then
98+
elif [[ "$os" = "Darwin" ]]; then
99+
if [[ "$arch" = "arm64" && ! "$mpmrelease" < "r2023b" ]]; then
81100
mwarch="maca64"
82101
else
83102
mwarch="maci64"
@@ -93,18 +112,18 @@ mkdir -p "$batchdir"
93112
mkdir -p "$mpmdir"
94113

95114
# install mpm
96-
curl -o "$mpmdir/mpm$binext" -sfL "$mpmbaseurl/$mwarch/mpm"
115+
download "$mpmbaseurl/$mwarch/mpm" "$mpmdir/mpm$binext"
97116
chmod +x "$mpmdir/mpm$binext"
98117

99118
# install matlab-batch
100-
curl -o "$batchdir/matlab-batch$binext" -sfL "$batchbaseurl/$mwarch/matlab-batch$binext"
119+
download "$batchbaseurl/$mwarch/matlab-batch$binext" "$batchdir/matlab-batch$binext"
101120
chmod +x "$batchdir/matlab-batch$binext"
102121

103122
# install matlab
104123
"$mpmdir/mpm$binext" install \
105-
--release=$mpmrelease \
124+
--release="$mpmrelease" \
106125
--destination="$rootdir" \
107126
--products ${PARAM_PRODUCTS} MATLAB
108127

109128
# add MATLAB and matlab-batch to path
110-
echo 'export PATH="'$rootdir'/bin:'$batchdir':$PATH"' >> $BASH_ENV
129+
echo 'export PATH="'$rootdir'/bin:'$batchdir':$PATH"' >> $BASH_ENV

src/scripts/run-build.sh

+34-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
1-
downloadAndRun() {
2-
url=$1
3-
shift
4-
if [[ -x $(command -v sudo) ]]; then
5-
curl -sfL $url | sudo -E bash -s -- "$@"
1+
#!/bin/bash
2+
3+
# Exit script if you try to use an uninitialized variable.
4+
set -o nounset
5+
6+
# Exit script if a statement returns a non-true return value.
7+
set -o errexit
8+
9+
# Use the error status of the first failure, rather than that of the last item in a pipeline.
10+
set -o pipefail
11+
12+
sudoIfAvailable() {
13+
if command -v sudo >/dev/null 2>&1; then
14+
sudo -E bash "$@"
15+
else
16+
bash "$@"
17+
fi
18+
}
19+
20+
stream() {
21+
local url="$1"
22+
if command -v wget >/dev/null 2>&1; then
23+
wget --retry-connrefused --waitretry=5 -qO- "$url"
24+
elif command -v curl >/dev/null 2>&1; then
25+
curl --retry 5 --retry-connrefused --retry-delay 5 -sSL "$url"
626
else
7-
curl -sfL $url | bash -s -- "$@"
27+
echo "Could not find wget or curl command" >&2
28+
return 1
829
fi
930
}
1031

11-
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'run-command')
32+
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'run-build')
1233

1334
# install run-matlab-command
14-
downloadAndRun https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/install.sh "${tmpdir}/bin"
35+
stream https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/install.sh | sudoIfAvailable -s -- "${tmpdir}/bin"
1536

1637
# form OS appropriate paths for MATLAB
1738
os=$(uname)
18-
scriptdir=$tmpdir
39+
scriptdir="$tmpdir"
1940
binext=""
20-
if [[ $os = CYGWIN* || $os = MINGW* || $os = MSYS* ]]; then
41+
if [[ "$os" = CYGWIN* || "$os" = MINGW* || "$os" = MSYS* ]]; then
2142
scriptdir=$(cygpath -w "$scriptdir")
2243
binext=".exe"
2344
fi
@@ -33,12 +54,12 @@ if [ -n "$PARAM_BUILD_OPTIONS" ]; then
3354
fi
3455

3556
# create script to execute
36-
script=command_${RANDOM}
37-
scriptpath=${tmpdir}/${script}.m
57+
script="command_${RANDOM}"
58+
scriptpath="${tmpdir}/${script}.m"
3859
echo "cd(getenv('MW_ORIG_WORKING_FOLDER'));" > "$scriptpath"
3960
cat << EOF >> "$scriptpath"
4061
$buildCommand
4162
EOF
4263

4364
# run MATLAB command
44-
"${tmpdir}/bin/run-matlab-command$binext" "setenv('MW_ORIG_WORKING_FOLDER', cd('${scriptdir//\'/\'\'}'));$script" $PARAM_STARTUP_OPTIONS
65+
"${tmpdir}/bin/run-matlab-command$binext" "setenv('MW_ORIG_WORKING_FOLDER', cd('${scriptdir//\'/\'\'}'));$script" $PARAM_STARTUP_OPTIONS

src/scripts/run-command.sh

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1-
downloadAndRun() {
2-
url=$1
3-
shift
4-
if [[ -x $(command -v sudo) ]]; then
5-
curl -sfL $url | sudo -E bash -s -- "$@"
1+
#!/bin/bash
2+
3+
# Exit script if you try to use an uninitialized variable.
4+
set -o nounset
5+
6+
# Exit script if a statement returns a non-true return value.
7+
set -o errexit
8+
9+
# Use the error status of the first failure, rather than that of the last item in a pipeline.
10+
set -o pipefail
11+
12+
sudoIfAvailable() {
13+
if command -v sudo >/dev/null 2>&1; then
14+
sudo -E bash "$@"
15+
else
16+
bash "$@"
17+
fi
18+
}
19+
20+
stream() {
21+
local url="$1"
22+
if command -v wget >/dev/null 2>&1; then
23+
wget --retry-connrefused --waitretry=5 -qO- "$url"
24+
elif command -v curl >/dev/null 2>&1; then
25+
curl --retry 5 --retry-connrefused --retry-delay 5 -sSL "$url"
626
else
7-
curl -sfL $url | bash -s -- "$@"
27+
echo "Could not find wget or curl command" >&2
28+
return 1
829
fi
930
}
1031

1132
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'run-command')
1233

1334
# install run-matlab-command
14-
downloadAndRun https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/install.sh "${tmpdir}/bin"
35+
stream https://ssd.mathworks.com/supportfiles/ci/run-matlab-command/v2/install.sh | sudoIfAvailable -s -- "${tmpdir}/bin"
1536

1637
# form OS appropriate paths for MATLAB
1738
os=$(uname)
18-
scriptdir=$tmpdir
39+
scriptdir="$tmpdir"
1940
binext=""
20-
if [[ $os = CYGWIN* || $os = MINGW* || $os = MSYS* ]]; then
41+
if [[ "$os" = CYGWIN* || "$os" = MINGW* || "$os" = MSYS* ]]; then
2142
scriptdir=$(cygpath -w "$scriptdir")
2243
binext=".exe"
2344
fi
2445

2546
# create script to execute
26-
script=command_${RANDOM}
27-
scriptpath=${tmpdir}/${script}.m
47+
script="command_${RANDOM}"
48+
scriptpath="${tmpdir}/${script}.m"
2849
echo "cd(getenv('MW_ORIG_WORKING_FOLDER'));" > "$scriptpath"
2950
cat << EOF >> "$scriptpath"
3051
${PARAM_COMMAND}

0 commit comments

Comments
 (0)