Honeynode Docker refactoring for Java11 38/88438/17
authorChristophe Betoule <christophe.betoule@orange.com>
Mon, 16 Mar 2020 11:59:11 +0000 (12:59 +0100)
committerGuillaume Lambert <guillaume.lambert@orange.com>
Fri, 27 Mar 2020 16:13:17 +0000 (16:13 +0000)
Upgrade Docker image generation process to use new Java11 simulators.
Both OpenROADM versions 1.2.1 and 2.2.1 simulators are now using a
same docker image and the same scripts.

This brings the following improvements:
- create a multi-stage docker file to isolate build and run processes
- decrease image size
- use an applicative user in Dockerfile rather than root
- add healthcheck
- expose both netconf and restconf ports
- use the docker option --name to set the name of the container
- rootless container and sudo management for docker command
- centralize most scripts parameters in dockercmd_profile.sh

JIRA: TRNSPRTPCE-239
Signed-off-by: Christophe Betoule <christophe.betoule@orange.com>
Co-authored-by: Emmanuelle Delfour <emmanuelle.delfour@orange.com>
Co-authored-by: Guillaume Lambert <guillaume.lambert@orange.com>
Change-Id: I86b47cac0f795c2ac9d1615efa703d8ca87b34e5

tests/Xtesting/DockerSims/Dockerfile [new file with mode: 0644]
tests/Xtesting/DockerSims/Dockerfile.orig.21 [deleted file]
tests/Xtesting/DockerSims/Dockerfile.orig.221 [deleted file]
tests/Xtesting/DockerSims/build_sims.sh [new file with mode: 0755]
tests/Xtesting/DockerSims/dockercmd_profile.sh [new file with mode: 0755]
tests/Xtesting/DockerSims/generate_sims21.sh [deleted file]
tests/Xtesting/DockerSims/generate_sims221.sh [deleted file]
tests/Xtesting/DockerSims/start_sims.sh [new file with mode: 0755]
tests/Xtesting/DockerSims/start_sims21.sh [deleted file]
tests/Xtesting/DockerSims/start_sims221.sh [deleted file]
tests/Xtesting/DockerSims/stop_sims.sh [new file with mode: 0755]

