Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly changed S2i extensible from kosciCZ #41

Merged
merged 11 commits into from
Jan 31, 2018
26 changes: 16 additions & 10 deletions 1.10/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ LABEL summary="${SUMMARY}" \
help="For more information visit https://github.com/sclorg/${NAME}-container" \
usage="s2i build <SOURCE-REPOSITORY> centos/${NAME}-${NGINX_SHORT_VER}-centos7:latest <APP-NAME>"

ENV NGINX_CONFIGURATION_PATH=/opt/app-root/etc/nginx.d
ENV NGINX_DEFAULT_CONF_PATH=/opt/app-root/etc/nginx.default.d
ENV NGINX_CONFIGURATION_PATH=${APP_ROOT}/etc/nginx.d \
NGINX_DEFAULT_CONF_PATH=${APP_ROOT}/etc/nginx.default.d \
NGINX_CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/nginx \
NGINX_APP_ROOT=${APP_ROOT}

RUN yum install -y yum-utils gettext hostname && \
yum install -y centos-release-scl-rh epel-release && \
Expand All @@ -54,13 +56,17 @@ COPY ./root/ /
# In order to drop the root user, we have to make some directories world
# writeable as OpenShift default security model is to run the container under
# random UID.
RUN sed -i -f /opt/app-root/nginxconf.sed /etc/opt/rh/rh-nginx110/nginx/nginx.conf && \
mkdir -p /opt/app-root/etc/nginx.d/ && \
mkdir -p /opt/app-root/etc/nginx.default.d/ && \
chmod -R a+rwx /opt/app-root/etc && \
RUN sed -i -f ${NGINX_APP_ROOT}/nginxconf.sed /etc/opt/rh/rh-nginx110/nginx/nginx.conf && \
mkdir -p ${NGINX_APP_ROOT}/etc/nginx.d/ && \
mkdir -p ${NGINX_APP_ROOT}/etc/nginx.default.d/ && \
mkdir -p ${NGINX_APP_ROOT}/src/nginx-start-hook/ && \
mkdir -p ${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook && \
chmod -R a+rwx ${NGINX_APP_ROOT}/etc && \
chmod -R a+rwx /var/opt/rh/rh-nginx110 && \
chown -R 1001:0 /opt/app-root && \
chmod -R a+rwx ${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook && \
chown -R 1001:0 ${NGINX_APP_ROOT} && \
chown -R 1001:0 /var/opt/rh/rh-nginx110 && \
chown -R 1001:0 ${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook && \
rpm-file-permissions

USER 1001
Expand All @@ -70,8 +76,8 @@ USER 1001
# VOLUME ["/opt/rh/rh-nginx110/root/usr/share/nginx/html"]
# VOLUME ["/var/opt/rh/rh-nginx110/log/nginx/"]

ENV BASH_ENV=/opt/app-root/etc/scl_enable \
ENV=/opt/app-root/etc/scl_enable \
PROMPT_COMMAND=". /opt/app-root/etc/scl_enable"
ENV BASH_ENV=${NGINX_APP_ROOT}/etc/scl_enable \
ENV=${NGINX_APP_ROOT}/etc/scl_enable \
PROMPT_COMMAND=". ${NGINX_APP_ROOT}/etc/scl_enable"

CMD $STI_SCRIPTS_PATH/usage
26 changes: 16 additions & 10 deletions 1.10/Dockerfile.rhel7
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ LABEL summary="${SUMMARY}" \
help="For more information visit https://github.com/sclorg/${NAME}-container" \
usage="s2i build <SOURCE-REPOSITORY> rhscl/${NAME}-${NGINX_SHORT_VER}-rhel7:latest <APP-NAME>"

ENV NGINX_CONFIGURATION_PATH=/opt/app-root/etc/nginx.d
ENV NGINX_DEFAULT_CONF_PATH=/opt/app-root/etc/nginx.default.d
ENV NGINX_CONFIGURATION_PATH=${APP_ROOT}/etc/nginx.d \
NGINX_DEFAULT_CONF_PATH=${APP_ROOT}/etc/nginx.default.d \
NGINX_CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/nginx \
NGINX_APP_ROOT=${APP_ROOT}

RUN yum install -y yum-utils && \
yum-config-manager --disable \* &> /dev/null && \
Expand All @@ -56,13 +58,17 @@ COPY ./root/ /
# In order to drop the root user, we have to make some directories world
# writeable as OpenShift default security model is to run the container under
# random UID.
RUN sed -i -f /opt/app-root/nginxconf.sed /etc/opt/rh/rh-nginx110/nginx/nginx.conf && \
mkdir -p /opt/app-root/etc/nginx.d/ && \
mkdir -p /opt/app-root/etc/nginx.default.d/ && \
chmod -R a+rwx /opt/app-root/etc && \
RUN sed -i -f ${NGINX_APP_ROOT}/nginxconf.sed /etc/opt/rh/rh-nginx110/nginx/nginx.conf && \
mkdir -p ${NGINX_APP_ROOT}/etc/nginx.d/ && \
mkdir -p ${NGINX_APP_ROOT}/etc/nginx.default.d/ && \
mkdir -p ${NGINX_APP_ROOT}/src/nginx-start-hook/ && \
mkdir -p ${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook && \
chmod -R a+rwx ${NGINX_APP_ROOT}/etc && \
chmod -R a+rwx /var/opt/rh/rh-nginx110 && \
chown -R 1001:0 /opt/app-root && \
chmod -R a+rwx ${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook && \
chown -R 1001:0 ${NGINX_APP_ROOT} && \
chown -R 1001:0 /var/opt/rh/rh-nginx110 && \
chown -R 1001:0 ${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook && \
rpm-file-permissions

USER 1001
Expand All @@ -72,8 +78,8 @@ USER 1001
# VOLUME ["/opt/rh/rh-nginx110/root/usr/share/nginx/html"]
# VOLUME ["/var/opt/rh/rh-nginx110/log/nginx/"]

ENV BASH_ENV=/opt/app-root/etc/scl_enable \
ENV=/opt/app-root/etc/scl_enable \
PROMPT_COMMAND=". /opt/app-root/etc/scl_enable"
ENV BASH_ENV=${NGINX_APP_ROOT}/etc/scl_enable \
ENV=${NGINX_APP_ROOT}/etc/scl_enable \
PROMPT_COMMAND=". ${NGINX_APP_ROOT}/etc/scl_enable"

CMD $STI_SCRIPTS_PATH/usage
23 changes: 15 additions & 8 deletions 1.10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,27 @@ S2I build support
Nginx server image can be extended using S2I tool (see Usage section).
S2I build folder structure:

| Folder name | Description |
| :-------------------------- | ----------------------------------------- |
| ./nginx-cfg/*.conf | Should contain all nginx configuration we want to include into image |
| ./nginx-default-cfg/*.conf | Contains any nginx config snippets to include in the default server block |
| ./ | Should contain nginx application source code |
**`./nginx-cfg/*.conf`**
Should contain all nginx configuration we want to include into image

**`./nginx-default-cfg/*.conf`**
Contains any nginx config snippets to include in the default server block

**`./nginx-start-hook/*.sh`**
Contains shell scripts that are sourced right before nginx is launched

**`./`**
Should contain nginx application source code


Environment variables and volumes
-------------
The nginx container image supports the following configuration variable, which can be set by using the `-e` option with the docker run command:


| Variable name | Description |
| :--------------------- | ----------------------------------------- |
| `NGINX_LOG_TO_VOLUME` | When `NGINX_LOG_TO_VOLUME` is set, nginx logs into `/var/opt/rh/rh-nginx110/log/nginx/` |
**`NGINX_LOG_TO_VOLUME`**
When `NGINX_LOG_TO_VOLUME` is set, nginx logs into `/var/opt/rh/rh-nginx110/log/nginx/`


You can mount your own web root like this:
```
Expand Down
31 changes: 31 additions & 0 deletions 1.10/root/usr/share/container-scripts/nginx/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

# get_matched_files finds file for image extending
function get_matched_files() {
local custom_dir default_dir
custom_dir="$1"
default_dir="$2"
files_matched="$3"
find "$default_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
[ -d "$custom_dir" ] && find "$custom_dir" -maxdepth 1 -type f -name "$files_matched" -printf "%f\n"
}

# process_extending_files process extending files in $1 and $2 directories
# - source all *.sh files
# (if there are files with same name source only file from $1)
function process_extending_files() {
local custom_dir default_dir
custom_dir=$1
default_dir=$2
while read filename ; do
if [ $filename ]; then
echo "=> sourcing $filename ..."
# Custom file is prefered
if [ -f $custom_dir/$filename ]; then
source $custom_dir/$filename
elif [ -f $default_dir/$filename ]; then
source $default_dir/$filename
fi
fi
done <<<"$(get_matched_files "$custom_dir" "$default_dir" '*.sh' | sort -u)"
}
10 changes: 10 additions & 0 deletions 1.10/s2i/bin/assemble
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if [ -d ./nginx-cfg ]; then
cp -v ./nginx-cfg/*.conf "${NGINX_CONFIGURATION_PATH}"
rm -rf ./nginx-cfg
fi
chmod -Rf g+rw ${NGINX_CONFIGURATION_PATH}
fi

if [ -d ./nginx-default-cfg ]; then
Expand All @@ -19,6 +20,15 @@ if [ -d ./nginx-default-cfg ]; then
cp -v ./nginx-default-cfg/*.conf "${NGINX_DEFAULT_CONF_PATH}"
rm -rf ./nginx-default-cfg
fi
chmod -Rf g+rw ${NGINX_DEFAULT_CONF_PATH}
fi

if [ -d ./nginx-start-hook ]; then
echo "---> Copying nginx start-hook scripts..."
if [ "$(ls -A ./nginx-start-hook/* 2>/dev/null)" ]; then
cp -v ./nginx-start-hook/* "${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook/"
rm -rf ./nginx-start-hook
fi
fi

# Fix source directory permissions
Expand Down
4 changes: 4 additions & 0 deletions 1.10/s2i/bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ source /opt/app-root/etc/generate_container_user

set -e

source ${NGINX_CONTAINER_SCRIPTS_PATH}/common.sh

process_extending_files ${NGINX_APP_ROOT}/src/nginx-start-hook ${NGINX_CONTAINER_SCRIPTS_PATH}/nginx-start-hook

exec nginx -g "daemon off;"
89 changes: 71 additions & 18 deletions 1.10/test/run
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ container_ip() {
}

run_s2i_build() {
s2i build ${s2i_args} file://${test_dir}/test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp
s2i build ${s2i_args} file://${test_dir}/${1} ${IMAGE_NAME} ${IMAGE_NAME}-${1}
}

prepare() {
Expand All @@ -43,7 +43,7 @@ prepare() {
# TODO: S2I build require the application is a valid 'GIT' repository, we
# should remove this restriction in the future when a file:// is used.
info "Build the test application image"
pushd ${test_dir}/test-app >/dev/null
pushd ${test_dir}/${1} >/dev/null
git init
git config user.email "build@localhost" && git config user.name "builder"
git add -A && git commit -m "Sample commit"
Expand All @@ -52,7 +52,7 @@ prepare() {

run_test_application() {
run_args=${CONTAINER_ARGS:-}
docker run --user=100001 ${run_args} --cidfile=${cid_file} ${IMAGE_NAME}-testapp
docker run --user=100001 ${run_args} --cidfile=${cid_file} ${IMAGE_NAME}-${1}
}

cleanup_test_app() {
Expand All @@ -68,17 +68,17 @@ cleanup_test_app() {

cleanup() {
info "Cleaning up the test application image"
if image_exists ${IMAGE_NAME}-testapp; then
docker rmi -f ${IMAGE_NAME}-testapp
if image_exists ${IMAGE_NAME}-${1}; then
docker rmi -f ${IMAGE_NAME}-${1}
fi
rm -rf ${test_dir}/test-app/.git
rm -rf ${test_dir}/${1}/.git
}

check_result() {
local result="$1"
if [[ "$result" != "0" ]]; then
info "TEST FAILED (${result})"
cleanup
cleanup $2
exit $result
fi
}
Expand Down Expand Up @@ -116,6 +116,18 @@ test_scl_usage() {
echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
test_command "$run_cmd" "$expected"
return $?
}

test_command() {
local run_cmd="$1"
local expected="$2"
local message="$3"

if [ $message ]; then
info ${3}
fi
out=$(docker exec $(cat ${cid_file}) /bin/bash -c "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
Expand All @@ -125,7 +137,9 @@ test_scl_usage() {
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
fi

return 0
}

test_for_output()
Expand Down Expand Up @@ -174,44 +188,83 @@ test_connection() {
return 1
}

test_connection_s2i() {
cat $cid_file
info "Testing the HTTP connection (http://$(container_ip):${test_port})"

if test_for_output "http://$(container_ip):${test_port}/" "NGINX is working" &&
test_for_output "http://$(container_ip):${test_port}/" "NGINX2 is working" localhost2 &&
test_for_output "http://$(container_ip):${test_port}/nginx-cfg/default.conf" "404"; then
return 0
fi

return 1
}

test_application() {
# Verify that the HTTP connection can be established to test application container
run_test_application &
run_test_application "test-app" &

# Wait for the container to write it's CID file
wait_for_cid

test_scl_usage "nginx -v" "nginx version: nginx/1.10."
check_result $?
check_result $? "test-app"

test_connection
check_result $?
check_result $? "test-app"
cleanup_test_app
}

test_pre_init_script() {
# Verify that the HTTP connection can be established to test application container
run_test_application "start-hook-test-app" &

# Wait for the container to write it's CID file
wait_for_cid

test_command "cat /opt/app-root/etc/nginx.d/default.conf | grep 'resolver' | sed -e 's/resolver\(.*\);/\1/' | grep 'DNS_SERVER'" ""
check_result $? "start-hook-test-app"

test_connection_s2i
check_result $? "start-hook-test-app"
cleanup_test_app

}

cid_file=$(mktemp -u --suffix=.cid)

# Since we built the candidate image locally, we don't want S2I attempt to pull
# it from Docker hub
s2i_args="--pull-policy=never"

prepare
run_s2i_build
check_result $?
prepare "test-app"
run_s2i_build "test-app"
check_result $? "test-app"

# Verify the 'usage' script is working properly when running the base image with 's2i usage ...'
test_s2i_usage
check_result $?
check_result $? "test-app"

# Verify the 'usage' script is working properly when running the base image with 'docker run ...'
test_docker_run_usage
check_result $?
check_result $? "test-app"

# Test application with default uid
test_application
test_application

# Test application with random uid
CONTAINER_ARGS="-u 12345" test_application
cleanup
cleanup "test-app"

# Test start-hook script functionality
cid_file=$(mktemp -u --suffix=.cid)

prepare "start-hook-test-app"
run_s2i_build "start-hook-test-app"
check_result $? "start-hook-test-app"

test_pre_init_script
cleanup "start-hook-test-app"

info "All tests finished successfully."
8 changes: 8 additions & 0 deletions 1.10/test/start-hook-test-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Test NGINX passed</title>
</head>
<body>
<h1>NGINX is working</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions 1.10/test/start-hook-test-app/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Test NGINX passed</title>
</head>
<body>
<h1>NGINX2 is working</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions 1.10/test/start-hook-test-app/nginx-cfg/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server {
listen 8080;
server_name localhost2;
root /opt/app-root/src;
index ${INDEX_FILE};
resolver ${DNS_SERVER};

}
Loading