From 5a118f19ebf0b358756aeed9cde4a4a21c9782cd Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Fri, 12 Apr 2019 13:47:20 +0000
Subject: [PATCH] SW-610: Upgrade ObservationControl2 to use Fabric v2+

---
 MAC/Services/src/ObservationControl2.py | 33 +++++++++----------------
 1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/MAC/Services/src/ObservationControl2.py b/MAC/Services/src/ObservationControl2.py
index 84527ec3190..027f4152f49 100644
--- a/MAC/Services/src/ObservationControl2.py
+++ b/MAC/Services/src/ObservationControl2.py
@@ -21,16 +21,8 @@ import os
 import logging
 from optparse import OptionParser
 
-from fabric.exceptions import NetworkError
-
-try:
-    # WARNING: This code only works with Fabric Version 1
-    from fabric import tasks
-    from fabric.api import env, run, settings
-except ImportError as e:
-    print(str(e))
-    print('Please install python3 package fabric: sudo apt-get install fabric')
-    exit(1)
+# WARNING: This code only works with Fabric Version 2
+from fabric.connection import Connection
 
 from lofar.messaging import Service
 from lofar.messaging import setQpidLogLevel
@@ -48,15 +40,18 @@ class ObservationControlHandler(MessageHandlerInterface):
             'AbortObservation': self.abort_observation
         }
 
-        env.hosts = ["localhost"]
+        host = "localhost"
 
         if "LOFARENV" in os.environ:
             lofar_environment = os.environ['LOFARENV']
 
             if lofar_environment == "PRODUCTION":
-                env.hosts = [config.PRODUCTION_OBSERVATION_CONTROL_HOST]
+                host = config.PRODUCTION_OBSERVATION_CONTROL_HOST
             elif lofar_environment == "TEST":
-                env.hosts = [config.TEST_OBSERVATION_CONTROL_HOST]
+                host = config.TEST_OBSERVATION_CONTROL_HOST
+
+        self.connection = Connection(host)
+
 
     def _abort_observation_task(self, sas_id):
         logger.info("trying to abort ObservationControl for SAS ID: %s", sas_id)
@@ -64,25 +59,21 @@ class ObservationControlHandler(MessageHandlerInterface):
         killed = False
 
         with settings(warn_only = True):
-            pid_line = run('pidof ObservationControl')
+            pid_line = self.connection.run('pidof ObservationControl').stdout
             pids = pid_line.split(' ')
 
             for pid in pids:
-                pid_sas_id = run("ps -p %s --no-heading -o command | awk -F[{}] '{ printf $2; }'" % pid)
+                pid_sas_id = self.connection.run("ps -p %s --no-heading -o command | awk -F[{}] '{ printf $2; }'" % pid).stdout
                 if str(pid_sas_id) == str(sas_id):
                     logger.info("Killing ObservationControl with PID: %s for SAS ID: %s", pid, sas_id)
-                    run('kill -SIGINT %s' % pid)
+                    self.connection.run('kill -SIGINT %s' % pid)
                     killed = True
 
         return killed
 
     def abort_observation(self, sas_id):
         """ aborts an observation for a single sas_id """
-        try:
-            result = tasks.execute(self._abort_observation_task, sas_id)
-            aborted = True in list(result.values())
-        except NetworkError:
-            aborted = False
+        aborted = self._abort_observation_task(sas_id)
 
         return {'aborted': aborted}
 
-- 
GitLab