From a3c750a220b810bcd66b187dc26d76f99c2fbbc7 Mon Sep 17 00:00:00 2001
From: Mattia Mancini <mancini@astron.nl>
Date: Fri, 18 May 2018 12:35:20 +0000
Subject: [PATCH] Story SW-300: fixing deploying scripts

---
 .gitattributes                                |  5 +-
 LCU/Maintenance/MDB_tools/CMakeLists.txt      |  7 +-
 LCU/Maintenance/MDB_tools/cli/release_code.py | 54 ---------------
 LCU/Maintenance/MDB_tools/deploy.sh           |  1 +
 LCU/Maintenance/MDB_tools/fabfile.py          | 65 +++++++++++++++++++
 .../requirements.txt                          |  3 +-
 6 files changed, 77 insertions(+), 58 deletions(-)
 delete mode 100644 LCU/Maintenance/MDB_tools/cli/release_code.py
 create mode 100644 LCU/Maintenance/MDB_tools/deploy.sh
 create mode 100644 LCU/Maintenance/MDB_tools/fabfile.py
 rename LCU/Maintenance/{DBInterface/django_postgresql => MDB_tools}/requirements.txt (85%)

diff --git a/.gitattributes b/.gitattributes
index fa50c3b9067..47ab2464ced 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1775,7 +1775,6 @@ LCU/Maintenance/DBInterface/django_postgresql/.pytest_cache/v/cache/lastfailed -
 LCU/Maintenance/DBInterface/django_postgresql/.pytest_cache/v/cache/nodeids -text
 LCU/Maintenance/DBInterface/django_postgresql/__init__.py -text
 LCU/Maintenance/DBInterface/django_postgresql/create_db.sql -text
-LCU/Maintenance/DBInterface/django_postgresql/requirements.txt -text
 LCU/Maintenance/DBInterface/django_postgresql/settings.py -text
 LCU/Maintenance/DBInterface/django_postgresql/urls.py -text
 LCU/Maintenance/DBInterface/django_postgresql/wsgi.py -text
@@ -1823,7 +1822,9 @@ LCU/Maintenance/DBInterface/monitoringdb/views.py -text
 LCU/Maintenance/MDB_tools/CMakeLists.txt -text
 LCU/Maintenance/MDB_tools/cli/mdb_loader.py -text
 LCU/Maintenance/MDB_tools/cli/probe_mdb.py -text
-LCU/Maintenance/MDB_tools/cli/release_code.py -text
+LCU/Maintenance/MDB_tools/deploy.sh -text
+LCU/Maintenance/MDB_tools/fabfile.py -text
+LCU/Maintenance/MDB_tools/requirements.txt -text
 LCU/Maintenance/__init__.py -text
 LCU/PPSTune/CMakeLists.txt -text
 LCU/PPSTune/MANIFEST.in -text
