From a4e75f54ed7400f53b58c09823f2a5a442058ce8 Mon Sep 17 00:00:00 2001 From: Petr Kubat Date: Thu, 13 Feb 2020 16:28:40 +0100 Subject: [PATCH] Pull changes from upstream for version 2.6 created from upstream commit: 2f676852fab45caf07c5958cf4b7fa6ffc2498d1 --- 2.5 => 2.6 | 0 Dockerfile | 6 +- README.md | 63 ++++++++-------- root/help.1 | 108 +++++++++++++++------------ root/opt/app-root/etc/scl_enable | 2 +- s2i/bin/assemble | 14 ++-- s2i/bin/usage | 8 +- test/run | 6 +- test/run-conu | 2 + test/run-openshift | 1 + test/run-openshift-remote-cluster | 30 ++++++++ test/test-lib-openshift.sh | 1 + test/test-lib-ruby.sh | 25 +++++++ test/test-lib.sh | 119 +++++++++++++++++++++++++++--- test/test_s2i.py | 83 +++++++++++++++++++++ 15 files changed, 358 insertions(+), 110 deletions(-) rename 2.5 => 2.6 (100%) create mode 100755 test/run-conu create mode 120000 test/run-openshift create mode 100755 test/run-openshift-remote-cluster create mode 120000 test/test-lib-openshift.sh create mode 100644 test/test-lib-ruby.sh create mode 100644 test/test_s2i.py diff --git a/2.5 b/2.6 similarity index 100% rename from 2.5 rename to 2.6 diff --git a/Dockerfile b/Dockerfile index c367bab..56d3627 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM registry.fedoraproject.org/f30/s2i-base:latest +FROM registry.fedoraproject.org/f31/s2i-base # This image provides a Ruby environment you can use to run your Ruby # applications. @@ -6,8 +6,8 @@ FROM registry.fedoraproject.org/f30/s2i-base:latest EXPOSE 8080 ENV NAME=ruby \ - RUBY_VERSION=2.5 \ - RUBY_SHORT_VER=25 \ + RUBY_VERSION=2.6 \ + RUBY_SHORT_VER=26 \ VERSION=0 ENV SUMMARY="Platform for building and running Ruby $RUBY_VERSION applications" \ diff --git a/README.md b/README.md index 94bddf3..c721fd5 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,19 @@ -Ruby 2.5 container image +Ruby 2.6 container image ================= +This container image includes Ruby 2.6 as a [S2I](https://github.com/openshift/source-to-image) base image for your Ruby 2.6 applications. +Users can choose between RHEL, CentOS and Fedora based builder images. +The RHEL images are available in the [Red Hat Container Catalog](https://access.redhat.com/containers/), +the CentOS images are available on [Docker Hub](https://hub.docker.com/r/centos/), +and the Fedora images are available in [Fedora Registry](https://registry.fedoraproject.org/). +The resulting image can be run using [podman](https://github.com/containers/libpod). -This container image includes Ruby 2.5 as a [S2I](https://github.com/openshift/source-to-image) base image for your Ruby 2.5 applications. -Users can choose between RHEL and CentOS based builder images. -The RHEL image is available in the [Red Hat Container Catalog](https://access.redhat.com/containers/#/registry.access.redhat.com/rhscl/ruby-25-rhel7) -as registry.access.redhat.com/rhscl/ruby-25-rhel7. -The CentOS image is then available on [Docker Hub](https://hub.docker.com/r/centos/ruby-25-centos7/) -as centos/ruby-25-centos7. -The resulting image can be run using [Docker](http://docker.io). +Note: while the examples in this README are calling `podman`, you can replace any such calls by `docker` with the same arguments Description ----------- -Ruby 2.5 available as container is a base platform for -building and running various Ruby 2.5 applications and frameworks. +Ruby 2.6 available as container is a base platform for +building and running various Ruby 2.6 applications and frameworks. Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, and extensible. @@ -25,20 +25,18 @@ the nodejs itself is included just to make the npm work. Usage --------------------- -To build a simple [ruby-sample-app](https://github.com/sclorg/s2i-ruby-container/tree/master/2.5/test/puma-test-app) application -using standalone [S2I](https://github.com/openshift/source-to-image) and then run the -resulting image with [Docker](http://docker.io) execute: +For this, we will assume that you are using the `ubi8/ruby-26 image`, available via `ruby:2.6` imagestream tag in Openshift. +Building a simple [ruby-sample-app](https://github.com/sclorg/s2i-ruby-container/tree/master/2.6/test/puma-test-app) application +in Openshift can be achieved with the following step: -* **For RHEL based image** ``` - $ s2i build https://github.com/sclorg/s2i-ruby-container.git --context-dir=2.5/test/puma-test-app/ rhscl/ruby-25-rhel7 ruby-sample-app - $ docker run -p 8080:8080 ruby-sample-app + $ oc new-app ruby:2.6~https://github.com/sclorg/s2i-ruby-container.git --context-dir=2.6/test/puma-test-app/ ``` -* **For CentOS based image** +The same application can also be built using the standalone [S2I](https://github.com/openshift/source-to-image) application on systems that have it available: + ``` - $ s2i build https://github.com/sclorg/s2i-ruby-container.git --context-dir=2.5/test/puma-test-app/ centos/ruby-25-centos7 ruby-sample-app - $ docker run -p 8080:8080 ruby-sample-app + $ s2i build https://github.com/sclorg/s2i-ruby-container.git --context-dir=2.6/test/puma-test-app/ ubi8/ruby-26 ruby-sample-app ``` **Accessing the application:** @@ -49,7 +47,7 @@ $ curl 127.0.0.1:8080 Environment variables --------------------- -To set these environment variables, you can place them as a key value pair into a `.sti/environment` +To set these environment variables, you can place them as a key value pair into a `.s2i/environment` file inside your source code repository. * **RACK_ENV** @@ -82,9 +80,9 @@ In order to dynamically pick up changes made in your application source code, yo * **For Ruby on Rails applications** - Run the built Rails image with the `RAILS_ENV=development` environment variable passed to the [Docker](http://docker.io) `-e` run flag: + Run the built Rails image with the `RAILS_ENV=development` environment variable passed to the [podman](https://github.com/containers/libpod) `-e` run flag: ``` - $ docker run -e RAILS_ENV=development -p 8080:8080 rails-app + $ podman run -e RAILS_ENV=development -p 8080:8080 rails-app ``` * **For other types of Ruby applications (Sinatra, Padrino, etc.)** @@ -95,17 +93,17 @@ In order to dynamically pick up changes made in your application source code, yo Please note that in order to be able to run your application in development mode, you need to modify the [S2I run script](https://github.com/openshift/source-to-image#anatomy-of-a-builder-image), so the web server is launched by the chosen gem, which checks for changes in the source code. - After you built your application image with your version of [S2I run script](https://github.com/openshift/source-to-image#anatomy-of-a-builder-image), run the image with the RACK_ENV=development environment variable passed to the [Docker](http://docker.io) -e run flag: + After you built your application image with your version of [S2I run script](https://github.com/openshift/source-to-image#anatomy-of-a-builder-image), run the image with the RACK_ENV=development environment variable passed to the [podman](https://github.com/containers/libpod) -e run flag: ``` - $ docker run -e RACK_ENV=development -p 8080:8080 sinatra-app + $ podman run -e RACK_ENV=development -p 8080:8080 sinatra-app ``` -To change your source code in running container, use Docker's [exec](http://docker.io) command: +To change your source code in running container, use Podman's [exec](https://github.com/containers/libpod) command: ``` -docker exec -it /bin/bash +$ podman exec -it /bin/bash ``` -After you [Docker exec](http://docker.io) into the running container, your current +After you [podman exec](https://github.com/containers/libpod) into the running container, your current directory is set to `/opt/app-root/src`, where the source code is located. Performance tuning @@ -117,9 +115,9 @@ cores that the container has available, as recommended by [Puma](https://github.com/puma/puma)'s documentation. This is determined using the cgroup [cpusets](https://www.kernel.org/doc/Documentation/cgroup-v1/cpusets.txt) subsystem. You can specify the cores that the container is allowed to use by passing -the `--cpuset-cpus` parameter to the [Docker](http://docker.io) run command: +the `--cpuset-cpus` parameter to the [podman](https://github.com/containers/libpod) run command: ``` -$ docker run -e PUMA_MAX_THREADS=32 --cpuset-cpus='0-2,3,5' -p 8080:8080 sinatra-app +$ podman run -e PUMA_MAX_THREADS=32 --cpuset-cpus='0-2,3,5' -p 8080:8080 sinatra-app ``` The number of workers is also limited by the memory limit that is enforced using cgroups. The builder image assumes that you will need 50 MiB as a base and @@ -132,7 +130,7 @@ container is computed using the following formula: ``` You can specify a memory limit using the `--memory` flag: ``` -$ docker run -e PUMA_MAX_THREADS=32 --memory=300m -p 8080:8080 sinatra-app +$ podman run -e PUMA_MAX_THREADS=32 --memory=300m -p 8080:8080 sinatra-app ``` If memory is more limiting then the number of available cores, the number of workers is scaled down accordingly to fit the above formula. The number of @@ -142,5 +140,6 @@ workers can also be set explicitly by setting `PUMA_WORKERS`. See also -------- Dockerfile and other sources are available on https://github.com/sclorg/s2i-ruby-container. -In that repository you also can find another versions of Python environment Dockerfiles. -Dockerfile for CentOS is called Dockerfile, Dockerfile for RHEL is called Dockerfile.rhel7. +In that repository you also can find another versions of Ruby environment Dockerfiles. +Dockerfile for CentOS is called `Dockerfile`, Dockerfile for RHEL7 is called `Dockerfile.rhel7`, +for RHEL8 it's `Dockerfile.rhel8` and the Fedora Dockerfile is called Dockerfile.fedora. diff --git a/root/help.1 b/root/help.1 index e5c88b5..3a13e87 100644 --- a/root/help.1 +++ b/root/help.1 @@ -1,21 +1,24 @@ -.TH Ruby 2.5 container image +.TH Ruby 2.6 container image .PP -This container image includes Ruby 2.5 as a S2I -\[la]https://github.com/openshift/source-to-image\[ra] base image for your Ruby 2.5 applications. -Users can choose between RHEL and CentOS based builder images. -The RHEL image is available in the Red Hat Container Catalog -\[la]https://access.redhat.com/containers/#/registry.access.redhat.com/rhscl/ruby-25-rhel7\[ra] -as registry.access.redhat.com/rhscl/ruby\-25\-rhel7. -The CentOS image is then available on Docker Hub -\[la]https://hub.docker.com/r/centos/ruby-25-centos7/\[ra] -as centos/ruby\-25\-centos7. -The resulting image can be run using Docker -\[la]http://docker.io\[ra]\&. +This container image includes Ruby 2.6 as a S2I +\[la]https://github.com/openshift/source-to-image\[ra] base image for your Ruby 2.6 applications. +Users can choose between RHEL, CentOS and Fedora based builder images. +The RHEL images are available in the Red Hat Container Catalog +\[la]https://access.redhat.com/containers/\[ra], +the CentOS images are available on Docker Hub +\[la]https://hub.docker.com/r/centos/\[ra], +and the Fedora images are available in Fedora Registry +\[la]https://registry.fedoraproject.org/\[ra]\&. +The resulting image can be run using podman +\[la]https://github.com/containers/libpod\[ra]\&. + +.PP +Note: while the examples in this README are calling \fB\fCpodman\fR, you can replace any such calls by \fB\fCdocker\fR with the same arguments .SH Description .PP -Ruby 2.5 available as container is a base platform for -building and running various Ruby 2.5 applications and frameworks. +Ruby 2.6 available as container is a base platform for +building and running various Ruby 2.6 applications and frameworks. Ruby is the interpreted scripting language for quick and easy object\-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight\-forward, and extensible. @@ -28,35 +31,33 @@ the nodejs itself is included just to make the npm work. .SH Usage .PP -To build a simple ruby\-sample\-app -\[la]https://github.com/sclorg/s2i-ruby-container/tree/master/2.5/test/puma-test-app\[ra] application -using standalone S2I -\[la]https://github.com/openshift/source-to-image\[ra] and then run the -resulting image with Docker -\[la]http://docker.io\[ra] execute: -.IP \(bu 2 +For this, we will assume that you are using the \fB\fCubi8/ruby\-26 image\fR, available via \fB\fCruby:2.6\fR imagestream tag in Openshift. +Building a simple ruby\-sample\-app +\[la]https://github.com/sclorg/s2i-ruby-container/tree/master/2.6/test/puma-test-app\[ra] application +in Openshift can be achieved with the following step: -.PP -\fBFor RHEL based image\fP .PP .RS .nf -$ s2i build https://github.com/sclorg/s2i\-ruby\-container.git \-\-context\-dir=2.5/test/puma\-test\-app/ rhscl/ruby\-25\-rhel7 ruby\-sample\-app -$ docker run \-p 8080:8080 ruby\-sample\-app +``` +$ oc new\-app ruby:2.6\~https://github.com/sclorg/s2i\-ruby\-container.git \-\-context\-dir=2.6/test/puma\-test\-app/ +``` .fi .RE -.IP \(bu 2 .PP -\fBFor CentOS based image\fP +The same application can also be built using the standalone S2I +\[la]https://github.com/openshift/source-to-image\[ra] application on systems that have it available: + .PP .RS .nf -$ s2i build https://github.com/sclorg/s2i\-ruby\-container.git \-\-context\-dir=2.5/test/puma\-test\-app/ centos/ruby\-25\-centos7 ruby\-sample\-app -$ docker run \-p 8080:8080 ruby\-sample\-app +``` +$ s2i build https://github.com/sclorg/s2i\-ruby\-container.git \-\-context\-dir=2.6/test/puma\-test\-app/ ubi8/ruby\-26 ruby\-sample\-app +``` .fi .RE @@ -75,8 +76,10 @@ $ curl 127.0.0.1:8080 .SH Environment variables .PP -To set these environment variables, you can place them as a key value pair into a \fB\fC\&.sti/environment\fR +To set these environment variables, you can place them as a key value pair into a \fB\fC\&.s2i/environment\fR file inside your source code repository. + +.RS .IP \(bu 2 .PP @@ -115,22 +118,26 @@ This variable indicate the number of worker processes that will be launched. See .PP Set this variable to use a custom RubyGems mirror URL to download required gem packages during build process. +.RE + .SH Hot deploy .PP In order to dynamically pick up changes made in your application source code, you need to make following steps: + +.RS .IP \(bu 2 .PP \fBFor Ruby on Rails applications\fP .PP -Run the built Rails image with the \fB\fCRAILS\_ENV=development\fR environment variable passed to the Docker -\[la]http://docker.io\[ra] \fB\fC\-e\fR run flag: +Run the built Rails image with the \fB\fCRAILS\_ENV=development\fR environment variable passed to the podman +\[la]https://github.com/containers/libpod\[ra] \fB\fC\-e\fR run flag: .PP .RS .nf -$ docker run \-e RAILS\_ENV=development \-p 8080:8080 rails\-app +$ podman run \-e RAILS\_ENV=development \-p 8080:8080 rails\-app .fi .RE @@ -140,6 +147,8 @@ $ docker run \-e RAILS\_ENV=development \-p 8080:8080 rails\-app \fBFor other types of Ruby applications (Sinatra, Padrino, etc.)\fP .PP Your application needs to be built with one of gems that reloads the server every time changes in source code are done inside the running container. Those gems are: + +.RS .IP \(bu 2 Shotgun \[la]https://github.com/rtomayko/shotgun\[ra] @@ -150,40 +159,44 @@ Rerun Rack\-livereload \[la]https://github.com/johnbintz/rack-livereload\[ra] +.RE + .PP Please note that in order to be able to run your application in development mode, you need to modify the S2I run script \[la]https://github.com/openshift/source-to-image#anatomy-of-a-builder-image\[ra], so the web server is launched by the chosen gem, which checks for changes in the source code. .PP After you built your application image with your version of S2I run script -\[la]https://github.com/openshift/source-to-image#anatomy-of-a-builder-image\[ra], run the image with the RACK\_ENV=development environment variable passed to the Docker -\[la]http://docker.io\[ra] \-e run flag: +\[la]https://github.com/openshift/source-to-image#anatomy-of-a-builder-image\[ra], run the image with the RACK\_ENV=development environment variable passed to the podman +\[la]https://github.com/containers/libpod\[ra] \-e run flag: .PP .RS .nf -$ docker run \-e RACK\_ENV=development \-p 8080:8080 sinatra\-app +$ podman run \-e RACK\_ENV=development \-p 8080:8080 sinatra\-app .fi .RE +.RE + .PP -To change your source code in running container, use Docker's exec -\[la]http://docker.io\[ra] command: +To change your source code in running container, use Podman's exec +\[la]https://github.com/containers/libpod\[ra] command: .PP .RS .nf -docker exec \-it /bin/bash +$ podman exec \-it /bin/bash .fi .RE .PP -After you Docker exec -\[la]http://docker.io\[ra] into the running container, your current +After you podman exec +\[la]https://github.com/containers/libpod\[ra] into the running container, your current directory is set to \fB\fC/opt/app\-root/src\fR, where the source code is located. .SH Performance tuning @@ -197,14 +210,14 @@ Puma the cgroup cpusets \[la]https://www.kernel.org/doc/Documentation/cgroup-v1/cpusets.txt\[ra] subsystem. You can specify the cores that the container is allowed to use by passing -the \fB\fC\-\-cpuset\-cpus\fR parameter to the Docker -\[la]http://docker.io\[ra] run command: +the \fB\fC\-\-cpuset\-cpus\fR parameter to the podman +\[la]https://github.com/containers/libpod\[ra] run command: .PP .RS .nf -$ docker run \-e PUMA\_MAX\_THREADS=32 \-\-cpuset\-cpus='0\-2,3,5' \-p 8080:8080 sinatra\-app +$ podman run \-e PUMA\_MAX\_THREADS=32 \-\-cpuset\-cpus='0\-2,3,5' \-p 8080:8080 sinatra\-app .fi .RE @@ -232,7 +245,7 @@ You can specify a memory limit using the \fB\fC\-\-memory\fR flag: .RS .nf -$ docker run \-e PUMA\_MAX\_THREADS=32 \-\-memory=300m \-p 8080:8080 sinatra\-app +$ podman run \-e PUMA\_MAX\_THREADS=32 \-\-memory=300m \-p 8080:8080 sinatra\-app .fi .RE @@ -246,5 +259,6 @@ workers can also be set explicitly by setting \fB\fCPUMA\_WORKERS\fR\&. .PP Dockerfile and other sources are available on \[la]https://github.com/sclorg/s2i-ruby-container\[ra]\&. -In that repository you also can find another versions of Python environment Dockerfiles. -Dockerfile for CentOS is called Dockerfile, Dockerfile for RHEL is called Dockerfile.rhel7. +In that repository you also can find another versions of Ruby environment Dockerfiles. +Dockerfile for CentOS is called \fB\fCDockerfile\fR, Dockerfile for RHEL7 is called \fB\fCDockerfile.rhel7\fR, +for RHEL8 it's \fB\fCDockerfile.rhel8\fR and the Fedora Dockerfile is called Dockerfile.fedora. diff --git a/root/opt/app-root/etc/scl_enable b/root/opt/app-root/etc/scl_enable index 826d94c..2bd988f 100644 --- a/root/opt/app-root/etc/scl_enable +++ b/root/opt/app-root/etc/scl_enable @@ -3,4 +3,4 @@ # # This will make scl collection binaries work out of box. unset BASH_ENV PROMPT_COMMAND ENV -source scl_source enable rh-ruby25 $NODEJS_SCL +source scl_source enable rh-ruby26 $NODEJS_SCL diff --git a/s2i/bin/assemble b/s2i/bin/assemble index f3a4b8d..b98b5af 100755 --- a/s2i/bin/assemble +++ b/s2i/bin/assemble @@ -23,6 +23,11 @@ shopt -s dotglob echo "---> Installing application source ..." mv /tmp/src/* ./ +# Change the npm registry mirror if provided +if [ -n "$NPM_MIRROR" ]; then + npm config set registry $NPM_MIRROR +fi + echo "---> Building your Ruby application from source ..." if [ -f Gemfile ]; then ADDTL_BUNDLE_ARGS="--retry 2" @@ -60,12 +65,3 @@ fi # Fix source directory permissions fix-permissions ./ - -# Make the ./tmp folder world writeable as Rails or other frameworks might use -# it to store temporary data (uploads/cache/sessions/etcd). -# The ./db folder has to be writeable as well because when Rails complete the -# migration it writes the schema version into ./db/schema.db -set +e -[[ -d ./tmp ]] && chgrp -R 0 ./tmp && chmod -R g+rw ./tmp -[[ -d ./db ]] && chgrp -R 0 ./db && chmod -R g+rw ./db -set -e diff --git a/s2i/bin/usage b/s2i/bin/usage index c04792f..203f845 100755 --- a/s2i/bin/usage +++ b/s2i/bin/usage @@ -1,8 +1,6 @@ #!/bin/sh -DISTRO=`cat /etc/*-release | grep ^ID= | grep -Po '".*?"' | tr -d '"'` -NAMESPACE=centos -[[ $DISTRO =~ rhel* ]] && NAMESPACE=rhscl +DISTRO="`grep ^ID= /etc/*-release | cut -d'"' -f2`" cat <${tmpdir}/version - if [ $? -ne 0 ] ; then echo "ERROR: 'npm --version' does not work inside the image ${IMAGE_NAME}." >&2 return 1 fi - docker run --rm ${IMAGE_NAME} /bin/bash -c "npm install jquery && test -f node_modules/jquery/src/jquery.js" + docker run -d $(ct_mount_ca_file) --rm --cidfile="$cid_file" ${IMAGE_NAME}-testapp + + # Wait for the container to write it's CID file + ct_wait_for_cid "$cid_file" || return 1 + + docker exec $(cat "$cid_file") /bin/bash -c "npm --verbose install jquery && test -f node_modules/jquery/src/jquery.js" >${tmpdir}/jquery 2>&1 + if [ $? -ne 0 ] ; then echo "ERROR: npm could not install jquery inside the image ${IMAGE_NAME}." >&2 return 1 fi + if [ -n "$NPM_REGISTRY" ] && [ -f "$(full_ca_file_path)" ]; then + if ! grep -qo "$NPM_REGISTRY" "${tmpdir}/jquery"; then + echo "ERROR: Internal repository is NOT set. Even it is requested." + return 1 + fi + fi + + if [ -f "$cid_file" ]; then + docker stop $(cat "$cid_file") + rm "$cid_file" + fi : " Success!" } - # ct_path_append PATH_VARNAME DIRECTORY # ------------------------------------- # Append DIRECTORY to VARIABLE of name PATH_VARNAME, the VARIABLE must consist @@ -372,7 +420,7 @@ ct_test_response() { ct_registry_from_os() { local registry="" case $1 in - rhel7) + rhel*) registry=registry.access.redhat.com ;; *) @@ -443,7 +491,8 @@ ct_s2i_usage() # Argument: APP_PATH - local path to the app sources to be used in the test # Argument: SRC_IMAGE - image to be used as a base for the s2i build # Argument: DST_IMAGE - image name to be used during the tagging of the s2i build result -# Argument: S2I_ARGS - Additional list of source-to-image arguments, currently only used to check for pull-policy=never. +# Argument: S2I_ARGS - Additional list of source-to-image arguments. +# Only used to check for pull-policy=never and environment variable definitions. ct_s2i_build_as_df() { local app_path=$1; shift @@ -455,18 +504,50 @@ ct_s2i_build_as_df() local user_id= local df_name= local tmpdir= + local incremental=false + local mount_options="" + + # Run the entire thing inside a subshell so that we do not leak shell options outside of the function + ( + # Error out if any part of the build fails + set -e + # Use /tmp to not pollute cwd tmpdir=$(mktemp -d) df_name=$(mktemp -p "$tmpdir" Dockerfile.XXXX) - pushd "$tmpdir" + cd "$tmpdir" # Check if the image is available locally and try to pull it if it is not docker images "$src_image" &>/dev/null || echo "$s2i_args" | grep -q "pull-policy=never" || docker pull "$src_image" - user_id=$(docker inspect -f "{{.ContainerConfig.User}}" "$src_image") + user=$(docker inspect -f "{{.Config.User}}" "$src_image") + # Default to root if no user is set by the image + user=${user:-0} + # run the user through the image in case it is non-numeric or does not exist + # NOTE: The '-eq' test is used to check if $user is numeric as it will fail if $user is not an integer + if ! [ "$user" -eq "$user" ] 2>/dev/null && ! user_id=$(docker run --rm "$src_image" bash -c "id -u $user 2>/dev/null"); then + echo "ERROR: id of user $user not found inside image $src_image." + echo "Terminating s2i build." + return 1 + else + user_id=${user_id:-$user} + fi + echo "$s2i_args" | grep -q "\-\-incremental" && incremental=true + if $incremental; then + inc_tmp=$(mktemp -d --tmpdir incremental.XXXX) + setfacl -m "u:$user_id:rwx" "$inc_tmp" + # Check if the image exists, build should fail (for testing use case) if it does not + docker images "$dst_image" &>/dev/null || (echo "Image $dst_image not found."; false) + # Run the original image with a mounted in volume and get the artifacts out of it + cmd="if [ -s /usr/libexec/s2i/save-artifacts ]; then /usr/libexec/s2i/save-artifacts > \"$inc_tmp/artifacts.tar\"; else touch \"$inc_tmp/artifacts.tar\"; fi" + docker run --rm -v "$inc_tmp:$inc_tmp:Z" "$dst_image" bash -c "$cmd" + # Move the created content into the $tmpdir for the build to pick it up + mv "$inc_tmp/artifacts.tar" "$tmpdir/" + fi # Strip file:// from APP_PATH and copy its contents into current context mkdir -p "$local_app" cp -r "${app_path/file:\/\//}/." "$local_app" [ -d "$local_app/.s2i/bin/" ] && mv "$local_app/.s2i/bin" "$local_scripts" # Create a Dockerfile named df_name and fill it with proper content + #FIXME: Some commands could be combined into a single layer but not sure if worth the trouble for testing purposes cat <"$df_name" FROM $src_image LABEL "io.openshift.s2i.build.image"="$src_image" \\ @@ -477,13 +558,25 @@ EOF [ -d "$local_scripts" ] && echo "COPY $local_scripts /tmp/scripts" >> "$df_name" && echo "RUN chown -R $user_id:0 /tmp/scripts" >>"$df_name" echo "RUN chown -R $user_id:0 /tmp/src" >>"$df_name" - # Check for custom environment variables + # Check for custom environment variables inside .s2i/ folder if [ -e "$local_app/.s2i/environment" ]; then # Remove any comments and add the contents as ENV commands to the Dockerfile sed '/^\s*#.*$/d' "$local_app/.s2i/environment" | while read -r line; do echo "ENV $line" >>"$df_name" done fi + # Filter out env var definitions from $s2i_args and create Dockerfile ENV commands out of them + echo "$s2i_args" | grep -o -e '\(-e\|--env\)[[:space:]=]\S*=\S*' | sed -e 's/-e /ENV /' -e 's/--env[ =]/ENV /' >>"$df_name" + # Check if CA autority is present on host and add it into Dockerfile + [ -f "$(full_ca_file_path)" ] && echo "RUN cd /etc/pki/ca-trust/source/anchors && update-ca-trust extract" >>"$df_name" + + # Add in artifacts if doing an incremental build + if $incremental; then + echo "RUN mkdir /tmp/artifacts" >>"$df_name" + echo "ADD artifacts.tar /tmp/artifacts" >>"$df_name" + echo "RUN chown -R $user_id:0 /tmp/artifacts" >>"$df_name" + fi + echo "USER $user_id" >>"$df_name" # If exists, run the custom assemble script, else default to /usr/libexec/s2i/assemble if [ -x "$local_scripts/assemble" ]; then @@ -497,7 +590,13 @@ EOF else echo "CMD /usr/libexec/s2i/run" >>"$df_name" fi + + # Check if -v parameter is present in s2i_args and add it into docker build command + mount_options=$(echo "$s2i_args" | grep -o -e '\(-v\)[[:space:]]\.*\S*' || true) + # Run the build and tag the result - docker build -f "$df_name" -t "$dst_image" . - popd + docker build $mount_options -f "$df_name" --no-cache=true -t "$dst_image" . + ) } + +# vim: set tabstop=2:shiftwidth=2:expandtab: diff --git a/test/test_s2i.py b/test/test_s2i.py new file mode 100644 index 0000000..83d6090 --- /dev/null +++ b/test/test_s2i.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3 +import logging +import os +import time + +from conu import DockerBackend, S2IDockerImage, Probe, DockerRunBuilder + +import pytest + + +image_name = os.environ.get("IMAGE_NAME", "ruby") +test_dir = os.path.abspath(os.path.dirname(__file__)) +puma_app_path = os.path.join(test_dir, "puma-test-app") +rack_app_path = os.path.join(test_dir, "rack-test-app") +app_paths = [ + puma_app_path, + rack_app_path +] + + +backend = DockerBackend(logging_level=logging.DEBUG) + + +@pytest.fixture(scope="module", params=app_paths) +def app(request): + i = S2IDockerImage(image_name) + app_name = os.path.basename(request.param) + app = i.extend(request.param, app_name) + yield app + pass + app.rmi() + + +class TestSuite: + def test_s2i_apps(self, app): + c = app.run_via_binary() + try: + c.wait_for_port(8080) + assert c.is_port_open(8080) + response = c.http_request("/", port="8080") + assert response.ok + output = c.execute(["bash", "-c", "ruby --version"])[0] + expected = "ruby %s." % os.environ["VERSION"] + assert expected in output.decode("utf-8") + finally: + c.stop() + c.wait() + # debugging + print(list(c.logs())) + c.delete() + + def test_invoking_container(self): + image = backend.ImageClass(image_name) + c = image.run_via_binary(DockerRunBuilder(command=["bash", "-c", "ruby --version"])) + try: + c.wait() + logs = list(c.logs())[0].decode("utf-8") + finally: + c.stop() + c.wait() + c.delete() + assert "ruby " in logs + if os.environ.get("VERSION", None): + assert os.environ["VERSION"] in logs + + def test_usage(self): + i = S2IDockerImage(image_name) + c = i.run_via_binary() + + def logs_received(): + return len(list(c.logs())) > 0 + + try: + c.wait() + # even after waiting there is still a race in journal logging driver + Probe(timeout=10, pause=0.05, count=20, fnc=logs_received).run() + logs = [x.decode("utf-8") for x in c.logs()] + logs = "\n".join(logs).strip() + usage = i.usage() + # FIXME: workaround: `docker logs` can't handle logs like these: '\n\n\n' + assert logs.replace("\n", "") == usage.replace("\n", "") + finally: + c.delete()