aboutsummaryrefslogtreecommitdiffstats
path: root/api/resources
diff options
context:
space:
mode:
Diffstat (limited to 'api/resources')
-rw-r--r--api/resources/v2/tasks.py36
-rw-r--r--api/resources/write_hosts.py14
2 files changed, 47 insertions, 3 deletions
diff --git a/api/resources/v2/tasks.py b/api/resources/v2/tasks.py
index 885a190c6..25a9cf109 100644
--- a/api/resources/v2/tasks.py
+++ b/api/resources/v2/tasks.py
@@ -8,6 +8,8 @@
##############################################################################
import uuid
import logging
+import os
+import errno
from datetime import datetime
from oslo_serialization import jsonutils
@@ -252,3 +254,37 @@ class V2Task(ApiResource):
task_thread.start()
return result_handler(consts.API_SUCCESS, {'uuid': task_id})
+
+
+class V2TaskLog(ApiResource):
+
+ def get(self, task_id):
+ try:
+ uuid.UUID(task_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'invalid task id')
+
+ task_handler = V2TaskHandler()
+ try:
+ task = task_handler.get_by_uuid(task_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'no such task id')
+
+ index = int(self._get_args().get('index', 0))
+
+ try:
+ with open(os.path.join(consts.TASK_LOG_DIR, '{}.log'.format(task_id))) as f:
+ f.seek(index)
+ data = f.readlines()
+ index = f.tell()
+ except OSError as e:
+ if e.errno == errno.ENOENT:
+ return result_handler(consts.API_ERROR, 'log file does not exist')
+ return result_handler(consts.API_ERROR, 'error with log file')
+
+ return_data = {
+ 'index': index,
+ 'data': data
+ }
+
+ return result_handler(task.status, return_data)
diff --git a/api/resources/write_hosts.py b/api/resources/write_hosts.py
index e4b69846b..a3025d3b0 100644
--- a/api/resources/write_hosts.py
+++ b/api/resources/write_hosts.py
@@ -13,11 +13,19 @@ import json
def write_hosts(hosts_ip):
- hosts_list = ('\n{} {}'.format(ip, host_name)
+
+ yardstick_flag = "# SUT hosts info for Yardstick"
+ hosts_list = ('\n{} {} {}'.format(ip, host_name, yardstick_flag)
for host_name, ip in hosts_ip.items())
- with open("/etc/hosts", 'a') as f:
+
+ with open("/etc/hosts", 'r') as f:
+ origin_lines = [line for line in f if yardstick_flag not in line]
+
+ with open("/etc/hosts", 'w') as f:
+ f.writelines(origin_lines)
+ f.write(yardstick_flag)
f.writelines(hosts_list)
- f.write("\n")
+
if __name__ == "__main__":
write_hosts(json.load(sys.stdin))