2019-04-11 19:06:06 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-07-07 15:20:37 -04:00
|
|
|
# VMConsoleWatcherInstance.py - Class implementing a console log watcher for PVC domains
|
2019-04-11 19:06:06 -04:00
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2022-10-06 11:55:27 -04:00
|
|
|
# Copyright (C) 2018-2022 Joshua M. Boniface <joshua@boniface.me>
|
2019-04-11 19:06:06 -04:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2021-03-25 16:57:17 -04:00
|
|
|
# the Free Software Foundation, version 3.
|
2019-04-11 19:06:06 -04:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2020-08-11 11:46:41 -04:00
|
|
|
from threading import Thread, Event
|
2019-04-11 19:06:06 -04:00
|
|
|
from collections import deque
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2019-07-07 15:20:37 -04:00
|
|
|
class VMConsoleWatcherInstance(object):
|
2019-04-11 19:06:06 -04:00
|
|
|
# Initialization function
|
2021-06-01 11:46:27 -04:00
|
|
|
def __init__(self, domuuid, domname, zkhandler, config, logger, this_node):
|
2019-04-11 19:06:06 -04:00
|
|
|
self.domuuid = domuuid
|
|
|
|
self.domname = domname
|
2021-06-01 11:46:27 -04:00
|
|
|
self.zkhandler = zkhandler
|
2019-04-11 19:06:06 -04:00
|
|
|
self.config = config
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logfile = "{}/{}.log".format(config["console_log_directory"], self.domname)
|
|
|
|
self.console_log_lines = config["console_log_lines"]
|
2019-04-11 19:06:06 -04:00
|
|
|
self.logger = logger
|
|
|
|
self.this_node = this_node
|
|
|
|
|
|
|
|
# Try to append (create) the logfile and set its permissions
|
2021-11-06 03:02:43 -04:00
|
|
|
open(self.logfile, "a").close()
|
2019-04-11 19:06:06 -04:00
|
|
|
os.chmod(self.logfile, 0o600)
|
|
|
|
|
2021-05-10 01:03:04 -04:00
|
|
|
try:
|
|
|
|
self.logdeque = deque(open(self.logfile), self.console_log_lines)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
# There is corruption in the log file; overwrite it
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out(
|
|
|
|
"Failed to decode console log file; clearing existing file",
|
|
|
|
state="w",
|
|
|
|
prefix="Domain {}".format(self.domuuid),
|
|
|
|
)
|
|
|
|
with open(self.logfile, "w") as lfh:
|
|
|
|
lfh.write("\n")
|
2021-05-10 01:03:04 -04:00
|
|
|
self.logdeque = deque(open(self.logfile), self.console_log_lines)
|
2019-04-11 19:06:06 -04:00
|
|
|
|
|
|
|
self.stamp = None
|
|
|
|
self.cached_stamp = None
|
|
|
|
|
|
|
|
# Set up the deque with the current contents of the log
|
|
|
|
self.last_loglines = None
|
|
|
|
self.loglines = None
|
|
|
|
|
|
|
|
# Thread options
|
|
|
|
self.thread = None
|
2020-08-11 11:46:41 -04:00
|
|
|
self.thread_stopper = Event()
|
2019-04-11 19:06:06 -04:00
|
|
|
|
|
|
|
# Start execution thread
|
|
|
|
def start(self):
|
|
|
|
self.thread_stopper.clear()
|
2020-08-11 11:46:41 -04:00
|
|
|
self.thread = Thread(target=self.run, args=(), kwargs={})
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out(
|
|
|
|
"Starting VM log parser", state="i", prefix="Domain {}".format(self.domuuid)
|
|
|
|
)
|
2019-04-11 19:06:06 -04:00
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
# Stop execution thread
|
|
|
|
def stop(self):
|
2021-06-13 14:36:27 -04:00
|
|
|
if self.thread and self.thread.is_alive():
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out(
|
|
|
|
"Stopping VM log parser",
|
|
|
|
state="i",
|
|
|
|
prefix="Domain {}".format(self.domuuid),
|
|
|
|
)
|
2019-05-11 12:08:23 -04:00
|
|
|
self.thread_stopper.set()
|
|
|
|
# Do one final flush
|
|
|
|
self.update()
|
2019-04-11 19:06:06 -04:00
|
|
|
|
|
|
|
# Main entrypoint
|
|
|
|
def run(self):
|
|
|
|
# Main loop
|
|
|
|
while not self.thread_stopper.is_set():
|
|
|
|
self.update()
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.stamp = os.stat(self.logfile).st_mtime
|
|
|
|
if self.stamp != self.cached_stamp:
|
|
|
|
self.cached_stamp = self.stamp
|
|
|
|
self.fetch_lines()
|
|
|
|
# Update Zookeeper with the new loglines if they changed
|
|
|
|
if self.loglines != self.last_loglines:
|
2021-11-06 03:02:43 -04:00
|
|
|
self.zkhandler.write(
|
|
|
|
[(("domain.console.log", self.domuuid), self.loglines)]
|
|
|
|
)
|
2019-04-11 19:06:06 -04:00
|
|
|
self.last_loglines = self.loglines
|
|
|
|
|
|
|
|
def fetch_lines(self):
|
|
|
|
self.logdeque = deque(open(self.logfile), self.console_log_lines)
|
2021-11-06 03:02:43 -04:00
|
|
|
self.loglines = "".join(self.logdeque)
|