Skip to content

Commit e256db0

Browse files
committed
Initial python image repo layout
1 parent 0a7a1d1 commit e256db0

17 files changed

Lines changed: 774 additions & 1 deletion

File tree

3.3/.sti/bin/assemble

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
source scl_source enable python33
4+
set -e
5+
6+
echo "---> Installing application source"
7+
cp -Rf /tmp/src/* ./
8+
9+
echo "---> Building your Python application from source"
10+
if [ -f setup.py ]; then
11+
python setup.py install --user
12+
elif [ -f requirements.txt ]; then
13+
pip install --user -r requirements.txt
14+
fi

3.3/.sti/bin/run

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
function is_gunicorn_installed() {
4+
pip show gunicorn
5+
}
6+
7+
source scl_source enable python33
8+
set -e
9+
10+
export APP_FILE=${APP_FILE:-"app.py"}
11+
12+
if [[ ! -v APP_MODULE && -f setup.py ]]; then
13+
APP_MODULE=`python setup.py --name`":application"
14+
fi
15+
16+
if is_gunicorn_installed && [[ -v APP_MODULE ]]; then
17+
if [[ -v APP_CONFIG ]]; then
18+
export CONFIG="--config ${APP_CONFIG}"
19+
fi
20+
exec gunicorn ${APP_MODULE} --bind=:8080 ${CONFIG}
21+
fi
22+
23+
exec python -u ${APP_FILE}

3.3/.sti/bin/usage

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
DISTRO=`cat /etc/*-release | grep ^ID= | grep -Po '".*?"' | tr -d '"'`
4+
5+
cat <<EOF
6+
This is a STI python-3.3 ${DISTRO} base image:
7+
To use it, install STI: https://github.com/openshift/source-to-image
8+
9+
Sample invocation:
10+
11+
sti build https://github.com/openshift/sti-python.git --contextDir=3.3/test/test-app/ openshift/python-33-${DISTRO}7 python-sample-app
12+
13+
14+
You can then run the resulting image via:
15+
docker run -p 8080:8080 python-sample-app
16+
EOF

3.3/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM openshift/base-centos7
2+
3+
# This image provides a Python 3.3 environment you can use to run your Python
4+
# applications.
5+
6+
MAINTAINER Maciej Szulik <maszulik@redhat.com>
7+
8+
# Image metadata
9+
ENV PYTHON_VERSION 3.3
10+
ENV IMAGE_DESCRIPTION Python 3.3
11+
ENV IMAGE_TAGS python,python33
12+
ENV IMAGE_EXPOSE_SERVICES 8080:http
13+
14+
RUN yum install -y \
15+
https://www.softwarecollections.org/en/scls/rhscl/python33/epel-7-x86_64/download/rhscl-python33-epel-7-x86_64.noarch.rpm && \
16+
yum install -y --setopt=tsflags=nodocs python33 python33-python-devel python33-python-setuptools && \
17+
yum clean all -y && \
18+
scl enable python33 "easy_install pip"
19+
20+
ENV PATH .local/bin/:$PATH
21+
22+
USER default
23+
24+
EXPOSE 8080

3.3/Dockerfile.rhel7

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM openshift/base-rhel7
2+
3+
# This image provides a Python 3.3 environment you can use to run your Python
4+
# applications.
5+
6+
MAINTAINER Maciej Szulik <maszulik@redhat.com>
7+
8+
# Image metadata
9+
ENV PYTHON_VERSION 3.3
10+
ENV IMAGE_DESCRIPTION Python 3.3
11+
ENV IMAGE_TAGS python,python33
12+
ENV IMAGE_EXPOSE_SERVICES 8080:http
13+
14+
RUN yum-config-manager --enable rhel-server-rhscl-7-rpms \
15+
yum-config-manager --enable rhel-7-server-optional-rpms && \
16+
yum install -y --setopt=tsflags=nodocs python33 python33-python-devel pip3 && \
17+
yum clean all -y && \
18+
scl enable python33 "easy_install pip"
19+
20+
ENV PATH .local/bin/:$PATH
21+
22+
USER default
23+
24+
EXPOSE 8080

3.3/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md

3.3/contrib/src/.bashrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This will make scl collection binaries work out of box.
2+
source scl_source enable python33

3.3/test/run

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/bin/bash
2+
#
3+
# The 'run' performs a simple test that verifies that STI image.
4+
# The main focus here is to excersise the STI scripts.
5+
#
6+
# IMAGE_NAME specifies a name of the candidate image used for testing.
7+
# The image has to be available before this script is executed.
8+
#
9+
IMAGE_NAME=${IMAGE_NAME-openshift/python33-centos7-candidate}
10+
11+
declare -a WEB_APPS=(standalone setup)
12+
13+
# TODO: Make command compatible for Mac users
14+
test_dir="$(readlink -zf $(dirname "${BASH_SOURCE[0]}"))"
15+
image_dir=$(readlink -zf ${test_dir}/..)
16+
scripts_url="file://${image_dir}/.sti/bin"
17+
18+
# TODO: This should be part of the image metadata
19+
test_port=8080
20+
21+
info() {
22+
echo -e "\n\e[1m[INFO] $@...\e[0m\n"
23+
}
24+
25+
image_exists() {
26+
docker inspect $1 &>/dev/null
27+
}
28+
29+
container_exists() {
30+
image_exists $(cat $cid_file)
31+
}
32+
33+
container_ip() {
34+
docker inspect --format="{{ .NetworkSettings.IPAddress }}" $(cat $cid_file)
35+
}
36+
37+
run_sti_build() {
38+
sti build ${sti_args} file://${test_dir}/${1}-test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp
39+
}
40+
41+
prepare() {
42+
if ! image_exists ${IMAGE_NAME}; then
43+
echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed."
44+
exit 1
45+
fi
46+
# TODO: STI build require the application is a valid 'GIT' repository, we
47+
# should remove this restriction in the future when a file:// is used.
48+
info "Build the ${1}-test application image"
49+
pushd ${test_dir}/${1}-test-app >/dev/null
50+
git init
51+
git config user.email "build@localhost" && git config user.name "builder"
52+
git add -A && git commit -m "Sample commit"
53+
popd >/dev/null
54+
}
55+
56+
run_test_application() {
57+
docker run --rm --cidfile=${cid_file} -p ${test_port} ${IMAGE_NAME}-testapp
58+
}
59+
60+
cleanup() {
61+
info "Cleaning up the test application image"
62+
if [ -f $cid_file ]; then
63+
if container_exists; then
64+
docker stop $(cat $cid_file)
65+
fi
66+
fi
67+
if image_exists ${IMAGE_NAME}-testapp; then
68+
docker rmi ${IMAGE_NAME}-testapp
69+
fi
70+
rm -rf ${test_dir}/${1}-test-app/.git
71+
}
72+
73+
check_result() {
74+
local result="$1"
75+
if [[ "$result" != "0" ]]; then
76+
info "TEST FAILED (${result})"
77+
cleanup
78+
exit $result
79+
fi
80+
}
81+
82+
wait_for_cid() {
83+
local max_attempts=10
84+
local sleep_time=1
85+
local attempt=1
86+
local result=1
87+
info "Waiting for application container to start"
88+
while [ $attempt -le $max_attempts ]; do
89+
[ -f $cid_file ] && break
90+
attempt=$(( $attempt + 1 ))
91+
sleep $sleep_time
92+
done
93+
}
94+
95+
test_sti_usage() {
96+
info "Testing 'sti usage'"
97+
sti usage ${sti_args} ${IMAGE_NAME} &>/dev/null
98+
}
99+
100+
test_docker_run_usage() {
101+
info "Testing 'docker run' usage"
102+
docker run ${IMAGE_NAME} &>/dev/null
103+
}
104+
105+
106+
test_connection() {
107+
info "Testing the HTTP connection (http://$(container_ip):${test_port})"
108+
local max_attempts=10
109+
local sleep_time=1
110+
local attempt=1
111+
local result=1
112+
while [ $attempt -le $max_attempts ]; do
113+
response_code=$(curl -s -w %{http_code} -o /dev/null http://$(container_ip):${test_port}/)
114+
status=$?
115+
if [ $status -eq 0 ]; then
116+
if [ $response_code -eq 200 ]; then
117+
result=0
118+
fi
119+
break
120+
fi
121+
attempt=$(( $attempt + 1 ))
122+
sleep $sleep_time
123+
done
124+
return $result
125+
}
126+
127+
for app in ${WEB_APPS[@]}; do
128+
cid_file=$(mktemp -u --suffix=.cid)
129+
130+
# Since we built the candidate image locally, we don't want STI attempt to pull
131+
# it from Docker hub
132+
sti_args="--forcePull=false -s ${scripts_url}"
133+
134+
prepare ${app}
135+
run_sti_build ${app}
136+
check_result $?
137+
138+
# Verify the 'usage' script is working properly when running the base image with 'sti usage ...'
139+
test_sti_usage
140+
check_result $?
141+
142+
# Verify the 'usage' script is working properly when running the base image with 'docker run ...'
143+
test_docker_run_usage
144+
check_result $?
145+
146+
# Verify that the HTTP connection can be established to test application container
147+
run_test_application &
148+
149+
# Wait for the container to write it's CID file
150+
wait_for_cid
151+
152+
test_connection
153+
check_result $?
154+
155+
info "All tests for the ${app}-test-app finished successfully."
156+
cleanup ${app}
157+
done
158+
159+
info "All tests finished successfully."

3.3/test/setup-test-app/setup.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from setuptools import setup, find_packages
2+
3+
setup (
4+
name = "testapp",
5+
version = "0.1",
6+
description = "Example application to be deployed.",
7+
packages = find_packages(),
8+
install_requires = ["gunicorn"],
9+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
def application(environ, start_response):
3+
start_response('200 OK', [('Content-Type','text/html')])
4+
return [b"Hello World from gunicorn WSGI application!"]

0 commit comments

Comments
 (0)