Skip to content

Commit c56fefd

Browse files
authored
Merge pull request #5 from vikinganalytics/more-python-versions
Adding python 3.6, 3.7 and 3.9
2 parents c69de36 + 3c09b32 commit c56fefd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1515
-3
lines changed

.github/workflows/ci_checks.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ on:
99
workflow_dispatch:
1010

1111
jobs:
12-
test-python38:
12+
test-s2i-builder-images:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v2
1616
- name: "Install s2i"
1717
run: |
1818
wget -c https://github.com/openshift/source-to-image/releases/download/v1.3.0/source-to-image-v1.3.0-eed2850f-linux-amd64.tar.gz -O - | tar -xz
1919
sudo cp s2i /usr/local/bin
20-
- name: Run the test script
20+
- name: Test python 3.6 image
21+
run: make test -C python36-slim/
22+
- name: Test python 3.7 image
23+
run: make test -C python37-slim/
24+
- name: Test python 3.8 image
2125
run: make test -C python38-slim/
26+
- name: Test python 3.9 image
27+
run: make test -C python39-slim/

.github/workflows/docker_image_release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ jobs:
2222
- name: Docker build and push
2323
run: |
2424
docker build -t daeploy/s2i-python:3.8-slim-${{ github.event.release.tag_name }} -t daeploy/s2i-python:latest ./python38-slim
25-
docker push --all-tags daeploy/s2i-python
25+
docker build -t daeploy/s2i-python:3.6-slim-${{ github.event.release.tag_name }} ./python36-slim
26+
docker build -t daeploy/s2i-python:3.7-slim-${{ github.event.release.tag_name }} ./python37-slim
27+
docker build -t daeploy/s2i-python:3.9-slim-${{ github.event.release.tag_name }} ./python39-slim
28+
docker push --all-tags daeploy/s2i-python

python36-slim/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# daeploy/s2i-python
2+
FROM python:3.6-slim
3+
4+
LABEL maintainer="Viking Analytics AB"
5+
6+
ENV BUILDER_VERSION=1.0 \
7+
APP_ROOT="/app_root" \
8+
STI_SCRIPTS_PATH="/.s2i/bin"
9+
10+
# Set labels used in OpenShift to describe the builder image
11+
LABEL io.k8s.description="Platform for building Daeploy images with reduced size" \
12+
io.k8s.display-name="Daeploy python builder" \
13+
io.openshift.expose-services="8080:http" \
14+
io.openshift.tags="builder,daeploy,python." \
15+
io.openshift.s2i.scripts-url="image://.s2i/bin"
16+
17+
# Setup virtualenv
18+
RUN python -m venv /opt/venv
19+
ENV PATH="/opt/venv/bin:$PATH"
20+
21+
# Copy the S2I scripts to the image
22+
COPY ./s2i/bin/ .s2i/bin
23+
24+
# TODO: Set the default port for applications built using this image
25+
EXPOSE 8080
26+
27+
# TODO: Set the default CMD for the image
28+
CMD [".s2i/bin/usage"]

python36-slim/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
IMAGE_NAME = daeploy/s2i-python
2+
3+
.PHONY: build
4+
build:
5+
docker build -t $(IMAGE_NAME) .
6+
7+
.PHONY: test
8+
test:
9+
docker build -t $(IMAGE_NAME)-candidate .
10+
IMAGE_NAME=$(IMAGE_NAME)-candidate test/run

