From 1f77b382ef59834965d355486a5690231f1106b9 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Wed, 25 Dec 2019 19:10:12 -0500 Subject: [PATCH] Support providing console log lines from API --- client-api/api_lib/pvcapi_helper.py | 29 +++++++++++++++++++ client-api/pvc-api.py | 43 +++++++++++++++++++++++++++++ client-common/vm.py | 10 ++----- 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/client-api/api_lib/pvcapi_helper.py b/client-api/api_lib/pvcapi_helper.py index 2d159877..45740f41 100755 --- a/client-api/api_lib/pvcapi_helper.py +++ b/client-api/api_lib/pvcapi_helper.py @@ -305,6 +305,35 @@ def vm_node(vm): pvc_common.stopZKConnection(zk_conn) return retdata, retcode +def vm_console(vm, lines=None): + """ + Return the current console log for VM. + """ + # Default to 10 lines of log if not set + if not lines: + lines = 10 + + zk_conn = pvc_common.startZKConnection(config['coordinators']) + retflag, retdata = pvc_vm.get_console_log(zk_conn, vm, lines) + + if retflag: + if retdata: + retcode = 200 + retdata = { + 'name': vm, + 'data': retdata + } + else: + retcode = 404 + retdata = { + 'message': 'VM not found.' + } + else: + retcode = 400 + + pvc_common.stopZKConnection(zk_conn) + return retdata ,retcode + def vm_list(node=None, state=None, limit=None, is_fuzzy=True): """ Return a list of VMs with limit LIMIT. diff --git a/client-api/pvc-api.py b/client-api/pvc-api.py index 23859688..f4e10b93 100755 --- a/client-api/pvc-api.py +++ b/client-api/pvc-api.py @@ -1292,6 +1292,49 @@ class API_VM_Locks(Resource): return api_helper.vm_flush_locks(vm) api.add_resource(API_VM_Locks, '/vm//locks') +# /vm//console') + ########################################################## # Client API - Network diff --git a/client-common/vm.py b/client-common/vm.py index 13a0b082..72488a8c 100644 --- a/client-common/vm.py +++ b/client-common/vm.py @@ -564,19 +564,13 @@ def get_console_log(zk_conn, domain, lines=1000): # Get the data from ZK console_log = zkhandler.readdata(zk_conn, '/domains/{}/consolelog'.format(dom_uuid)) + print(lines) # Shrink the log buffer to length lines shrunk_log = console_log.split('\n')[-lines:] loglines = '\n'.join(shrunk_log) - # Show it in the pager (less) - try: - pager = subprocess.Popen(['less', '-R'], stdin=subprocess.PIPE) - pager.communicate(input=loglines.encode('utf8')) - except FileNotFoundError: - return False, 'ERROR: The "less" pager is required to view console logs.' - - return True, '' + return True, loglines def follow_console_log(zk_conn, domain, lines=10): # Validate that VM exists in cluster