Skip to content

Commit 635d1f7

Browse files
authored
Merge pull request #1513 from RS-PYTHON/layer-cleanup
Use layer-cleanup and restore-apt
2 parents 1f8735f + 05bd259 commit 635d1f7

File tree

12 files changed

+125
-56
lines changed

12 files changed

+125
-56
lines changed

.github/actions/publish-docker/action.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,13 @@ runs:
105105
if [[ ${{ inputs.debug_mode }} == true ]]; then
106106
commands=$(cat << "EOF"
107107
USER root
108-
COPY ./git_debug_image.sh ./git_debug_image.sh
109-
RUN set -e; chmod u+x ./git_debug_image.sh; apt update; ./git_debug_image.sh ${{ inputs.branch_name }}; \
110-
rm -f ./git_debug_image.sh ; \
111-
layer-cleanup.sh
108+
COPY ./git_debug_image.sh ./restore-apt.sh /usr/local/bin/
109+
# Use ';' not '&&' because they are not well handled by awk.
110+
RUN set -e; \
111+
cd /usr/local/bin/; \
112+
chmod u+x ./git_debug_image.sh ./restore-apt.sh; \
113+
./git_debug_image.sh ${{ inputs.branch_name }}; \
114+
rm -f ./git_debug_image.sh ./restore-apt.sh
112115
WORKDIR /home/user/rs-server
113116
EOF
114117
)

.github/scripts/git_debug_image.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ ROOT_DIR="$(realpath $SCRIPT_DIR/..)"
2626
BRANCH_NAME="$1" # git branch name
2727

2828
# Install components in the docker images
29-
. /etc/os-release
30-
echo "deb http://deb.debian.org/debian $VERSION_CODENAME main" > /etc/apt/sources.list
29+
restore-apt.sh
3130
apt update
3231
apt install -y --no-install-recommends git vim-tiny mg
3332

@@ -76,4 +75,4 @@ done
7675
chown -R user:user /home/user/rs-server
7776

