Skip to content

Commit a0d2740

Browse files
authored
Merge pull request #5 from nyu-devops/sp24-updates
Updates for Spring 2024 Semester
2 parents 0747a52 + 6add53d commit a0d2740

File tree

10 files changed

+1159
-129
lines changed

10 files changed

+1159
-129
lines changed

.devcontainer/devcontainer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
"customizations": {
99
"vscode": {
1010
"settings": {
11+
"cSpell.words": [
12+
"sqlalchemy",
13+
"psycopg",
14+
"pytest",
15+
"tekton",
16+
"creds",
17+
"virtualenvs"
18+
],
1119
"[python]": {
1220
"editor.defaultFormatter": "ms-python.black-formatter",
1321
"editor.formatOnSave": true

.devcontainer/scripts/install-tools.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ echo "**********************************************************************"
3535
curl -Lo skaffold "https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-$ARCH"
3636
sudo install skaffold /usr/local/bin/
3737

38+
echo "**********************************************************************"
39+
echo "Installing DevSpace..."
40+
echo "**********************************************************************"
41+
curl -Lo devspace "https://github.com/loft-sh/devspace/releases/latest/download/devspace-linux-$ARCH"
42+
sudo install -c -m 0755 devspace /usr/local/bin
43+
3844
echo "**********************************************************************"
3945
echo "Installing Stern..."
4046
echo "**********************************************************************"

.devcontainer/scripts/setup-lab.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash
2+
echo "**********************************************************************"
23
echo "Setting up Docker lab environment..."
4+
echo "**********************************************************************"
5+
6+
echo "Pulling custom Python:3.11-slim image from quay.io..."
37
docker pull quay.io/rofrano/python:3.11-slim
48
# docker run -d --name postgresql --restart always -p 5432:5432 -v postgres:/var/lib/postgresql/data postgres:alpine
59

@@ -9,4 +13,6 @@ sudo bash -c "echo '127.0.0.1 cluster-registry' >> /etc/hosts"
913
echo "Making git stop complaining about unsafe folders"
1014
git config --global --add safe.directory /app
1115

16+
echo "**********************************************************************"
1217
echo "Setup complete"
18+
echo "**********************************************************************"

.tekton/pipeline.yaml

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Completed cd-pipeline from the OpenShift lab
2+
---
13
apiVersion: tekton.dev/v1
24
kind: Pipeline
35
metadata:
@@ -6,26 +8,21 @@ spec:
68
workspaces:
79
- name: pipeline-workspace
810
params:
9-
- description: The URL to the git repo
10-
name: GIT_REPO
11+
- name: GIT_REPO
12+
description: The URL to the git repo
1113
type: string
12-
- default: master
14+
- name: GIT_REF
1315
description: The reference (branch or ref)
14-
name: GIT_REF
1516
type: string
16-
- default: '-vvv --processes=1 --run-coverage --minimum-coverage=95'
17-
description: Arguments for green testing
18-
name: GREEN_ARGS
19-
type: string
20-
- default: petshop
17+
default: master
18+
- name: APP_NAME
2119
description: Name of the application
22-
name: APP_NAME
2320
type: string
24-
- default: >-
25-
image-registry.openshift-image-registry.svc:5000/$(context.pipelineRun.namespace)/$(params.APP_NAME):latest
21+
default: petshop
22+
- name: IMAGE_NAME
2623
description: The name of the image to build
27-
name: IMAGE_NAME
2824
type: string
25+
default: 'image-registry.openshift-image-registry.svc:5000/$(context.pipelineRun.namespace)/$(params.APP_NAME):latest'
2926
tasks:
3027
- name: git-clone
3128
params:
@@ -68,18 +65,16 @@ spec:
6865
workspaces:
6966
- name: output
7067
workspace: pipeline-workspace
71-
- name: pylint
68+
- name: linting
7269
params:
7370
- name: image
7471
value: 'docker.io/python:3.11-slim'
7572
- name: path
7673
value: service
77-
- name: requirements_file
78-
value: requirements.txt
7974
- name: args
8075
value: []
81-
- name: pip_conf_file
82-
value: pip.conf
76+
- name: requirements_file
77+
value: requirements.txt
8378
runAfter:
8479
- git-clone
8580
taskRef:
@@ -90,8 +85,11 @@ spec:
9085
workspace: pipeline-workspace
9186
- name: testing
9287
params:
93-
- name: ARGS
94-
value: $(params.GREEN_ARGS)
88+
- name: PYTEST_ARGS
89+
value:
90+
- '--pspec'
91+
- '--cov=service'
92+
- '--cov-fail-under=95'
9593
- name: SECRET_NAME
9694
value: postgres-creds
9795
- name: SECRET_KEY
@@ -100,7 +98,7 @@ spec:
10098
- git-clone
10199
taskRef:
102100
kind: Task
103-
name: green
101+
name: pytest-env
104102
workspaces:
105103
- name: source
106104
workspace: pipeline-workspace
@@ -109,8 +107,7 @@ spec:
109107
- name: IMAGE
110108
value: $(params.IMAGE_NAME)
111109
- name: BUILDER_IMAGE
112-
value: >-
113-
registry.redhat.io/rhel8/buildah@sha256:00795fafdab9bbaa22cd29d1faa1a01e604e4884a2c935c1bf8e3d1f0ad1c084
110+
value: 'registry.redhat.io/rhel8/buildah@sha256:b48f410efa0ff8ab0db6ead420a5d8d866d64af846fece5efb185230d7ecf591'
114111
- name: STORAGE_DRIVER
115112
value: vfs
116113
- name: DOCKERFILE
@@ -128,7 +125,7 @@ spec:
128125
- name: SKIP_PUSH
129126
value: 'false'
130127
runAfter:
131-
- pylint
128+
- linting
132129
- testing
133130
taskRef:
134131
kind: ClusterTask
@@ -138,8 +135,6 @@ spec:
138135
workspace: pipeline-workspace
139136
- name: deploy-image
140137
params:
141-
- name: old_image_name
142-
value: 'cluster-registry:32000/petshop:latest'
143138
- name: image_name
144139
value: $(params.IMAGE_NAME)
145140
- name: manifest_dir

.tekton/tasks.yaml

Lines changed: 160 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,94 @@
1+
# These are custom tasks that are not on Tekton Hub
2+
---
3+
apiVersion: tekton.dev/v1beta1
4+
kind: Task
5+
metadata:
6+
name: pylint
7+
labels:
8+
app.kubernetes.io/version: "0.4"
9+
annotations:
10+
tekton.dev/categories: Code Quality
11+
tekton.dev/pipelines.minVersion: "0.17.0"
12+
tekton.dev/tags: python, pylint, poetry
13+
tekton.dev/displayName: "pylint"
14+
tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le"
15+
spec:
16+
workspaces:
17+
- name: source
18+
description: The workspace with the source code.
19+
description: >-
20+
Use to run pylint on the provided input source. If Poetry is being used
21+
it will detect the poetry.lock file and install via requirements export.
22+
params:
23+
- name: image
24+
description: The container image with pylint
25+
default: docker.io/python:3.11-slim
26+
- name: path
27+
description: The path to the module which should be analyzed by pylint
28+
default: "."
29+
type: string
30+
- name: args
31+
description: The arguments to pass to the pylint CLI.
32+
type: array
33+
default: []
34+
- name: requirements_file
35+
description: The name of the requirements file inside the source location
36+
default: "requirements.txt"
37+
steps:
38+
- name: pylint
39+
image: $(params.image)
40+
workingDir: $(workspaces.source.path)
41+
script: |
42+
#!/bin/bash
43+
set -e
44+
export PATH=$PATH:$HOME/.local/bin:
45+
46+
echo "***** Installing dependencies *****"
47+
if [ -e "poetry.lock" ]; then
48+
echo "Found poetry.lock file: using poetry "
49+
python -m pip install poetry poetry-plugin-export
50+
poetry export --with=dev -f requirements.txt --output requirements.txt
51+
python -m pip install --user -r requirements.txt
52+
elif [ -n "$(params.requirements_file)" ] && [ -e "$(params.requirements_file)" ]; then
53+
python -m pip install --user -r "$(params.requirements_file)"
54+
fi
55+
56+
# Make sure pylint is installed
57+
python -m pip install pylint
58+
59+
echo "***** Running Linting *****"
60+
pylint $@ "$(params.path)"
61+
args:
62+
- "$(params.args)"
63+
64+
---
165
apiVersion: tekton.dev/v1beta1
266
kind: Task
367
metadata:
4-
name: green
68+
name: pytest-env
569
labels:
670
app.kubernetes.io/version: "0.1"
771
annotations:
872
tekton.dev/categories: Testing
973
tekton.dev/pipelines.minVersion: "0.17.0"
10-
tekton.dev/tags: python, green
11-
tekton.dev/displayName: "green tests"
12-
tekton.dev/platforms: "linux/amd64"
74+
tekton.dev/tags: python, pytest
75+
tekton.dev/displayName: "pytest tests"
76+
tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le"
1377
spec:
1478
workspaces:
1579
- name: source
1680
description: >-
17-
This task can be used to perform unit tests with green.
81+
This task can be used to perform unit tests with pytest.
82+
It supports both requirements.txt and poetry.lock files.
1883
19-
If you define a secret with the key `database_uri`
20-
it will create an environment variable named DATABASE_URI
21-
that can be used to connect to a test database.
84+
It also has the ability to create an environment variable
85+
that is sourced from a Secret. This allows you to define
86+
credentials that can be used to connect to a test database.
2287
params:
23-
- name: ARGS
24-
description: The additional arguments to be used with green
25-
type: string
26-
default: "-vvv --processes=1 --run-coverage --minimum-coverage=95"
88+
- name: PYTEST_ARGS
89+
description: The arguments to pass to the pytest CLI.
90+
type: array
91+
default: []
2792
- name: SECRET_NAME
2893
description: The name of the secret containing a database_uri key
2994
type: string
@@ -33,8 +98,8 @@ spec:
3398
type: string
3499
default: "database_uri"
35100
steps:
36-
- name: green
37-
image: python:3.11-slim
101+
- name: pytest
102+
image: docker.io/python:3.11-slim
38103
workingDir: $(workspaces.source.path)
39104
env:
40105
- name: DATABASE_URI
@@ -45,13 +110,26 @@ spec:
45110
script: |
46111
#!/bin/bash
47112
set -e
113+
export PATH=$PATH:$HOME/.local/bin:
48114
49115
echo "***** Installing dependencies *****"
50-
python -m pip install --upgrade pip wheel
51-
pip install -qr requirements.txt
116+
if [ -e "poetry.lock" ]; then
117+
echo "Found poetry.lock file: using poetry "
118+
python -m pip install poetry poetry-plugin-export
119+
poetry export --with=dev -f requirements.txt --output requirements.txt
120+
python -m pip install --user -r requirements.txt
121+
elif -e "requirements.txt" ]; then
122+
python -m pip install --user -r requirements.txt
123+
fi
124+
125+
# Make sure pylint is installed
126+
python -m pip install pytest
52127
53128
echo "***** Running Tests *****"
54-
green $(params.ARGS)
129+
pytest --version
130+
pytest
131+
args:
132+
- "$(params.PYTEST_ARGS)"
55133

56134
---
57135
apiVersion: tekton.dev/v1beta1
@@ -73,9 +151,6 @@ spec:
73151
This task will update the deployment.yaml with the latest image name
74152
and then apply that yaml file and it's service file.
75153
params:
76-
- name: old_image_name
77-
description: The fully qualified name of the old image to replace
78-
type: string
79154
- name: image_name
80155
description: The fully qualified name of the new image to deploy
81156
type: string
@@ -95,10 +170,17 @@ spec:
95170
96171
echo Applying manifests in $(inputs.params.manifest_dir) directory
97172
173+
echo "**********************************************************************"
174+
echo "Installing YQ..."
175+
echo "**********************************************************************"
176+
wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
177+
chmod a+x /usr/bin/yq
178+
98179
echo "********************* DEPLOYMENT ***********************"
99180
echo "Deploying $(inputs.params.image_name) ..."
100181
101-
sed -i 's|'"$(inputs.params.old_image_name)"'|'"$(inputs.params.image_name)"'|g' $(inputs.params.manifest_dir)/deployment.yaml
182+
yq -e -i '.spec.template.spec.containers[0].image="$(inputs.params.image_name)"' $(inputs.params.manifest_dir)/deployment.yaml
183+
# sed -i 's|'"$(inputs.params.old_image_name)"'|'"$(inputs.params.image_name)"'|g' $(inputs.params.manifest_dir)/deployment.yaml
102184
cat $(inputs.params.manifest_dir)/deployment.yaml
103185
104186
echo "************************************************************"
@@ -145,3 +227,60 @@ spec:
145227
echo Applying manifests in $(inputs.params.manifest_dir) directory
146228
oc apply -f $(inputs.params.manifest_dir)
147229
echo -----------------------------------
230+
231+
---
232+
apiVersion: tekton.dev/v1beta1
233+
kind: Task
234+
metadata:
235+
name: behave
236+
labels:
237+
app.kubernetes.io/version: "0.1"
238+
annotations:
239+
tekton.dev/categories: Testing
240+
tekton.dev/pipelines.minVersion: "0.17.0"
241+
tekton.dev/tags: python, bdd, behave
242+
tekton.dev/displayName: "bdd tests"
243+
tekton.dev/platforms: "linux/amd64"
244+
spec:
245+
workspaces:
246+
- name: source
247+
description: >-
248+
This task can be used to perform bdd tests with behave.
249+
params:
250+
- name: BASE_URL
251+
description: The url of the application to test
252+
type: string
253+
- name: WAIT_SECONDS
254+
description: The number of seconds to wait for a reply
255+
type: string
256+
default: "60"
257+
- name: DRIVER
258+
description: The web driver to use (chrome or firefox)
259+
type: string
260+
default: "chrome"
261+
steps:
262+
- name: behave
263+
image: rofrano/pipeline-selenium
264+
workingDir: $(workspaces.source.path)
265+
env:
266+
- name: BASE_URL
267+
value: $(params.BASE_URL)
268+
- name: WAIT_SECONDS
269+
value: $(params.WAIT_SECONDS)
270+
- name: DRIVER
271+
value: $(params.DRIVER)
272+
script: |
273+
#!/bin/bash
274+
set -e
275+
export PATH=$PATH:$HOME/.local/bin:
276+
277+
echo "***** Installing dependencies *****"
278+
if [ -e "poetry.lock" ]; then
279+
echo "Found poetry.lock file: using poetry"
280+
python -m pip install poetry poetry-plugin-export
281+
poetry export --with=dev -f requirements.txt --output requirements.txt
282+
fi
283+
python -m pip install --user -r requirements.txt
284+
285+
echo "***** Running Tests *****"
286+
behave

0 commit comments

Comments
 (0)