Skip to content

Commit e817e9e

Browse files
authored
Add caching support (#86)
1 parent 68c9282 commit e817e9e

File tree

5 files changed

+147
-53
lines changed

5 files changed

+147
-53
lines changed

.circleci/test-deploy.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,39 @@ jobs:
9595
- matlab/run-command:
9696
command: "exp='<<parameters.release>>'; assert(strcmp(version('-release'),exp(2:6)))"
9797

98+
integration-test-install-with-cache:
99+
parameters:
100+
executor:
101+
type: executor
102+
executor: <<parameters.executor>>
103+
steps:
104+
- checkout
105+
- run:
106+
name: Clear cache directory before initial installation
107+
command: rm -rf $HOME/MATLAB
108+
shell: bash
109+
- matlab/install:
110+
no-output-timeout: 30m
111+
products: Simulink
112+
release: R2023a
113+
cache: true
114+
- run:
115+
name: Verify MATLAB exists in cache
116+
command: |
117+
set -e
118+
matlabroot="$HOME/MATLAB/R2023a"
119+
if [[ "$(uname)" == "Darwin" ]]; then
120+
matlabroot="$matlabroot/MATLAB.app"
121+
fi
122+
test -x "$matlabroot/bin/matlab" || {
123+
echo "MATLAB not found in cache."
124+
exit 1
125+
}
126+
echo "MATLAB found in cache."
127+
shell: bash
128+
- matlab/run-command:
129+
command: version
130+
98131
integration-test-run-command:
99132
parameters:
100133
executor:
@@ -500,6 +533,11 @@ workflows:
500533
executor: [linux, windows, macos]
501534
release: [R2023bU1]
502535

536+
- integration-test-install-with-cache:
537+
matrix:
538+
parameters:
539+
executor: [linux, windows, macos]
540+
503541
- integration-test-run-command:
504542
matrix:
505543
parameters:

src/commands/install.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,45 @@ parameters:
2727
maximum time a job is allowed to run.
2828
type: string
2929
default: 10m
30+
cache:
31+
description: >
32+
Option to enable caching, specified as false or true. By default, the value is false and the command does not store MATLAB and the specified products in a cache for future use.
33+
type: boolean
34+
default: false
3035

3136
steps:
37+
- run:
38+
name: Prepare MATLAB installation
39+
environment:
40+
PARAM_RELEASE: <<parameters.release>>
41+
PARAM_PRODUCTS: <<parameters.products>>
42+
UTILS: <<include(scripts/utils.sh)>>
43+
command: <<include(scripts/prepare-install-metadata.sh)>>
44+
shell: bash
45+
- when:
46+
condition:
47+
equal: [ << parameters.cache >>, true ]
48+
steps:
49+
- restore_cache:
50+
name: Restore MATLAB cache
51+
keys:
52+
- matlab-cache-{{ arch }}-{{ checksum "~/.matlab-circleci-orb/install-metadata.sh" }}
3253
- run:
3354
name: Install MATLAB
3455
environment:
3556
PARAM_RELEASE: <<parameters.release>>
3657
PARAM_PRODUCTS: <<parameters.products>>
58+
PARAM_CACHE: <<parameters.cache>>
59+
UTILS: <<include(scripts/utils.sh)>>
3760
command: <<include(scripts/install.sh)>>
3861
shell: bash
3962
no_output_timeout: <<parameters.no-output-timeout>>
63+
- when:
64+
condition:
65+
equal: [ << parameters.cache >>, true ]
66+
steps:
67+
- save_cache:
68+
name: Save MATLAB cache
69+
key: matlab-cache-{{ arch }}-{{ checksum "~/.matlab-circleci-orb/install-metadata.sh" }}
70+
paths:
71+
- ~/MATLAB/<<parameters.release>>

src/scripts/install.sh

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,6 @@ sudoIfAvailable() {
1717
fi
1818
}
1919

20-
stream() {
21-
local url="$1"
22-
local status=0
23-
24-
if command -v wget >/dev/null 2>&1; then
25-
wget --retry-connrefused --waitretry=5 -qO- "$url" || status=$?
26-
elif command -v curl >/dev/null 2>&1; then
27-
curl --retry 5 --retry-connrefused --retry-delay 5 -sSL "$url" || status=$?
28-
else
29-
echo "Could not find wget or curl command" >&2
30-
return 1
31-
fi
32-
33-
if [ $status -ne 0 ]; then
34-
echo "Error streaming file from $url" >&2
35-
fi
36-
37-
return $status
38-
}
39-
4020
download() {
4121
local url="$1"
4222
local filename="$2"
@@ -61,39 +41,26 @@ download() {
6141
os=$(uname)
6242
arch=$(uname -m)
6343
binext=""
44+
rootdir="$HOME/MATLAB/$PARAM_RELEASE"
6445
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'install')
65-
rootdir="$tmpdir/matlab_root"
6646
batchdir="$tmpdir/matlab-batch"
6747
mpmdir="$tmpdir/mpm"
6848
batchbaseurl="https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1"
6949
mpmbaseurl="https://www.mathworks.com/mpm"
70-
releasestatus=""
71-
72-
# resolve release
73-
parsedrelease=$(echo "$PARAM_RELEASE" | tr '[:upper:]' '[:lower:]')
74-
if [[ "$parsedrelease" = "latest" ]]; then
75-
mpmrelease=$(stream https://ssd.mathworks.com/supportfiles/ci/matlab-release/v0/latest)
76-
elif [[ "$parsedrelease" = "latest-including-prerelease" ]]; then
77-
fetched=$(stream https://ssd.mathworks.com/supportfiles/ci/matlab-release/v0/latest-including-prerelease)
78-
if [[ "$fetched" == *prerelease ]]; then
79-
mpmrelease="${fetched%prerelease}"
80-
releasestatus="--release-status=Prerelease"
81-
else
82-
mpmrelease="$fetched"
83-
fi
84-
else
85-
mpmrelease="$parsedrelease"
86-
fi
8750

88-
# validate release is supported
89-
if [[ "$mpmrelease" < "r2020b" ]]; then
90-
echo "Release '${mpmrelease}' is not supported. Use 'R2020b' or a later release.">&2
91-
exit 1
92-
fi
51+
eval "$UTILS"
52+
# shellcheck disable=SC1090
53+
source ~/.matlab-circleci-orb/install-metadata.sh
54+
# RELEASE and RELEASE_STATUS are defined in install-metadata.sh
55+
# shellcheck disable=SC2153
56+
mpmrelease=$RELEASE
57+
# shellcheck disable=SC2153
58+
releasestatus=$RELEASE_STATUS
9359

9460
# install system dependencies
9561
if [[ "$os" = "Linux" ]]; then
9662
# install MATLAB dependencies
63+
# shellcheck disable=SC2154
9764
release=$(echo "${mpmrelease}" | grep -ioE "(r[0-9]{4}[a-b])")
9865
stream https://ssd.mathworks.com/supportfiles/ci/matlab-deps/v0/install.sh | sudoIfAvailable -s -- "$release"
9966
# install mpm depencencies
@@ -136,20 +103,25 @@ mkdir -p "$rootdir"
136103
mkdir -p "$batchdir"
137104
mkdir -p "$mpmdir"
138105

139-
# install mpm
140-
download "$mpmbaseurl/$mwarch/mpm" "$mpmdir/mpm$binext"
141-
chmod +x "$mpmdir/mpm$binext"
142-
143106
# install matlab-batch
144107
download "$batchbaseurl/$mwarch/matlab-batch$binext" "$batchdir/matlab-batch$binext"
145108
chmod +x "$batchdir/matlab-batch$binext"
146109

147-
# install matlab
148-
"$mpmdir/mpm$binext" install \
149-
--release="$mpmrelease" \
150-
--destination="$rootdir" \
151-
${releasestatus} \
152-
--products ${PARAM_PRODUCTS} MATLAB
110+
# Short-circuit if MATLAB already exists and PARAM_CACHE is true
111+
if [[ "$PARAM_CACHE" == "1" && -x "$rootdir/bin/matlab" ]]; then
112+
echo "Skipping installation because MATLAB already exists at $rootdir."
113+
else
114+
# install mpm
115+
download "$mpmbaseurl/$mwarch/mpm" "$mpmdir/mpm$binext"
116+
chmod +x "$mpmdir/mpm$binext"
117+
118+
# install matlab
119+
"$mpmdir/mpm$binext" install \
120+
--release="$mpmrelease" \
121+
--destination="$rootdir" \
122+
${releasestatus} \
123+
--products ${PARAM_PRODUCTS} MATLAB
124+
fi
153125

154126
# add MATLAB and matlab-batch to path
155127
echo 'export PATH="'$rootdir'/bin:'$batchdir':$PATH"' >> $BASH_ENV
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
mkdir -p ~/.matlab-circleci-orb
4+
eval "$UTILS"
5+
6+
mpmrelease=""
7+
releasestatus=""
8+
parsedrelease=$(echo "$PARAM_RELEASE" | tr '[:upper:]' '[:lower:]')
9+
if [[ "$parsedrelease" = "latest" ]]; then
10+
mpmrelease=$(stream https://ssd.mathworks.com/supportfiles/ci/matlab-release/v0/latest)
11+
elif [[ "$parsedrelease" = "latest-including-prerelease" ]]; then
12+
fetched=$(stream https://ssd.mathworks.com/supportfiles/ci/matlab-release/v0/latest-including-prerelease)
13+
if [[ "$fetched" == *prerelease ]]; then
14+
mpmrelease="${fetched%prerelease}"
15+
releasestatus="--release-status=Prerelease"
16+
else
17+
mpmrelease="$fetched"
18+
fi
19+
else
20+
mpmrelease="$parsedrelease"
21+
fi
22+
23+
if [[ "$mpmrelease" < "r2020b" ]]; then
24+
echo "Release '${mpmrelease}' is not supported. Use 'R2020b' or a later release." >&2
25+
exit 1
26+
fi
27+
28+
echo "export RELEASE=\"$mpmrelease\"" > ~/.matlab-circleci-orb/install-metadata.sh
29+
echo "export RELEASE_STATUS=\"$releasestatus\"" >> ~/.matlab-circleci-orb/install-metadata.sh
30+
SORTED_PRODUCTS=$(echo "$PARAM_PRODUCTS" | tr ' ' '\n' | sort | xargs)
31+
echo "export PRODUCTS=\"$SORTED_PRODUCTS\"" >> ~/.matlab-circleci-orb/install-metadata.sh

src/scripts/utils.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
stream() {
4+
local url="$1"
5+
local status=0
6+
7+
if command -v wget >/dev/null 2>&1; then
8+
wget --retry-connrefused --waitretry=5 -qO- "$url" || status=$?
9+
elif command -v curl >/dev/null 2>&1; then
10+
curl --retry 5 --retry-connrefused --retry-delay 5 -sSL "$url" || status=$?
11+
else
12+
echo "Unable to find wget or the curl command" >&2
13+
return 1
14+
fi
15+
16+
if [ $status -ne 0 ]; then
17+
echo "Error streaming file from $url" >&2
18+
fi
19+
20+
return $status
21+
}

0 commit comments

Comments
 (0)