Skip to content

Commit a6ac96b

Browse files
ntaroccolrdossan
authored andcommitted
deployment refactoring
- update CI to correctly build docker images - add requirement.txt - update Dockerfile to correctly build the app
1 parent dba6324 commit a6ac96b

File tree

12 files changed

+262
-242
lines changed

12 files changed

+262
-242
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ __pycache__
33
*.egg-info
44
*.DS_Store
55
*.pyc
6+
dist
67

78
# Editor stuff
89
*.swp
@@ -18,4 +19,4 @@ app-config/openshift/caimira-test
1819
app-config/openshift/caimira-prod
1920

2021
# documentation build folder
21-
caimira/docs/_build
22+
caimira/docs/_build

.gitlab-ci.yml

+73-42
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
stages:
22
- test
33
- docker-build
4-
- oc-tag
4+
- deploy
55

66
# Use the acc-py-devtools templates found at
77
# https://gitlab.cern.ch/-/ide/project/acc-co/devops/python/acc-py-devtools/blob/master/-/acc_py_devtools/templates/gitlab-ci/python.yml.
@@ -18,6 +18,8 @@ variables:
1818
.test-base:
1919
image: registry.cern.ch/docker.io/library/python:${PY_VERSION}
2020
stage: test
21+
except:
22+
- live/caimira-test # do not run tests on live/caimira-test branch
2123

2224
.test-run:
2325
extends:
@@ -62,11 +64,7 @@ test-cern-caimira-py39:
6264

6365
.test_openshift_config:
6466
stage: test
65-
rules:
66-
- if: '$OC_TOKEN && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == $BRANCH'
67-
allow_failure: true # The branch must represent what is deployed. FIXME: change to true because of a diff between ConfigMaps
68-
- if: '$OC_TOKEN && $CI_MERGE_REQUEST_EVENT_TYPE != "detached"'
69-
allow_failure: true # Anything other than the branch may fail without blocking the pipeline.
67+
allow_failure: true
7068
image: registry.cern.ch/docker.io/mambaorg/micromamba
7169
before_script:
7270
- micromamba create --yes -p $HOME/env python=3.9 ruamel.yaml wget -c conda-forge
@@ -86,6 +84,9 @@ test-cern-caimira-py39:
8684
paths:
8785
- ./app-config/openshift/${CAIMIRA_INSTANCE}/actual
8886
- ./app-config/openshift/${CAIMIRA_INSTANCE}/expected
87+
only:
88+
- master
89+
- live/caimira-test # do not run tests on live/caimira-test branch
8990

9091
check_openshift_config_test:
9192
extends: .test_openshift_config
@@ -108,6 +109,7 @@ check_openshift_config_test:
108109
# ###################################################################################################
109110
# Build docker images
110111

112+
# base
111113
.docker-build:
112114
stage: docker-build
113115
image:
@@ -116,6 +118,7 @@ check_openshift_config_test:
116118
name: gcr.io/kaniko-project/executor:debug
117119
entrypoint: [""]
118120
script:
121+
- echo "Building image for ${CI_COMMIT_REF_NAME} branch with tag ${IMAGE_TAG}"
119122
# Prepare Kaniko configuration file
120123
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
121124
- echo "Building ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:latest Docker image..."
@@ -124,67 +127,95 @@ check_openshift_config_test:
124127
# Print the full registry path of the pushed image
125128
- echo "Image pushed successfully to ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG}"
126129

127-
.docker-build-live-test:
130+
.docker-build-auth-service:
128131
variables:
129-
IMAGE_TAG: caimira-test-latest
132+
IMAGE_NAME: auth-service
133+
DOCKERFILE_DIRECTORY: app-config/auth-service
134+
DOCKER_CONTEXT_DIRECTORY: app-config/auth-service
130135
extends: .docker-build
131-
before_script:
132-
- echo "Branch is $CI_COMMIT_REF_NAME"
133-
- echo "Building image for live/caimira-test branch with tag ${IMAGE_TAG}"
136+
137+
.docker-build-calculator-app:
138+
variables:
139+
IMAGE_NAME: calculator-app
140+
DOCKERFILE_DIRECTORY: app-config/calculator-app
141+
DOCKER_CONTEXT_DIRECTORY: ""
142+
extends: .docker-build
143+
144+
# on push to live/caimira-test
145+
.docker-build-test:
146+
variables:
147+
IMAGE_TAG: caimira-test-latest
148+
149+
docker-build-auth-service-test:
150+
extends:
151+
- .docker-build-test
152+
- .docker-build-auth-service
153+
only:
154+
- live/caimira-test
155+
156+
docker-build-calculator-app-test:
157+
extends:
158+
- .docker-build-test
159+
- .docker-build-calculator-app
134160
only:
135161
- live/caimira-test
136162

163+
# on release
137164
.docker-build-release:
138-
extends: .docker-build
139165
before_script:
140-
- echo "Tag is $CI_COMMIT_REF_NAME"
141166
# Extract version number without 'v' prefix as IMAGE_TAG
142167
- IMAGE_TAG=$(echo "$CI_COMMIT_REF_NAME" | sed 's/^v//')
143168
- echo "Version is $IMAGE_TAG"
144-
only:
145-
- tags
146169

