aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-packages/mdbc-docker/src/main
diff options
context:
space:
mode:
authorRob Daugherty <rd472p@att.com>2018-12-04 12:22:49 -0500
committerRob Daugherty <rd472p@att.com>2018-12-04 12:23:57 -0500
commitc873e7629db2741ab675f2df88ecbc4d53b5215c (patch)
tree335b19857123db9a90bcfb12484951a1c7df3ce7 /mdbc-packages/mdbc-docker/src/main
parentc913f4200caf3d2b0721bc3537112eb4d055b291 (diff)
MUSIC-224 Dockerize MdbcServer
This is a single site installation, with a cassandra container, a mariadb container, and an mdbc-server container. To build the docker images, first build mdbc software normally, then use the 'docker' maven profile: mvn -P docker To bring up the environment: cd mdbc-packages/mdbc-docker/compose/one-site docker-compose up Change-Id: Ie48487fc6c7853b80017dfa45aff52801da52cf0 Issue-ID: MUSIC-224 Signed-off-by: Rob Daugherty <rd472p@att.com>
Diffstat (limited to 'mdbc-packages/mdbc-docker/src/main')
-rw-r--r--mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-cassandra9
-rw-r--r--mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-server30
-rw-r--r--mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-cassandra.sh148
-rw-r--r--mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-mdbc-server.sh82
-rwxr-xr-xmdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/wait-for.sh103
5 files changed, 372 insertions, 0 deletions
diff --git a/mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-cassandra b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-cassandra
new file mode 100644
index 0000000..2583234
--- /dev/null
+++ b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-cassandra
@@ -0,0 +1,9 @@
+FROM cassandra:3.11.1
+
+VOLUME /docker-entrypoint-initdb.d
+
+COPY scripts/start-cassandra.sh /
+RUN chmod 755 /start-cassandra.sh
+
+CMD []
+ENTRYPOINT ["/start-cassandra.sh"]
diff --git a/mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-server b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-server
new file mode 100644
index 0000000..0de50aa
--- /dev/null
+++ b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/Dockerfile.mdbc-server
@@ -0,0 +1,30 @@
+FROM openjdk:8-jdk-alpine
+
+ARG http_proxy
+ARG https_proxy
+ENV HTTP_PROXY=$http_proxy
+ENV HTTPS_PROXY=$https_proxy
+ENV http_proxy=$HTTP_PROXY
+ENV https_proxy=$HTTPS_PROXY
+
+# Update the package list and upgrade installed packages
+RUN apk update && apk upgrade
+
+# Install commonly needed tools
+RUN apk --no-cache add curl netcat-openbsd sudo
+
+# Create 'mdbc' user
+RUN addgroup -g 1000 mdbc && adduser -S -u 1000 -G mdbc -s /bin/sh mdbc
+
+RUN mkdir /app && mkdir /app/config
+
+COPY maven/mdbc-server.jar /app
+COPY scripts/start-mdbc-server.sh /app
+COPY scripts/wait-for.sh /app
+
+RUN chown -R mdbc:mdbc /app && chmod 700 /app/*.sh
+
+VOLUME /app/config
+
+WORKDIR /app
+CMD ["/app/start-mdbc-server.sh"]
diff --git a/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-cassandra.sh b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-cassandra.sh
new file mode 100644
index 0000000..362c9e9
--- /dev/null
+++ b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-cassandra.sh
@@ -0,0 +1,148 @@
+#!/bin/bash
+
+# This enhances the standard docker entrypoint script by running cql scripts
+# and shell scripts present in /docker-entrypoint-initdb.d. The public native
+# transport port is not exposed until all the scripts have been completed.
+
+if [[ $PRIVATE_PORT == "" ]]
+then
+ PRIVATE_PORT=19042
+fi
+
+if [[ $START_TIMEOUT_SECS == "" ]]
+then
+ START_TIMEOUT_SECS=300
+fi
+
+function getPort
+{
+ grep "^native_transport_port:" /etc/cassandra/cassandra.yaml | cut -d: -f2 | tr -d '\t '
+}
+
+function setPort
+{
+ local port=$1
+ sed -i "s/^native_transport_port:.*/native_transport_port: $port/" /etc/cassandra/cassandra.yaml
+}
+
+function usage
+{
+ echo "usage: $(basename $0) [-sX]"
+}
+
+sleep=0
+args=
+
+while [[ $# != 0 ]]
+do
+ case "$1" in
+ -s*)
+ sleep=${1:2}
+ shift 1
+ ;;
+ *)
+ usage && exit 1
+ ;;
+ esac
+done
+
+if [[ $# != 0 ]]
+then
+ usage && exit 1
+fi
+
+if [[ $sleep != 0 ]]
+then
+ echo "Delaying startup by $sleep seconds"
+ sleep $sleep
+fi
+
+# Note: this does not start up cassandra
+echo "Running docker-entrypoint.sh to configure cassandra"
+/docker-entrypoint.sh true || exit 1
+
+echo "Enabling password authentication"
+sed -i 's/^authenticator: AllowAllAuthenticator/authenticator: PasswordAuthenticator/g' /etc/cassandra/cassandra.yaml || exit 1
+
+if [[ $(/bin/ls -1 /docker-entrypoint-initdb.d 2>/dev/null | wc -l) == 0 ]]
+then
+ echo "Starting cassandra"
+else
+ # Get the public native transport port from cassandra.yaml
+
+ public_port=$(getPort)
+
+ if [[ $public_port == "" ]]
+ then
+ echo "ERROR: No native_transport_port in /etc/cassandra/cassandra.yaml"
+ exit 1
+ fi
+
+ echo "Starting cassandra on port $PRIVATE_PORT"
+ setPort $PRIVATE_PORT || exit 1
+
+ /docker-entrypoint.sh cassandra
+
+ if [[ $? != 0 ]]
+ then
+ setPort $public_port
+ exit 1
+ fi
+
+ tries=$START_TIMEOUT_SECS
+
+ while true
+ do
+ if echo "describe keyspaces;" | cqlsh -u cassandra -p cassandra 127.0.0.1 $PRIVATE_PORT >/dev/null 2>&1
+ then
+ break
+ fi
+
+ tries=$((tries-1))
+
+ if [[ $tries == 0 ]]
+ then
+ setPort $public_port
+ echo "Timed out waiting for cassandra to start on port $PRIVATE_PORT"
+ exit 1
+ fi
+
+ sleep 1
+ done
+
+ echo "Cassandra is started on port $PRIVATE_PORT"
+
+ for file in $(/bin/ls -1 /docker-entrypoint-initdb.d 2>/dev/null)
+ do
+ case "$file" in
+ *.sh)
+ echo "Running $file"
+ source "/docker-entrypoint-initdb.d/$file"
+ ;;
+ *.cql)
+ echo "Running $file"
+ cqlsh -u cassandra -p cassandra -f "/docker-entrypoint-initdb.d/$file" 127.0.0.1 $PRIVATE_PORT
+ ;;
+ *)
+ echo "Ignoring $file"
+ esac
+ done
+
+ sleep 5
+
+ echo "Restarting cassandra on port $public_port"
+
+ setPort $public_port
+ pkill java
+
+ if [[ $? != 0 ]]
+ then
+ echo "Failed to restart cassandra (kill failed)"
+ exit 1
+ fi
+
+ sleep 5
+fi
+
+# NOTE: this starts cassandra in the foreground
+/docker-entrypoint.sh cassandra -f
diff --git a/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-mdbc-server.sh b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-mdbc-server.sh
new file mode 100644
index 0000000..bb74415
--- /dev/null
+++ b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-mdbc-server.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+if [ `id -u` = 0 ]
+then
+ # Perform tasks that need to be run as root
+
+ # Re-exec this script as the application user.
+ this=`readlink -f $0`
+ exec su mdbc -c "$this"
+fi
+
+if [ -z "${TABLE_CONFIG_PATH}" ]
+then
+ export TABLE_CONFIG_PATH=$PWD/config/tableConfiguration.json
+fi
+
+if [ -z "${CONFIG_BASE}" ]
+then
+ export CONFIG_BASE=config
+fi
+
+if [ -z "$AVATICA_PORT" ]
+then
+ AVATICA_PORT=30000
+fi
+
+if [ -z "$JDBC_URL" ]
+then
+ echo "JDBC_URL environment variable is not set" 1>&2
+ exit 1
+fi
+
+if [ -z "$JDBC_USER" ]
+then
+ echo "JDBC_USER environment variable is not set" 1>&2
+ exit 1
+fi
+
+if [ -z "$JDBC_PASSWORD" ]
+then
+ echo "JDBC_PASSWORD environment variable is not set" 1>&2
+ exit 1
+fi
+
+jvmargs="${JVM_ARGS}"
+
+echo "JVM Arguments: ${jvmargs}"
+
+if [ ! -s ${CONFIG_BASE}-0.json ]
+then
+ echo "Running CreateNodeConfigurations"
+
+ java ${jvmargs} -cp config:mdbc-server.jar org.onap.music.mdbc.tools.CreateNodeConfigurations -t ${TABLE_CONFIG_PATH} -b ${CONFIG_BASE} -o $PWD
+
+ if [[ $? != 0 ]]
+ then
+ echo "CreateNodeConfigurations failed"
+ exit 1
+ fi
+
+ if [ ! -s ${CONFIG_BASE}-0.json ]
+ then
+ echo "Configuration not created correctlly: ${CONFIG_BASE}-0.json"
+ exit 1
+ fi
+
+ echo "CreateNodeConfigurations created ${CONFIG_BASE}-0.json"
+fi
+
+echo "Running MdbcServer"
+
+java ${jvmargs} -cp config:mdbc-server.jar org.onap.music.mdbc.MdbcServer -c ${CONFIG_BASE}-0.json -p ${AVATICA_PORT} -u ${JDBC_URL} -s ${JDBC_USER} -a ${JDBC_PASSWORD}
+rc=$?
+
+echo "Application exiting with status code $rc"
+
+if [ ! -z "${EXIT_DELAY}" -a "${EXIT_DELAY}" != 0 ]; then
+ echo "Delaying exit for $EXIT_DELAY seconds"
+ sleep $EXIT_DELAY
+fi
+
+exit $rc
diff --git a/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/wait-for.sh b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/wait-for.sh
new file mode 100755
index 0000000..cee1cfe
--- /dev/null
+++ b/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/wait-for.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+# https://github.com/Eficode/wait-for.git
+# MIT License
+# Modified to wait for multiple ports to open
+
+TIMEOUT=15
+QUIET=0
+ADDRESSES=
+
+echoerr() {
+ if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
+}
+
+usage() {
+ exitcode="$1"
+ cat << USAGE >&2
+Usage:
+ wait-for host:port [host:port ... ] [-t timeout] [-- command args]
+ -q | --quiet Do not output any status messages
+ -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
+ -- COMMAND ARGS Execute command with args after the test finishes
+USAGE
+ exit "$exitcode"
+}
+
+wait_for() {
+ command="$*"
+ if [ "$QUIET" -ne 1 ]; then echo "$0: probing host $HOST port $PORT"; fi
+ for i in `seq $TIMEOUT` ; do
+ ready=TRUE
+ set DUMMY $ADDRESSES; shift 1
+ while [ $# -gt 0 ] ; do
+ host=$1
+ port=$2
+ shift 2
+ if ! nc -z "$host" "$port" > /dev/null 2>&1 ; then
+ ready=FALSE
+ break
+ fi
+ done
+ if [ $ready = TRUE ] ; then
+ if [ "$QUIET" -ne 1 ] ; then
+ echo "$0: operation succeeded on try $i"
+ fi
+ if [ -n "$command" ] ; then
+ if [ "$QUIET" -ne 1 ] ;
+ then echo "$0: exec-ing command $command" ;
+ fi
+ exec $command
+ fi
+ exit 0
+ fi
+ if [ "$QUIET" -ne 1 ] ; then
+ echo "$0: sleeping after try $i" ;
+ fi
+ sleep 1
+ done
+ echo "$0: Operation timed out" >&2
+ exit 1
+}
+
+while [ $# -gt 0 ]
+do
+ case "$1" in
+ *:* )
+ host=$(printf "%s\n" "$1"| cut -d : -f 1)
+ port=$(printf "%s\n" "$1"| cut -d : -f 2)
+ ADDRESSES="$ADDRESSES $host $port"
+ shift 1
+ ;;
+ -q | --quiet)
+ QUIET=1
+ shift 1
+ ;;
+ -t)
+ TIMEOUT="$2"
+ if [ "$TIMEOUT" = "" ]; then break; fi
+ shift 2
+ ;;
+ --timeout=*)
+ TIMEOUT="${1#*=}"
+ shift 1
+ ;;
+ --)
+ shift
+ break
+ ;;
+ --help)
+ usage 0
+ ;;
+ *)
+ echoerr "Unknown argument: $1"
+ usage 1
+ ;;
+ esac
+done
+
+if [ "$ADDRESSES" = "" ] ; then
+ echoerr "Error: you need to provide at least one host and port to test."
+ usage 2
+fi
+
+wait_for "$@"