Skip to content

Instantly share code, notes, and snippets.

@osowski
Created July 2, 2015 13:12
Show Gist options
  • Save osowski/22eadd9aab50fe5dbbab to your computer and use it in GitHub Desktop.
Save osowski/22eadd9aab50fe5dbbab to your computer and use it in GitHub Desktop.
WebSphere Liberty Dockerfile & custom scripts (executing prior to server start) updating server configuration files with IBM Bluemix service credential information dynamically
FROM registry-ice.ng.bluemix.net/ibmliberty:latest
RUN apt-get update && apt-get install -y \
git \
python-lxml
# Add a SSH key, this will allow to perform SSH into containers
COPY id_rsa.pub /root/.ssh/
RUN chmod 600 /root/.ssh/id_rsa.pub \
&& cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys \
&& rm -rf /opt/ibm/wlp/usr/servers/defaultServer/apps/* \
&& mkdir -p /opt/ibm/wlp/usr/extension/lib
# Extract binaries from public IDS repo
RUN rm -rf /src
RUN mkdir /src
# Clone MobileFirst Platform Foundation application into the local image
RUN git clone --verbose --progress https://github.com/osowski/mobilefirst-liberty-resources.git /src/wishlist
COPY update-server-xml.py /src/update-server-xml.py
COPY init-liberty.sh /src/init-liberty.sh
RUN chmod +x /src/init-liberty.sh
CMD ["/src/init-liberty.sh"]
# Accept Liberty license
ENV LICENSE accept
EXPOSE 9080
EXPOSE 22
#!/bin/bash
python /src/update-server-xml.py /opt/ibm/wlp/usr/servers/defaultServer/server.xml /opt/ibm/wlp/usr/servers/defaultServer/server.xml
/opt/ibm/wlp/bin/server run defaultServer
## update-server-xml.py
##
## Usage: python update-server-xml.py [input server.xml] [output server.xml]
##
## This script will extract the appropriate Cloudant service instance credentials
## from the VCAP_SERVICES environment variable inside a running container. It will
## then update those credentials in the server.xml file, passed in as the primary parameter.
## These credentials are used to configure the Cloudant Data Proxy inside the MobileFirst
## Foundations Platform and saved in the output file passed in as the second parameter.
##
import sys
import os
import json
from lxml import etree as ETree
from io import StringIO
if "VCAP_SERVICES" in os.environ:
vcaps = json.loads(os.environ["VCAP_SERVICES"])
if "cloudantNoSQLDB" in vcaps:
hostname = vcaps["cloudantNoSQLDB"][0]["credentials"]["host"]
username = vcaps["cloudantNoSQLDB"][0]["credentials"]["username"]
password = vcaps["cloudantNoSQLDB"][0]["credentials"]["password"]
port = vcaps["cloudantNoSQLDB"][0]["credentials"]["port"]
protocol = vcaps["cloudantNoSQLDB"][0]["credentials"]["url"]
protocol = protocol.split(":")[0]
serverxml = sys.argv[1]
tree = ETree.parse(serverxml)
root = tree.getroot()
#set hostname
for element in root.xpath("/server/jndiEntry[@jndiName='datastore/CloudantProxyDbAccount']"):
element.attrib['value'] = hostname
#set protocol
for element in root.xpath("/server/jndiEntry[@jndiName='datastore/CloudantProtocol']"):
element.attrib['value'] = protocol
#set port
for element in root.xpath("/server/jndiEntry[@jndiName='datastore/CloudantPort']"):
element.attrib['value'] = str(port)
#set username
for element in root.xpath("/server/jndiEntry[@jndiName='datastore/CloudantProxyDbAccountUser']"):
element.attrib['value'] = username
#set password
for element in root.xpath("/server/jndiEntry[@jndiName='datastore/CloudantProxyDbAccountPassword']"):
element.attrib['value'] = password
tree.write(sys.argv[2], pretty_print=True, xml_declaration=True);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment