From e8619fda76f9e8b6f08076261601be3c176b275c Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Fri, 21 Jun 2024 13:07:13 +0200 Subject: [PATCH 1/8] feat(modules): jolokia module --- .../opt/jboss/container/jolokia/jolokia-opts | 92 +++++++++++++++++++ .../2.0.0/configure.sh | 23 +++++ .../2.0.0/module.yaml | 68 ++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 modules/org.eclipse.jkube.jolokia/2.0.0/artifacts/opt/jboss/container/jolokia/jolokia-opts create mode 100644 modules/org.eclipse.jkube.jolokia/2.0.0/configure.sh create mode 100644 modules/org.eclipse.jkube.jolokia/2.0.0/module.yaml diff --git a/modules/org.eclipse.jkube.jolokia/2.0.0/artifacts/opt/jboss/container/jolokia/jolokia-opts b/modules/org.eclipse.jkube.jolokia/2.0.0/artifacts/opt/jboss/container/jolokia/jolokia-opts new file mode 100644 index 0000000..f0a3e12 --- /dev/null +++ b/modules/org.eclipse.jkube.jolokia/2.0.0/artifacts/opt/jboss/container/jolokia/jolokia-opts @@ -0,0 +1,92 @@ +#!/bin/sh + +# Check whether a given config is contained in AB_JOLOKIA_OPTS +is_in_jolokia_opts() { + local prop=$1 + if [ -n "${AB_JOLOKIA_OPTS}" ] && [ x"${AB_JOLOKIA_OPTS}" != x"${AB_JOLOKIA_OPTS/${prop}/}" ]; then + echo "yes" + else + echo "no" + fi +} + +get_jolokia_properties() { + + echo "host=${AB_JOLOKIA_HOST:-*}" + echo "port=${AB_JOLOKIA_PORT:-8778}" + echo "discoveryEnabled=${AB_JOLOKIA_DISCOVERY_ENABLED:=false}" + + if [ -n "$AB_JOLOKIA_PASSWORD" ]; then + echo "user=${AB_JOLOKIA_USER:-jolokia}" + echo "password=${AB_JOLOKIA_PASSWORD}" + fi + if [ -n "$AB_JOLOKIA_HTTPS" ]; then + echo "protocol=https" + use_https=1 + fi + + # Integration with OpenShift client cert auth is enabled + # by default if not explicitly turned off by setting to 'false' + if [ "x${AB_JOLOKIA_AUTH_OPENSHIFT}" != "xfalse" ] && [ -f "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" ]; then + echo "useSslClientAuthentication=true" + echo "extraClientCheck=true" + + if [ -z ${use_https+x} ]; then + echo "protocol=https" + fi + if [ $(is_in_jolokia_opts "caCert") != "yes" ]; then + echo "caCert=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + fi + + if [ $(is_in_jolokia_opts "clientPrincipal") != "yes" ]; then + if [ x"${AB_JOLOKIA_AUTH_OPENSHIFT}" != x"${AB_JOLOKIA_AUTH_OPENSHIFT/=/}" ]; then + # Supposed to contain a principal name to check + echo "clientPrincipal=`echo ${AB_JOLOKIA_AUTH_OPENSHIFT} | sed -e 's/ /\\\\ /g'`" + else + echo "clientPrincipal=cn=system:master-proxy" + fi + fi + fi + + # Add extra opts + if [ -n "${AB_JOLOKIA_OPTS}" ]; then + echo "${AB_JOLOKIA_OPTS}" | tr "," "\n" + fi + +} + +write_jolokia_properties() { + local jolokia_property_file="$1" + + # Setup Jolokia to accept basic auth, using a randomly generated password that is stored + # in the container in the ${DEPLOYMENTS_DIR}/jolokia.pw file. + if [ "$AB_JOLOKIA_PASSWORD_RANDOM" == "true" ]; then + pw_file="${JBOSS_CONTAINER_JOLOKIA_MODULE}/etc/jolokia.pw" + if [ -f "${pw_file}" ] ; then + AB_JOLOKIA_PASSWORD=`cat "${pw_file}"` + else + AB_JOLOKIA_PASSWORD=`tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1` + touch "${pw_file}" + chmod 660 "${pw_file}" + cat > "${pw_file}" < "${jolokia_property_file}" < Date: Fri, 21 Jun 2024 13:07:34 +0200 Subject: [PATCH 2/8] feat(modules): jvm bash module --- .../jboss/container/java/jvm/debug-options | 20 +++++ .../container/java/jvm/java-default-options | 89 +++++++++++++++++++ .../org.eclipse.jkube.jvm/bash/configure.sh | 14 +++ .../org.eclipse.jkube.jvm/bash/module.yaml | 62 +++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/debug-options create mode 100644 modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options create mode 100644 modules/org.eclipse.jkube.jvm/bash/configure.sh create mode 100644 modules/org.eclipse.jkube.jvm/bash/module.yaml diff --git a/modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/debug-options b/modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/debug-options new file mode 100644 index 0000000..d65e160 --- /dev/null +++ b/modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/debug-options @@ -0,0 +1,20 @@ +#!/bin/sh + +# Check for debug options and echo them if enabled. Meant to be included by +# a run script. + +debug_options() { + if [ "x${JAVA_ENABLE_DEBUG}" != "x" -o "x${JAVA_DEBUG_ENABLE}" != "x" -o "x${JAVA_DEBUG}" != "x" ]; then + local debug_port="${JAVA_DEBUG_PORT:-5005}" + local suspend_mode="n" + if [ -n "${JAVA_DEBUG_SUSPEND:-}" ]; then + if ! echo "${JAVA_DEBUG_SUSPEND}" | grep -q -e '^\(false\|n\|no\|0\)$'; then + suspend_mode="y" + fi + fi + echo "-agentlib:jdwp=transport=dt_socket,server=y,suspend=${suspend_mode},address=${debug_port}" + fi +} + +## Echo options, trimming trailing and multiple spaces +echo "$(debug_options)" | awk '$1=$1' diff --git a/modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options b/modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options new file mode 100644 index 0000000..fba9d15 --- /dev/null +++ b/modules/org.eclipse.jkube.jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options @@ -0,0 +1,89 @@ +#!/bin/sh +# ================================================================= +# Detect whether running in a container and set appropriate options +# for limiting Java VM resources +# +# Usage: JAVA_OPTS="$(java-default-options.sh)" + +# stubs for jvm specific overrides +jvm_specific_options() { + : +} + +jvm_specific_diagnostics() { + : +} + +# Include overridden jvm_specific_*() functions +if [ -f "${JBOSS_CONTAINER_OPENJDK_JDK_MODULE}/jvm-options" ]; then + source "${JBOSS_CONTAINER_OPENJDK_JDK_MODULE}/jvm-options" +fi + +# Check for memory options and calculate a sane default if not given +max_memory() { + case "$JAVA_MAX_MEM_RATIO" in + "0") # explicitly disabled + return + ;; + "") + maxmem="80.0" + ;; + *) + maxmem="$(printf "%.0f.0" "$JAVA_MAX_MEM_RATIO")" + ;; + esac + echo "-XX:MaxRAMPercentage=$maxmem" +} + +# Switch on diagnostics except when switched off +diagnostics() { + if [ "x$JAVA_DIAGNOSTICS" != "x" ]; then + echo "$(jvm_specific_diagnostics)" + fi +} + +gc_config() { + local minHeapFreeRatio=${GC_MIN_HEAP_FREE_RATIO:-10} + local maxHeapFreeRatio=${GC_MAX_HEAP_FREE_RATIO:-20} + local timeRatio=${GC_TIME_RATIO:-4} + local adaptiveSizePolicyWeight=${GC_ADAPTIVE_SIZE_POLICY_WEIGHT:-90} + local gcOptions="${GC_CONTAINER_OPTIONS:--XX:+UseParallelGC}" + + # for compat reasons we don't set a default value for metaspaceSize + local metaspaceSize + # We also don't set a default value for maxMetaspaceSize + local maxMetaspaceSize=${GC_MAX_METASPACE_SIZE} + + if [ -n "${GC_METASPACE_SIZE}" ]; then + metaspaceSize=${GC_METASPACE_SIZE} + if [ -n "${maxMetaspaceSize}" ]; then + # clamp the max size of metaspaceSize to be <= maxMetaspaceSize + if [ "${metaspaceSize}" -gt "${maxMetaspaceSize}" ]; then + metaspaceSize=${maxMetaspaceSize} + fi + fi + fi + + local allOptions="$(jvm_specific_options) " + allOptions+="${gcOptions} " + allOptions+="-XX:MinHeapFreeRatio=${minHeapFreeRatio} " + allOptions+="-XX:MaxHeapFreeRatio=${maxHeapFreeRatio} " + allOptions+="-XX:GCTimeRatio=${timeRatio} " + allOptions+="-XX:AdaptiveSizePolicyWeight=${adaptiveSizePolicyWeight} " + # if no value was specified for maxMetaSpaceSize we should skip passing it entirely + if [ -n "${maxMetaspaceSize}" ]; then + allOptions+="-XX:MaxMetaspaceSize=${maxMetaspaceSize}m " + fi + if [ -n "${metaspaceSize}" ]; then + allOptions+="-XX:MetaspaceSize=${metaspaceSize}m " + fi + + echo "${allOptions}" +} + +error_handling() { + echo "-XX:+ExitOnOutOfMemoryError" +} + +## Echo options, trimming trailing and multiple spaces +echo "$(max_memory) $(gc_config) $(diagnostics) $(error_handling)" | awk '$1=$1' diff --git a/modules/org.eclipse.jkube.jvm/bash/configure.sh b/modules/org.eclipse.jkube.jvm/bash/configure.sh new file mode 100644 index 0000000..79e86ec --- /dev/null +++ b/modules/org.eclipse.jkube.jvm/bash/configure.sh @@ -0,0 +1,14 @@ +#!/bin/sh +# Configure module +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +chown -R $USER:root $SCRIPT_DIR +chmod -R ug+rwX $SCRIPT_DIR +chmod ug+x ${ARTIFACTS_DIR}/opt/jboss/container/java/jvm/* + +pushd ${ARTIFACTS_DIR} +cp -pr * / +popd diff --git a/modules/org.eclipse.jkube.jvm/bash/module.yaml b/modules/org.eclipse.jkube.jvm/bash/module.yaml new file mode 100644 index 0000000..7aa4804 --- /dev/null +++ b/modules/org.eclipse.jkube.jvm/bash/module.yaml @@ -0,0 +1,62 @@ +schema_version: 1 +name: org.eclipse.jkube.jvm.bash +version: 1.0.0 +description: > + Provides support for configuring Java JVM, e.g. GC settings, etc. Basic usage + is opts=$($JBOSS_CONTAINER_JAVA_JVM_MODULE/java-default-options). + + Adapted from: + - https://github.com/jboss-container-images/openjdk/blob/d14ec7f363956b73684409c8b6bd9c766507013b/modules/jvm/ + - https://github.com/jboss-openshift/cct_module/blob/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/java/jvm/bash/ + +execute: + - script: configure.sh + +modules: + install: + - name: org.eclipse.jkube.user + - name: jboss.container.java.proxy.bash + +envs: + - name: JBOSS_CONTAINER_JAVA_JVM_MODULE + value: /opt/jboss/container/java/jvm + - name: JAVA_OPTS + description: JVM options passed to the `java` command. + example: "-verbose:class" + - name: JAVA_OPTS_APPEND + description: User specified Java options to be appended to the generated options. + This variable has no effect if `JAVA_OPTS` has been defined. + example: "-Dsome.property=foo" + - name: JAVA_MAX_MEM_RATIO + description: Specify the maximum heap memory. Corresponds to the JVM argument `-XX:MaxRAMPercentage`. The default is `80.0` which means 80% of the available memory. You can disable this mechanism by setting the value to `0`. The supplied value can be an integer or float, but only the whole number part is used. + example: "90.0" + - name: JAVA_DIAGNOSTICS + description: "Set this to get some diagnostics information to standard output when things are happening. **Note: ** This option, if set to true, will set `-XX :+UnlockDiagnosticVMOptions`. **Disabled by default.**" + example: "true" + - name: JAVA_DEBUG + description: If set remote debugging will be switched on. **Disabled by default.** + example: "true" + - name: JAVA_DEBUG_PORT + description: Port used for remote debugging. Defaults to *5005*. + example: "8787" + - name: GC_MIN_HEAP_FREE_RATIO + description: Minimum percentage of heap free after GC to avoid expansion. + example: "20" + - name: GC_MAX_HEAP_FREE_RATIO + description: Maximum percentage of heap free after GC to avoid shrinking. + example: "40" + - name: GC_TIME_RATIO + description: Specifies the ratio of the time spent outside the garbage collection (for example, the time spent for application execution) to the time spent in the garbage collection. + example: "4" + - name: GC_ADAPTIVE_SIZE_POLICY_WEIGHT + description: The weighting given to the current GC time versus previous GC times. + example: "90" + - name: GC_METASPACE_SIZE + description: The initial metaspace size. + example: "20" + - name: GC_MAX_METASPACE_SIZE + description: The maximum metaspace size. + example: "100" + - name: GC_CONTAINER_OPTIONS + description: specify Java GC to use. The value of this variable should contain the necessary JRE command-line options to specify the required GC, which will override the default of `-:+UseParallelGC`. + example: -XX:+UseG1GC From c2aeb542886d8dc491b3f7773a73a91ea18614e2 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Fri, 21 Jun 2024 13:08:24 +0200 Subject: [PATCH 3/8] feat(modules): moved singleton-jdk-module --- .../singleton-jdk}/configure.sh | 0 .../singleton-jdk}/module.yaml | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename modules/{org.eclipse.jkube.jvm.singleton-jdk => org.eclipse.jkube.jvm/singleton-jdk}/configure.sh (100%) rename modules/{org.eclipse.jkube.jvm.singleton-jdk => org.eclipse.jkube.jvm/singleton-jdk}/module.yaml (100%) diff --git a/modules/org.eclipse.jkube.jvm.singleton-jdk/configure.sh b/modules/org.eclipse.jkube.jvm/singleton-jdk/configure.sh similarity index 100% rename from modules/org.eclipse.jkube.jvm.singleton-jdk/configure.sh rename to modules/org.eclipse.jkube.jvm/singleton-jdk/configure.sh diff --git a/modules/org.eclipse.jkube.jvm.singleton-jdk/module.yaml b/modules/org.eclipse.jkube.jvm/singleton-jdk/module.yaml similarity index 100% rename from modules/org.eclipse.jkube.jvm.singleton-jdk/module.yaml rename to modules/org.eclipse.jkube.jvm/singleton-jdk/module.yaml From 4ca73df4231f86c18b89f77e19a558c04b687100 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Fri, 21 Jun 2024 13:08:43 +0200 Subject: [PATCH 4/8] feat(modules): maven modules --- .../8.2.3.8/configure.sh | 12 + .../8.2.3.8/module.yaml | 26 ++ .../maven/default/jboss-settings.xml | 243 +++++++++++ .../jboss/container/maven/default/maven.sh | 393 ++++++++++++++++++ .../default/configure.sh | 29 ++ .../default/module.yaml | 158 +++++++ .../module/8.2.3.8/artifacts/maven.module | 5 + .../module/8.2.3.8/configure.sh | 7 + .../module/8.2.3.8/module.yaml | 8 + .../jboss/container/maven/s2i/maven-overrides | 32 ++ .../opt/jboss/container/maven/s2i/maven-s2i | 140 +++++++ .../s2i/artifacts/usr/local/s2i/assemble | 9 + .../artifacts/usr/local/s2i/save-artifacts | 9 + .../org.eclipse.jkube.maven/s2i/configure.sh | 15 + .../org.eclipse.jkube.maven/s2i/module.yaml | 42 ++ 15 files changed, 1128 insertions(+) create mode 100644 modules/org.eclipse.jkube.maven/8.2.3.8/configure.sh create mode 100644 modules/org.eclipse.jkube.maven/8.2.3.8/module.yaml create mode 100644 modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/jboss-settings.xml create mode 100644 modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/maven.sh create mode 100644 modules/org.eclipse.jkube.maven/default/configure.sh create mode 100644 modules/org.eclipse.jkube.maven/default/module.yaml create mode 100644 modules/org.eclipse.jkube.maven/module/8.2.3.8/artifacts/maven.module create mode 100644 modules/org.eclipse.jkube.maven/module/8.2.3.8/configure.sh create mode 100644 modules/org.eclipse.jkube.maven/module/8.2.3.8/module.yaml create mode 100644 modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-overrides create mode 100644 modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-s2i create mode 100644 modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/assemble create mode 100644 modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/save-artifacts create mode 100644 modules/org.eclipse.jkube.maven/s2i/configure.sh create mode 100644 modules/org.eclipse.jkube.maven/s2i/module.yaml diff --git a/modules/org.eclipse.jkube.maven/8.2.3.8/configure.sh b/modules/org.eclipse.jkube.maven/8.2.3.8/configure.sh new file mode 100644 index 0000000..81ffba5 --- /dev/null +++ b/modules/org.eclipse.jkube.maven/8.2.3.8/configure.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -e + +# This file is shipped by a Maven package and sets JAVA_HOME to +# an OpenJDK-specific path. This causes problems for OpenJ9 containers +# as the path is not correct for them. We don't need this in any of +# the containers because ww set JAVA_HOME in the container metadata. +# Blank the file rather than removing it, to avoid a warning message +# from /usr/bin/mvn. +if [ -f /etc/java/maven.conf ]; then + :> /etc/java/maven.conf +fi diff --git a/modules/org.eclipse.jkube.maven/8.2.3.8/module.yaml b/modules/org.eclipse.jkube.maven/8.2.3.8/module.yaml new file mode 100644 index 0000000..693c141 --- /dev/null +++ b/modules/org.eclipse.jkube.maven/8.2.3.8/module.yaml @@ -0,0 +1,26 @@ +schema_version: 1 +name: org.eclipse.jkube.maven +version: "8.2.3.8" +description: Provides Maven v3.8 capabilities to an image. + +labels: + - name: io.fabric8.s2i.version.maven + value: "3.8" + +envs: + - name: JBOSS_CONTAINER_MAVEN_38_MODULE + value: /opt/jboss/container/maven/38/ + - name: MAVEN_VERSION + value: "3.8" + +modules: + install: + - name: org.eclipse.jkube.maven.module + version: "8.2.3.8" + +packages: + install: + - maven + +execute: + - script: configure.sh diff --git a/modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/jboss-settings.xml b/modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/jboss-settings.xml new file mode 100644 index 0000000..7c422ae --- /dev/null +++ b/modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/jboss-settings.xml @@ -0,0 +1,243 @@ + + + + + + + + + + + + + + + + + + + + + + jboss-eap-repository + + + com.redhat.xpaas.repo.redhatga + + + + + + redhat-ga-repository + https://maven.repository.redhat.com/ga/ + + true + + + false + + + + redhat-ea-repository + https://maven.repository.redhat.com/earlyaccess/all/ + + true + + + false + + + + + + redhat-ga-plugin-repository + https://maven.repository.redhat.com/ga/ + + true + + + false + + + + redhat-ea-plugin-repository + https://maven.repository.redhat.com/earlyaccess/all/ + + true + + + false + + + + + + + + jboss-eap-repository-insecure + + + redhat-ga-repository + http://maven.repository.redhat.com/ga/ + + true + + + false + + + + redhat-ea-repository + http://maven.repository.redhat.com/earlyaccess/all/ + + true + + + false + + + + + + redhat-ga-plugin-repository + http://maven.repository.redhat.com/ga/ + + true + + + false + + + + redhat-ea-plugin-repository + http://maven.repository.redhat.com/earlyaccess/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 + + + diff --git a/modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/maven.sh b/modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/maven.sh new file mode 100644 index 0000000..61e704e --- /dev/null +++ b/modules/org.eclipse.jkube.maven/default/artifacts/opt/jboss/container/maven/default/maven.sh @@ -0,0 +1,393 @@ +# common shell routines for use with maven +source "$JBOSS_CONTAINER_UTIL_LOGGING_MODULE/logging.sh" + +# default settings.xml file +__JBOSS_MAVEN_DEFAULT_SETTINGS_FILE="${HOME}/.m2/settings.xml" + +# initialize maven +function maven_init() { + maven_init_vars + maven_init_settings + maven_init_local_repo +} + +# initialize maven variables +function maven_init_vars() { + maven_init_var_MAVEN_LOCAL_REPO + maven_init_var_MAVEN_SETTINGS_XML + maven_init_var_MAVEN_OPTS + maven_init_var_MAVEN_ARGS +} + +function maven_init_var_MAVEN_LOCAL_REPO() { + MAVEN_LOCAL_REPO="${MAVEN_LOCAL_REPO:-${HOME}/.m2/repository}" +} + +function maven_init_var_MAVEN_SETTINGS_XML() { + if [ -f "${MAVEN_SETTINGS_XML}" ]; then + : + elif [ -f "${HOME}/.m2/settings.xml" ]; then + MAVEN_SETTINGS_XML="${HOME}/.m2/settings.xml" + else + MAVEN_SETTINGS_XML="${JBOSS_CONTAINER_MAVEN_DEFAULT_MODULE}/jboss-settings.xml" + fi +} + +function maven_init_var_MAVEN_OPTS() { + export MAVEN_OPTS="${MAVEN_OPTS:-$(${JBOSS_CONTAINER_JAVA_JVM_MODULE}/java-default-options) -XX:MaxRAMPercentage=25.0}" +} + +function maven_init_var_MAVEN_ARGS() { + # Add jkube.skip for apps that are using jkube's openshift-maven-plugin (OPENJDK-242) + MAVEN_ARGS=${MAVEN_ARGS:--e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga -Djkube.skip=true} + # Use maven batch mode (CLOUD-579) + # Always force IPv4 (CLOUD-188) + MAVEN_ARGS="$MAVEN_ARGS --batch-mode -Djava.net.preferIPv4Stack=true" + # manually configure settings (to simplify using custom settings vs default) + MAVEN_ARGS="$MAVEN_ARGS -s ${MAVEN_SETTINGS_XML}" + # manually configure local repository (to simplify configuration of custom vs default) + MAVEN_ARGS="$MAVEN_ARGS -Dmaven.repo.local=${MAVEN_LOCAL_REPO}" + + # Append user-supplied arguments (CLOUD-412) + MAVEN_ARGS="$MAVEN_ARGS ${MAVEN_ARGS_APPEND}" +} + +function maven_init_settings() { + process_maven_settings_xml "${MAVEN_SETTINGS_XML}" +} + +function maven_init_local_repo() { + : +} + +# perform a maven build +# $1 build directory; defaults to cwd +# $2 goals to execute; defaults to package +# e.g. cd $1; mvn $2 +function maven_build() { + local build_dir=${1:-$(cwd)} + local goals=${2:-package} + log_info "Performing Maven build in $build_dir" + + pushd $build_dir &> /dev/null + + log_info "Using MAVEN_OPTS ${MAVEN_OPTS}" + log_info "Using $(mvn $MAVEN_ARGS --version)" + log_info "Running 'mvn $MAVEN_ARGS $goals'" + + # Execute the actual build (ensuring MAVEN_ARGS is unset, OPENJDK-1549) + REAL_MAVEN_ARGS="$MAVEN_ARGS" + unset MAVEN_ARGS + mvn $REAL_MAVEN_ARGS $goals + + popd &> /dev/null + +} + +# post build cleanup. deletes local repository after a build, if MAVEN_CLEAR_REPO is set +function maven_cleanup() { + # Remove repo if desired + if [ "${MAVEN_CLEAR_REPO,,}" == "true" -a -n "$(find ${MAVEN_LOCAL_REPO} -maxdepth 0 -type d ! -empty 2> /dev/null)" ]; then + log_info "Clearing local maven repository at ${MAVEN_LOCAL_REPO}" + rm -rf "${MAVEN_LOCAL_REPO}" + if [ $? -ne 0 ]; then + log_error "Cannot remove local Maven repository ${MAVEN_LOCAL_REPO}" + fi + fi +} + +# apply environment to settings.xml file +function process_maven_settings_xml() { + local settings="${1:-${__JBOSS_MAVEN_DEFAULT_SETTINGS_FILE}}" + add_maven_proxy_settings "${settings}" + add_maven_mirrors "${settings}" + add_maven_repos "${settings}" +} + +# add proxy configuration to settings.xml +# internal function, use process_maven_settings_xml which applies all configuration +function add_maven_proxy_settings() { + local settings="$1" + + if [ -n "${https_proxy}" ] ; then + source "$JBOSS_CONTAINER_JAVA_PROXY_MODULE"/parse-proxy-url.sh "${https_proxy}" https 443 + else + if [ -n "${http_proxy}" ] ; then + source "$JBOSS_CONTAINER_JAVA_PROXY_MODULE"/parse-proxy-url.sh "${http_proxy}" http 80 + fi + fi + _add_maven_proxy "${settings}" +} + +# insert settings for HTTP proxy into settings.xml if supplied as +# separate variables JAVA_PROXY_HOST, _PORT, _SCHEME, _USERNAME, +# _PASSWORD, _NONPROXYHOSTS +# internal function +function _add_maven_proxy() { + local settings="${1:-${__JBOSS_MAVEN_DEFAULT_SETTINGS_FILE}}" + if [ -n "$JAVA_PROXY_HOST" -a -n "$JAVA_PROXY_PORT" ]; then + xml="\ + genproxy\ + true\ + ${JAVA_PROXY_SCHEME:-http}\ + $JAVA_PROXY_HOST\ + $JAVA_PROXY_PORT" + if [ -n "$JAVA_PROXY_USERNAME" -a -n "$JAVA_PROXY_PASSWORD" ]; then + xml="$xml\ + $JAVA_PROXY_USERNAME\ + $JAVA_PROXY_PASSWORD" + fi + source "$JBOSS_CONTAINER_JAVA_PROXY_MODULE"/translate-no-proxy.sh + if [ -n "$JAVA_PROXY_NONPROXYHOSTS" ]; then + xml="$xml\ + $JAVA_PROXY_NONPROXYHOSTS" + fi + xml="$xml\ + " + local sub="" + sed -i "s^${sub}^${xml}^" "$settings" + fi +} + +# Finds the environment variable and returns its value if found. +# Otherwise returns the default value if provided. +# +# Arguments: +# $1 env variable name to check +# $2 default value if environment variable was not set +function _maven_find_env() { + local var=${!1} + echo "${var:-$2}" +} + +# Finds the environment variable with the given prefix. If not found +# the default value will be returned. If no prefix is provided will +# rely on _maven_find_env +# +# Arguments +# - $1 prefix. Transformed to uppercase and replace - by _ +# - $2 variable name. Prepended by "prefix_" +# - $3 default value if the variable is not defined +function _maven_find_prefixed_env() { + local prefix=$1 + + if [[ -z $prefix ]]; then + _maven_find_env $2 $3 + else + prefix=${prefix^^} # uppercase + prefix=${prefix//-/_} #replace - by _ + + local var_name=$prefix"_"$2 + echo ${!var_name:-$3} + fi +} +# insert settings for mirrors/repository managers into settings.xml if supplied +# internal function, use process_maven_settings_xml which applies all configuration +function add_maven_mirrors() { + local settings="${1:-${__JBOSS_MAVEN_DEFAULT_SETTINGS_FILE}}" + local counter=1 + + # Be backwards compatible + if [ -n "${MAVEN_MIRROR_URL}" ]; then + local mirror_id=$(_maven_find_env "MAVEN_MIRROR_ID" "mirror.default") + local mirror_of=$(_maven_find_env "MAVEN_MIRROR_OF" "external:*") + + _add_maven_mirror "${settings}" "${mirror_id}" "${MAVEN_MIRROR_URL}" "${mirror_of}" + fi + + IFS=',' read -a maven_mirror_prefixes <<< ${MAVEN_MIRRORS} + for maven_mirror_prefix in ${maven_mirror_prefixes[@]}; do + local mirror_id=$(_maven_find_prefixed_env "${maven_mirror_prefix}" "MAVEN_MIRROR_ID" "mirror${counter}") + local mirror_url=$(_maven_find_prefixed_env "${maven_mirror_prefix}" "MAVEN_MIRROR_URL") + local mirror_of=$(_maven_find_prefixed_env "${maven_mirror_prefix}" "MAVEN_MIRROR_OF" "external:*") + + if [ -z "${mirror_url}" ]; then + log_warning "Variable \"${maven_mirror_prefix}_MAVEN_MIRROR_URL\" not set. Skipping maven mirror setup for the prefix \"${maven_mirror_prefix}\"." + else + _add_maven_mirror "${settings}" "${mirror_id}" "${mirror_url}" "${mirror_of}" + fi + + counter=$((counter+1)) + done +} + +# private +function _add_maven_mirror() { + local settings="${1}" + local mirror_id="${2}" + local mirror_url="${3}" + local mirror_of="${4}" + + local xml="\n\ + ${mirror_id}\n\ + ${mirror_url}\n\ + ${mirror_of}\n\ + \n\ + " + + sed -i "s||$xml|" "${settings}" + +} + +function add_maven_repos() { + local settings="${1}" + + # set the local repository + local local_repo_xml="\n\ + ${MAVEN_LOCAL_REPO}" + sed -i "s||${local_repo_xml}|" "${settings}" + + # single remote repository scenario: respect fully qualified url if specified, otherwise find and use service + local single_repo_url="${MAVEN_REPO_URL}" + if [ -n "$single_repo_url" ]; then + local single_repo_id=$(_maven_find_env "MAVEN_REPO_ID" "repo-$(_generate_random_id)") + add_maven_repo "$settings" "$single_repo_url" "$single_repo_id" + fi + + # multiple remote repositories scenario: respect fully qualified url(s) if specified, otherwise find and use service(s); can be used together with "single repo scenario" above + local multi_repo_counter=1 + IFS=',' read -a multi_repo_prefixes <<< ${MAVEN_REPOS} + for multi_repo_prefix in ${multi_repo_prefixes[@]}; do + local multi_repo_url=$(_maven_find_prefixed_env "${multi_repo_prefix}" "MAVEN_REPO_URL") + local multi_repo_id=$(_maven_find_prefixed_env "${multi_repo_prefix}" "MAVEN_REPO_ID" "repo${multi_repo_counter}-$(_generate_random_id)") + add_maven_repo "$settings" "$multi_repo_url" "$multi_repo_id" "$multi_repo_prefix" + multi_repo_counter=$((multi_repo_counter+1)) + done +} + +function add_maven_repo() { + local settings=$1 + local repo_url=$2 + local repo_id=$3 + if [[ -z $4 ]]; then + local prefix="MAVEN" + else + local prefix="${4}_MAVEN" + fi + + if [[ -z ${repo_url} ]]; then + local repo_service=$(_maven_find_prefixed_env "${prefix}" "REPO_SERVICE") + # host + local repo_host=$(_maven_find_prefixed_env "${prefix}" "REPO_HOST") + if [[ -z ${repo_host} ]]; then + repo_host=$(_maven_find_prefixed_env "${repo_service}" "SERVICE_HOST") + fi + if [[ ! -z ${repo_host} ]]; then + # protocol + local repo_protocol=$(_maven_find_prefixed_env "${prefix}" "REPO_PROTOCOL" "http") + # port + local repo_port=$(_maven_find_prefixed_env "${prefix}" "REPO_PORT") + if [ "${repo_port}" = "" ]; then + repo_port=$(_maven_find_prefixed_env "${repo_service}" "SERVICE_PORT" "8080") + fi + local repo_path=$(_maven_find_prefixed_env "${prefix}" "REPO_PATH") + # strip leading slash if exists + if [[ "${repo_path}" =~ ^/ ]]; then + repo_path="${repo_path:1:${#repo_path}}" + fi + # url + repo_url="${repo_protocol}://${repo_host}:${repo_port}/${repo_path}" + fi + fi + if [[ ! -z ${repo_url} ]]; then + _add_maven_repo_profile "${settings}" "${repo_id}" "${repo_url}" "${prefix}" + _add_maven_repo_server "${settings}" "${repo_id}" "${prefix}" + else + log_warning "Variable \"${prefix}_REPO_URL\" not set. Skipping maven repo setup for the prefix \"${prefix}\"." + fi +} + +# private +function _add_maven_repo_profile() { + local settings=$1 + local repo_id=$2 + local url=$3 + local prefix=$4 + + local name=$(_maven_find_prefixed_env "${prefix}" "REPO_NAME" "${repo_id}") + local layout=$(_maven_find_prefixed_env "${prefix}" "REPO_LAYOUT" "default") + local releases_enabled=$(_maven_find_prefixed_env "${prefix}" "REPO_RELEASES_ENABLED" "true") + local releases_update_policy=$(_maven_find_prefixed_env "${prefix}" "REPO_RELEASES_UPDATE_POLICY" "always") + local releases_checksum_policy=$(_maven_find_prefixed_env "${prefix}" "REPO_RELEASES_CHECKSUM_POLICY" "warn") + local snapshots_enabled=$(_maven_find_prefixed_env "${prefix}" "REPO_SNAPSHOTS_ENABLED" "true") + local snapshots_update_policy=$(_maven_find_prefixed_env "${prefix}" "REPO_SNAPSHOTS_UPDATE_POLICY" "always") + local snapshots_checksum_policy=$(_maven_find_prefixed_env "${prefix}" "REPO_SNAPSHOTS_CHECKSUM_POLICY" "warn") + + # configure the repository in a profile + local profile_id="${repo_id}-profile" + local xml="\n\ + \n\ + ${profile_id}\n\ + \n\ + \n\ + ${repo_id}\n\ + ${name}\n\ + ${url}\n\ + ${layout}\n\ + \n\ + ${releases_enabled}\n\ + ${releases_update_policy}\n\ + ${releases_checksum_policy}\n\ + \n\ + \n\ + ${snapshots_enabled}\n\ + ${snapshots_update_policy}\n\ + ${snapshots_checksum_policy}\n\ + \n\ + \n\ + \n\ + \n\ + " + sed -i "s||${xml}|" "${settings}" + + # activate the configured profile + xml="\n\ + ${profile_id}\n\ + " + sed -i "s||${xml}|" "${settings}" +} + +# private +function _add_maven_repo_server() { + local settings=$1 + local server_id=$2 + local prefix=$3 + + local username=$(_maven_find_prefixed_env "$prefix" "REPO_USERNAME") + local password=$(_maven_find_prefixed_env "$prefix" "REPO_PASSWORD") + local private_key=$(_maven_find_prefixed_env "$prefix" "REPO_PRIVATE_KEY") + local passphrase=$(_maven_find_prefixed_env "$prefix" "REPO_PASSPHRASE") + local file_permissions=$(_maven_find_prefixed_env "$prefix" "REPO_FILE_PERMISSIONS") + local directory_permissions=$(_maven_find_prefixed_env "$prefix" "REPO_DIRECTORY_PERMISSIONS") + + local xml="\n\ + \n\ + ${server_id}" + if [ "${username}" != "" -a "${password}" != "" ]; then + xml="${xml}\n\ + ${username}\n\ + " + fi + if [ "${private_key}" != "" -a "${passphrase}" != "" ]; then + xml="${xml}\n\ + ${private_key}\n\ + " + fi + if [ "${file_permissions}" != "" ]; then + xml="${xml}\n\ + ${file_permissions}" + fi + if [ "${directory_permissions}" != "" ]; then + xml="${xml}\n\ + ${directory_permissions}" + fi + xml="${xml}\n\ + \n\ + " + sed -i "s||${xml}|" "${settings}" +} + +# private +function _generate_random_id() { + cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1 +} diff --git a/modules/org.eclipse.jkube.maven/default/configure.sh b/modules/org.eclipse.jkube.maven/default/configure.sh new file mode 100644 index 0000000..7542b69 --- /dev/null +++ b/modules/org.eclipse.jkube.maven/default/configure.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# Configure module +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +# configure artifact permissions +chown -R $USER:root $ARTIFACTS_DIR +chmod -R ug+rwX $ARTIFACTS_DIR +chmod ug+x ${ARTIFACTS_DIR}/opt/jboss/container/maven/default/maven.sh + +# install artifacts +pushd ${ARTIFACTS_DIR} +cp -pr * / +popd + +MAVEN_VERSION_SQUASHED=${MAVEN_VERSION/./} + +# pull in specific maven version to serve as default +ln -s /opt/jboss/container/maven/${MAVEN_VERSION_SQUASHED}/* /opt/jboss/container/maven/default +chown -h $USER:root /opt/jboss/container/maven/default/* + +# install default settings.xml file in user home +mkdir -p $HOME/.m2 +ln -s /opt/jboss/container/maven/default/jboss-settings.xml $HOME/.m2/settings.xml + +chown -R $USER:root $HOME/.m2 +chmod -R ug+rwX $HOME/.m2 diff --git a/modules/org.eclipse.jkube.maven/default/module.yaml b/modules/org.eclipse.jkube.maven/default/module.yaml new file mode 100644 index 0000000..c2138fb --- /dev/null +++ b/modules/org.eclipse.jkube.maven/default/module.yaml @@ -0,0 +1,158 @@ +schema_version: 1 +name: org.eclipse.jkube.maven.default +version: 1.0.0 +description: > + Provides basic Maven capabilities to an image, with support for configuring + settings.xml using the environment variables described here. + + Adapted from: + - https://github.com/jboss-container-images/openjdk/blob/d14ec7f363956b73684409c8b6bd9c766507013b/modules/maven/default + - https://github.com/jboss-openshift/cct_module/tree/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/maven/default + - https://github.com/jboss-openshift/cct_module/blob/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/maven/api + +execute: +- script: configure.sh + +modules: + install: + - name: org.eclipse.jkube.user + - name: org.eclipse.jkube.jvm.bash + - name: org.eclipse.jkube.maven + - name: jboss.container.util.logging.bash + - name: jboss.container.java.proxy.bash + +packages: + install: + - findutils + +envs: +- name: JBOSS_CONTAINER_MAVEN_DEFAULT_MODULE + value: /opt/jboss/container/maven/default/ +- name: MAVEN_ARGS + description: Arguments to use when calling Maven, replacing the default. To append additional arguments, see `MAVEN_ARGS_APPEND`. + example: "-e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga" +- name: MAVEN_ARGS_APPEND + description: Additional Maven arguments. + example: "-X -am -pl" +- name: MAVEN_CLEAR_REPO + description: If set then the Maven repository is removed after the artifact is built. This is useful for keeping the created application image small, but prevents *incremental* builds. Will be overridden by **S2I_ENABLE_INCREMENTAL_BUILDS**. Defaults to *false*. +- name: MAVEN_MIRROR_URL + description: > + The base URL of a mirror used for retrieving artifacts. + For multi-mirror support, see `MAVEN_MIRRORS`. + example: "http://10.0.0.1:8080/repository/internal/" +- name: MAVEN_MIRROR_OF + description: > + Repository IDs mirrored by the mirror specified in `MAVEN_MIRROR_URL`. + For multi-mirror support, see `MAVEN_MIRRORS`. + Defaults to "external:*". + example: external:* +- name: MAVEN_MIRRORS + description: > + Enables multi-mirror support. Specify a comma-delimited list of capitalized + mirror identifiers. The configuration for each mirror will be determined by + correspondingly prefixed `MAVEN_MIRROR_*` variables. Any dashes in mirror + names will be replaced by underscores. For example: Specifying + `DEV-ONE,QE-TWO` configures two mirrors and their URLs will be read from + the `DEV_ONE_MAVEN_MIRROR_URL` and `QE_TWO_MAVEN_MIRROR_URL` variables. + See also: `_MAVEN_MIRROR_ID`; `_MAVEN_MIRROR_OF`; + `_MAVEN_MIRROR_URL`. + example: "DEV-ONE,QE-TWO" +- name: _MAVEN_MIRROR_ID + description: "ID to be used for the specified mirror. If omitted, a unique ID will be generated." + example: "internal-mirror" +- name: _MAVEN_MIRROR_OF + description: "Repository IDs mirrored by this entry. Defaults to external:*" +- name: _MAVEN_MIRROR_URL + description: "The URL of the mirror." + example: "http://10.0.0.1:8080/repository/internal" +- name: MAVEN_SETTINGS_XML + description: Location of custom Maven settings.xml file to use. + example: /home/jboss/.m2/settings.xml +- name: MAVEN_LOCAL_REPO + description: Directory to use as the local Maven repository. + example: /home/jboss/.m2/repository +- name: "MAVEN_REPO_URL" + example: "http://repo.example.com:8080/maven2/" + description: > + Specify a Maven repository by URL. + See MAVEN_REPOS for specifying multiple repositories. +- name: "MAVEN_REPO_ID" + example: "myrepo" + description: > + Provide a static ID for the Maven repository specified by + MAVEN_REPO_URL. The default is to generate a random ID. + See MAVEN_REPOS for specifying multiple repositories. +- name: "MAVEN_REPOS" + example: "DEV-ONE,QE-TWO" + description: > + Enables multi-repo support. Specify a comma-delimited list of capitalized + repository identifiers. The configuration for each repository will be + determined by correspondingly prefixed `MAVEN_REPO_*` variables. Any dashes + in repository names will be replaced with underscores. For example: + Specifying `DEV-ONE,QE-TWO` configures two repositories and their + URLs will be read from `DEV_ONE_MAVEN_REPO_URL` and + `QE_TWO_MAVEN_REPO_URL`. +- name: "_MAVEN_REPO_ID" + example: "my-repo-id" + description: "Maven repository id" +- name: "_MAVEN_REPO_NAME" + example: "my-repo-name" + description: "Maven repository name" +- name: "_MAVEN_REPO_LAYOUT" + example: "default" + description: "Maven repository layout" +- name: "_MAVEN_REPO_RELEASES_ENABLED" + example: "true" + description: "Maven repository releases enabled" +- name: "_MAVEN_REPO_RELEASES_UPDATE_POLICY" + example: "always" + description: "Maven repository releases update policy" +- name: "_MAVEN_REPO_RELEASES_CHECKSUM_POLICY" + example: "warn" + description: "Maven repository releases checksum policy" +- name: "_MAVEN_REPO_SNAPSHOTS_ENABLED" + example: "true" + description: "Maven repository snapshots enabled" +- name: "_MAVEN_REPO_SNAPSHOTS_UPDATE_POLICY" + example: "always" + description: "Maven repository snapshots update policy" +- name: "_MAVEN_REPO_SNAPSHOTS_CHECKSUM_POLICY" + example: "warn" + description: "Maven repository snapshots checksum policy" +- name: "_MAVEN_REPO_USERNAME" + example: "mavenUser" + description: "Maven repository username" +- name: "_MAVEN_REPO_PASSWORD" + example: "maven1!" + description: "Maven repository password" +- name: "_MAVEN_REPO_PRIVATE_KEY" + example: "${user.home}/.ssh/id_dsa" + description: "Maven repository private key" +- name: "_MAVEN_REPO_PASSPHRASE" + example: "maven1!" + description: "Maven repository passphrase" +- name: "_MAVEN_REPO_FILE_PERMISSIONS" + example: "664" + description: "Maven repository file permissions" +- name: "_MAVEN_REPO_DIRECTORY_PERMISSIONS" + example: "775" + description: "Maven repository directory permissions" +- name: "_MAVEN_REPO_URL" + example: "http://repo.example.com:8080/maven2/" + description: "Maven repository url (fully defined)" +- name: "_MAVEN_REPO_PROTOCOL" + example: "http" + description: "Maven repository protocol (if not using fully defined url; will fallback to service)" +- name: "_MAVEN_REPO_HOST" + example: "repo.example.com" + description: "Maven repository host (if not using fully defined url; will fallback to service)" +- name: "_MAVEN_REPO_PORT" + example: "8080" + description: "Maven repository port (if not using fully defined url; will fallback to service)" +- name: "_MAVEN_REPO_PATH" + example: "/maven2/" + description: "Maven repository path (if not using fully defined url; will fallback to service)" +- name: "_MAVEN_REPO_SERVICE" + example: "buscentr-myapp" + description: "Maven repository service to lookup if `_MAVEN_REPO_URL` not specified" diff --git a/modules/org.eclipse.jkube.maven/module/8.2.3.8/artifacts/maven.module b/modules/org.eclipse.jkube.maven/module/8.2.3.8/artifacts/maven.module new file mode 100644 index 0000000..e437f0e --- /dev/null +++ b/modules/org.eclipse.jkube.maven/module/8.2.3.8/artifacts/maven.module @@ -0,0 +1,5 @@ +[maven] +name=maven +stream=3.8 +profiles= +state=enabled diff --git a/modules/org.eclipse.jkube.maven/module/8.2.3.8/configure.sh b/modules/org.eclipse.jkube.maven/module/8.2.3.8/configure.sh new file mode 100644 index 0000000..d42c27a --- /dev/null +++ b/modules/org.eclipse.jkube.maven/module/8.2.3.8/configure.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +cp ${ARTIFACTS_DIR}/maven.module /etc/dnf/modules.d/maven.module diff --git a/modules/org.eclipse.jkube.maven/module/8.2.3.8/module.yaml b/modules/org.eclipse.jkube.maven/module/8.2.3.8/module.yaml new file mode 100644 index 0000000..8e7e67b --- /dev/null +++ b/modules/org.eclipse.jkube.maven/module/8.2.3.8/module.yaml @@ -0,0 +1,8 @@ +schema_version: 1 +name: org.eclipse.jkube.maven.module +version: '8.2.3.8' +description: ^ + Enables the AppStream RPM Module for Maven 3.8 packages. + +execute: + - script: configure.sh diff --git a/modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-overrides b/modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-overrides new file mode 100644 index 0000000..ab5e9f0 --- /dev/null +++ b/modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-overrides @@ -0,0 +1,32 @@ +# Overrides basic functionality by using archived repo by default +function maven_init_var_MAVEN_LOCAL_REPO() { + MAVEN_LOCAL_REPO="${MAVEN_LOCAL_REPO:-${_MAVEN_S2I_ARCHIVED_REPO}}" +} + +# Overrides basic functionality by looking fore settings.xml in /configuration +function maven_init_var_MAVEN_SETTINGS_XML() { + if [ -f "${MAVEN_SETTINGS_XML}" ]; then + : + elif [ -f "${S2I_SOURCE_DIR}/configuration/settings.xml" ]; then + MAVEN_SETTINGS_XML="${HOME}/.m2/settings.xml" + mkdir -p $(dirname "${MAVEN_SETTINGS_XML}") + cp "${S2I_SOURCE_DIR}/configuration/settings.xml" "${MAVEN_SETTINGS_XML}" + elif [ -f "${HOME}/.m2/settings.xml" ]; then + MAVEN_SETTINGS_XML="${HOME}/.m2/settings.xml" + else + MAVEN_SETTINGS_XML="${_MAVEN_S2I_SETTINGS_XML}" + cp "${JBOSS_CONTAINER_MAVEN_DEFAULT_MODULE}/jboss-settings.xml" "${MAVEN_SETTINGS_XML}" + fi +} + +# Overrides basic functionality by copying over archived maven repository +function maven_init_local_repo() { + # unpack artifacts from previous build + if [ -d "${_MAVEN_S2I_ARCHIVED_REPO}" -a "${MAVEN_LOCAL_REPO}" != "${_MAVEN_S2I_ARCHIVED_REPO}" ]; then + # copy to expected repo location + cp -rpn "${_MAVEN_S2I_ARCHIVED_REPO}" "${MAVEN_LOCAL_REPO}" + rm -rf "${_MAVEN_S2I_ARCHIVED_REPO}" + # allows save-artifacts to work without modification + ln -s "${MAVEN_LOCAL_REPO}" "${_MAVEN_S2I_ARCHIVED_REPO}" + fi +} diff --git a/modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-s2i b/modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-s2i new file mode 100644 index 0000000..11e276f --- /dev/null +++ b/modules/org.eclipse.jkube.maven/s2i/artifacts/opt/jboss/container/maven/s2i/maven-s2i @@ -0,0 +1,140 @@ + +# include dependencies +source "${JBOSS_CONTAINER_UTIL_LOGGING_MODULE}/logging.sh" +source "${JBOSS_CONTAINER_S2I_CORE_MODULE}/s2i-core" + +# initialization of maven s2i module +function maven_s2i_init() { + # backward compatibility; s2i_core_init will initialize S2I_ENABLE_INCREMENTAL_BUILDS + if [ "x$S2I_ENABLE_INCREMENTAL_BUILDS" == "x" ]; then + if [ "${MAVEN_CLEAR_REPO,,}" == "true" ]; then + S2I_ENABLE_INCREMENTAL_BUILDS="false" + else + S2I_ENABLE_INCREMENTAL_BUILDS="true" + fi + fi + # core environment + s2i_core_init + + # initialize maven s2i environment variables + # use - instead of :- to allow empty artifact dir setting for access to maven_s2i_custom_binary_build + MAVEN_S2I_ARTIFACT_DIRS="${MAVEN_S2I_ARTIFACT_DIRS-target}" + MAVEN_S2I_GOALS="${MAVEN_S2I_GOALS:-package}" + + # make sure MAVEN_CLEAR_REPO and S2I_ENABLE_INCREMENTAL_BUILDS are in sync + if [ "${S2I_ENABLE_INCREMENTAL_BUILDS,,}" == "true" ]; then + MAVEN_CLEAR_REPO="false" + else + MAVEN_CLEAR_REPO="true" + fi + export MAVEN_CLEAR_REPO + + # Internal variables: + # Location of Maven settings.xml file to use. + _MAVEN_S2I_SETTINGS_XML="${S2I_ARTIFACTS_DIR}/configuration/settings.xml" + + # Location of archived local Maven repository. Used with incremental builds. + _MAVEN_S2I_ARCHIVED_REPO="${S2I_ARTIFACTS_DIR}/m2" + + # include maven scripts + if test -r "${JBOSS_CONTAINER_MAVEN_DEFAULT_MODULE}"/scl-enable-maven; then + source "${JBOSS_CONTAINER_MAVEN_DEFAULT_MODULE}"/scl-enable-maven + fi + source "${JBOSS_CONTAINER_MAVEN_DEFAULT_MODULE}"/maven.sh + + # Overrides for use with maven s2i + source "${JBOSS_CONTAINER_MAVEN_S2I_MODULE}"/maven-overrides + + # let users override anything if they need to + maven_s2i_source_maven_overrides + + # initialize the maven environment + maven_init +} + +function maven_s2i_source_maven_overrides() { + # extensions can use this to override functions provided by this module + # For example: + # source custom-maven.sh + : #noop +} + +# main entry point, perform the build +function maven_s2i_build() { + maven_s2i_init + if [ -f "${S2I_SOURCE_DIR}/pom.xml" ]; then + # maven build + maven_s2i_maven_build + else + # binary build + maven_s2i_binary_build + fi + s2i_core_copy_artifacts "${S2I_SOURCE_DIR}" + + s2i_core_process_image_mounts + + s2i_core_cleanup + + # Remove java tmp perf data dir owned by 185 + rm -rf /tmp/hsperfdata_jboss +} + +# perform a maven build, i.e. mvn ... +# internal method +function maven_s2i_maven_build() { + maven_build "${S2I_SOURCE_DIR}" "${MAVEN_S2I_GOALS}" + maven_s2i_deploy_artifacts + maven_cleanup +} + +# copy build output to deployments folder +# internal method +function maven_s2i_deploy_artifacts() { + if [ -n "$(type -t maven_s2i_deploy_artifacts_override)" ]; then + eval maven_s2i_deploy_artifacts_override $* + return $? + fi + + local artifact_dirs=${1:-${MAVEN_S2I_ARTIFACT_DIRS}} + + if [[ ! "${S2I_SOURCE_DIR}" =~ ^\/ ]]; then + log_error "$FUNCNAME: Absolute path required for source directory \"$source_dir\"!" + exit 1 + fi + + IFS=',' read -a artifact_dirs <<< "${artifact_dirs:-target}" + ( + for artifact_dir in "${artifact_dirs[@]}" + do + if [[ "${artifact_dir}" =~ ^\/ ]]; then + log_error "Absolute path found in MAVEN_S2I_ARTIFACT_DIRS: ${artifact_dir}" + exit 1 + else + # temporarily override source dir so it copies from the dir we pass in + S2I_SOURCE_DEPLOYMENTS_DIR=${artifact_dir} + s2i_core_copy_deployments "${S2I_SOURCE_DIR}" + fi + done + ) +} + +# perform a binary build, basically copy what's here to deployments +# internal method +function maven_s2i_binary_build() { + log_info "S2I source build with plain binaries detected" + if [ -n "$(type -t maven_s2i_custom_binary_build)" ]; then + eval maven_s2i_custom_binary_build $* + return $? + fi + maven_s2i_deploy_artifacts "." +} + +# persist artifacts for use with incremental builds +function maven_s2i_save_artifacts() { + # only process artifacts dir if it exists and is not empty + if [ -n "${S2I_ARTIFACTS_DIR}" -a -n "$(find ${S2I_ARTIFACTS_DIR} -maxdepth 0 -type d ! -empty 2> /dev/null)" ]; then + pushd "${S2I_ARTIFACTS_DIR}" &> /dev/null + tar chf - * + popd &> /dev/null + fi +} diff --git a/modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/assemble b/modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/assemble new file mode 100644 index 0000000..e926d46 --- /dev/null +++ b/modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/assemble @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +source "${JBOSS_CONTAINER_UTIL_LOGGING_MODULE}/logging.sh" +source "${JBOSS_CONTAINER_MAVEN_S2I_MODULE}/maven-s2i" + +# invoke the build +maven_s2i_build diff --git a/modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/save-artifacts b/modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/save-artifacts new file mode 100644 index 0000000..96cb032 --- /dev/null +++ b/modules/org.eclipse.jkube.maven/s2i/artifacts/usr/local/s2i/save-artifacts @@ -0,0 +1,9 @@ +#!/bin/sh + +source "${JBOSS_CONTAINER_MAVEN_S2I_MODULE}/maven-s2i" + +# initialize the module +maven_s2i_init + +# persist the artifacts +maven_s2i_save_artifacts diff --git a/modules/org.eclipse.jkube.maven/s2i/configure.sh b/modules/org.eclipse.jkube.maven/s2i/configure.sh new file mode 100644 index 0000000..3be021b --- /dev/null +++ b/modules/org.eclipse.jkube.maven/s2i/configure.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# Configure module +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +chown -R $USER:root $SCRIPT_DIR +chmod -R ug+rwX $SCRIPT_DIR +chmod ug+x ${ARTIFACTS_DIR}/opt/jboss/container/maven/s2i/* +chmod ug+x ${ARTIFACTS_DIR}/usr/local/s2i/* + +pushd ${ARTIFACTS_DIR} +cp -pr * / +popd diff --git a/modules/org.eclipse.jkube.maven/s2i/module.yaml b/modules/org.eclipse.jkube.maven/s2i/module.yaml new file mode 100644 index 0000000..530d147 --- /dev/null +++ b/modules/org.eclipse.jkube.maven/s2i/module.yaml @@ -0,0 +1,42 @@ +schema_version: 1 +name: org.eclipse.jkube.maven.s2i +version: 1.0.0 +description: > + Provides s2i capabilities built around Maven. + Defines environment variables and labels used by Maven s2i. + Adapted from: + - https://github.com/jboss-container-images/openjdk/blob/d14ec7f363956b73684409c8b6bd9c766507013b/modules/maven/s2i + - https://github.com/jboss-openshift/cct_module/tree/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/maven/s2i + + +envs: +- name: MAVEN_S2I_ARTIFACT_DIRS + description: > + Relative paths of source directories to scan for build output, + which will be copied to $DEPLOY_DIR. + Paths should be delimited by a comma (,). + Defaults to **target** + example: target + +- name: MAVEN_S2I_GOALS + description: > + Space separated list of goals to be executed with maven build, e.g. + mvn $MAVEN_S2I_GOALS. Defaults to **package** + example: package install + +- name: JBOSS_CONTAINER_MAVEN_S2I_MODULE + value: /opt/jboss/container/maven/s2i + +execute: +- script: configure.sh + +modules: + install: + - name: org.eclipse.jkube.user + - name: org.eclipse.jkube.s2i.core + - name: org.eclipse.jkube.maven.default + - name: jboss.container.util.logging.bash + +packages: + install: + - tar From 20d78e373efd987eabfc63eff80bd1a14df155de Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Fri, 21 Jun 2024 13:09:35 +0200 Subject: [PATCH 5/8] feat(modules): prometheus module --- .../prometheus/etc/jmx-exporter-config.yaml | 5 ++++ .../container/prometheus/prometheus-opts | 27 +++++++++++++++++ .../0.20.0/configure.sh | 20 +++++++++++++ .../0.20.0/module.yaml | 30 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml create mode 100644 modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/prometheus-opts create mode 100644 modules/org.eclipse.jkube.prometheus/0.20.0/configure.sh create mode 100644 modules/org.eclipse.jkube.prometheus/0.20.0/module.yaml diff --git a/modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml b/modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml new file mode 100644 index 0000000..b1f7f71 --- /dev/null +++ b/modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml @@ -0,0 +1,5 @@ +lowercaseOutputName: true +lowercaseOutputLabelNames: true +blacklistObjectNames: + # handled by agent's default exporter + - "java.lang:*" diff --git a/modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/prometheus-opts b/modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/prometheus-opts new file mode 100644 index 0000000..48b3ec4 --- /dev/null +++ b/modules/org.eclipse.jkube.prometheus/0.20.0/artifacts/opt/jboss/container/prometheus/prometheus-opts @@ -0,0 +1,27 @@ +#!/bin/sh +# ============================================================================== +# Configure JVM options for Prometheus Agent +# +# Usage: JAVA_OPTS="$JAVA_OPTS $(source ${JBOSS_CONTAINER_PROMETHEUS_MODULE}/prometheus-opts && get_prometheus_opts)" +# +# Env Vars respected: +# +# AB_PROMETHEUS_OFF: Disables the use of Prometheus Java Agent. +# +# AB_PROMETHEUS_PORT: Port to use for the Prometheus JMX Exporter. +# Defaults to 9779. +# +# AB_PROMETHEUS_JMX_EXPORTER_CONFIG: Path to configuration to use for the +# Prometheus JMX Exporter. Defaults to: +# /opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml +# + +source "$JBOSS_CONTAINER_UTIL_LOGGING_MODULE/logging.sh" + +# echo's a complete -javaagent option based on the above variables. +function get_prometheus_opts() { + if ! echo "${AB_PROMETHEUS_OFF:-false}" | grep -q -e '^\(true\|y\|yes\|1\)$'; then + echo "-javaagent:/usr/share/java/prometheus-jmx-exporter/jmx_prometheus_javaagent.jar=${AB_PROMETHEUS_PORT:-9779}:${AB_PROMETHEUS_JMX_EXPORTER_CONFIG:-/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml}" + fi + echo "" +} \ No newline at end of file diff --git a/modules/org.eclipse.jkube.prometheus/0.20.0/configure.sh b/modules/org.eclipse.jkube.prometheus/0.20.0/configure.sh new file mode 100644 index 0000000..98e73e3 --- /dev/null +++ b/modules/org.eclipse.jkube.prometheus/0.20.0/configure.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# Configure module +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +# Copy main artifact +mkdir -p /usr/share/java/prometheus-jmx-exporter/ +cp /tmp/artifacts/jmx_prometheus_javaagent.jar /usr/share/java/prometheus-jmx-exporter/ + +# Copy module artifacts +chown -R jboss:root ${ARTIFACTS_DIR} +chmod 755 ${ARTIFACTS_DIR}/opt/jboss/container/prometheus/prometheus-opts +chmod 775 ${ARTIFACTS_DIR}/opt/jboss/container/prometheus/etc +chmod 775 ${ARTIFACTS_DIR}/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml + +pushd ${ARTIFACTS_DIR} +cp -pr * / +popd diff --git a/modules/org.eclipse.jkube.prometheus/0.20.0/module.yaml b/modules/org.eclipse.jkube.prometheus/0.20.0/module.yaml new file mode 100644 index 0000000..9613c40 --- /dev/null +++ b/modules/org.eclipse.jkube.prometheus/0.20.0/module.yaml @@ -0,0 +1,30 @@ +# Ported from https://github.com/jboss-openshift/cct_module/tree/8411125f8e1b45d48c93c8bcd51d39541ce4a755/jboss/container/prometheus/8.2 +# Uses Maven Central Artifact instead of RPM package because there's no package (yet) for RHEL 9 +schema_version: 1 + +name: org.eclipse.jkube.prometheus +version: '0.20.0' +description: ^ + Provides support for configuring Prometheus. Basic usage is + JAVA_OPTS="$JAVA_OPTS $(source $JBOSS_CONTAINER_PROMETHEUS_MODULE/prometheus-opts; get_prometheus_opts)" + +envs: + - name: JBOSS_CONTAINER_PROMETHEUS_MODULE + value: /opt/jboss/container/prometheus + - name: AB_PROMETHEUS_OFF + description: Disable the use of prometheus agent + example: true + - name: AB_PROMETHEUS_PORT + description: Port to use for the Prometheus JMX Exporter. + example: 9799 + - name: AB_PROMETHEUS_JMX_EXPORTER_CONFIG + value: /opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml + description: Path to configuration to use for the Prometheus JMX Exporter + +artifacts: + - name: jmx_prometheus_javaagent.jar + target: jmx_prometheus_javaagent.jar + url: https://search.maven.org/remotecontent?filepath=io/prometheus/jmx/jmx_prometheus_javaagent/0.20.0/jmx_prometheus_javaagent-0.20.0.jar + md5: ea10089dd3c0224391bd9bf01d3e1154 +execute: + - script: configure.sh From 3c88362063a2a42ba0a0b2c8782f0933430b5dce Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Fri, 21 Jun 2024 13:09:50 +0200 Subject: [PATCH 6/8] feat(modules): run bash module --- .../opt/jboss/container/java/run/run-java.sh | 250 ++++++++++++++++++ .../org.eclipse.jkube.run.bash/configure.sh | 31 +++ .../org.eclipse.jkube.run.bash/module.yaml | 66 +++++ 3 files changed, 347 insertions(+) create mode 100644 modules/org.eclipse.jkube.run.bash/artifacts/opt/jboss/container/java/run/run-java.sh create mode 100644 modules/org.eclipse.jkube.run.bash/configure.sh create mode 100644 modules/org.eclipse.jkube.run.bash/module.yaml diff --git a/modules/org.eclipse.jkube.run.bash/artifacts/opt/jboss/container/java/run/run-java.sh b/modules/org.eclipse.jkube.run.bash/artifacts/opt/jboss/container/java/run/run-java.sh new file mode 100644 index 0000000..f359438 --- /dev/null +++ b/modules/org.eclipse.jkube.run.bash/artifacts/opt/jboss/container/java/run/run-java.sh @@ -0,0 +1,250 @@ +#!/bin/bash + +# Fail on a single failed command +set -eo pipefail + +export JBOSS_CONTAINER_UTIL_LOGGING_MODULE="${JBOSS_CONTAINER_UTIL_LOGGING_MODULE-/opt/jboss/container/util/logging}" +export JBOSS_CONTAINER_JAVA_RUN_MODULE="${JBOSS_CONTAINER_JAVA_RUN_MODULE-/opt/jboss/container/java/run}" + +# Default the application dir to the S2I deployment dir +if [ -z "$JAVA_APP_DIR" ] + then JAVA_APP_DIR=/deployments +fi + +source "$JBOSS_CONTAINER_UTIL_LOGGING_MODULE/logging.sh" + +# ========================================================== +# Generic run script for running arbitrary Java applications +# +# This has forked (and diverged) from: +# at https://github.com/fabric8io/run-java-sh +# +# ========================================================== + +# Error is indicated with a prefix in the return value +check_error() { + local msg=$1 + if echo ${msg} | grep -q "^ERROR:"; then + log_error ${msg} + exit 1 + fi +} + +# detect Quarkus fast-jar package type (OPENJDK-631) +is_quarkus_fast_jar() { + if test -f quarkus-app/quarkus-run.jar; then + log_info "quarkus fast-jar package type detected" + echo quarkus-app/quarkus-run.jar + return 0 + else + return 1 + fi +} + +# Try hard to find a sane default jar-file +auto_detect_jar_file() { + local dir=$1 + + # Filter out temporary jars from the shade plugin which start with 'original-' + local old_dir=$(pwd) + cd ${dir} + if [ $? = 0 ]; then + + if quarkus="$(is_quarkus_fast_jar)"; then + echo "$quarkus" + return + fi + + local nr_jars=`ls *.jar 2>/dev/null | grep -v '^original-' | wc -l | tr -d '[[:space:]]'` + if [ ${nr_jars} = 1 ]; then + ls *.jar | grep -v '^original-' + exit 0 + fi + + log_error "Neither \$JAVA_MAIN_CLASS nor \$JAVA_APP_JAR is set and ${nr_jars} JARs found in ${dir} (1 expected)" + cd ${old_dir} + else + log_error "No directory ${dir} found for auto detection" + fi +} + +# Check directories (arg 2...n) for a jar file (arg 1) +get_jar_file() { + local jar=$1 + shift; + + if [ "${jar:0:1}" = "/" ]; then + if [ -f "${jar}" ]; then + echo "${jar}" + else + log_error "No such file ${jar}" + fi + else + for dir in $*; do + if [ -f "${dir}/$jar" ]; then + echo "${dir}/$jar" + return + fi + done + log_error "No ${jar} found in $*" + fi +} + +load_env() { + # Configuration stuff is read from this file + local run_env_sh="run-env.sh" + + # Load default default config + if [ -f "${JBOSS_CONTAINER_JAVA_RUN_MODULE}/${run_env_sh}" ]; then + source "${JBOSS_CONTAINER_JAVA_RUN_MODULE}/${run_env_sh}" + fi + + # Check also $JAVA_APP_DIR. Overrides other defaults + # It's valid to set the app dir in the default script + if [ -f "${JAVA_APP_DIR}/${run_env_sh}" ]; then + source "${JAVA_APP_DIR}/${run_env_sh}" + fi + + export JAVA_APP_DIR + + # JAVA_LIB_DIR defaults to JAVA_APP_DIR + export JAVA_LIB_DIR="${JAVA_LIB_DIR:-${JAVA_APP_DIR}}" + if [ -z "${JAVA_MAIN_CLASS}" ] && [ -z "${JAVA_APP_JAR}" ]; then + JAVA_APP_JAR="$(auto_detect_jar_file ${JAVA_APP_DIR})" + check_error "${JAVA_APP_JAR}" + fi + + if [ "x${JAVA_APP_JAR}" != x ]; then + local jar="$(get_jar_file ${JAVA_APP_JAR} ${JAVA_APP_DIR} ${JAVA_LIB_DIR})" + check_error "${jar}" + export JAVA_APP_JAR=${jar} + else + export JAVA_MAIN_CLASS + fi +} + +# Combine all java options +get_java_options() { + local jvm_opts + local debug_opts + local proxy_opts + local opts + if [ -f "${JBOSS_CONTAINER_JAVA_JVM_MODULE}/java-default-options" ]; then + jvm_opts=$(${JBOSS_CONTAINER_JAVA_JVM_MODULE}/java-default-options) + fi + if [ -f "${JBOSS_CONTAINER_JAVA_JVM_MODULE}/debug-options" ]; then + debug_opts=$(${JBOSS_CONTAINER_JAVA_JVM_MODULE}/debug-options) + fi + if [ -f "${JBOSS_CONTAINER_JAVA_PROXY_MODULE}/proxy-options" ]; then + source "${JBOSS_CONTAINER_JAVA_PROXY_MODULE}/proxy-options" + proxy_opts="$(proxy_options)" + fi + + opts="${JAVA_OPTS} ${debug_opts} ${proxy_opts} ${jvm_opts} ${JAVA_OPTS_APPEND}" + # Normalize spaces with awk (i.e. trim and eliminate double spaces) + echo "${opts}" | awk '$1=$1' +} + +# Read in a classpath either from a file with a single line, colon separated +# or given line-by-line in separate lines +# Arg 1: path to claspath (must exist), optional arg2: application jar, which is stripped from the classpath in +# multi line arrangements +format_classpath() { + local cp_file="$1" + local app_jar="$2" + + local wc_out=`wc -l $1 2>&1` + if [ $? -ne 0 ]; then + log_error "Cannot read lines in ${cp_file}: $wc_out" + exit 1 + fi + + local nr_lines=`echo $wc_out | awk '{ print $1 }'` + if [ ${nr_lines} -gt 1 ]; then + local sep="" + local classpath="" + while read file; do + local full_path="${JAVA_LIB_DIR}/${file}" + # Don't include app jar if include in list + if [ x"${app_jar}" != x"${full_path}" ]; then + classpath="${classpath}${sep}${full_path}" + fi + sep=":" + done < "${cp_file}" + echo "${classpath}" + else + # Supposed to be a single line, colon separated classpath file + cat "${cp_file}" + fi +} + +# Fetch classpath from env or from a local "run-classpath" file +get_classpath() { + local cp_path="." + if [ "x${JAVA_LIB_DIR}" != "x${JAVA_APP_DIR}" ]; then + cp_path="${cp_path}:${JAVA_LIB_DIR}" + fi + if [ -z "${JAVA_CLASSPATH}" ] && [ "x${JAVA_MAIN_CLASS}" != x ]; then + if [ "x${JAVA_APP_JAR}" != x ]; then + cp_path="${cp_path}:${JAVA_APP_JAR}" + fi + if [ -f "${JAVA_LIB_DIR}/classpath" ]; then + # Classpath is pre-created and stored in a 'run-classpath' file + cp_path="${cp_path}:`format_classpath ${JAVA_LIB_DIR}/classpath ${JAVA_APP_JAR}`" + else + # No order implied + cp_path="${cp_path}:${JAVA_APP_DIR}/*" + fi + elif [ "x${JAVA_CLASSPATH}" != x ]; then + # Given from the outside + cp_path="${JAVA_CLASSPATH}" + fi + echo "${cp_path}" +} + +# Mask secrets before printing +mask_passwords() { + local content="$1" + local result="" + + IFS=' ' read -r -a key_value_pairs <<< "$content" + + for pair in "${key_value_pairs[@]}"; do + key=$(echo "$pair" | cut -d '=' -f 1) + value=$(echo "$pair" | cut -d '=' -f 2-) + + if [[ $key =~ [Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd] ]]; then + result+="$key=***** " + else + result+="$pair " + fi + done + + echo "${result% }" +} + +# Start JVM +startup() { + # Initialize environment + load_env + + local args + cd ${JAVA_APP_DIR} + if [ "x${JAVA_MAIN_CLASS}" != x ] ; then + args="${JAVA_MAIN_CLASS}" + else + args="-jar ${JAVA_APP_JAR}" + fi + + local procname="${JAVA_APP_NAME-java}" + + local masked_opts=$(mask_passwords "$(get_java_options)") + + log_info "exec -a \"${procname}\" java ${masked_opts} -cp \"$(get_classpath)\" ${args} $*" + log_info "running in $PWD" + exec -a "${procname}" java $(get_java_options) -cp "$(get_classpath)" ${args} $* +} + +# ============================================================================= +# Fire up +startup $* diff --git a/modules/org.eclipse.jkube.run.bash/configure.sh b/modules/org.eclipse.jkube.run.bash/configure.sh new file mode 100644 index 0000000..218e0d0 --- /dev/null +++ b/modules/org.eclipse.jkube.run.bash/configure.sh @@ -0,0 +1,31 @@ +#!/bin/sh +# Configure module +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +chown -R $USER:root $SCRIPT_DIR +chmod -R ug+rwX $SCRIPT_DIR +chmod ug+x ${ARTIFACTS_DIR}/opt/jboss/container/java/run/* + +pushd ${ARTIFACTS_DIR} +cp -pr * / +popd + +mkdir -p /deployments/data \ + && chmod -R "ug+rwX" /deployments/data \ + && chown -R $USER:root /deployments/data + +# OPENJDK-100: turn off negative DNS caching +if [ -w ${JAVA_HOME}/jre/lib/security/java.security ]; then + # JDK8 location + javasecurity="${JAVA_HOME}/jre/lib/security/java.security" +elif [ -w ${JAVA_HOME}/lib/security/java.security ]; then + # JDK8 JRE location + javasecurity="${JAVA_HOME}/lib/security/java.security" +else + # JDK11 location + javasecurity="${JAVA_HOME}/conf/security/java.security" +fi +sed -i 's/\(networkaddress.cache.negative.ttl\)=[0-9]\+$/\1=0/' "$javasecurity" diff --git a/modules/org.eclipse.jkube.run.bash/module.yaml b/modules/org.eclipse.jkube.run.bash/module.yaml new file mode 100644 index 0000000..f0bf71e --- /dev/null +++ b/modules/org.eclipse.jkube.run.bash/module.yaml @@ -0,0 +1,66 @@ +schema_version: 1 +name: org.eclipse.jkube.run.bash +version: 1.0.0 +description: > + Provides support for running Java applications. Basic usage is + $JBOSS_CONTAINER_JAVA_RUN_MODULE/run-java.sh. + + Adapted from: + - https://github.com/jboss-container-images/openjdk/blob/d14ec7f363956b73684409c8b6bd9c766507013b/modules/run/ + - https://github.com/jboss-openshift/cct_module/blob/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/java/run/bash/ + +envs: +- name: JBOSS_CONTAINER_JAVA_RUN_MODULE + value: /opt/jboss/container/java/run + +- name: JAVA_APP_DIR + description: ^ + The directory where the application resides. All paths in your application + are relative to this directory. + example: "myapplication/" + +- name: JAVA_MAIN_CLASS + description: ^ + A main class to use as argument for `java`. When this environment variable + is given, all jar files in **JAVA_APP_DIR** are added to the classpath as + well as **JAVA_LIB_DIR**. + example: "com.example.MainClass" + +- name: JAVA_LIB_DIR + description: ^ + Directory holding the Java jar files as well an optional `classpath` file + which holds the classpath. Either as a single line classpath (colon + separated) or with jar files listed line-by-line. If not set + **JAVA_LIB_DIR** is the same as **JAVA_APP_DIR**. + +- name: JAVA_DATA_DIR + description: ^ + The location of the directory which should be used by the application for + reading/writing application data. Users should override the default if + their application should use a different directory, e.g. if a persistent + volume is used to persist data across restarts. + value: "/deployments/data" + example: "/var/cache/application" + +- name: JAVA_CLASSPATH + description: ^ + The classpath to use. If not given, the startup script checks for a file + `**JAVA_APP_DIR/classpath**` and use its content literally as classpath. If + this file doesn't exists all jars in the app dir are added + (`classes:**JAVA_APP_DIR/***`). + +- name: JAVA_ARGS + description: Arguments passed to the `java` application. + +- name: JAVA_APP_NAME + description: To set the process or application name by the user. + +execute: +- script: configure.sh + +modules: + install: + - name: org.eclipse.jkube.user + - name: org.eclipse.jkube.jvm.bash + - name: jboss.container.util.logging.bash + - name: jboss.container.openjdk.jdk From bb2bde12e61bdb413a2ee8e1d0a47a6e86c61bdd Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Fri, 21 Jun 2024 13:10:08 +0200 Subject: [PATCH 7/8] feat(modules): s2i modules --- .../jboss/container/java/s2i/maven-overrides | 12 ++ .../container/java/s2i/maven-s2i-overrides | 44 +++++ .../jboss/container/java/s2i/s2i-core-hooks | 7 + .../bash/artifacts/usr/local/s2i/assemble | 15 ++ .../bash/artifacts/usr/local/s2i/run | 31 ++++ .../bash/artifacts/usr/local/s2i/usage | 2 + .../org.eclipse.jkube.s2i/bash/configure.sh | 15 ++ .../org.eclipse.jkube.s2i/bash/module.yaml | 30 ++++ .../opt/jboss/container/s2i/core/s2i-core | 151 ++++++++++++++++++ .../org.eclipse.jkube.s2i/core/configure.sh | 22 +++ .../org.eclipse.jkube.s2i/core/module.yaml | 127 +++++++++++++++ 11 files changed, 456 insertions(+) create mode 100644 modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-overrides create mode 100644 modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-s2i-overrides create mode 100644 modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/s2i-core-hooks create mode 100644 modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/assemble create mode 100644 modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/run create mode 100644 modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/usage create mode 100644 modules/org.eclipse.jkube.s2i/bash/configure.sh create mode 100644 modules/org.eclipse.jkube.s2i/bash/module.yaml create mode 100644 modules/org.eclipse.jkube.s2i/core/artifacts/opt/jboss/container/s2i/core/s2i-core create mode 100644 modules/org.eclipse.jkube.s2i/core/configure.sh create mode 100644 modules/org.eclipse.jkube.s2i/core/module.yaml diff --git a/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-overrides b/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-overrides new file mode 100644 index 0000000..5989cd2 --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-overrides @@ -0,0 +1,12 @@ +# Overrides basic functionality by looking fore settings.xml in /configuration +function maven_init_var_MAVEN_SETTINGS_XML() { + if [ -f "${MAVEN_SETTINGS_XML}" ]; then + : + elif [ -f "${S2I_SOURCE_DIR}/configuration/settings.xml" ]; then + MAVEN_SETTINGS_XML="${S2I_SOURCE_DIR}/configuration/settings.xml" + else + MAVEN_SETTINGS_XML="${_MAVEN_S2I_SETTINGS_XML}" + mkdir -p $(dirname "${MAVEN_SETTINGS_XML}") + cp "${JBOSS_CONTAINER_MAVEN_DEFAULT_MODULE}/jboss-settings.xml" "${MAVEN_SETTINGS_XML}" + fi +} diff --git a/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-s2i-overrides b/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-s2i-overrides new file mode 100644 index 0000000..69917a8 --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/maven-s2i-overrides @@ -0,0 +1,44 @@ + +source "${JBOSS_CONTAINER_UTIL_LOGGING_MODULE}/logging.sh" + +# inject our overridden maven_*() functions +function maven_s2i_source_maven_overrides() { + source "${JBOSS_CONTAINER_JAVA_S2I_MODULE}/maven-overrides" +} + +# Accommodate fabric8 +# TODO: is this needed for JKube? +function maven_s2i_custom_binary_build() { + if [ -f "${S2I_SOURCE_DIR}/Dockerfile" ]; then + # This is a S2I binary build coming from fabric8-maven-plugin + log_info "S2I binary build from fabric8-maven-plugin detected" + if [ -d "${S2I_SOURCE_DIR}/maven" ]; then + binary_dir="${S2I_SOURCE_DIR}/maven" + elif [ -d "${S2I_SOURCE_DIR}/${S2I_SOURCE_DEPLOYMENTS_DIR}" ]; then + binary_dir="${S2I_SOURCE_DIR}/${S2I_SOURCE_DEPLOYMENTS_DIR}" + elif [ $(find "${S2I_SOURCE_DIR}" -maxdepth 1 -type d | grep -v -e "^${S2I_SOURCE_DIR}$" | wc -l) == 1 ]; then + # Found a single directory, take this + binary_dir=$(find "${S2I_SOURCE_DIR}" -maxdepth 1 -type d | grep -v -e "^${S2I_SOURCE_DIR}$") + else + log_error "No single directory found in ${S2I_SOURCE_DIR} but:\n $(ls -l ${S2I_SOURCE_DIR})" + return 1 + fi + elif [ -d "${S2I_SOURCE_DIR}/${S2I_SOURCE_DEPLOYMENTS_DIR}" ]; then + binary_dir="${S2I_SOURCE_DIR}/${S2I_SOURCE_DEPLOYMENTS_DIR}" + else + binary_dir="${S2I_SOURCE_DIR}" + fi + log_info "Copying binaries from ${binary_dir} to ${S2I_TARGET_DEPLOYMENTS_DIR} ..." + + ( # OPENJDK-2850: use glob (dotglob to match hidden files) to stop rsync altering + # timestamps of S2I_TARGET_DEPLOYMENTS_DIR. Don't alter parent shell's dotglob. + shopt -s dotglob + rsync --archive --out-format='%n' "${binary_dir}"/* "${S2I_TARGET_DEPLOYMENTS_DIR}" + ) +} + +function maven_s2i_deploy_artifacts_override() { + unset -f maven_s2i_deploy_artifacts_override + eval maven_s2i_deploy_artifacts $* + return $? +} diff --git a/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/s2i-core-hooks b/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/s2i-core-hooks new file mode 100644 index 0000000..6cc5e52 --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/artifacts/opt/jboss/container/java/s2i/s2i-core-hooks @@ -0,0 +1,7 @@ + +source "${JBOSS_CONTAINER_UTIL_LOGGING_MODULE}/logging.sh" + +# override core variables +function s2i_core_env_init_hook() { + S2I_TARGET_DATA_DIR="${S2I_TARGET_DATA_DIR:-${JAVA_DATA_DIR:-/deployments/data}}" +} diff --git a/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/assemble b/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/assemble new file mode 100644 index 0000000..5ffec92 --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/assemble @@ -0,0 +1,15 @@ +#!/bin/sh + +set -e + +source "${JBOSS_CONTAINER_UTIL_LOGGING_MODULE}/logging.sh" +source "${JBOSS_CONTAINER_MAVEN_S2I_MODULE}/maven-s2i" + +# include our s2i_core_*() overrides/extensions +source "${JBOSS_CONTAINER_JAVA_S2I_MODULE}/s2i-core-hooks" + +# inject our overridden maven_s2i_*() functions +source "${JBOSS_CONTAINER_JAVA_S2I_MODULE}/maven-s2i-overrides" + +# invoke the build +maven_s2i_build diff --git a/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/run b/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/run new file mode 100644 index 0000000..fa7589a --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/run @@ -0,0 +1,31 @@ +#!/bin/bash + +# Command line arguments given to this script +args="$*" + +source "${JBOSS_CONTAINER_UTIL_LOGGING_MODULE}/logging.sh" +source "${JBOSS_CONTAINER_S2I_CORE_MODULE}/s2i-core" +# include our s2i_core_*() overrides/extensions +source "${JBOSS_CONTAINER_JAVA_S2I_MODULE}/s2i-core-hooks" + +# Global S2I variable setup +s2i_core_env_init + +JAVA_OPTS="${JAVA_OPTS:-${JAVA_OPTIONS}}" +if [ -f "${JBOSS_CONTAINER_JOLOKIA_MODULE}/jolokia-opts" ]; then + # Always include jolokia-opts, which can be empty if switched off via env + JAVA_OPTS="${JAVA_OPTS} $(${JBOSS_CONTAINER_JOLOKIA_MODULE}/jolokia-opts)" +fi +if [ -f "${JBOSS_CONTAINER_PROMETHEUS_MODULE}/prometheus-opts" ]; then + JAVA_OPTS="${JAVA_OPTS} $(source ${JBOSS_CONTAINER_PROMETHEUS_MODULE}/prometheus-opts && get_prometheus_opts)" +fi +export JAVA_OPTS +export JAVA_OPTIONS="$JAVA_OPTS" + +if [ -f "${S2I_TARGET_DEPLOYMENTS_DIR}/bin/run.sh" ]; then + echo "Starting the application using the bundled ${S2I_TARGET_DEPLOYMENTS_DIR}/bin/run.sh ..." + exec ${DEPLOYMENTS_DIR}/bin/run.sh $args ${JAVA_ARGS} +else + echo "Starting the Java application using ${JBOSS_CONTAINER_JAVA_RUN_MODULE}/run-java.sh $args..." + exec "${JBOSS_CONTAINER_JAVA_RUN_MODULE}/run-java.sh" $args ${JAVA_ARGS} +fi diff --git a/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/usage b/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/usage new file mode 100644 index 0000000..342b8ba --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/artifacts/usr/local/s2i/usage @@ -0,0 +1,2 @@ +#!/bin/sh +cat $(dirname $0)/usage.txt diff --git a/modules/org.eclipse.jkube.s2i/bash/configure.sh b/modules/org.eclipse.jkube.s2i/bash/configure.sh new file mode 100644 index 0000000..a56b65c --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/configure.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# Configure module +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +chown -R $USER:root $SCRIPT_DIR +chmod -R ug+rwX $SCRIPT_DIR +chmod ug+x ${ARTIFACTS_DIR}/opt/jboss/container/java/s2i/* +chmod ug+x ${ARTIFACTS_DIR}/usr/local/s2i/* + +pushd ${ARTIFACTS_DIR} +cp -pr * / +popd diff --git a/modules/org.eclipse.jkube.s2i/bash/module.yaml b/modules/org.eclipse.jkube.s2i/bash/module.yaml new file mode 100644 index 0000000..caf5a7f --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/bash/module.yaml @@ -0,0 +1,30 @@ +schema_version: 1 +name: org.eclipse.jkube.s2i.bash +version: 1.0.0 +description: > + Customization of common Maven S2I for Java S2I image. + + Adapted from: + - https://github.com/jboss-container-images/openjdk/blob/d14ec7f363956b73684409c8b6bd9c766507013b/modules/s2i/bash/ + - https://github.com/jboss-openshift/cct_module/tree/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/java/s2i/bash + +envs: + - name: JBOSS_CONTAINER_JAVA_S2I_MODULE + value: /opt/jboss/container/java/s2i + - name: S2I_SOURCE_DEPLOYMENTS_FILTER + value: "*.jar" + +execute: + - script: configure.sh + +modules: + install: + - name: org.eclipse.jkube.user + - name: org.eclipse.jkube.maven.s2i + - name: org.eclipse.jkube.run.bash + - name: org.eclipse.jkube.prometheus + - name: jboss.container.util.logging.bash + +packages: + install: + - rsync diff --git a/modules/org.eclipse.jkube.s2i/core/artifacts/opt/jboss/container/s2i/core/s2i-core b/modules/org.eclipse.jkube.s2i/core/artifacts/opt/jboss/container/s2i/core/s2i-core new file mode 100644 index 0000000..b0399b2 --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/core/artifacts/opt/jboss/container/s2i/core/s2i-core @@ -0,0 +1,151 @@ +# include dependencies +source "${JBOSS_CONTAINER_UTIL_LOGGING_MODULE}/logging.sh" + +# initializes the environment used by common s2i_core_*() functions +function s2i_core_init() { + # ensure all files are created with correct permissions for runtime + umask ug+rwx + + # initialize core s2i environmet variables + s2i_core_env_init + + # setup file permissions for injected content + if [ -d "${S2I_ARTIFACTS_DIR}" ]; then + chmod -R ug+rwX "${S2I_ARTIFACTS_DIR}" + fi + if [ -d "${S2I_SOURCE_DIR}" ]; then + chmod -R ug+rwX "${S2I_SOURCE_DIR}" + fi +} + +function s2i_core_env_init() { + # initialize core s2i environmet variables + s2i_core_env_init_hook + S2I_DESTINATION_DIR="${S2I_DESTINATION_DIR:-/tmp}" + S2I_ARTIFACTS_DIR="${S2I_DESTINATION_DIR}/artifacts" + S2I_SOURCE_DIR="${S2I_DESTINATION_DIR}/src" + S2I_SOURCE_CONFIGURATION_DIR="${S2I_SOURCE_CONFIGURATION_DIR:-configuration}" + S2I_SOURCE_DATA_DIR="${S2I_SOURCE_DATA_DIR:-${APP_DATADIR:-data}}" + S2I_SOURCE_DEPLOYMENTS_DIR="${S2I_SOURCE_DEPLOYMENTS_DIR:-deployments}" + S2I_TARGET_DEPLOYMENTS_DIR="${S2I_TARGET_DEPLOYMENTS_DIR:-${DEPLOYMENTS_DIR:-/deployments}}" + S2I_TARGET_CONFIGURATION_DIR="${S2I_TARGET_CONFIGURATION_DIR:-${S2I_TARGET_DEPLOYMENTS_DIR}}" + S2I_TARGET_DATA_DIR="${S2I_TARGET_DATA_DIR:-${JAVA_DATA_DIR:-${DATA_DIR:-${S2I_TARGET_DEPLOYMENTS_DIR}}}}" + S2I_IMAGE_SOURCE_MOUNTS="${S2I_IMAGE_SOURCE_MOUNTS:-${CUSTOM_INSTALL_DIRECTORIES}}" + S2I_ENABLE_INCREMENTAL_BUILDS="${S2I_ENABLE_INCREMENTAL_BUILDS:-true}" + S2I_DELETE_SOURCE="${S2I_DELETE_SOURCE:-true}" +} + +# extensions may override this method to initialize environment variables +# to suit their own needs. +function s2i_core_env_init_hook() { + : +} + +# copy configuration files +# $1 - the base directory to which $S2I_SOURCE_CONFIGURATION_DIR is appended +function s2i_core_copy_configuration() { + if [ -d "${1}/${S2I_SOURCE_CONFIGURATION_DIR}" ]; then + if [ -z "${S2I_TARGET_CONFIGURATION_DIR}" ]; then + log_warning "Unable to copy configuration files. No target directory specified for S2I_TARGET_CONFIGURATION_DIR" + else + if [ ! -d "${S2I_TARGET_CONFIGURATION_DIR}" ]; then + log_info "S2I_TARGET_CONFIGURATION_DIR does not exist, creating ${S2I_TARGET_CONFIGURATION_DIR}" + mkdir -pm 775 "${S2I_TARGET_CONFIGURATION_DIR}" + fi + log_info "Copying configuration from $(realpath --relative-to ${S2I_SOURCE_DIR} ${1}/${S2I_SOURCE_CONFIGURATION_DIR}) to ${S2I_TARGET_CONFIGURATION_DIR}..." + rsync --archive --out-format='%n' "${1}/${S2I_SOURCE_CONFIGURATION_DIR}"/ "${S2I_TARGET_CONFIGURATION_DIR}" + fi + fi +} + +# copy data files +# $1 - the base directory to which $S2I_SOURCE_DATA_DIR is appended +function s2i_core_copy_data() { + if [ -d "${1}/${S2I_SOURCE_DATA_DIR}" ]; then + if [ -z "${S2I_TARGET_DATA_DIR}" ]; then + log_warning "Unable to copy data files. No target directory specified for S2I_TARGET_DATA_DIR" + else + if [ ! -d "${S2I_TARGET_DATA_DIR}" ]; then + log_info "S2I_TARGET_DATA_DIR does not exist, creating ${S2I_TARGET_DATA_DIR}" + mkdir -pm 775 "${S2I_TARGET_DATA_DIR}" + fi + log_info "Copying app data from $(realpath --relative-to ${S2I_SOURCE_DIR} ${1}/${S2I_SOURCE_DATA_DIR}) to ${S2I_TARGET_DATA_DIR}..." + rsync --archive --out-format='%n' "${1}/${S2I_SOURCE_DATA_DIR}"/ "${S2I_TARGET_DATA_DIR}" + # s2i used to be more forgiving, but the build will fail if this call + # fails. emit a warning and allow the build to succeed + chmod -R g+rwX "${S2I_TARGET_DATA_DIR}" || log_warning "Errors occurred while adding read/write permissions to S2I_TARGET_DATA_DIR ($S2I_TARGET_DATA_DIR)." + fi + fi +} + +# copy deployment (binary) files +# $1 - the base directory to which $S2I_SOURCE_DEPLOYMENTS_DIR is appended +function s2i_core_copy_deployments() { + if [ -d "${1}/${S2I_SOURCE_DEPLOYMENTS_DIR}" ]; then + if [ -z "${S2I_TARGET_DEPLOYMENTS_DIR}" ]; then + log_warning "Unable to copy deployment files. No target directory specified for S2I_TARGET_DEPLOYMENTS_DIR" + else + if [ ! -d "${S2I_TARGET_DEPLOYMENTS_DIR}" ]; then + log_info "S2I_TARGET_DEPLOYMENTS_DIR does not exist, creating ${S2I_TARGET_DEPLOYMENTS_DIR}" + mkdir -pm 775 "${S2I_TARGET_DEPLOYMENTS_DIR}" + fi + local relative_source=$(realpath --relative-to "${S2I_SOURCE_DIR}" "${1}/${S2I_SOURCE_DEPLOYMENTS_DIR}") + log_info "Copying deployments from $relative_source to ${S2I_TARGET_DEPLOYMENTS_DIR}..." + for filter in ${S2I_SOURCE_DEPLOYMENTS_FILTER:-*}; do + find "${S2I_SOURCE_DIR}/${relative_source}/" -maxdepth 1 -name "${filter}" | xargs -I '{}' -r cp -Lrpv '{}' "${S2I_TARGET_DEPLOYMENTS_DIR}" + done + fi + fi +} + +# extension may override this method to provide additional copy functions +# $1 - the base directory +function s2i_core_copy_artifacts_hook() { + : +} + +# main entry point for copying artifacts from the build to the target +# $1 - the base directory +function s2i_core_copy_artifacts() { + s2i_core_copy_configuration $* + s2i_core_copy_data $* + s2i_core_copy_deployments $* + s2i_core_copy_artifacts_hook $* +} + +# processes any directories mounted into the build. see S2I_IMAGE_SOURCE_MOUNTS +function s2i_core_process_image_mounts() { + if [ "x${S2I_IMAGE_SOURCE_MOUNTS}" = "x" ]; then + return 0 + fi + + log_info "Processing ImageSource mounts: $S2I_IMAGE_SOURCE_MOUNTS" + IFS=',' read -a install_dir_entries <<< $S2I_IMAGE_SOURCE_MOUNTS + for install_dir_entry in "${install_dir_entries[@]}"; do + for install_dir in $(find ${S2I_SOURCE_DIR}/$install_dir_entry -maxdepth 0 2>/dev/null); do + log_info "Processing ImageSource from $install_dir" + chmod -R ug+x ${install_dir} + if [ -f ${install_dir}/install.sh ]; then + ${install_dir}/install.sh "${install_dir}" + else + s2i_core_copy_artifacts "${install_dir}" + fi + done + done +} + +function s2i_core_cleanup() { + if [ "${S2I_DELETE_SOURCE,,}" == "true" ]; then + if [ -n "$(find ${S2I_SOURCE_DIR} -maxdepth 0 -type d ! -empty 2> /dev/null)" ]; then + log_info "Cleaning up source directory (${S2I_SOURCE_DIR})" + rm -rf ${S2I_SOURCE_DIR} + fi + fi + if [ "${S2I_ENABLE_INCREMENTAL_BUILDS,,}" == "true" ]; then + return 0 + fi + if [ -n "$(find ${S2I_ARTIFACTS_DIR} -maxdepth 0 -type d ! -empty 2> /dev/null)" ]; then + log_info "Cleaning up artifacts directory (${S2I_ARTIFACTS_DIR})" + rm -rf ${S2I_ARTIFACTS_DIR} + fi +} diff --git a/modules/org.eclipse.jkube.s2i/core/configure.sh b/modules/org.eclipse.jkube.s2i/core/configure.sh new file mode 100644 index 0000000..b924639 --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/core/configure.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Configure module +set -e + +SCRIPT_DIR=$(dirname $0) +ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts + +chown -R $USER:root $SCRIPT_DIR +chmod -R ug+rwX $SCRIPT_DIR +chmod ug+x ${ARTIFACTS_DIR}/opt/jboss/container/s2i/core/* + +pushd ${ARTIFACTS_DIR} +cp -pr * / +popd + +mkdir -p /usr/local/s2i \ + && chmod 775 /usr/local/s2i \ + && chown -R $USER:root /usr/local/s2i + +mkdir -p /deployments \ + && chmod -R "ug+rwX" /deployments \ + && chown -R $USER:root /deployments diff --git a/modules/org.eclipse.jkube.s2i/core/module.yaml b/modules/org.eclipse.jkube.s2i/core/module.yaml new file mode 100644 index 0000000..f1b1f74 --- /dev/null +++ b/modules/org.eclipse.jkube.s2i/core/module.yaml @@ -0,0 +1,127 @@ +schema_version: 1 +name: org.eclipse.jkube.s2i.core +version: 1.0.0 +description: > + Provides support for core s2i capabilities. + + Adapted from: + - https://github.com/jboss-container-images/openjdk/blob/d14ec7f363956b73684409c8b6bd9c766507013b/modules/s2i/core + - https://github.com/jboss-openshift/cct_module/blob/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/s2i/core/bash + - https://github.com/jboss-openshift/cct_module/blob/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/s2i/core/api/ + +execute: +- script: configure.sh + +packages: + install: + - findutils + - rsync + +labels: +- name: "io.openshift.s2i.scripts-url" + value: "image:///usr/local/s2i" +- name: io.openshift.s2i.destination + value: "/tmp" +- name: org.jboss.container.deployments-dir + value: "/deployments" + +envs: +- name: JBOSS_CONTAINER_S2I_CORE_MODULE + value: /opt/jboss/container/s2i/core/ + +- name: S2I_DESTINATION_DIR + description: ^ + Root directory for S2I mount, as specified by the + **io.openshift.s2i.destination** label. This should not be overridden by + end users. + example: /tmp + +- name: S2I_ARTIFACTS_DIR + description: ^ + Location mount for artifacts persisted with **save-artifacts** script, which + are used with incremental builds. This should not be overridden by end users. + example: "${S2I_DESTINATION_DIR}/artifacts}" + +- name: S2I_SOURCE_DIR + description: ^ + Location of mount for source code to be built. This should not be + overridden by end users. + example: "${S2I_DESTINATION_DIR}/src}" + +- name: S2I_SOURCE_CONFIGURATION_DIR + description: ^ + Relative path to directory containing application configuration files to be + copied over to the product configuration directory, see + **S2I_TARGET_CONFIGURATION_DIR**. Defaults to **configuration**. + example: configuration + +- name: S2I_TARGET_CONFIGURATION_DIR + description: ^ + Absolute path to which files located in + $S2I_SOURCE_DIR/$S2I_SOURCE_CONFIGURATION_DIR are copied. + example: /opt/eap/standalone/configuration + +- name: S2I_SOURCE_DATA_DIR + description: ^ + Relative path to directory containing application data files to be copied + over to the product data directory, see **S2I_TARGET_DATA_DIR**. Defaults + to **data**. + example: data + +- name: S2I_TARGET_DATA_DIR + description: ^ + Absolute path to which files located in + $S2I_SOURCE_DIR/$S2I_SOURCE_DATA_DIR are copied. + example: /opt/eap/standalone/data + +- name: S2I_SOURCE_DEPLOYMENTS_DIR + description: ^ + Relative path to directory containing binary files to be copied over to the + product deployment directory, see **S2I_TARGET_DEPLOYMENTS_DIR**. Defaults + to **deployments**. + example: deployments + +- name: S2I_SOURCE_DEPLOYMENTS_FILTER + description: > + Space separated list of filters to be applied when copying deployments. + Defaults to ** *.jar quarkus-app ** + value: "*.jar quarkus-app" + example: "*.jar *.war *.ear" + +- name: S2I_TARGET_DEPLOYMENTS_DIR + description: ^ + Absolute path to which files located in + $S2I_SOURCE_DIR/$S2I_SOURCE_DEPLOYMENTS_DIR are copied. Additionally, this + is the directory to which build output is copied + example: /deployments + +- name: S2I_IMAGE_SOURCE_MOUNTS + description: ^ + Comma separated list of relative paths in source directory which should be + included in the image. List may include wildcards, which are expanded + using find. By default, the contents of mounted directories are processed + similarly to source folders, where the contents of + $S2I_SOURCE_CONFIGURATION_DIR, $S2I_SOURCE_DATA_DIR, and + $S2I_SOURCE_DEPLOYMENTS_DIR are copied to their respective target + directories. Alternatively, if an **install.sh** file is located in the + root of the mount point, it is executed instead. + example: extras/* + +- name: S2I_ENABLE_INCREMENTAL_BUILDS + description: ^ + Do not remove intermediate build files so they can be saved for + use with future builds. Defaults to true. + example: "true" + +- name: S2I_DELETE_SOURCE + description: ^ + Delete source files at the end of build. Defaults to true. + example: "false" + +run: + cmd: + - "/usr/local/s2i/run" + +modules: + install: + - name: org.eclipse.jkube.user From 6d41de34c1390acfef1487627659b3713d430864 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Fri, 21 Jun 2024 13:10:22 +0200 Subject: [PATCH 8/8] feat(modules): user module --- modules/org.eclipse.jkube.user/configure.sh | 11 ++++++++ modules/org.eclipse.jkube.user/module.yaml | 30 +++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 modules/org.eclipse.jkube.user/configure.sh create mode 100644 modules/org.eclipse.jkube.user/module.yaml diff --git a/modules/org.eclipse.jkube.user/configure.sh b/modules/org.eclipse.jkube.user/configure.sh new file mode 100644 index 0000000..1b47d93 --- /dev/null +++ b/modules/org.eclipse.jkube.user/configure.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +# Create a user and group used to launch processes +# We use the ID 185 for the group as well as for the user. +# This ID is registered static ID for the JBoss EAP product +# on RHEL which makes it safe to use. +groupadd -r $USER -g $UID && useradd -u $UID -r -g root -G $USER -m -d $HOME -s /sbin/nologin -c "$GECOS" $USER + +# OPENJDK-533, OPENJDK-556: correct permissions for OpenShift etc +chmod 0770 $HOME diff --git a/modules/org.eclipse.jkube.user/module.yaml b/modules/org.eclipse.jkube.user/module.yaml new file mode 100644 index 0000000..010b409 --- /dev/null +++ b/modules/org.eclipse.jkube.user/module.yaml @@ -0,0 +1,30 @@ +schema_version: 1 +name: org.eclipse.jkube.user +version: 1.0.0 +description: > + Configures the default user and permissions. This module should be included by all images. + + Adapted from: + - https://github.com/jboss-container-images/openjdk/blob/d14ec7f363956b73684409c8b6bd9c766507013b/modules/user/ + - https://github.com/jboss-openshift/cct_module/blob/f91fb2f80dd880ed7498d4dfc3afb35dfcef60bd/jboss/container/user/ + +envs: + - name: "USER" + value: "jboss" + - name: "UID" + value: &uid 185 + - name: "HOME" + value: "/home/jboss" + - name: "GECOS" + value: "JBoss user" + +packages: + install: + - shadow-utils # groupadd + +execute: + - script: configure.sh + +run: + user: *uid + workdir: "/home/jboss"