147-
build-auth-service-image:
170+
docker-build-auth-service-release:
148171
extends:
149-
- .docker-build-live-test
150172
- .docker-build-release
151-
variables:
152-
IMAGE_NAME: auth-service
153-
DOCKERFILE_DIRECTORY: app-config/auth-service
154-
DOCKER_CONTEXT_DIRECTORY: app-config/auth-service
173+
- .docker-build-auth-service
174+
only:
175+
- tag
155176

156-
build-calculator-app-image:
177+
docker-build-calculator-app-release:
157178
extends:
158-
- .docker-build-live-test
159179
- .docker-build-release
160-
variables:
161-
IMAGE_NAME: calculator-app
162-
DOCKERFILE_DIRECTORY: app-config/calculator-app
163-
DOCKER_CONTEXT_DIRECTORY: ""
180+
- .docker-build-calculator-app
181+
only:
182+
- tag
164183

165184
# ###################################################################################################
166-
# Link build Docker images OpenShift <-> GitLab registry
167-
168-
.link_docker_images_with_gitlab_registry:
169-
stage: oc-tag
170-
image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
185+
# Deploy to OpenShift
186+
.deploy:
187+
stage: deploy
188+
image: gitlab-registry.cern.ch/paas-tools/openshift-client
171189
variables:
172-
OC_PROJECT: "caimira-test"
173-
OC_TOKEN: ${OPENSHIFT_CAIMIRA_TEST_DEPLOY_TOKEN}
174190
IMAGE_TAG: caimira-test-latest
191+
OPENSHIFT_SERVER: https://api.paas.okd.cern.ch
192+
OPENSHIFT_PROJECT: caimira-test
175193
script:
176-
- oc tag --source=docker ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest --token ${OC_TOKEN} --server=https://api.paas.okd.cern.ch -n ${OC_PROJECT}
194+
- echo "Deploying ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG} to OpenShift"
195+
- oc login $OPENSHIFT_SERVER --token=$OPENSHIFT_CAIMIRA_TEST_DEPLOY_TOKEN
196+
- oc project $OPENSHIFT_PROJECT
197+
- oc set image dc/$OPENSHIFT_DEPLOYMENT $OPENSHIFT_CONTAINER_NAME=${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG}
198+
- oc rollout status dc/$OPENSHIFT_DEPLOYMENT
177199
only:
178-
- live/caimira-test # for prod, we want to manually deploy the tag that we need
200+
- live/caimira-test
179201

180-
link_auth-service_with_gitlab_registry:
181-
extends:
182-
- .link_docker_images_with_gitlab_registry
202+
deploy-auth-service-test:
203+
extends: .deploy
183204
variables:
184205
IMAGE_NAME: auth-service
206+
OPENSHIFT_DEPLOYMENT: auth-service
207+
OPENSHIFT_CONTAINER_NAME: auth-service
185208

186-
link_calculator-app_with_gitlab_registry:
187-
extends:
188-
- .link_docker_images_with_gitlab_registry
209+
deploy-calculator-app-test:
210+
extends: .deploy
211+
variables:
212+
IMAGE_NAME: calculator-app
213+
OPENSHIFT_DEPLOYMENT: calculator-app
214+
OPENSHIFT_CONTAINER_NAME: calculator-app
215+
216+
deploy-calculator-open-app-test:
217+
extends: .deploy
189218
variables:
190219
IMAGE_NAME: calculator-app
220+
OPENSHIFT_DEPLOYMENT: calculator-open-app
221+
OPENSHIFT_CONTAINER_NAME: calculator-open-app

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,25 @@ pip install -e . # At the root of the repository
103103
### Running the Calculator app in development mode
104104

105105
```
106-
python -m ui.apps.calculator
106+
python -m cern_caimira.apps.calculator
107107
```
108108

109109
To run with a specific template theme created:
110110

111111
```
112-
python -m ui.apps.calculator --theme=ui/apps/templates/{theme}
112+
python -m cern_caimira.apps.calculator --theme=ui/apps/templates/{theme}
113113
```
114114

115115
To run the entire app in a different `APPLICATION_ROOT` path:
116116

117117
```
118-
python -m ui.apps.calculator --app_root=/myroot
118+
python -m cern_caimira.apps.calculator --app_root=/myroot
119119
```
120120

121121
To run the calculator on a different URL path:
122122

123123
```
124-
python -m ui.apps.calculator --prefix=/mycalc
124+
python -m cern_caimira.apps.calculator --prefix=/mycalc
125125
```
126126

127127
Each of these commands will start a local version of CAiMIRA, which can be visited at http://localhost:8080/.

app-config/calculator-app/Dockerfile

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
FROM registry.cern.ch/docker.io/condaforge/mambaforge as conda
1+
FROM registry.cern.ch/docker.io/condaforge/mambaforge AS conda
2+
3+
ARG PYTHON_VERSION=3.12
4+
RUN mamba create --yes -p /opt/app python=${PYTHON_VERSION}
25

