Skip to content

Commit b104f69

Browse files
Make install_packages resilient to 500 Server Error (#3286)
* Make `install_packages` resilient to 500 Server Error * Move script to .docker --------- Co-authored-by: Brandon Morelli <brandon.morelli@elastic.co>
1 parent 3e38a6d commit b104f69

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

.docker/install_packages.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
MAX_ATTEMPTS=5
4+
WAIT_TIME=10
5+
RETRY_COUNT=0
6+
7+
# Check if any package names were provided
8+
if [ $# -eq 0 ]; then
9+
echo "Error: install_packages requires at least one package name." >&2
10+
exit 1
11+
fi
12+
13+
# Loop for retry attempts
14+
while [ $RETRY_COUNT -lt $MAX_ATTEMPTS ]; do
15+
echo "Attempt $((RETRY_COUNT + 1)) of $MAX_ATTEMPTS: Running install_packages $@"
16+
17+
# Execute the actual install command
18+
install_packages "$@"
19+
20+
# Check the exit status of the previous command
21+
if [ $? -eq 0 ]; then
22+
echo "Packages installed successfully."
23+
exit 0 # Success, exit the function
24+
else
25+
RETRY_COUNT=$((RETRY_COUNT + 1))
26+
if [ $RETRY_COUNT -lt $MAX_ATTEMPTS ]; then
27+
echo "Installation failed. Retrying in $WAIT_TIME seconds..."
28+
sleep $WAIT_TIME
29+
fi
30+
fi
31+
done
32+
33+
echo "ERROR: Package installation failed after $MAX_ATTEMPTS attempts." >&2
34+
exit 1

Dockerfile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ RUN echo "deb http://archive.debian.org/debian/ buster main" > /etc/apt/sources.
1515

1616
# TODO install_packages calls apt-get update and then nukes the list files after. We should avoid multiple calls to apt-get update.....
1717
# We could probably fix this by running the update and installs ourself with `RUN --mount type=cache` but that is "experimental"
18+
COPY --chmod=755 .docker/install_packages.sh /usr/local/bin/
1819

1920
# Fix for Debian Buster EOL - point to archive repositories
2021
RUN sed -i 's/deb.debian.org/archive.debian.org/g' /etc/apt/sources.list && \
2122
sed -i 's/security.debian.org/archive.debian.org/g' /etc/apt/sources.list && \
2223
sed -i '/buster-updates/d' /etc/apt/sources.list
2324

24-
RUN install_packages apt-transport-https gnupg2 ca-certificates
25+
RUN install_packages.sh apt-transport-https gnupg2 ca-certificates
2526
COPY .docker/apt/keys/nodesource.gpg /
2627
RUN apt-key add /nodesource.gpg
2728
COPY .docker/apt/sources.list.d/nodesource.list /etc/apt/sources.list.d/
28-
RUN install_packages \
29+
RUN install_packages.sh \
2930
build-essential python2 \
3031
# needed for compiling native modules on ARM
3132
nodejs ruby \
@@ -43,7 +44,7 @@ ENV LC_ALL en_US.UTF-8
4344

4445

4546
FROM base AS ruby_deps
46-
RUN install_packages \
47+
RUN install_packages.sh \
4748
bundler \
4849
# Fetches ruby dependencies
4950
ruby-dev make cmake gcc libc-dev patch
@@ -61,7 +62,7 @@ FROM base AS node_deps
6162
COPY .docker/apt/keys/yarn.gpg /
6263
RUN apt-key add /yarn.gpg
6364
COPY .docker/apt/sources.list.d/yarn.list /etc/apt/sources.list.d/
64-
RUN install_packages yarn=1.22.19-1
65+
RUN install_packages.sh yarn=1.22.19-1
6566
COPY package.json /
6667
COPY yarn.lock /
6768
ENV YARN_CACHE_FOLDER=/tmp/.yarn-cache
@@ -74,7 +75,7 @@ RUN yarn install --frozen-lockfile --production
7475
# Dockerfiles to make the images to serve previews and air gapped docs.
7576
FROM base AS build
7677
LABEL MAINTAINERS="Nik Everett <nik@elastic.co>"
77-
RUN install_packages \
78+
RUN install_packages.sh \
7879
git \
7980
# Clone source repositories and commit to destination repositories
8081
libnss-wrapper \
@@ -110,7 +111,7 @@ RUN rm -rf /var/log/nginx && rm -rf /run/nginx
110111
##### Everything below this run tests
111112
FROM base AS py_test
112113
# There's not a published wheel for yamale, so we need setuptools and wheel
113-
RUN install_packages python3 python3-pip python3-setuptools python3-wheel python3-dev libxml2-dev libxslt-dev zlib1g-dev
114+
RUN install_packages.sh python3 python3-pip python3-setuptools python3-wheel python3-dev libxml2-dev libxslt-dev zlib1g-dev
114115
RUN pip3 install \
115116
beautifulsoup4==4.8.1 \
116117
lxml==4.4.2 \
@@ -131,4 +132,4 @@ COPY --from=ruby_test /usr/local/bin/rspec /usr/local/bin/rspec
131132
COPY --from=ruby_test /usr/local/bin/rubocop /usr/local/bin/rubocop
132133

133134
FROM py_test AS diff_tool
134-
RUN install_packages git
135+
RUN install_packages.sh git

0 commit comments

Comments
 (0)