From 00fe5c7525bfa07efbb7f4fd27531da7216bf512 Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Wed, 13 Oct 2021 13:02:56 +0000
Subject: [PATCH] L2SS-287: Update docker entry points

---
 bin/start-DS.sh                               | 43 -------------------
 bin/start-ds.sh                               | 29 +++++++++++++
 docker-compose/device-docker.yml              |  9 ++--
 docker-compose/device-observation_control.yml |  9 ++--
 docker-compose/device-recv.yml                |  9 ++--
 docker-compose/device-sdp.yml                 |  9 ++--
 docker-compose/device-sst.yml                 |  9 ++--
 docker-compose/device-unb2.yml                |  9 ++--
 docker-compose/device-xst.yml                 |  9 ++--
 docker-compose/itango/lofar-requirements.txt  |  7 +--
 .../lofar-device-base/lofar-requirements.txt  |  5 +--
 tangostationcontrol/requirements.txt          |  2 +-
 12 files changed, 54 insertions(+), 95 deletions(-)
 delete mode 100755 bin/start-DS.sh
 create mode 100755 bin/start-ds.sh

diff --git a/bin/start-DS.sh b/bin/start-DS.sh
deleted file mode 100755
index 83a6eec6d..000000000
--- a/bin/start-DS.sh
+++ /dev/null
@@ -1,43 +0,0 @@
-#!/bin/bash
-function help()
-{
-    why="${1}"
-    echo -e "*** Cannot start the Python device server.\\n${why}\\n\\n* The Python file for the device server must be the 1st parameter that is provided.\\n* The instance of this device server must be the 2nd parameter that is provided."
-    exit -1
-}
-
-# Check if the mandatory parameters are present:
-# ${1}:  device server's Python file
-# ${2}:  instance of the device server's executable in the configDB
-case ${#} in
-    0)
-        help "The device server's Python file and the instance are missing."
-        ;;
-    1)
-        help "The device server's instance is missing."
-        ;;
-    *)
-        deviceServer="${1}"
-        shift
-        instance="${1}"
-        shift
-        ;;
-esac
-
-# Find the path to the device server's Python file that is
-# relative to the /hosthome directory (in Docker the user's
-# mounted ${HOME}).
-# ATTENTION
-# This is assuming that the device server's Python file exists
-# on the Docker's host in the user's ${HOME} directory.
-runThis=$(basename "${deviceServer}")
-runThis=${runThis//.sh/.py}
-if [ -f "${runThis}" ]; then
-    myDir=${PWD}
-else
-    myDir=${PWD}/$(dirname "${deviceServer}")
-fi
-deviceServerPath=${myDir/${HOME}/\/hosthome}
-
-# Tango log lines start with a UNIX timestamp. Replace them with the UTC time.
-docker exec -it itango python3 "${deviceServerPath}/${runThis}" "${instance}" "${@}" | perl -ne 'use Time::Piece; s/^([0-9]+)/gmtime($1)->strftime("%F %T")/e; print;'
diff --git a/bin/start-ds.sh b/bin/start-ds.sh
new file mode 100755
index 000000000..413eb7c51
--- /dev/null
+++ b/bin/start-ds.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+# Serves as entrypoint script for docker containers
+
+# Check required support file exists
+if [[ ! -f "/usr/local/bin/wait-for-it.sh" ]]; then
+    >&2 echo "/usr/local/bin/wait-for-it.sh file does not exist!"
+    exit 1
+fi
+
+# Check required environment variable is set
+if [[ ! $TANGO_HOST ]]; then
+  >&2 echo "TANGO_HOST environment variable unset!"
+  exit 1
+fi
+
+# Check if configured for specific version
+if [[ $TANGOSTATIONCONTROL ]]; then
+  # TODO (Corne): Download version from artifacts or pypi.
+  # Consider exit 2 an UnImplementedError
+  exit 2
+else
+  # Install the package, exit 1 if it fails
+  cd tangostationcontrol || exit 1
+  python3 setup.py bdist_wheel || exit 1
+  sudo pip install dist/$(ls -Art dist/ | tail -n 1)
+fi
+
+/usr/local/bin/wait-for-it.sh ${TANGO_HOST} --timeout=30 --strict -- $@
diff --git a/docker-compose/device-docker.yml b/docker-compose/device-docker.yml
index d9e1e1e35..cbbd1595f 100644
--- a/docker-compose/device-docker.yml
+++ b/docker-compose/device-docker.yml
@@ -32,13 +32,10 @@ services:
     user: 1000:${DOCKER_GID} # uid 1000 is the default "tango" user
     environment:
       - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
     entrypoint:
-      - /usr/local/bin/wait-for-it.sh
-      - ${TANGO_HOST}
-      - --timeout=30
-      - --strict
-      - --
+      - bin/start-ds.sh
       # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
       # can't know about our Docker port forwarding
-      - python3 -u /opt/lofar/tango/devices/devices/docker_device.py LTS -v -ORBendPoint giop:tcp:0:5705 -ORBendPointPublish giop:tcp:${HOSTNAME}:5705
+      - l2ss-docker-device LTS -v -ORBendPoint giop:tcp:0:5705 -ORBendPointPublish giop:tcp:${HOSTNAME}:5705
     restart: unless-stopped
diff --git a/docker-compose/device-observation_control.yml b/docker-compose/device-observation_control.yml
index 827a558a1..b802d29af 100644
--- a/docker-compose/device-observation_control.yml
+++ b/docker-compose/device-observation_control.yml
@@ -29,13 +29,10 @@ services:
         - ..:/opt/lofar/tango:rw
     environment:
       - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
     entrypoint:
-      - /usr/local/bin/wait-for-it.sh
-      - ${TANGO_HOST}
-      - --timeout=30
-      - --strict
-      - --
+      - bin/start-ds.sh
       # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
       # can't know about our Docker port forwarding
-      - python3 -u /opt/lofar/tango/devices/devices/observation_control.py LTS -v -ORBendPoint giop:tcp:0:5703 -ORBendPointPublish giop:tcp:${HOSTNAME}:5703
+      - l2ss-observation-control LTS -v -ORBendPoint giop:tcp:0:5703 -ORBendPointPublish giop:tcp:${HOSTNAME}:5703
     restart: unless-stopped
diff --git a/docker-compose/device-recv.yml b/docker-compose/device-recv.yml
index f3bc3eea1..f7ad52cae 100644
--- a/docker-compose/device-recv.yml
+++ b/docker-compose/device-recv.yml
@@ -30,13 +30,10 @@ services:
         - ..:/opt/lofar/tango:rw
     environment:
       - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
     entrypoint:
-      - /usr/local/bin/wait-for-it.sh
-      - ${TANGO_HOST}
-      - --timeout=30
-      - --strict
-      - --
+      - bin/start-ds.sh
       # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
       # can't know about our Docker port forwarding
-      - python3 -u /opt/lofar/tango/devices/devices/recv.py LTS -v -ORBendPoint giop:tcp:0:5707 -ORBendPointPublish giop:tcp:${HOSTNAME}:5707
+      - l2ss-receiver LTS -v -ORBendPoint giop:tcp:0:5707 -ORBendPointPublish giop:tcp:${HOSTNAME}:5707
     restart: unless-stopped
diff --git a/docker-compose/device-sdp.yml b/docker-compose/device-sdp.yml
index 8fefa3f35..c1d92f80e 100644
--- a/docker-compose/device-sdp.yml
+++ b/docker-compose/device-sdp.yml
@@ -30,13 +30,10 @@ services:
         - ..:/opt/lofar/tango:rw
     environment:
       - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
     entrypoint:
-      - /usr/local/bin/wait-for-it.sh
-      - ${TANGO_HOST}
-      - --timeout=30
-      - --strict
-      - --
+      - bin/start-ds.sh
       # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
       # can't know about our Docker port forwarding
-      - python3 -u /opt/lofar/tango/devices/devices/sdp/sdp.py LTS -v -ORBendPoint giop:tcp:0:5701 -ORBendPointPublish giop:tcp:${HOSTNAME}:5701
+      - l2ss-sdp LTS -v -ORBendPoint giop:tcp:0:5701 -ORBendPointPublish giop:tcp:${HOSTNAME}:5701
     restart: unless-stopped
diff --git a/docker-compose/device-sst.yml b/docker-compose/device-sst.yml
index 7d922a61b..9b601efc3 100644
--- a/docker-compose/device-sst.yml
+++ b/docker-compose/device-sst.yml
@@ -33,13 +33,10 @@ services:
         - ..:/opt/lofar/tango:rw
     environment:
       - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
     entrypoint:
-      - /usr/local/bin/wait-for-it.sh
-      - ${TANGO_HOST}
-      - --timeout=30
-      - --strict
-      - --
+      - bin/start-ds.sh
       # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
       # can't know about our Docker port forwarding
-      - python3 -u /opt/lofar/tango/devices/devices/sdp/sst.py LTS -v -ORBendPoint giop:tcp:0:5702 -ORBendPointPublish giop:tcp:${HOSTNAME}:5702
+      - l2ss-sst LTS -v -ORBendPoint giop:tcp:0:5702 -ORBendPointPublish giop:tcp:${HOSTNAME}:5702
     restart: unless-stopped
diff --git a/docker-compose/device-unb2.yml b/docker-compose/device-unb2.yml
index b1d7b945c..fb2ea95db 100644
--- a/docker-compose/device-unb2.yml
+++ b/docker-compose/device-unb2.yml
@@ -30,13 +30,10 @@ services:
         - ..:/opt/lofar/tango:rw
     environment:
       - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
     entrypoint:
-      - /usr/local/bin/wait-for-it.sh
-      - ${TANGO_HOST}
-      - --timeout=30
-      - --strict
-      - --
+      - bin/start-ds.sh
       # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
       # can't know about our Docker port forwarding
-      - python3 -u /opt/lofar/tango/devices/devices/unb2.py LTS -v -ORBendPoint giop:tcp:0:5704 -ORBendPointPublish giop:tcp:${HOSTNAME}:5704
+      - l2ss-unb2 LTS -v -ORBendPoint giop:tcp:0:5704 -ORBendPointPublish giop:tcp:${HOSTNAME}:5704
     restart: unless-stopped
diff --git a/docker-compose/device-xst.yml b/docker-compose/device-xst.yml
index c634e5d83..b85b14fad 100644
--- a/docker-compose/device-xst.yml
+++ b/docker-compose/device-xst.yml
@@ -33,13 +33,10 @@ services:
         - ..:/opt/lofar/tango:rw
     environment:
       - TANGO_HOST=${TANGO_HOST}
+    working_dir: /opt/lofar/tango
     entrypoint:
-      - /usr/local/bin/wait-for-it.sh
-      - ${TANGO_HOST}
-      - --timeout=30
-      - --strict
-      - --
+      - bin/start-ds.sh
       # configure CORBA to _listen_ on 0:port, but tell others we're _reachable_ through ${HOSTNAME}:port, since CORBA
       # can't know about our Docker port forwarding
-      - python3 -u /opt/lofar/tango/devices/devices/sdp/xst.py LTS -v -ORBendPoint giop:tcp:0:5706 -ORBendPointPublish giop:tcp:${HOSTNAME}:5706
+      - l2ss-xst LTS -v -ORBendPoint giop:tcp:0:5706 -ORBendPointPublish giop:tcp:${HOSTNAME}:5706
     restart: unless-stopped
diff --git a/docker-compose/itango/lofar-requirements.txt b/docker-compose/itango/lofar-requirements.txt
index 1349c50ca..c3b73750d 100644
--- a/docker-compose/itango/lofar-requirements.txt
+++ b/docker-compose/itango/lofar-requirements.txt
@@ -1,8 +1,5 @@
 parso == 0.7.1
 jedi == 0.17.2
-asyncua
-astropy 
+astropy
+GitPython >= 3.1.24 # BSD
 python-logstash-async
-gitpython
-PyMySQL[rsa]
-sqlalchemy
diff --git a/docker-compose/lofar-device-base/lofar-requirements.txt b/docker-compose/lofar-device-base/lofar-requirements.txt
index 31b22c716..7cadcb9cc 100644
--- a/docker-compose/lofar-device-base/lofar-requirements.txt
+++ b/docker-compose/lofar-device-base/lofar-requirements.txt
@@ -1,7 +1,4 @@
-asyncua
 astropy
 python-logstash-async
-gitpython
-PyMySQL[rsa]
-sqlalchemy
 docker
+GitPython >= 3.1.24 # BSD
diff --git a/tangostationcontrol/requirements.txt b/tangostationcontrol/requirements.txt
index fc06d01e7..fdef2e56d 100644
--- a/tangostationcontrol/requirements.txt
+++ b/tangostationcontrol/requirements.txt
@@ -2,7 +2,7 @@
 # order of appearance. Changing the order has an impact on the overall
 # integration process, which may cause wedges in the gate later.
 
-opcua >= 0.98.9
+asyncua
 PyMySQL[rsa]
 sqlalchemy
 GitPython >= 3.1.24 # BSD
-- 
GitLab