From 5a7dc12c8ee1b3eb5f206701eb9e05d00a6989d1 Mon Sep 17 00:00:00 2001 From: Maxime Beck Date: Mon, 12 Aug 2019 16:51:44 +0200 Subject: [PATCH] Introduce Tomcat Source-to-Image --- res/tomcat-s2i/Dockerfile | 60 +++++++ res/tomcat-s2i/README.md | 46 ++++++ res/tomcat-s2i/conf/logging.properties | 79 +++++++++ res/tomcat-s2i/conf/server.xml | 120 ++++++++++++++ res/tomcat-s2i/conf/tomcat-users.xml | 44 +++++ res/tomcat-s2i/m2/settings.xml | 214 +++++++++++++++++++++++++ res/tomcat-s2i/s2i/bin/assemble | 66 ++++++++ res/tomcat-s2i/s2i/bin/run | 25 +++ res/tomcat-s2i/s2i/bin/save-artifacts | 26 +++ res/tomcat-s2i/s2i/bin/usage | 28 ++++ 10 files changed, 708 insertions(+) create mode 100644 res/tomcat-s2i/Dockerfile create mode 100644 res/tomcat-s2i/README.md create mode 100644 res/tomcat-s2i/conf/logging.properties create mode 100644 res/tomcat-s2i/conf/server.xml create mode 100644 res/tomcat-s2i/conf/tomcat-users.xml create mode 100644 res/tomcat-s2i/m2/settings.xml create mode 100644 res/tomcat-s2i/s2i/bin/assemble create mode 100644 res/tomcat-s2i/s2i/bin/run create mode 100644 res/tomcat-s2i/s2i/bin/save-artifacts create mode 100644 res/tomcat-s2i/s2i/bin/usage diff --git a/res/tomcat-s2i/Dockerfile b/res/tomcat-s2i/Dockerfile new file mode 100644 index 000000000000..f4d06a201958 --- /dev/null +++ b/res/tomcat-s2i/Dockerfile @@ -0,0 +1,60 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM openshift/base-centos7 + +EXPOSE 8080 + +ENV TOMCAT_VERSION=9.0.22 \ + MAVEN_VERSION=3.6.1 + +LABEL io.k8s.description="Platform for building and running JEE applications on Tomcat" \ + io.k8s.display-name="Tomcat Builder" \ + io.openshift.expose-services="8080:http" \ + io.openshift.tags="builder,tomcat" \ + io.openshift.s2i.destination="/opt/s2i/destination" + +# Install Maven, Tomcat 9.0.22 +RUN INSTALL_PKGS="tar java-1.8.0-openjdk java-1.8.0-openjdk-devel" && \ + yum install -y --enablerepo=centosplus $INSTALL_PKGS && \ + rpm -V $INSTALL_PKGS && \ + yum clean all -y && \ + (curl -v https://www.apache.org/dist/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz | \ + tar -zx -C /usr/local) && \ + ln -sf /usr/local/apache-maven-$MAVEN_VERSION/bin/mvn /usr/local/bin/mvn && \ + mkdir -p $HOME/.m2 && \ + mkdir -p /tomcat && \ + (curl -v http://www.apache.org/dist/tomcat/tomcat-9/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz | tar -zx --strip-components=1 -C /tomcat) && \ + rm -rf /tomcat/webapps/* && \ + mkdir -p /opt/s2i/destination + +# Add s2i customizations +ADD ./m2/settings.xml $HOME/.m2/ +ADD ./conf /tomcat/conf + +# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH +COPY ./s2i/bin/ $STI_SCRIPTS_PATH + +RUN chmod -R a+rw /tomcat && \ + chmod a+rwx /tomcat/* && \ + chmod +x /tomcat/bin/*.sh && \ + chmod -R a+rw $HOME && \ + chmod -R +x $STI_SCRIPTS_PATH && \ + chmod -R g+rw /opt/s2i/destination + +USER 1001 + +CMD $STI_SCRIPTS_PATH/usage \ No newline at end of file diff --git a/res/tomcat-s2i/README.md b/res/tomcat-s2i/README.md new file mode 100644 index 000000000000..0f176ed832b1 --- /dev/null +++ b/res/tomcat-s2i/README.md @@ -0,0 +1,46 @@ + + +# Tomcat - Source-to-Image Builder +This repository provides a CentOS-based Docker image that enables Source-to-Image (S2I) building for Tomcat. It builds the sources of your webapp and deploys it to a fully functional containerized Tomcat Server. The generated image can then easily be run locally or deployed to your Openshift Server. + +## Build the Image +You can build the Tomcat-S2I Docker image by cloning the present repository and running `docker build`: +```bash +$ docker build . -t [IMAGE_BUILDER_TAG] +``` +Where `[IMAGE_BUILDER_TAG]` is the tag you would like to assign to the tomcat-s2i image builder. + +## Usage +To use the provided image, you'll first need to install [Openshift S2I](https://github.com/openshift/source-to-image#installation). Then simply run the following command: +```bash +$ s2i build [SOURCE_URL] [IMAGE_BUILDER_TAG] [IMAGE_TAG] +``` +Where `[SOURCE_URL]` is either the URL to your Git repository or the path to your local sources and `[IMAGE_TAG]` is simply the tag you would like to assign to the generated image. + +Once built, you can run it locally: +```bash +$ docker run -p 8080:8080 [IMAGE_TAG] +``` +Or push it to a Docker Registry for further use on your Openshift Server: +```bash +$ docker push [IMAGE_TAG] +``` \ No newline at end of file diff --git a/res/tomcat-s2i/conf/logging.properties b/res/tomcat-s2i/conf/logging.properties new file mode 100644 index 000000000000..e8b7b16bc4ea --- /dev/null +++ b/res/tomcat-s2i/conf/logging.properties @@ -0,0 +1,79 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler + +.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler + +############################################################ +# Handler specific properties. +# Describes specific configuration info for Handlers. +############################################################ + +1catalina.org.apache.juli.AsyncFileHandler.level = FINE +1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs +1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina. +1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90 +1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8 + +2localhost.org.apache.juli.AsyncFileHandler.level = FINE +2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs +2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost. +2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90 +2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8 + +3manager.org.apache.juli.AsyncFileHandler.level = FINE +3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs +3manager.org.apache.juli.AsyncFileHandler.prefix = manager. +3manager.org.apache.juli.AsyncFileHandler.maxDays = 90 +3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8 + +4host-manager.org.apache.juli.AsyncFileHandler.level = FINE +4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs +4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager. +4host-manager.org.apache.juli.AsyncFileHandler.maxDays = 90 +4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8 + +java.util.logging.ConsoleHandler.level = FINE +java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter +java.util.logging.ConsoleHandler.encoding = UTF-8 + + +############################################################ +# Facility specific properties. +# Provides extra control for each logger. +############################################################ + +org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO +org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler + +org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO +org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler + +org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO +org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler + +# For example, set the org.apache.catalina.util.LifecycleBase logger to log +# each component that extends LifecycleBase changing state: +#org.apache.catalina.util.LifecycleBase.level = FINE + +# To see debug messages in TldLocationsCache, uncomment the following line: +#org.apache.jasper.compiler.TldLocationsCache.level = FINE + +# To see debug messages for HTTP/2 handling, uncomment the following line: +#org.apache.coyote.http2.level = FINE + +# To see debug messages for WebSocket handling, uncomment the following line: +#org.apache.tomcat.websocket.level = FINE diff --git a/res/tomcat-s2i/conf/server.xml b/res/tomcat-s2i/conf/server.xml new file mode 100644 index 000000000000..7c24b18f7b9f --- /dev/null +++ b/res/tomcat-s2i/conf/server.xml @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/tomcat-s2i/conf/tomcat-users.xml b/res/tomcat-s2i/conf/tomcat-users.xml new file mode 100644 index 000000000000..aef66d0f4c88 --- /dev/null +++ b/res/tomcat-s2i/conf/tomcat-users.xml @@ -0,0 +1,44 @@ + + + + + + + diff --git a/res/tomcat-s2i/m2/settings.xml b/res/tomcat-s2i/m2/settings.xml new file mode 100644 index 000000000000..b31ab577b969 --- /dev/null +++ b/res/tomcat-s2i/m2/settings.xml @@ -0,0 +1,214 @@ + + + + + + + + + + + + + + + + + jboss-eap-repository + + + com.redhat.xpaas.repo.redhatga + + + + + + jboss-eap-repository + https://maven.repository.redhat.com/techpreview/all + + true + + + false + + + + + + jboss-eap-plugin-repository + https://maven.repository.redhat.com/techpreview/all + + true + + + false + + + + + + + + jboss-eap-repository-insecure + + + jboss-eap-repository + http://maven.repository.redhat.com/techpreview/all + + true + + + false + + + + + + jboss-eap-plugin-repository + http://maven.repository.redhat.com/techpreview/all + + true + + + false + + + + + + + + jboss-community-repository + + + com.redhat.xpaas.repo.jbossorg + + + + + + jboss-community-repository + https://repository.jboss.org/nexus/content/groups/public/ + + true + + + false + + + + + + jboss-community-plugin-repository + https://repository.jboss.org/nexus/content/groups/public/ + + true + + + false + + + + + + + + jboss-community-repository-insecure + + + jboss-community-repository + http://repository.jboss.org/nexus/content/groups/public/ + + true + + + false + + + + + + jboss-community-plugin-repository + http://repository.jboss.org/nexus/content/groups/public/ + + true + + + false + + + + + + + + securecentral + + + central + https://repo1.maven.org/maven2 + + true + + + + + + central + https://repo1.maven.org/maven2 + + true + + + + + + + + insecurecentral + + + central + http://repo1.maven.org/maven2 + + true + + + + + + central + http://repo1.maven.org/maven2 + + true + + + + + + + + securecentral + + \ No newline at end of file diff --git a/res/tomcat-s2i/s2i/bin/assemble b/res/tomcat-s2i/s2i/bin/assemble new file mode 100644 index 000000000000..9917c2b9ec42 --- /dev/null +++ b/res/tomcat-s2i/s2i/bin/assemble @@ -0,0 +1,66 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +#!/bin/bash -e +# +# S2I assemble script for the 's2i-tomcat' 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 +# + +# insert settings for mirrors/repository managers into settings.xml if supplied +function configure_mirrors() { + if [ -n "$MAVEN_MIRROR_URL" ]; then + xml=" \ + mirror.default\ + $MAVEN_MIRROR_URL\ + external:*\ + " + sed -i "s||$xml|" $HOME/.m2/settings.xml + fi +} + +# If the 's2i-tomcat' assemble script is executed with the '-h' flag, print the usage. +if [[ "$1" == "-h" ]]; then + exec /usr/libexec/s2i/usage +fi + +# Restore artifacts from the previous build (if they exist). +# +if [ "$(ls /opt/s2i/destination/artifacts/ 2>/dev/null)" ]; then + echo "---> Restoring build artifacts..." + mv /opt/s2i/destination/artifacts/. ./ +fi + +echo "---> Installing application source..." +cp -Rf /opt/s2i/destination/src/. ./ + +echo "---> Building application from source..." +# TODO: Add build steps for your application, eg npm install, bundle install, pip install, etc. + +configure_mirrors + + +if [ -n "$MAVEN_PROFILE" ]; then + mvn package -P$MAVEN_PROFILE -Dmaven.test.skip=true +else + mvn -Dmaven.test.skip=true package +fi + +mv ./target/*.war /tomcat/webapps/ +mvn clean diff --git a/res/tomcat-s2i/s2i/bin/run b/res/tomcat-s2i/s2i/bin/run new file mode 100644 index 000000000000..b3872878d0c4 --- /dev/null +++ b/res/tomcat-s2i/s2i/bin/run @@ -0,0 +1,25 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +#!/bin/bash -e +# +# S2I run script for the 's2i-tomcat' 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 +# +bash -c "/tomcat/bin/catalina.sh run" diff --git a/res/tomcat-s2i/s2i/bin/save-artifacts b/res/tomcat-s2i/s2i/bin/save-artifacts new file mode 100644 index 000000000000..3c2ca922267e --- /dev/null +++ b/res/tomcat-s2i/s2i/bin/save-artifacts @@ -0,0 +1,26 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +#!/bin/sh -e +# +# S2I save-artifacts script for the 'tomcat-s2i' 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 - diff --git a/res/tomcat-s2i/s2i/bin/usage b/res/tomcat-s2i/s2i/bin/usage new file mode 100644 index 000000000000..ed249ca37998 --- /dev/null +++ b/res/tomcat-s2i/s2i/bin/usage @@ -0,0 +1,28 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +#!/bin/bash -e +cat < tomcat-s2i + +You can then run the resulting image via: +docker run -p 8080:8080 +EOF