In a DockerFile, I'm starting with a TomEE base image. I need to modify the "conf/tomee.xml" to add a DataSource resource. I've stored the DataSource definition in a separate file. I want to find the "" end tag and insert the DataSource definition before that line.
I found this SO posting, which seems to be what I need, but for some reason this just isn't replacing the contents of the original file. I tried piping the result to the file, but that results in a zero-length file (as if it's writing its output to stderr, which I can't believe).
Here is the original contents of "conf/tomee.xml":
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<!-- see http://tomee.apache.org/containers-and-resources.html -->
<!-- activate next line to be able to deploy applications in apps -->
<!-- <Deployments dir="apps" /> -->
</tomee>
Here is the script I have so far:
excerpt=$(<${TOMEE_HOME}/tomee.xml.excerpt)
awk -vexcerpt="${excerpt}" '/<\/tomee>/{print excerpt;print;next}1' ${TOMEE_HOME}/conf/tomee.xml
Here is the "tomee.xml.excerpt" file:
# The SID may very well be sus2, but it could be something else.
<Resource id="sus2" type="DataSource">
JdbcDriver = oracle.jdbc.driver.OracleDriver
MaxActive = 10
MinIdle = 2
MaxIdle = 2
MaxWait = 10000
JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
UserName = ${DB_USER}
Password = ${DB_PASSWORD}
</Resource>
Note that this file has env var references. That obviously won't work as written, but I will address that when I figure out how to get the excerpt file into the correct place in the tomee.xml file.
The following is what I see when I run the script manually:
+ excerpt=' # The SID may very well be sus2, but it could be something else.
<Resource id="sus2" type="DataSource">
JdbcDriver = oracle.jdbc.driver.OracleDriver
MaxActive = 10
MinIdle = 2
MaxIdle = 2
MaxWait = 10000
JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
UserName = ${DB_USER}
Password = ${DB_PASSWORD}
</Resource>'
+ awk '-vexcerpt= # The SID may very well be sus2, but it could be something else.
<Resource id="sus2" type="DataSource">
JdbcDriver = oracle.jdbc.driver.OracleDriver
MaxActive = 10
MinIdle = 2
MaxIdle = 2
MaxWait = 10000
JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
UserName = ${DB_USER}
Password = ${DB_PASSWORD}
</Resource>' '/<\/tomee>/{print excerpt;print;next}1' /usr/local/tomee/conf/tomee.xml
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<!-- see http://tomee.apache.org/containers-and-resources.html -->
<!-- activate next line to be able to deploy applications in apps -->
<!-- <Deployments dir="apps" /> -->
# The SID may very well be sus2, but it could be something else.
<Resource id="sus2" type="DataSource">
JdbcDriver = oracle.jdbc.driver.OracleDriver
MaxActive = 10
MinIdle = 2
MaxIdle = 2
MaxWait = 10000
JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
UserName = ${DB_USER}
Password = ${DB_PASSWORD}
</Resource>
</tomee>
When I inspect the "conf/tomee.xml" file, its contents haven't changed, including the last modification timestamp.