Olaf Bohlen
2022-11-30 20e36bbddc8e3386c06f516394e66bdf8fcb356d
sample s2i gradle builder
9 files added
286 ■■■■■ changed files
.gitignore 1 ●●●● patch | view | raw | blame | history
Dockerfile 43 ●●●●● patch | view | raw | blame | history
Makefile 10 ●●●●● patch | view | raw | blame | history
s2i/bin/assemble 35 ●●●●● patch | view | raw | blame | history
s2i/bin/run 10 ●●●●● patch | view | raw | blame | history
s2i/bin/save-artifacts 10 ●●●●● patch | view | raw | blame | history
s2i/bin/usage 12 ●●●●● patch | view | raw | blame | history
test/run 155 ●●●●● patch | view | raw | blame | history
test/test-app/index.html 10 ●●●●● patch | view | raw | blame | history
.gitignore
New file
@@ -0,0 +1 @@
*~
Dockerfile
New file
@@ -0,0 +1,43 @@
# s2i-gradle-kbs
## ATTENTION ##
# This is a demo, do not use for anything else.
# no warranty, if it breaks your systems, you have to keep the pieces.
# Olaf Bohlen, obohlen@redhat.com, 2022-11-30
FROM registry.access.redhat.com/ubi9
# TODO: Put the maintainer name in the image metadata
LABEL maintainer="Olaf Bohlen <obohlen@redhat.com>"
# TODO: Rename the builder environment variable to inform users about application you provide them
ENV BUILDER_VERSION 0.1 \
    PATH=${PATH}:/opt/gradle/gradle-7.6/bin
# TODO: Set labels used in OpenShift to describe the builder image
LABEL io.k8s.description="Platform for building springboot with Gradle" \
      io.k8s.display-name="builder 0.1.0" \
      io.openshift.expose-services="8080:http" \
      io.openshift.tags="builder,jee,java,gradle"
# TODO: Install required packages here:
# RUN yum install -y ... && yum clean all -y
RUN yum install -y java-11-openjdk unzip && \
    yum clean all -y && \
    mkdir -p /opt/gradle /opt/app-root /opt/app-src && \
    cd /opt/gradle && \
    curl -o /tmp/gradle.zip https://downloads.gradle-dn.com/distributions/gradle-7.6-bin.zip && \
    unzip -d /opt/gradle /tmp/gradle.zip && \
    chown -R 1001:1001 /opt/app-root && \
    chown -R 1001:0 /opt/app-src
# TODO: Copy the S2I scripts to /usr/libexec/s2i, since openshift/base-centos7 image
# sets io.openshift.s2i.scripts-url label that way, or update that label
COPY ./s2i/bin/ /usr/libexec/s2i
# This default user is created in the openshift/base-centos7 image
USER 1001
# TODO: Set the default port for applications built using this image
EXPOSE 8080
# TODO: Set the default CMD for the image
CMD ["/usr/libexec/s2i/usage"]
Makefile
New file
@@ -0,0 +1,10 @@
IMAGE_NAME = s2i-gradle-kbs
.PHONY: build
build:
    docker build -t $(IMAGE_NAME) .
.PHONY: test
test:
    docker build -t $(IMAGE_NAME)-candidate .
    IMAGE_NAME=$(IMAGE_NAME)-candidate test/run
s2i/bin/assemble
New file
@@ -0,0 +1,35 @@
#!/bin/bash -e
#
# S2I assemble script for the 's2i-gradle-kbs' image.
# The 'assemble' script builds your application source so that it is ready to run.
#
# For more information refer to the documentation:
#    https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
# If the 's2i-gradle-kbs' assemble script is executed with the '-h' flag, print the usage.
if [[ "$1" == "-h" ]]; then
    exec /usr/libexec/s2i/usage
