Support providing console log lines from API

This commit is contained in:
Joshua Boniface 2019-12-25 19:10:12 -05:00
parent 8a0a278fe9
commit 1f77b382ef
3 changed files with 74 additions and 8 deletions

View File

@ -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.

View File

@ -1292,6 +1292,49 @@ class API_VM_Locks(Resource):
return api_helper.vm_flush_locks(vm)
api.add_resource(API_VM_Locks, '/vm/<vm>/locks')
# /vm/<vm</console
class API_VM_Console(Resource):
@RequestParser([
{ 'name': 'lines' }
])
@Authenticator
def get(self, vm, reqargs):
"""
Return the recent console log of {vm}
---
tags:
- vm
responses:
200:
description: OK
schema:
type: object
id: VMLog
properties:
name:
type: string
description: The name of the VM
data:
type: string
description: The recent console log text
parameters:
- in: query
name: lines
type: integer
required: false
description: The number of lines to retrieve
404:
description: Not found
schema:
type: object
id: Message
"""
return api_helper.vm_console(
vm,
int(reqargs.get('lines', None))
)
api.add_resource(API_VM_Console, '/vm/<vm>/console')
##########################################################
# Client API - Network

View File

@ -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