0

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.

Community
  • 1
  • 1
David M. Karr
  • 14,317
  • 20
  • 94
  • 199
  • Awk normally prints to standard output. If you have GNU Awk, you can use the `--inplace` option to update the file; otherwise, write to a temporary file, and move it back over the original. – tripleee Aug 16 '16 at 03:47

2 Answers2

0

All of the answers on the page you reference are wrong. Do this to print the contents of excerpt file exactly as-is:

awk 'NR==FNR{excerpt=$0;next} {print} /<\/tomee>/{print excerpt}' RS='^$' "${TOMEE_HOME}/tomee.xml.excerpt" RS="\n" "${TOMEE_HOME}/conf/tomee.xml" > tmp && mv tmp "${TOMEE_HOME}/conf/tomee.xml"

The above uses GNU awk for multi-char RS but you were already gawk-specific due to no space after -v.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

Side note on that topic: tomee.xml is one way to configure tomee but not very nice to dynamically configure the server, an alternative is to use conf/system.properties syntax this way you add the resource you want at the end without caring of XML:

db = new://Resource?type=DataSource
db.JdbcUrl = jdbc:oracle:thin:@xxx:yyyy:zzzz
db.UserName = aaaaa
....
Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13
  • That does seem better, but I couldn't get that to work. I posted the following with the details: http://stackoverflow.com/questions/38982237/configuree-a-datasource-in-tomee-in-system-properties-instead-of-tomee-conf . – David M. Karr Aug 16 '16 at 18:40