python36-slim/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
# Creating a basic S2I builder image
3+
4+
## Getting started
5+
6+
### Files and Directories
7+
8+
| File | Required? | Description |
9+
|------------------------|-----------|--------------------------------------------------------------|
10+
| Dockerfile | Yes | Defines the base builder image |
11+
| s2i/bin/assemble | Yes | Script that builds the application |
12+
| s2i/bin/usage | No | Script that prints the usage of the builder |
13+
| s2i/bin/run | Yes | Script that runs the application |
14+
| s2i/bin/save-artifacts | No | Script for incremental builds that saves the built artifacts |
15+
| test/run | No | Test script for the builder image |
16+
| test/test-app | Yes | Test application source code |
17+
18+
#### Dockerfile
19+
20+
Create a *Dockerfile* that installs all of the necessary tools and libraries that are needed to build and run our application. This file will also handle copying the s2i scripts into the created image.
21+
22+
#### S2I scripts
23+
24+
##### assemble
25+
26+
Create an *assemble* script that will build our application, e.g.:
27+
28+
- build python modules
29+
- bundle install ruby gems
30+
- setup application specific configuration
31+
32+
The script can also specify a way to restore any saved artifacts from the previous image.
33+
34+
##### run
35+
36+
Create a *run* script that will start the application.
37+
38+
##### save-artifacts (optional)
39+
40+
Create a *save-artifacts* script which allows a new build to reuse content from a previous version of the application image.
41+
42+
##### usage (optional)
43+
44+
Create a *usage* script that will print out instructions on how to use the image.
45+
46+
##### Make the scripts executable
47+
48+
Make sure that all of the scripts are executable by running *chmod +x s2i/bin/**
49+
50+
#### Create the builder image
51+
52+
The following command will create a builder image named daeploy/s2i-python based on the Dockerfile that was created previously.
53+
54+
```bash
55+
docker build -t daeploy/s2i-python .
56+
```
57+
58+
The builder image can also be created by using the *make* command since a *Makefile* is included.
59+
60+
Once the image has finished building, the command *s2i usage daeploy/s2i-python* will print out the help info that was defined in the *usage* script.
61+
62+
#### Testing the builder image
63+
64+
The builder image can be tested using the following commands:
65+
66+
```bash
67+
docker build -t daeploy/s2i-python-candidate .
68+
IMAGE_NAME=daeploy/s2i-python-candidate test/run
69+
```
70+
71+
The builder image can also be tested by using the *make test* command since a *Makefile* is included.
72+
73+
#### Creating the application image
74+
75+
The application image combines the builder image with your applications source code, which is served using whatever application is installed via the *Dockerfile*, compiled using the *assemble* script, and run using the *run* script.
76+
The following command will create the application image:
77+
78+
```bash
79+
s2i build test/test-app daeploy/s2i-python daeploy/s2i-python-app
80+
---> Building and installing application from source...
81+
```
82+
83+
Using the logic defined in the *assemble* script, s2i will now create an application image using the builder image as a base and including the source code from the test/test-app directory.
84+
85+
#### Running the application image
86+
87+
Running the application image is as simple as invoking the docker run command:
88+
89+
```bash
90+
docker run -d -p 8080:8080 daeploy/s2i-python-app
91+
```
92+
93+
The application, which consists of a simple static web page, should now be accessible at [http://localhost:8080](http://localhost:8080).
94+
95+
#### Using the saved artifacts script
96+
97+
Rebuilding the application using the saved artifacts can be accomplished using the following command:
98+
99+
```bash
100+
s2i build --incremental=true test/test-app nginx-centos7 nginx-app
101+
---> Restoring build artifacts...
102+
---> Building and installing application from source...
103+
```
104+
105+
This will run the *save-artifacts* script which includes the custom code to backup the currently running application source, rebuild the application image, and then re-deploy the previously saved source using the *assemble* script.

python36-slim/s2i/bin/assemble

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
#
3+
# S2I assemble script for the 'daeploy/s2i-python' image.
4+
# The 'assemble' script builds your application source so that it is ready to run.
5+
#
6+
# For more information refer to the documentation:
7+
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
8+
#
9+
10+
# If the 'daeploy/s2i-python' assemble script is executed with the '-h' flag, print the usage.
11+
if [[ "$1" == "-h" ]]; then
12+
exec /usr/lib/s2i/usage
13+
fi
14+
15+
echo "---> Installing application source ..."
16+
mkdir -p "$APP_ROOT/src"
17+
mv /tmp/src/* "$APP_ROOT/src"
18+
19+
# Install dependencies
20+
echo "---> Upgrading pip and setuptools"
21+
pip install -U pip setuptools
22+
23+
cd "$APP_ROOT/src"
24+
if [[ -f requirements.txt ]]; then
25+
echo "---> Installing dependencies..."
26+
python -m pip install -r requirements.txt
27+
fi

python36-slim/s2i/bin/run

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
#
3+
# S2I run script for the 'daeploy/s2i-python' image.
4+
# The run script executes the server that runs your application.
5+
#
6+
# For more information see the documentation:
7+
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
8+
#
9+
10+
cd "$APP_ROOT/src"
11+
12+
if [ -z "$APP_SCRIPT" ] && [ -z "$APP_FILE" ] && [ -z "$APP_MODULE" ]; then
13+
# Set default values for APP_SCRIPT and APP_FILE only when all three APP_
14+
# variables are not defined by user. This prevents a situation when
15+
# APP_MODULE is defined to app:application but the app.py file is found as the
16+
# APP_FILE and then executed by Python instead of gunicorn.
17+
APP_SCRIPT="app.sh"
18+
APP_SCRIPT_DEFAULT=1
19+
APP_FILE="app.py"
20+
APP_FILE_DEFAULT=1
21+
fi
22+
23+
if [ ! -z "$APP_SCRIPT" ]; then
24+
if [[ -f "$APP_SCRIPT" ]]; then
25+
echo "---> Running application from script ($APP_SCRIPT) ..."
26+
if [[ "$APP_SCRIPT" != /* ]]; then
27+
APP_SCRIPT="$APP_ROOT/src/$APP_SCRIPT"
28+
fi
29+
chmod +x "$APP_SCRIPT"
30+
"$APP_SCRIPT"
31+
elif [[ -z "$APP_SCRIPT_DEFAULT" ]]; then
32+
echo "ERROR: file '$APP_SCRIPT' not found." && exit 1
33+
fi
34+
fi
35+
36+
if [ ! -z "$APP_FILE" ]; then
37+
if [[ -f "$APP_FILE" ]]; then
38+
echo "---> Running application from Python script ($APP_FILE) ..."
39+
python "$APP_FILE"
40+
elif [[ -z "$APP_FILE_DEFAULT" ]]; then
41+
echo "ERROR: file '$APP_FILE' not found." && exit 1
42+
fi
43+
fi

python36-slim/s2i/bin/usage

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
cat <<EOF
3+
This is the daeploy/s2i-python S2I image:
4+
To use it, install S2I: https://github.com/openshift/source-to-image
5+
6+
Sample invocation:
7+
8+
s2i build <source code path/URL> daeploy/s2i-python <application image>
9+
10+
You can then run the resulting image via:
11+
docker run <application image>
12+
EOF

0 commit comments

Comments
 (0)