From 3f2fa624d34a0b3fc722c2f8be5041fa673a0495 Mon Sep 17 00:00:00 2001
From: Stefano Alberto Russo <stefano.russo@gmail.com>
Date: Fri, 1 May 2020 17:38:35 +0200
Subject: [PATCH] Added support for viewing Slurm task logs.

---
 .../rosetta/base_app/computing_managers.py    | 20 ++++++++++++++++++-
 .../base_app/templates/components/task.html   |  5 +++--
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/services/webapp/code/rosetta/base_app/computing_managers.py b/services/webapp/code/rosetta/base_app/computing_managers.py
index 211d846..c740915 100644
--- a/services/webapp/code/rosetta/base_app/computing_managers.py
+++ b/services/webapp/code/rosetta/base_app/computing_managers.py
@@ -412,7 +412,25 @@ class SlurmComputingManager(ComputingManager):
 
 
     def _get_task_log(self, task, **kwargs):
-        raise NotImplementedError('Not implemented')
+        
+        # Get user keys
+        if task.computing.require_user_auth_keys:
+            user_keys = Keys.objects.get(user=task.user, default=True)
+        else:
+            raise NotImplementedError('Remote tasks not requiring keys are not yet supported')
+
+        # Get computing host
+        host = task.computing.get_conf_param('master')
+        user = task.computing.get_conf_param('user')
+
+        # Stop the task remotely
+        view_log_command = 'ssh -i {} -4 -o StrictHostKeyChecking=no {}@{} \'/bin/bash -c "cat \$HOME/{}.log"\''.format(user_keys.private_key_file, user, host, task.uuid)
+
+        out = os_shell(view_log_command, capture=True)
+        if out.exit_code != 0:
+            raise Exception(out.stderr)
+        else:
+            return out.stdout
 
 
 
diff --git a/services/webapp/code/rosetta/base_app/templates/components/task.html b/services/webapp/code/rosetta/base_app/templates/components/task.html
index bbebbdc..791f5cd 100644
--- a/services/webapp/code/rosetta/base_app/templates/components/task.html
+++ b/services/webapp/code/rosetta/base_app/templates/components/task.html
@@ -74,11 +74,12 @@
             <a href="?uuid={{task.uuid}}&action=delete&details=False">Delete</a>
             {% if task.status == "running" %}
              | <a href="?uuid={{task.uuid}}&action=connect">Connect</a>
-             | <a href="/task_log/?uuid={{task.uuid}}&action=viewlog">View Log</a>
             {% else %}
              | <font color="#c0c0c0">Connect</font>
-             | <font color="#c0c0c0">View Log</font>
             {% endif%}
+             
+            | <a href="/task_log/?uuid={{task.uuid}}&action=viewlog">View Log</a>
+
             </td>
   
            </tr>
-- 
GitLab