fi
PATH=/opt/gradle/gradle-7.6/bin:$PATH
export PATH
# Restore artifacts from the previous build (if they exist).
#
if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then
  echo "---> Restoring build artifacts..."
  shopt -s dotglob
  mv /tmp/artifacts/* ./
  shopt -u dotglob
fi
echo "---> Installing application source..."
cd /opt/app-src
cp -Rf /tmp/src/. ./
echo "---> Building application from source..."
# TODO: Add build steps for your application, eg npm install, bundle install, pip install, etc.
gradle clean build
tjar=$(find build/libs -name \*.jar | head -1) # this is a hack
cp ${tjar} /opt/app-root/fat.jar
s2i/bin/run
New file
@@ -0,0 +1,10 @@
#!/bin/bash -e
#
# S2I run script for the 's2i-gradle-kbs' image.
# The run script executes the server that runs your application.
#
# For more information see the documentation:
#    https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
exec java -Djava.security.egd=file:/dev/urandom -jar /opt/app-root/fat.jar
s2i/bin/save-artifacts
New file
@@ -0,0 +1,10 @@
#!/bin/sh -e
#
# S2I save-artifacts script for the 's2i-gradle-kbs' image.
# The save-artifacts script streams a tar archive to standard output.
# The archive contains the files and folders you want to re-use in the next build.
#
# For more information see the documentation:
#    https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
# tar cf - <list of files and folders>
s2i/bin/usage
New file
@@ -0,0 +1,12 @@
#!/bin/bash -e
cat <<EOF
This is the s2i-gradle-kbs S2I image:
To use it, install S2I: https://github.com/openshift/source-to-image
Sample invocation:
s2i build <source code path/URL> s2i-gradle-kbs <application image>
You can then run the resulting image via:
docker run <application image>
EOF
test/run
New file
@@ -0,0 +1,155 @@
#!/bin/bash
#
# The 'run' performs a simple test that verifies the S2I image.
# The main focus here is to exercise the S2I scripts.
#
# For more information see the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
IMAGE_NAME=${IMAGE_NAME-s2i-gradle-kbs-candidate}
# Determining system utility executables (darwin compatibility check)
READLINK_EXEC="readlink -zf"
MKTEMP_EXEC="mktemp --suffix=.cid"
if [[ "$OSTYPE" =~ 'darwin' ]]; then
  READLINK_EXEC="readlink"
  MKTEMP_EXEC="mktemp"
  ! type -a "greadlink" &>"/dev/null" || READLINK_EXEC="greadlink"
  ! type -a "gmktemp" &>"/dev/null" || MKTEMP_EXEC="gmktemp"
fi
_dir="$(dirname "${BASH_SOURCE[0]}")"
test_dir="$($READLINK_EXEC ${_dir} || echo ${_dir})"
image_dir=$($READLINK_EXEC ${test_dir}/.. || echo ${test_dir}/..)
scripts_url="${image_dir}/.s2i/bin"
cid_file=$($MKTEMP_EXEC -u)
# Since we built the candidate image locally, we don't want S2I to attempt to pull
# it from Docker hub
s2i_args="--pull-policy=never --loglevel=2"
# Port the image exposes service to be tested
test_port=8080
image_exists() {
  docker inspect $1 &>/dev/null
}
container_exists() {
  image_exists $(cat $cid_file)
}
container_ip() {
  docker inspect --format="{{(index .NetworkSettings.Ports \"$test_port/tcp\" 0).HostIp }}" $(cat $cid_file) | sed 's/0.0.0.0/localhost/'
}
container_port() {
  docker inspect --format="{{(index .NetworkSettings.Ports \"$test_port/tcp\" 0).HostPort }}" "$(cat "${cid_file}")"
}
run_s2i_build() {
  s2i build --incremental=true ${s2i_args} ${test_dir}/test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp
}
prepare() {
  if ! image_exists ${IMAGE_NAME}; then
    echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed."
    exit 1
  fi
  # s2i build requires the application is a valid 'Git' repository
  pushd ${test_dir}/test-app >/dev/null
  git init
  git config user.email "build@localhost" && git config user.name "builder"
  git add -A && git commit -m "Sample commit"
  popd >/dev/null
  run_s2i_build
}
run_test_application() {
  docker run --rm --cidfile=${cid_file} -p ${test_port}:${test_port} ${IMAGE_NAME}-testapp
}
cleanup() {
  if [ -f $cid_file ]; then
    if container_exists; then
      docker stop $(cat $cid_file)
    fi
  fi
  if image_exists ${IMAGE_NAME}-testapp; then
    docker rmi ${IMAGE_NAME}-testapp
  fi
}
check_result() {
  local result="$1"
  if [[ "$result" != "0" ]]; then
    echo "S2I image '${IMAGE_NAME}' test FAILED (exit code: ${result})"
    cleanup
    exit $result
  fi
}
wait_for_cid() {
  local max_attempts=10
  local sleep_time=1
  local attempt=1
  local result=1
  while [ $attempt -le $max_attempts ]; do
    [ -f $cid_file ] && break
    echo "Waiting for container to start..."
    attempt=$(( $attempt + 1 ))
    sleep $sleep_time
  done
}
test_usage() {
  echo "Testing 's2i usage'..."
  s2i usage ${s2i_args} ${IMAGE_NAME} &>/dev/null
}
test_connection() {
  echo "Testing HTTP connection (http://$(container_ip):$(container_port))"
  local max_attempts=10
  local sleep_time=1
  local attempt=1
  local result=1
  while [ $attempt -le $max_attempts ]; do
    echo "Sending GET request to http://$(container_ip):$(container_port)/"
    response_code=$(curl -s -w %{http_code} -o /dev/null http://$(container_ip):$(container_port)/)
    status=$?
    if [ $status -eq 0 ]; then
      if [ $response_code -eq 200 ]; then
        result=0
      fi
      break
    fi
    attempt=$(( $attempt + 1 ))
    sleep $sleep_time
  done
  return $result
}
# Build the application image twice to ensure the 'save-artifacts' and
# 'restore-artifacts' scripts are working properly
prepare
run_s2i_build
check_result $?
# Verify the 'usage' script is working properly
test_usage
check_result $?
# Verify that the HTTP connection can be established to test application container
run_test_application &
# Wait for the container to write its CID file
wait_for_cid
test_connection
check_result $?
cleanup
test/test-app/index.html
New file
@@ -0,0 +1,10 @@
<!doctype html>
<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>