add gluster mounting control

This commit is contained in:
petrucci4prez 2017-05-22 01:44:16 -04:00
parent 06a8a4443f
commit 903ec16bb0
1 changed files with 35 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import logging
import logging, os
from subprocess import run, PIPE, CalledProcessError
from logging.handlers import TimedRotatingFileHandler, QueueListener, QueueHandler
def SlaveLogger(name, level, queue):
@ -8,19 +9,44 @@ def SlaveLogger(name, level, queue):
logger.propagate = False
return logger
#TODO: need to add mounting code here for gluster. since this app is the only
# gluster user, (un)mounting should be handled here instead of by systemd
class GlusterFS():
def __init__(self, server, volume, mountpoint):
self.server = server
self.volume = volume
if not os.path.exists(mountpoint):
raise FileNotFoundError
self.mountpoint = mountpoint
def mount(self):
self._run(['mount', '-t', 'glusterfs', self.server + ':/' + self.volume, self.mountpoint])
def unmount(self):
self._run(['umount', self.mountpoint])
def _run(self, cmd):
try:
run(cmd, check=True, stdout=PIPE, stderr=PIPE)
except CalledProcessError as e:
print(e.stderr.decode('ascii').rstrip())
class MasterLogger():
def __init__(self, name, level, queue):
mountpoint = '/mnt/glusterfs/pyledriver'
self.fs = GlusterFS('192.168.11.39', 'pyledriver', mountpoint)
self.fs.mount()
consoleFormat = logging.Formatter('[%(name)s] [%(levelname)s] %(message)s')
fileFormat = logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s')
console = logging.StreamHandler()
console.setFormatter(consoleFormat)
rotatingFile = TimedRotatingFileHandler('/mnt/glusterfs/pyledriver/logs/pyledriver-log', when='midnight')
rotatingFile.setFormatter(fileFormat)
# TODO: check that 'logs' actually exists in the mp
self.rotatingFile = TimedRotatingFileHandler(mountpoint + '/logs/pyledriver-log', when='midnight')
self.rotatingFile.setFormatter(fileFormat)
logging.basicConfig(level=getattr(logging, level), handlers=[QueueHandler(queue)])
logger = logging.getLogger(name)
@ -30,8 +56,10 @@ class MasterLogger():
for i in ['debug', 'info', 'warning', 'error', 'critical']:
setattr(self, i, getattr(logger, i))
self.queListener = QueueListener(queue, console, rotatingFile)
self.queListener = QueueListener(queue, console, self.rotatingFile)
self.queListener.start()
def stop(self):
self.queListener.stop()
self.rotatingFile.close() # must close file stream before unmounting
self.fs.unmount()