Skip to content

Commit

Permalink
fix(runtime-env): remove dependency on dockerize for cross-plattform …
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
arnediekmann committed Mar 5, 2024
1 parent b982af4 commit 6a7408a
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 157 deletions.
22 changes: 4 additions & 18 deletions images/runtime-env/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
FROM scratch
FROM alpine AS dockerize
ENV DOCKERIZE_VERSION=v0.6.1
RUN apk add --no-cache wget ca-certificates && \
cd /tmp && \
wget -q https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
tar -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && \
rm -f dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

FROM maven:3.6.3-jdk-13 as java-entrypoint
FROM maven:3.9.6-eclipse-temurin-17 as java-entrypoint
WORKDIR /root
COPY entrypoint ./
RUN mvn -T 1C package

FROM tomcat:9.0.33-jdk13-openjdk-oracle
FROM tomcat:10.1.19-jdk17-temurin-jammy
LABEL maintainer="Neoskop DevOps <[email protected]>"

# Setup dockerize
COPY --from=dockerize /tmp/dockerize /usr/local/bin

# Setup custom entrypoint
COPY --from=java-entrypoint /root/target/docker-entrypoint.jar /usr/local/bin

# Setup Tomcat config template
RUN mkdir -p $CATALINA_HOME/conf/Catalina/localhost
COPY context.xml.tmpl $CATALINA_HOME/conf/Catalina/localhost/ROOT.xml.tmpl

# Overwrite server.xml
COPY server.xml $CATALINA_HOME/conf/
Expand Down Expand Up @@ -52,9 +39,8 @@ RUN mkdir -p /home/tomcat/light-modules
# Set workdir
WORKDIR /home/tomcat

# Expose port 8080 for http access and 5005 for debugging
# Expose port 8080 for http access and 5005 for debugging
EXPOSE 8080 5005

CMD ["/usr/local/bin/run.sh"]
VOLUME [ "/home/tomcat/magnolia_tmp/repositories" ]
ENTRYPOINT ["dockerize", "-template", "/usr/local/tomcat/conf/Catalina/localhost/ROOT.xml.tmpl:/usr/local/tomcat/conf/Catalina/localhost/ROOT.xml"]
VOLUME [ "/home/tomcat/magnolia_tmp/repositories" ]
7 changes: 2 additions & 5 deletions images/runtime-env/build/bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ set -e

if ! [ -d /home/tomcat/light-modules/mtk ] && [ -w /home/tomcat/light-modules ]; then
mkdir -p /home/tomcat/light-modules/mtk/templates/pages/
echo "visible: false" > /home/tomcat/light-modules/mtk/templates/pages/basic.yaml
fi

if [ -z "$DISABLE_CONNECTION_CHECK" ]; then
java -jar /usr/local/bin/docker-entrypoint.jar
echo "visible: false" >/home/tomcat/light-modules/mtk/templates/pages/basic.yaml
fi

java -jar /usr/local/bin/docker-entrypoint.jar
export JAVA_OPTS="-Dlog4j2.formatMsgNoLookups=true -XX:InitialRAMPercentage=10 -XX:MaxRAMPercentage=80 -XX:MinRAMPercentage=50 -Dmagnolia.repositories.home=/home/tomcat/magnolia_tmp/repositories -Djava.awt.headless=true -Dfile.encoding=UTF-8 $JAVA_OPTS"
${CATALINA_HOME}/bin/catalina.sh run
15 changes: 0 additions & 15 deletions images/runtime-env/build/context.xml.tmpl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package de.neoskop;

import de.neoskop.service.ContextSetupService;
import de.neoskop.service.WaitOnMySQLService;