diff --git a/LCU/Maintenance/MDB_tools/CMakeLists.txt b/LCU/Maintenance/MDB_tools/CMakeLists.txt
index 8ac31e1d110..94e86d1ea10 100644
--- a/LCU/Maintenance/MDB_tools/CMakeLists.txt
+++ b/LCU/Maintenance/MDB_tools/CMakeLists.txt
@@ -3,4 +3,9 @@
 install(DIRECTORY cli
 	DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
   USE_SOURCE_PERMISSIONS
-  PATTERN ".svn" EXCLUDE)
\ No newline at end of file
+  PATTERN ".svn" EXCLUDE)
+
+install(FILES requirements.txt
+    DESTINATION ${CMAKE_INSTALL_PREFIX})
+install(FILES fabfile.py
+    DESTINATION ${CMAKE_INSTALL_PREFIX})
diff --git a/LCU/Maintenance/MDB_tools/cli/release_code.py b/LCU/Maintenance/MDB_tools/cli/release_code.py
deleted file mode 100644
index adbc39c8e0e..00000000000
--- a/LCU/Maintenance/MDB_tools/cli/release_code.py
+++ /dev/null
@@ -1,54 +0,0 @@
-import logging
-import argparse
-from fabric.transfer import Transfer
-from fabric.connection import Connection
-import requests
-import sys
-import json
-from glob import glob
-import os
-import re
-
-from datetime import datetime
-
-logger = logging.getLogger('release_code')
-
-"""
-This program is meant to realise the code in the testing/production environment
-"""
-
-def setup_argument_parser():
-    parser = argparse.ArgumentParser(prog='probe_mdb')
-    parser.add_argument('path', help='format and path format of the installed code. es. /where/is/stored/*.dat')
-    parser.add_argument('-u', '--user', help='username', default=False)
-    parser.add_argument('--address', help='address of the server. default [localhost]:8000',
-                        default='lofarmonitortest.control.lofar')
-
-    parser.add_argument('--remote_install_path', help='remote path where to install the code',
-                        default='deploy/test')
-    return parser
-
-
-def installing_files(local_root_path, remote_root_path, address):
-    """
-    Copies the django installation to the remote path
-    :param local_root_path: path to the local directory containing the files
-    :param remote_root_path: remote path where to install the files
-    :return:
-    """
-    with Connection(address) as connection:
-        transfer = Transfer(connection)
-        transfer.put(local_root_path, remote_root_path)
-
-
-
-
-
-def parse_arguments(parser):
-    return parser.parse_args()
-
-if __name__=='__main__':
-    logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s",level=logging.DEBUG)
-    parser = setup_argument_parser()
-    args = parse_arguments(parser)
-    installing_files(args.path, args.remote_install_path, args.address)
\ No newline at end of file
diff --git a/LCU/Maintenance/MDB_tools/deploy.sh b/LCU/Maintenance/MDB_tools/deploy.sh
new file mode 100644
index 00000000000..f1f641af19b
--- /dev/null
+++ b/LCU/Maintenance/MDB_tools/deploy.sh
@@ -0,0 +1 @@
+#!/usr/bin/env bash
diff --git a/LCU/Maintenance/MDB_tools/fabfile.py b/LCU/Maintenance/MDB_tools/fabfile.py
new file mode 100644
index 00000000000..8654a59c39b
--- /dev/null
+++ b/LCU/Maintenance/MDB_tools/fabfile.py
@@ -0,0 +1,65 @@
+
+from invoke import task
+import fabric
+from fabric.connection import Connection
+
+
+import os
+
+BUILD_DIR = '/home/mmancini/svn-tree/MonitoringMaintenance-SW300/build/gnu_debug/'
+LOCAL_DIR = '/home/mmancini/svn-tree/MonitoringMaintenance-SW300/build/gnu_debug/installed'
+REMOTE_DIR = '/data/mancini/deploy/'
+REMOTE_INSTALL_DIR = 'installed'
+PATH_SETTINGS_FILE = 'lib64/python3.6/site-packages/lofar/maintenance/django_postgresql/settings.py'
+BASE_SETTINGS_FILE_PATH = 'settings.py'
+
+users = {
+    'test' : 'mancini'
+}
+
+servers = {
+    'test':'lofarmonitortest'
+}
+
+def rsync_compressed(c, from_path='address', to_path='address'):
+    to_address = '{}@{}:{}'.format(c.user, c.host, to_path)
+    command = 'rsync -avz {} {}'.format(from_path, to_address)
+    c.local(command)
+
+
+def configure_remote_server(c, type):
+    # Copying django settings file
+    from_path = os.path.join(REMOTE_DIR, type, BASE_SETTINGS_FILE_PATH)
+    to_path = os.path.join(REMOTE_DIR, type, REMOTE_INSTALL_DIR, PATH_SETTINGS_FILE)
+    print('copying django settings file {} to {}'.format(from_path, to_path))
+    cp_cmd = 'cp {} {}'.format(from_path, to_path)
+    c.run(cp_cmd)
+    print('file copied')
+
+
+def copy_to_remote_server(c, type):
+    deploy_dir = os.path.join(REMOTE_DIR, type)
+    print('deploying on host {} directory {}'.format(c.host, deploy_dir))
+    rsync_compressed(c, LOCAL_DIR, deploy_dir)
+
+@task
+def deploy_to_server(c, type):
+    if isinstance(c, fabric.Connection):
+        print('To be implemented')
+        raise NotImplementedError
+    else:
+        host = servers[type]
+        user = users[type]
+        with Connection(host, user=user) as c:
+            copy_to_remote_server(c, type)
+            configure_remote_server(c, type)
+
+@task
+def update(c):
+
+    # make
+    with c.cd(BUILD_DIR):
+        c.run('pwd')
+        c.run('make')
+        c.run('make install')
+    print('ALL Updated')
\ No newline at end of file
diff --git a/LCU/Maintenance/DBInterface/django_postgresql/requirements.txt b/LCU/Maintenance/MDB_tools/requirements.txt
similarity index 85%
rename from LCU/Maintenance/DBInterface/django_postgresql/requirements.txt
rename to LCU/Maintenance/MDB_tools/requirements.txt
index 5d7195d1beb..51f068d7301 100644
--- a/LCU/Maintenance/DBInterface/django_postgresql/requirements.txt
+++ b/LCU/Maintenance/MDB_tools/requirements.txt
@@ -5,4 +5,5 @@ django-polymorphic
 django-rest-polymorphic
 requests
 pandas
-fabric
\ No newline at end of file
+fabric
+invocations
\ No newline at end of file
-- 
GitLab