diff --git a/tests/Xtesting/DockerSims/Dockerfile b/tests/Xtesting/DockerSims/Dockerfile
new file mode 100644 (file)
index 0000000..59906c4
--- /dev/null
@@ -0,0 +1,38 @@
+FROM maven:3.6.3-jdk-11-slim AS builder
+ARG GIT_REF=master
+RUN apt-get update \
+    && apt-get -f -m install -y git \
+    && rm -rf /var/lib/apt/lists/* \
+    && mkdir /tmp/git_repo \
+    && git clone -b "$GIT_REF"  https://git.opendaylight.org/gerrit/transportpce /tmp/git_repo \
+    && cd /tmp/git_repo/tests \
+    && ./buildHoneynode.sh  \
+    && ./buildHoneynode.sh 2.2.1 \
+    && mkdir -p /tmp/honeynode/honeynode_1.2.1/config \
+    && mkdir -p /tmp/honeynode/honeynode_2.2.1/config \
+    && mv /tmp/git_repo/tests/honeynode/1.2.1/honeynode-distribution/target/honeynode-distribution-1.19.04-hc/honeynode-distribution-1.19.04/* /tmp/honeynode/honeynode_1.2.1/. \
+    && mv /tmp/git_repo/tests/honeynode/2.2.1/honeynode-distribution/target/honeynode-distribution-1.19.04-hc/honeynode-distribution-1.19.04/* /tmp/honeynode/honeynode_2.2.1/. \
+    && cp /tmp/git_repo/tests/sample_configs/openroadm/1.2.1/* /tmp/honeynode/honeynode_1.2.1/config/. \
+    && cp /tmp/git_repo/tests/sample_configs/openroadm/2.2.1/* /tmp/honeynode/honeynode_2.2.1/config/.
+
+
+
+
+FROM adoptopenjdk/openjdk11:alpine-jre
+LABEL Name="OpenROADM compliant ROADM and XPDR NETCONF simulators"
+LABEL Description="Simulators for Openroadm devices based on honeycomb project"
+LABEL Maintainer="transportpce-dev@lists.opendaylight.org"
+LABEL Url="https://git.opendaylight.org/gerrit/transportpce"
+
+ENV DEVICE_VERSION=1.2.1
+ENV DEVICE_FILE=oper-ROADMA-full.xml
+RUN addgroup gsimulator && adduser -D usimulator -G gsimulator -u 100001\
+  && mkdir -p /opt/application/simulators
+COPY --chown=usimulator:gsimulator --from=builder /tmp/honeynode/ /opt/application/simulators/honeynode
+COPY --chown=usimulator:gsimulator --from=builder /tmp/git_repo/tests/sample_configs/openroadm/ /opt/application/simulators/honeynode/config
+RUN find /opt/application/simulators -name "honeycomb-tpce" -exec  sed -i "/reflectwarn.sh/d" {} +
+USER usimulator
+WORKDIR /opt/application/simulators/honeynode
+HEALTHCHECK CMD wget --header "Authorization: Basic `echo -n "${USER}:${PASSWORD}" | base64`"  http://localhost:8130/restconf/ -S -O -
+EXPOSE 1830/tcp 8130/tcp
+CMD /opt/application/simulators/honeynode/honeynode_$DEVICE_VERSION/honeycomb-tpce 1830 /opt/application/simulators/honeynode/config/$DEVICE_VERSION/$DEVICE_FILE
diff --git a/tests/Xtesting/DockerSims/Dockerfile.orig.21 b/tests/Xtesting/DockerSims/Dockerfile.orig.21
deleted file mode 100644 (file)
index c5e6a19..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-FROM openjdk:8-jdk-alpine
-
-RUN apk add --update --no-cache bash procps maven git \
-  && mkdir /opt/sims \
-  && cd /root \
-  && git clone -b master https://git.opendaylight.org/gerrit/transportpce \
-  && cd transportpce/tests \
-  && ./buildHoneynode.sh \
-  && mv honeynode/2.1 /opt/sims/honeynode \
-  && echo -e '#/bin/sh\n\ncd /opt/sims\n./honeynode/honeynode-distribution/target/honeynode-distribution-1.18.01-hc/honeynode-distribution-1.18.01/honeycomb-tpce $@\n'>/usr/bin/honeynode \
-  && chmod +x /usr/bin/honeynode \
-  && mv sample_configs/ /opt/sims/ \
-  && cd ../.. \
-  && rm -rf transportpce/ .m2/ \
-  && apk del git maven
-
-#CMD honeynode 830 sample_configs/openroadm/2.1/oper-ROADMA.xml
diff --git a/tests/Xtesting/DockerSims/Dockerfile.orig.221 b/tests/Xtesting/DockerSims/Dockerfile.orig.221
deleted file mode 100644 (file)
index a2da3e5..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-FROM openjdk:8-jdk-alpine
-
-RUN apk add --update --no-cache bash procps maven git \
-  && mkdir /opt/sims \
-  && cd /root \
-  && git clone -b master https://git.opendaylight.org/gerrit/transportpce \
-  && cd transportpce/tests \
-  && ./buildHoneynode.sh 2.2.1\
-  && mv honeynode/2.2.1 /opt/sims/honeynode \
-  && echo -e '#/bin/sh\n\ncd /opt/sims\n./honeynode/honeynode-distribution/target/honeynode-distribution-1.18.01-hc/honeynode-distribution-1.18.01/honeycomb-tpce $@\n'>/usr/bin/honeynode \
-  && chmod +x /usr/bin/honeynode \
-  && mv sample_configs/ /opt/sims/ \
-  && cd ../.. \
-  && rm -rf transportpce/ .m2/ \
-  && apk del git maven
-
-#CMD honeynode 830 sample_configs/openroadm/2.2.1/oper-ROADMA.xml
diff --git a/tests/Xtesting/DockerSims/build_sims.sh b/tests/Xtesting/DockerSims/build_sims.sh
new file mode 100755 (executable)
index 0000000..ee9e224
--- /dev/null
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+. $(dirname $0)/dockercmd_profile.sh
+
+set -x
+${DOCKER_CMD} build --no-cache --tag=${DOCKER_IMAGE} .
diff --git a/tests/Xtesting/DockerSims/dockercmd_profile.sh b/tests/Xtesting/DockerSims/dockercmd_profile.sh
new file mode 100755 (executable)
index 0000000..1f3aa4b
--- /dev/null
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+DEVICE_VERSION=${1:-1.2.1}
+
+# this file centralizes inb one place parameters needed by docker scripts,
+# more especially the image lists for 1.2.1 and 2.2.1 devices.
+# device versions supported as scripts argument are 1.2.1 and 2.2.1 and "all"
+
+#IMAGE_LIST format is suffix_port : container/image name : config file to fill netconf operational datastore
+
+IMAGE121_LIST="30:xpdra-1.2.1:oper-XPDRA.xml
+31:roadma-1.2.1:oper-ROADMA.xml
+32:roadmb-1.2.1:oper-ROADMB.xml
+33:roadmc-1.2.1:oper-ROADMC.xml
+34:xpdrc-1.2.1:oper-XPDRC.xml
+21:roadma-full-1.2.1:oper-ROADMA-full.xml
+23:roadmc-full-1.2.1:oper-ROADMC-full.xml
+"
+
+IMAGE221_LIST="40:xpdra-2.2.1:oper-XPDRA.xml
+41:roadma-2.2.1:oper-ROADMA.xml
+42:roadmb-2.2.1:oper-ROADMB.xml
+43:roadmc-2.2.1:oper-ROADMC.xml
+44:xpdrc-2.2.1:oper-XPDRC.xml
+45:spdrav2-2.2.1:oper-SPDRAv2.xml
+46:spdrav1-2.2.1:oper-SPDRAv1.xml
+"
+
+case "$DEVICE_VERSION" in
+    "1.2.1") IMAGE_LIST=$IMAGE121_LIST
+    ;;
+    "2.2.1") IMAGE_LIST=$IMAGE221_LIST
+    ;;
+    *) echo "unsupported device version"
+    exit 1
+    ;;
+esac
+
+DOCKER_CMD=docker
+DOCKER_IMAGE=honeynode:alpine-jre11
+RESTCONF_USER=admin
+RESTCONF_PASSWORD=admin
+if [ `which podman` ]
+then DOCKER_CMD=podman
+else
+    if [ ! `which $DOCKER_CMD` ]
+    then  echo "no docker command available" >&2
+        exit 1
+    fi
+    #if "docker ps" cannot be run without error, prepend sudo
+    if ( ! $DOCKER_CMD ps >/dev/null 2>&1 );then
+        echo "docker command only usable as root, using sudo" >&2
+        DOCKER_CMD="sudo docker"
+    fi
+fi
+
+
diff --git a/tests/Xtesting/DockerSims/generate_sims21.sh b/tests/Xtesting/DockerSims/generate_sims21.sh
deleted file mode 100755 (executable)
index e42b912..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/bash
-
-cp Dockerfile.orig.21 Dockerfile
-sudo docker build --tag=alpine_honeynode:2.1 .
-for conf_file in `(cd ../.. && ls sample_configs/openroadm/2.1/)`
-do  DOCKNAME=honeynode_`echo $conf_file |cut -d\. -f1 |tr '[:upper:]' '[:lower:]'`
-        echo "generating docker image '$DOCKNAME'"
-        echo -e "FROM alpine_honeynode:2.1\n\nCMD honeynode 830 sample_configs/openroadm/2.1/$conf_file\n" >Dockerfile
-        sudo docker build --tag=$DOCKNAME:2.1 .
-done
diff --git a/tests/Xtesting/DockerSims/generate_sims221.sh b/tests/Xtesting/DockerSims/generate_sims221.sh
deleted file mode 100755 (executable)
index d789a32..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/bash
-
-cp Dockerfile.orig.221 Dockerfile
-sudo docker build --tag=alpine_honeynode:2.2.1 .
-for conf_file in `(cd ../.. && ls sample_configs/openroadm/2.2.1/)`
-do  DOCKNAME=honeynode_`echo $conf_file |cut -d\. -f1 |tr '[:upper:]' '[:lower:]'`
-        echo "generating docker image '$DOCKNAME'"
-        echo -e "FROM alpine_honeynode:2.2.1\n\nCMD honeynode 830 sample_configs/openroadm/2.2.1/$conf_file\n" >Dockerfile
-        sudo docker build --tag=$DOCKNAME:2.2.1 .
-done
diff --git a/tests/Xtesting/DockerSims/start_sims.sh b/tests/Xtesting/DockerSims/start_sims.sh
new file mode 100755 (executable)
index 0000000..dc594f7
--- /dev/null
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+. $(dirname $0)/dockercmd_profile.sh
+
+DOCKER_OPTIONS="-e USER=${RESTCONF_USER} -e PASSWORD=${RESTCONF_PASSWORD} -e DEVICE_VERSION=${DEVICE_VERSION} -dit ${DOCKER_IMAGE}"
+
+for image in $IMAGE_LIST;do
+    suffix_port=`echo -n $image| cut -d: -f1`
+    image_name=`echo -n $image| cut -d: -f2`
+    device_file=`echo -n $image| cut -d: -f3`
+    #echo "$image_name $suffix_port $device_file"
+    if [ ! "$(${DOCKER_CMD} ps -q -f name=${image_name})" ];then
+       if [  "$(${DOCKER_CMD} ps -aq -f status=exited -f name=${image_name})" ];then
+          ${DOCKER_CMD} rm ${image_name}
+       fi
+    elif [  "$(${DOCKER_CMD} ps -q -f status=running -f name=${image_name})" ];then
+          ${DOCKER_CMD} stop ${image_name}
+    fi
+    echo ${image_name}
+    ${DOCKER_CMD} run --rm -p 178$suffix_port:1830 -p 81$suffix_port:8130 --name ${image_name} -e DEVICE_FILE=${device_file} ${DOCKER_OPTIONS}
+done
+
+exit
+
diff --git a/tests/Xtesting/DockerSims/start_sims21.sh b/tests/Xtesting/DockerSims/start_sims21.sh
deleted file mode 100755 (executable)
index f5c11ff..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/sh
-
-echo "killing previous sims containers"
-if [ -f kill_sims21.sh ]; then ./kill_sims21.sh; rm sims21.pid kill_sims21.sh; else echo "nothing to do"; fi
-
-echo `sudo docker run -d -p 17830:830  honeynode_oper-xpdra:2.1`>>sims21.pid
-echo `sudo docker run -d -p 17831:830  honeynode_oper-roadma:2.1`>>sims21.pid
-echo `sudo docker run -d -p 17832:830  honeynode_oper-roadmb:2.1`>>sims21.pid
-echo `sudo docker run -d -p 17833:830  honeynode_oper-roadmc:2.1`>>sims21.pid
-echo `sudo docker run -d -p 17834:830  honeynode_oper-xpdrc:2.1`>>sims21.pid
-echo `sudo docker run -d -p 17821:830  honeynode_oper-roadma-full:2.1`>>sims21.pid
-echo `sudo docker run -d -p 17823:830  honeynode_oper-roadmc-full:2.1`>>sims21.pid
-
-echo -n "#!/bin/sh\n\nsudo docker container kill "`cat sims21.pid`" \n" >kill_sims21.sh
-chmod +x kill_sims21.sh
diff --git a/tests/Xtesting/DockerSims/start_sims221.sh b/tests/Xtesting/DockerSims/start_sims221.sh
deleted file mode 100755 (executable)
index cd4e13f..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-
-echo "killing previous sims containers"
-if [ -f kill_sims221.sh ]; then ./kill_sims221.sh; rm sims221.pid kill_sims221.sh; else echo "nothing to do"; fi
-
-echo `sudo docker run -d -p 17840:830  honeynode_oper-xpdra:2.2.1`>>sims221.pid
-echo `sudo docker run -d -p 17841:830  honeynode_oper-roadma:2.2.1`>>sims221.pid
-#echo `sudo docker run -d -p 17842:830  honeynode_oper-roadmb:2.2.1`>>sims221.pid
-echo `sudo docker run -d -p 17843:830  honeynode_oper-roadmc:2.2.1`>>sims221.pid
-echo `sudo docker run -d -p 17844:830  honeynode_oper-xpdrc:2.2.1`>>sims221.pid
-
-echo -n "#!/bin/sh\n\nsudo docker container kill "`cat sims221.pid`" \n" >kill_sims221.sh
-chmod +x kill_sims221.sh
diff --git a/tests/Xtesting/DockerSims/stop_sims.sh b/tests/Xtesting/DockerSims/stop_sims.sh
new file mode 100755 (executable)
index 0000000..2d33930
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+. $(dirname $0)/dockercmd_profile.sh
+
+stop_list=""
+for image in $IMAGE_LIST;do
+    image_name=`echo -n $image| cut -d: -f2`
+    stop_list=$stop_list" "$image_name
+done
+${DOCKER_CMD} container stop ${stop_list}