public class DockerEntrypoint {
public static void main(String[] args) {
ContextSetupService.setupContext();
WaitOnMySQLService.waitForAllConnections();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package de.neoskop.model;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Datasource {
private static final Logger logger = LogManager.getLogger(Datasource.class);

public final String name;
public final String host;
public final String username;
public final String password;
public final String database;
public final String port;
public final boolean useSsl;
public final String trustStore;
public final String trustStorePassword;
public final String enabledTLSProtocols;

public Datasource(String name, String host, String username, String password, String database, String port,
boolean useSsl,
String trustStore, String trustStorePassword, String enabledTLSProtocols) {
this.name = name;
this.host = host;
this.username = username;
this.password = password;
this.database = database;
this.port = port;
this.useSsl = useSsl;
this.trustStore = trustStore;
this.trustStorePassword = trustStorePassword;
this.enabledTLSProtocols = enabledTLSProtocols;
}

public String getConnectionUrl() {
final StringBuilder sb = new StringBuilder("jdbc:mysql://");
sb.append(host);
sb.append(":");
sb.append(port);
sb.append("/");
sb.append(database);
sb.append("?user=");
sb.append(username);
sb.append("&password=");
sb.append(password);
sb.append("&useSSL=");
sb.append(useSsl);

if (trustStore != null) {
sb.append("&trustCertificateKeyStoreUrl=file://");
sb.append(trustStore);
sb.append("&trustCertificateKeyStorePassword=");
sb.append(trustStorePassword);
}

if (enabledTLSProtocols != null) {
sb.append("&enabledTLSProtocols=");
sb.append(enabledTLSProtocols);
}

logger.debug("Connection URL: " + sb.toString());
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.neoskop.service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.neoskop.model.Datasource;

public class ContextSetupService {
private static final Logger LOGGER = LogManager.getLogger(ContextSetupService.class);
private static final String CATALINA_HOME = System.getenv("CATALINA_HOME");
private static final String MAGNOLIA_CONFIG = System.getenv("MAGNOLIA_CONFIG");

public static void setupContext() {
File contextFile = new File(CATALINA_HOME + "/conf/Catalina/localhost/ROOT.xml");
try (PrintWriter writer = new PrintWriter(new FileWriter(contextFile))) {
writer.println("<?xml version='1.0' encoding='utf-8'?>");
writer.println("<Context>");

writer.println(" <Manager pathname=\"\" />");
writer.println(" <WatchedResource>WEB-INF/web.xml</WatchedResource>");
writer.println(" <Resources cachingAllowed=\"true\" cacheMaxSize=\"100000\" />");
writer.println(" <CookieProcessor sameSiteCookies=\"strict\" />");
writer.println(" <JarScanner scanClassPath=\"false\" />");

if (MAGNOLIA_CONFIG != null) {
writer.printf(
" <Parameter name=\"magnolia.initialization.file\" value=\"WEB-INF/config/%s/magnolia.properties,WEB-INF/config/default/magnolia.properties,WEB-INF/config/magnolia.properties\" override=\"false\" />\n",
MAGNOLIA_CONFIG);
}

final List<Datasource> datasources = DatasourceParserService.getDatasources();

if (datasources != null) {
datasources.stream().forEach(datasource -> {
writer.printf(
" <Resource name=\"jdbc/%s\" auth=\"Container\" type=\"javax.sql.DataSource\" maxTotal=\"100\" maxIdle=\"30\" maxWaitMillis=\"10000\" url=\"%s",
datasource.name, datasource.getConnectionUrl());
writer.println(
"\" driverClassName=\"com.mysql.cj.jdbc.Driver\" validationQuery=\"SELECT 1\" testOnBorrow=\"true\" />");
});
}

writer.println("</Context>");
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.neoskop.service;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.neoskop.model.Datasource;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.StreamSupport;

class DatasourceParserService {
public static List<Datasource> getDatasources() {
final String json = System.getenv("DATASOURCES");

if (json == null || json.equals("")) {
return null;
}

final JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
final JsonArray datasources = jsonObject.get("datasources").getAsJsonArray();

return StreamSupport.stream(datasources.spliterator(), false).map(JsonElement::getAsJsonObject)
.map(datasource -> {
final String name = getStringWithDefault(datasource, "name", "");
final String host = getStringWithDefault(datasource, "host", "");
final String username = getStringWithDefault(datasource, "username", "root");
final String password = getStringWithDefault(datasource, "password", "");
final String database = getStringWithDefault(datasource, "database", "mysql");
final String port = getStringWithDefault(datasource, "port", "3306");
final boolean useSsl = getBooleanWithDefault(datasource, "useSsl", false);
final String trustStore = getStringWithDefault(datasource, "trustStore", null, false);
final String trustStorePassword = getStringWithDefault(datasource, "trustStorePassword",
"changeit");
final String enabledTLSProtocols = getStringWithDefault(datasource, "enabledTLSProtocols", null);
return new Datasource(name, host, username, password, database, port, useSsl, trustStore,
trustStorePassword, enabledTLSProtocols);
}).toList();
}

private static String getStringWithDefault(JsonObject object, String property, String defaultValue) {
return getStringWithDefault(object, property, defaultValue, true);
}

private static String getStringWithDefault(JsonObject object, String property, String defaultValue,
boolean urlEncode) {
if (object.has(property)) {
final String value = object.get(property).getAsString();

if (urlEncode) {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
} else {
return value;
}
}

return defaultValue;
}

private static boolean getBooleanWithDefault(JsonObject object, String property, boolean defaultValue) {
if (object.has(property)) {
return object.get(property).getAsBoolean();
}

return defaultValue;
}
}
Loading

0 comments on commit 6a7408a

Please sign in to comment.