-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
executable file
·212 lines (171 loc) · 10.8 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/bash
set -e
MCR_SAVE_DIR="${MCR_CONFIG_DIR}save/"
MCR_CONFIG_DIR_ESCAPED=$(echo "$MCR_CONFIG_DIR" | sed 's/\//\\\//g')
MCR_DATA_DIR_ESCAPED=$(echo "$MCR_DATA_DIR" | sed 's/\//\\\//g')
MCR_SAVE_DIR_ESCAPED=$(echo "$MCR_SAVE_DIR" | sed 's/\//\\\//g')
MCR_LOG_DIR_ESCAPED=$(echo "$MCR_LOG_DIR" | sed 's/\//\\\//g')
SOLR_URL_ESCAPED=$(echo "$SOLR_URL" | sed 's/\//\\\//g')
SOLR_CORE_ESCAPED=$(echo "$SOLR_CORE" | sed 's/\//\\\//g')
SOLR_CLASSIFICATION_CORE_ESCAPED=$(echo "$SOLR_CLASSIFICATION_CORE" | sed 's/\//\\\//g')
JDBC_NAME_ESCAPED=$(echo "$JDBC_NAME" | sed 's/\//\\\//g')
JDBC_PASSWORD_ESCAPED=$(echo "$JDBC_PASSWORD" | sed 's/\//\\\//g')
JDBC_DRIVER_ESCAPED=$(echo "$JDBC_DRIVER" | sed 's/\//\\\//g')
JDBC_URL_ESCAPED=$(echo "$JDBC_URL" | sed 's/\//\\\//g')
HIBERNATE_SCHEMA_ESCAPED=$(echo "$JDBC_URL" | sed 's/\//\\\//g')
MYCORE_PROPERTIES="${MCR_CONFIG_DIR}mycore.properties"
PERSISTENCE_XML="${MCR_CONFIG_DIR}resources/META-INF/persistence.xml"
function fixDirectoryRights() {
find "$1" \! -user "$2" -exec chown "$2:$2" '{}' +
}
echo "Running MIR Starter Script as User: $(whoami)"
if [ "$EUID" -eq 0 ]
then
fixDirectoryRights "$MCR_CONFIG_DIR" "mcr"
fixDirectoryRights "$MCR_DATA_DIR" "mcr"
fixDirectoryRights "$MCR_LOG_DIR" "mcr"
exec gosu mcr "$0"
exit 0;
fi
sleep 5 # wait for database (TODO: replace with wait-for-it)
cd /usr/local/tomcat/
function setupLog4jConfig() {
if [[ ! -f "${MCR_CONFIG_DIR}resources/log4j2.xml" ]]
then
cp /opt/ubo/log4j2.xml "${MCR_CONFIG_DIR}resources/"
fi
}
function downloadDriver {
FILENAME=$(basename $1)
if [[ ! -f "${MCR_CONFIG_DIR}lib/$FILENAME" ]]
then
curl -o "${MCR_CONFIG_DIR}lib/$FILENAME" "$1"
fi
}
function migrateC3P0toHikari {
# check if c3p0 is used (if any c3p0-* file present)
if ls "${MCR_CONFIG_DIR}lib/c3p0-"*.jar 1> /dev/null 2>&1; then
echo "Migrate from c3p0 to HikariCP"
# delete old c3p0 drivers
rm "${MCR_CONFIG_DIR}lib/c3p0-"*.jar
rm "${MCR_CONFIG_DIR}lib/mchange-commons-java-"*.jar
rm "${MCR_CONFIG_DIR}lib/hibernate-c3p0-"*.jar
# delete old database drivers
rm "${MCR_CONFIG_DIR}lib/postgresql-42.2.9.jar"
rm "${MCR_CONFIG_DIR}lib/mariadb-java-client-2.5.4.jar"
rm "${MCR_CONFIG_DIR}lib/h2-1.4.200.jar"
rm "${MCR_CONFIG_DIR}lib/mysql-connector-java-8.0.19.jar"
# delete old configuration and add new configuration
if grep -q "hibernate.c3p0" "${PERSISTENCE_XML}"; then
sed -ri "s/.*hibernate.c3p0.*//" "${PERSISTENCE_XML}"
sed -ri "s/(<property name=\"hibernate.connection.provider_class\" value=\")(.*)(\" \/>)/\1org.hibernate.hikaricp.internal.HikariCPConnectionProvider\3/" "${PERSISTENCE_XML}"
sed -ri "s/(<\/properties>)/<property name=\"hibernate.hikari.maximumPoolSize\" value=\"30\" \/>\n<property name=\"hibernate.hikari.minimumIdle\" value=\"2\" \/>\n<property name=\"hibernate.hikari.idleTimeout\" value=\"30000\" \/>\n<property name=\"hibernate.hikari.maxLifetime\" value=\"1800000\" \/>\n<property name=\"hibernate.hikari.leakDetectionThreshold\" value=\"9000\" \/>\n<property name=\"hibernate.hikari.registerMbeans\" value=\"true\" \/>\n\1/" "${PERSISTENCE_XML}"
fi
/opt/ubo/ubo-cli/target/bin/ubo.sh reload mappings in jpa configuration file
else
echo "No c3p0 driver found. Skip migration."
fi
}
function migrateJavaxPropertiesToJakarta() {
if grep -q "javax.persistence" "${PERSISTENCE_XML}"; then
echo "Migrate properties in persistence.xml from javax to jakarta"
sed -ri "s/(<property name=\")javax.persistence(.*\" value=\".*\" \/>)/\1jakarta.persistence\2/" "${PERSISTENCE_XML}"
fi
if grep -q "xmlns.jcp.org" "${PERSISTENCE_XML}"; then
echo "Migrate xmlns in persistence.xml from jcp.org to jakarta.ee"
sed -ri "s/xmlns=\".+persistence\"/xmlns=\"https:\/\/jakarta.ee\/xml\/ns\/persistence\"/" "${PERSISTENCE_XML}"
echo "Migrate schemaLocation in persistence.xml from jcp.org to jakarta.ee"
sed -ri "s/(xsi:schemaLocation=\").*jcp.org.*(\")/\1https:\/\/jakarta.ee\/xml\/ns\/persistence https:\/\/jakarta.ee\/xml\/ns\/persistence\/persistence_3_0.xsd\2/" "${PERSISTENCE_XML}"
fi
if grep -q "version=\"2" "${PERSISTENCE_XML}"; then
echo "Migrate version in persistence.xml from 2.* to 3.0"
sed -ri "s/version=\"2.*\"/version=\"3.0\"/" "${PERSISTENCE_XML}"
fi
}
function setDockerValues() {
echo "Set Docker Values to Config!"
migrateJavaxPropertiesToJakarta
if [ -n "${SOLR_URL}" ]; then
sed -ri "s/#?(MCR\.Solr\.ServerURL=).+/\1${SOLR_URL_ESCAPED}/" "${MYCORE_PROPERTIES}";
fi
if [ -n "${SOLR_CORE}" ]; then
sed -ri "s/#?(MCR\.Solr\.Core\.main\.Name=).+/\1${SOLR_CORE_ESCAPED}/" "${MYCORE_PROPERTIES}";
fi
if [ -n "${SOLR_CLASSIFICATION_CORE}" ]; then
sed -ri "s/#?(MCR\.Solr\.Core\.classification\.Name=).+/\1${SOLR_CLASSIFICATION_CORE_ESCAPED}/" "${MYCORE_PROPERTIES}"
fi
if [ -n "${JDBC_NAME}" ]; then
sed -ri "s/(name=\"jakarta.persistence.jdbc.user\" value=\").*(\")/\1${JDBC_NAME_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi
if [ -n "${JDBC_PASSWORD}" ]; then
sed -ri "s/(name=\"jakarta.persistence.jdbc.password\" value=\").*(\")/\1${JDBC_PASSWORD_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi
if [ -n "${JDBC_DRIVER}" ]; then
sed -ri "s/(name=\"jakarta.persistence.jdbc.driver\" value=\").*(\")/\1${JDBC_DRIVER_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi
if [ -n "${JDBC_URL}" ]; then
sed -ri "s/(name=\"jakarta.persistence.jdbc.url\" value=\").*(\")/\1${JDBC_URL_ESCAPED}\2/" "${PERSISTENCE_XML}"
fi
if [ -n "${JDBC_SCHEMA}" ]; then
if grep -q "hibernate.default_schema" "${PERSISTENCE_XML}"; then
sed -ri "s/(name=\"hibernate.default_schema\" value=\").*(\")/\1${JDBC_SCHEMA_ESCAPED}\2/" "${PERSISTENCE_XML}"
else
sed -ri "s/(<\/properties>)/<property name=\"hibernate.default_schema\" value=\"${JDBC_SCHEMA_ESCAPED}\" \/>\n\1/" "${PERSISTENCE_XML}"
fi
if grep -q "hibernate.hbm2ddl.create_namespaces" "${PERSISTENCE_XML}"; then
sed -ri "s/(name=\"hibernate.hbm2ddl.create_namespaces\" value=\").*(\")/\1true\2/" "${PERSISTENCE_XML}"
else
sed -ri "s/(<\/properties>)/<property name=\"hibernate.hbm2ddl.create_namespaces\" value=\"true\" \/>\n\1/" "${PERSISTENCE_XML}"
fi
fi
sed -ri "s/(name=\"hibernate.hbm2ddl.auto\" value=\").*(\")/\1update\2/" "${PERSISTENCE_XML}"
if grep -q "MCR.datadir=" "${MYCORE_PROPERTIES}" ; then
sed -ri "s/#?(MCR\.datadir=).+/\1${MCR_DATA_DIR_ESCAPED}/" "${MYCORE_PROPERTIES}"
else
echo "MCR.datadir=${MCR_DATA_DIR}">>"${MYCORE_PROPERTIES}"
fi
if grep -q "MCR.Solr.NestedDocuments=" "${MYCORE_PROPERTIES}" ; then
sed -ri "s/#?(MCR\.Solr\.NestedDocuments=).+/\1true/" "${MYCORE_PROPERTIES}"
else
echo "MCR.Solr.NestedDocuments=true">>"${MYCORE_PROPERTIES}";
fi
if grep -q "MCR.Save.FileSystem=" "${MYCORE_PROPERTIES}" ; then
sed -ri "s/#?(MCR\.Save\.FileSystem=).+/\1${MCR_SAVE_DIR_ESCAPED}/" "${MYCORE_PROPERTIES}"
else
echo "MCR.Save.FileSystem=${MCR_SAVE_DIR}">>"${MYCORE_PROPERTIES}"
fi
migrateC3P0toHikari
case $JDBC_DRIVER in
org.postgresql.Driver) downloadDriver "https://jdbc.postgresql.org/download/postgresql-42.7.0.jar";;
org.mariadb.jdbc.Driver) downloadDriver "https://repo.maven.apache.org/maven2/org/mariadb/jdbc/mariadb-java-client/3.3.0/mariadb-java-client-3.3.0.jar";;
org.h2.Driver) downloadDriver "https://repo1.maven.org/maven2/com/h2database/h2/2.2.224/h2-2.2.224.jar";;
com.mysql.jdbc.Driver) downloadDriver "https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.2.0/mysql-connector-j-8.2.0.jar";;
esac
mkdir -p "${MCR_CONFIG_DIR}lib"
downloadDriver https://repo1.maven.org/maven2/com/zaxxer/HikariCP/5.1.0/HikariCP-5.1.0.jar
}
function setUpMyCoRe {
/opt/ubo/ubo-cli/target/bin/ubo.sh create configuration directory
setDockerValues
setupLog4jConfig
/opt/ubo/ubo-cli/target/bin/ubo.sh reload mappings in jpa configuration file
sed -ri "s/(<\/properties>)/<property name=\"hibernate.hikari.maximumPoolSize\" value=\"30\" \/>\n<property name=\"hibernate.hikari.minimumIdle\" value=\"2\" \/>\n<property name=\"hibernate.hikari.idleTimeout\" value=\"30000\" \/>\n<property name=\"hibernate.hikari.maxLifetime\" value=\"1800000\" \/>\n<property name=\"hibernate.hikari.leakDetectionThreshold\" value=\"9000\" \/>\n<property name=\"hibernate.hikari.registerMbeans\" value=\"true\" \/>\n\1/" "${MCR_CONFIG_DIR}resources/META-INF/persistence.xml"
/opt/ubo/ubo-cli/target/bin/ubo.sh init superuser
/opt/ubo/ubo-cli/target/bin/ubo.sh update all classifications from directory /opt/ubo/ubo-cli/src/main/setup/classifications
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission create-mods for id POOLPRIVILEGE with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-always-allowed.xml described by always allowed
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission create-users for id POOLPRIVILEGE with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-administrators-only.xml described by administrators only
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission administrate-users for id POOLPRIVILEGE with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-administrators-only.xml described by administrators only
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission read for id default with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-always-allowed.xml described by always allowed
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission writedb for id default with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-administrators-only.xml described by administrators only
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission read for id restapi:/ with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-always-allowed.xml described by always allowed
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission read for id restapi:/classifications with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-always-allowed.xml described by always allowed
/opt/ubo/ubo-cli/target/bin/ubo.sh update permission deletedb for id default with rulefile /opt/ubo/ubo-cli/src/main/setup/acl/acl-rule-administrators-only.xml described by administrators only
/opt/ubo/ubo-cli/target/bin/ubo.sh reload solr configuration main in core main
}
sed -ri "s/(-DMCR.AppName=).+( \\\\)/\-DMCR.ConfigDir=${MCR_CONFIG_DIR_ESCAPED}\2/" /opt/ubo/ubo-cli/target/bin/ubo.sh
sed -ri "s/(-DMCR.ConfigDir=).+( \\\\)/\-DMCR.ConfigDir=${MCR_CONFIG_DIR_ESCAPED}\2/" /opt/ubo/ubo-cli/target/bin/ubo.sh
[ "$(ls -A "$MCR_CONFIG_DIR")" ] && setDockerValues || setUpMyCoRe
rm -rf /usr/local/tomcat/webapps/*
cp /opt/ubo/ubo.war "/usr/local/tomcat/webapps/${APP_CONTEXT}.war"
export JAVA_OPTS="-DMCR.ConfigDir=${MCR_CONFIG_DIR} -Xmx${XMX} -Xms${XMS} -XX:+CrashOnOutOfMemoryError ${APP_OPTS}"
catalina.sh run