3-
RUN mamba create --yes -p /opt/app python=3.9
46
COPY . /opt/app-source
5-
RUN cd /opt/app-source && conda run -p /opt/app python -m pip install -r ./requirements.txt .[app]
7+
WORKDIR /opt/app-source
8+
# install Python deps
9+
RUN cd cern_caimira \
10+
&& conda run -p /opt/app python -m pip install -r requirements.txt
11+
RUN cd caimira \
12+
&& conda run -p /opt/app python -m pip install .
13+
RUN cd cern_caimira \
14+
&& conda run -p /opt/app python -m pip install .
15+
616
COPY app-config/calculator-app/app.sh /opt/app/bin/calculator-app.sh
17+
718
RUN cd /opt/app \
8-
&& find -name '*.a' -delete \
9-
&& rm -rf /opt/app/conda-meta \
10-
&& rm -rf /opt/app/include \
11-
&& find -name '__pycache__' -type d -exec rm -rf '{}' '+' \
12-
&& rm -rf /opt/app/lib/python*/site-packages/pip /opt/app/lib/python*/idlelib /opt/app/lib/python*/ensurepip \
13-
/opt/app/bin/x86_64-conda-linux-gnu-ld \
14-
/opt/app/bin/sqlite3 \
15-
/opt/app/bin/openssl \
16-
/opt/app/share/terminfo \
17-
&& find /opt/app/lib/ -name 'tests' -type d -exec rm -rf '{}' '+' \
18-
&& find /opt/app/lib -name '*.pyx' -delete \
19-
;
19+
&& find -name '*.a' -delete \
20+
&& rm -rf /opt/app/conda-meta \
21+
&& rm -rf /opt/app/include \
22+
&& find -name '__pycache__' -type d -exec rm -rf '{}' '+' \
23+
&& rm -rf /opt/app/lib/python*/site-packages/pip /opt/app/lib/python*/idlelib /opt/app/lib/python*/ensurepip \
24+
/opt/app/bin/x86_64-conda-linux-gnu-ld \
25+
/opt/app/bin/sqlite3 \
26+
/opt/app/bin/openssl \
27+
/opt/app/share/terminfo \
28+
&& find /opt/app/lib/ -name 'tests' -type d -exec rm -rf '{}' '+' \
29+
&& find /opt/app/lib -name '*.pyx' -delete \
30+
;
2031

2132
FROM registry.cern.ch/docker.io/library/debian
2233

@@ -25,12 +36,10 @@ ENV PATH=/opt/app/bin/:$PATH
2536
# Make a convenient location to the installed CAiMIRA package (i.e. a directory called caimira in the CWD).
2637
# It is important that this directory is also writable by a non-root user.
2738
RUN mkdir -p /scratch \
28-
&& chmod a+wx /scratch
39+
&& chmod a+wx /scratch
2940
# Set the HOME directory to something that anybody can write to (to support non root users, such as on openshift).
3041
ENV HOME=/scratch
3142
WORKDIR /scratch
32-
RUN CAIMIRA_INIT_FILE=$(/opt/app/bin/python -c "import caimira; print(caimira.__file__)") \
33-
&& ln -s $(dirname ${CAIMIRA_INIT_FILE}) /scratch/caimira
34-
CMD [ \
35-
"calculator-app.sh" \
36-
]
43+
RUN CERN_CAIMIRA_INIT_FILE=$(python -c "import cern_caimira; print(cern_caimira.__file__)") \
44+
&& ln -s $(dirname ${CERN_CAIMIRA_INIT_FILE}) /scratch/cern_caimira
45+
CMD [ "calculator-app.sh" ]

app-config/calculator-app/app.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if [[ "$APP_NAME" == "calculator-app" ]]; then
1616
if [ ! -z "$CAIMIRA_THEME" ]; then
1717
args+=("--theme=${CAIMIRA_THEME}")
1818
fi
19-
19+
2020
export "ARVE_API_KEY"="$ARVE_API_KEY"
2121
export "ARVE_CLIENT_ID"="$ARVE_CLIENT_ID"
2222
export "ARVE_CLIENT_SECRET"="$ARVE_CLIENT_SECRET"
@@ -26,8 +26,8 @@ if [[ "$APP_NAME" == "calculator-app" ]]; then
2626
export "DATA_SERVICE_ENABLED"="${DATA_SERVICE_ENABLED:=0}"
2727
export "CAIMIRA_PROFILER_ENABLED"="${CAIMIRA_PROFILER_ENABLED:=0}"
2828

29-
echo "Starting the caimira webservice with: python -m ui.apps.calculator ${args[@]}"
30-
python -m ui.apps.calculator "${args[@]}"
29+
echo "Starting the caimira webservice with: python -m cern_caimira.apps.calculator ${args[@]}"
30+
python -m cern_caimira.apps.calculator "${args[@]}"
3131

3232
else
3333
echo "No APP_NAME specified"

0 commit comments

Comments
 (0)