7877
# Clean everything
79-
rm -rf /tmp/whl /root/.cache/pip /var/cache/apt/archives /var/lib/apt/lists/* /etc/apt/sources.list
78+
layer-cleanup.sh

.github/scripts/layer-cleanup.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ apt-get autoclean --yes
1717
apt-get autoremove --yes
1818

1919
rm -rf /var/lib/apt/lists/*
20-
rm -rf /etc/apt/sources.list.d/*
2120
rm -rf /usr/local/src/*
2221

2322
rm -rf /var/cache/apt/*
2423
rm -rf /root/.cache/*
2524
# including /root/.cache/pip
25+
rm -rf /home/*/.cache/*
2626
rm -rf /usr/local/share/.cache/*
2727
# including /usr/local/share/.cache/yarn
2828

@@ -31,4 +31,9 @@ rm -rf /opt/conda/pkgs/cache
3131

3232
rm -rf /tmp/whl
3333

34+
# WARNING: this removes the apt repository list. To restore it and be able to run 'apt update',
35+
# you must run (works only in Debian):
36+
# . /etc/os-release && echo "deb http://deb.debian.org/debian $VERSION_CODENAME main" > /etc/apt/sources.list
37+
rm -rf /etc/apt/sources.list.d/*
38+
3439
exit 0

.github/scripts/restore-apt.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
# Copyright 2024 CS Group
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Restore the apt repository list.
17+
# WARNING: works only in Debian/Ubuntu !
18+
19+
# Source OS release info
20+
. /etc/os-release
21+
22+
if [[ "$ID" == "debian" ]]; then
23+
cat > /etc/apt/sources.list <<EOF
24+
deb http://deb.debian.org/debian $VERSION_CODENAME main
25+
deb http://security.debian.org/debian-security $VERSION_CODENAME-security main
26+
deb http://deb.debian.org/debian $VERSION_CODENAME-updates main
27+
deb http://deb.debian.org/debian $VERSION_CODENAME-backports main
28+
EOF
29+
elif [[ "$ID" == "ubuntu" ]]; then
30+
cat > /etc/apt/sources.list <<EOF
31+
deb http://archive.ubuntu.com/ubuntu $VERSION_CODENAME main
32+
deb http://security.ubuntu.com/ubuntu $VERSION_CODENAME-security main
33+
deb http://archive.ubuntu.com/ubuntu $VERSION_CODENAME-updates main
34+
deb http://archive.ubuntu.com/ubuntu $VERSION_CODENAME-backports main
35+
EOF
36+
else
37+
echo "Unsupported distribution: $ID"
38+
exit 1
39+
fi
40+
41+
exit 0

.github/workflows/publish-binaries.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ jobs:
292292
name: ${{ needs.services-adgs-whl.outputs.package_name }}
293293
path: ./build_context_path
294294

295-
- name: Copy layer-cleanup.sh script
296-
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh
295+
- name: Copy Dockerfile requirements
296+
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh ./.github/scripts/restore-apt.sh
297297
shell: bash
298298

299299
- name: Copy debug mode dependencies
@@ -339,8 +339,8 @@ jobs:
339339
name: ${{ needs.services-prip-whl.outputs.package_name }}
340340
path: ./build_context_path
341341

342-
- name: Copy layer-cleanup.sh script
343-
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh
342+
- name: Copy Dockerfile requirements
343+
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh ./.github/scripts/restore-apt.sh
344344
shell: bash
345345

346346
- name: Copy debug mode dependencies
@@ -386,8 +386,8 @@ jobs:
386386
name: ${{ needs.services-cadip-whl.outputs.package_name }}
387387
path: ./build_context_path
388388

389-
- name: Copy layer-cleanup.sh script
390-
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh
389+
- name: Copy Dockerfile requirements
390+
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh ./.github/scripts/restore-apt.sh
391391
shell: bash
392392

393393
- name: Copy debug mode dependencies
@@ -432,8 +432,8 @@ jobs:
432432
name: ${{ needs.services-catalog-whl.outputs.package_name }}
433433
path: ./build_context_path
434434

435-
- name: Copy layer-cleanup.sh script
436-
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh
435+
- name: Copy Dockerfile requirements
436+
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh ./.github/scripts/restore-apt.sh
437437
shell: bash
438438

439439
- name: Copy debug mode dependencies
@@ -478,8 +478,8 @@ jobs:
478478
run: cp ./services/frontend/.github/entrypoint_frontend.sh ./build_context_path
479479
shell: bash
480480

481-
- name: Copy layer-cleanup.sh script
482-
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh
481+
- name: Copy Dockerfile requirements
482+
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh ./.github/scripts/restore-apt.sh
483483
shell: bash
484484

485485
- name: Copy debug mode dependencies
@@ -525,8 +525,8 @@ jobs:
525525
name: ${{ needs.services-staging-whl.outputs.package_name }}
526526
path: ./build_context_path
527527

528-
- name: Copy layer-cleanup.sh script
529-
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh
528+
- name: Copy Dockerfile requirements
529+
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh ./.github/scripts/restore-apt.sh
530530
shell: bash
531531

532532
- name: Copy debug mode dependencies
@@ -572,8 +572,8 @@ jobs:
572572
name: ${{ needs.services-staging-whl.outputs.package_name }}
573573
path: ./build_context_path
574574

575-
- name: Copy layer-cleanup.sh script
576-
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh
575+
- name: Copy Dockerfile requirements
576+
run: cp -t ./build_context_path ./.github/scripts/layer-cleanup.sh ./.github/scripts/restore-apt.sh
577577
shell: bash
578578

579579
- id: publish-docker

services/adgs/.github/Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ RUN opentelemetry-bootstrap -a install && layer-cleanup.sh
4545
RUN apt autoremove -y git && layer-cleanup.sh
4646

4747
# Add a default user
48-
RUN useradd -m user
49-
USER user
50-
WORKDIR /home/user
48+
ARG USER=user
49+
RUN useradd -m $USER
50+
USER $USER
51+
WORKDIR /home/$USER
5152

5253
# The CI/CD will replace this tag in debug mode or else remove it
5354
# [DEBUG_MODE_COMMANDS]
5455

55-
# After this, make sure we're still non-root
56-
USER user
56+
# Remove apt repository list and custom scripts
57+
USER root
58+
RUN rm -f /etc/apt/sources.list /usr/local/bin/layer-cleanup.sh /usr/local/bin/restore-apt.sh
59+
USER $USER
5760

5861
ENTRYPOINT [ \
5962
"python", "-m", "uvicorn", "rs_server_adgs.fastapi.adgs_app:app", \

services/cadip/.github/Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ RUN opentelemetry-bootstrap -a install && layer-cleanup.sh
4545
RUN apt autoremove -y git && layer-cleanup.sh
4646

4747
# Add a default user
48-
RUN useradd -m user
49-
USER user
50-
WORKDIR /home/user
48+
ARG USER=user
49+
RUN useradd -m $USER
50+
USER $USER
51+
WORKDIR /home/$USER
5152

5253
# The CI/CD will replace this tag in debug mode or else remove it
5354
# [DEBUG_MODE_COMMANDS]
5455

55-
# After this, make sure we're still non-root
56-
USER user
56+
# Remove apt repository list and custom scripts
57+
USER root
58+
RUN rm -f /etc/apt/sources.list /usr/local/bin/layer-cleanup.sh /usr/local/bin/restore-apt.sh
59+
USER $USER
5760

5861
ENTRYPOINT [ \
5962
"python", "-m", "uvicorn", "rs_server_cadip.fastapi.cadip_app:app", \

services/catalog/.github/Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ RUN opentelemetry-bootstrap -a install && layer-cleanup.sh
4545
RUN apt autoremove -y git && layer-cleanup.sh
4646

4747
# Add a default user
48-
RUN useradd -m user
49-
USER user
50-
WORKDIR /home/user
48+
ARG USER=user
49+
RUN useradd -m $USER
50+
USER $USER
51+
WORKDIR /home/$USER
5152

5253
# The CI/CD will replace this tag in debug mode or else remove it
5354
# [DEBUG_MODE_COMMANDS]
5455

5556
# [DEBUG_MODE_RELOAD_CATALOG]
5657

57-
# After this, make sure we're still non-root
58-
USER user
58+
# Remove apt repository list and custom scripts
59+
USER root
60+
RUN rm -f /etc/apt/sources.list /usr/local/bin/layer-cleanup.sh /usr/local/bin/restore-apt.sh
61+
USER $USER
5962

6063
# See: rs_server_catalog/main.py
6164
# uvicorn.run(

services/frontend/.github/Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ RUN chmod ugo+x /scripts/entrypoint_frontend.sh
6060
RUN chmod -R ugo+w $(dirname "${RSPY_OPENAPI_FILE}")
6161

6262
# Add a default user
63-
RUN useradd -m user
64-
USER user
65-
WORKDIR /home/user
63+
ARG USER=user
64+
RUN useradd -m $USER
65+
USER $USER
66+
WORKDIR /home/$USER
6667

6768
# The CI/CD will replace this tag in debug mode or else remove it
6869
# [DEBUG_MODE_COMMANDS]
6970

70-
# After this, make sure we're still non-root
71-
USER user
71+
# Remove apt repository list and custom scripts
72+
USER root
73+
RUN rm -f /etc/apt/sources.list /usr/local/bin/layer-cleanup.sh /usr/local/bin/restore-apt.sh
74+
USER $USER
7275

7376
ENTRYPOINT [ "/bin/bash", "-c", "set -x; /scripts/entrypoint_frontend.sh [DEBUG_MODE_RELOAD_FRONTEND]" ]
7477

services/prip/.github/Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ RUN opentelemetry-bootstrap -a install && layer-cleanup.sh
4545
RUN apt autoremove -y git && layer-cleanup.sh
4646

4747
# Add a default user
48-
RUN useradd -m user
49-
USER user
50-
WORKDIR /home/user
48+
ARG USER=user
49+
RUN useradd -m $USER
50+
USER $USER
51+
WORKDIR /home/$USER
5152

5253
# The CI/CD will replace this tag in debug mode or else remove it
5354
# [DEBUG_MODE_COMMANDS]
5455

55-
# After this, make sure we're still non-root
56-
USER user
56+
# Remove apt repository list and custom scripts
57+
USER root
58+
RUN rm -f /etc/apt/sources.list /usr/local/bin/layer-cleanup.sh /usr/local/bin/restore-apt.sh
59+
USER $USER
5760

5861
ENTRYPOINT [ \
5962
"python", "-m", "uvicorn", "rs_server_prip.fastapi.prip_app:app", \

0 commit comments

Comments
 (0)