daemon: Log output from state hooks to a file.

Also close other file descriptors when running state hooks.
This commit is contained in:
Tom Goff 2016-01-29 16:18:39 -05:00
parent 6e092389af
commit 6fb1eb9bd6

View file

@ -14,6 +14,7 @@ that manages a CORE session.
import os, sys, tempfile, shutil, shlex, atexit, gc, pwd
import threading, time, random
import traceback
import subprocess
from core.api import coreapi
if os.uname()[0] == "Linux":
@ -293,8 +294,16 @@ class Session(object):
self.warn("Error writing hook '%s': %s" % (filename, e))
self.info("Running hook %s for state %s" % (filename, state))
try:
check_call(["/bin/sh", filename], cwd=self.sessiondir,
env=self.getenviron())
stdout = open(os.path.join(self.sessiondir,
filename + '.log'), 'w')
stderr = subprocess.STDOUT
except:
stdout = None
stderr = None
try:
check_call(["/bin/sh", filename], stdout=stdout,
stderr=stderr, close_fds=True,
cwd=self.sessiondir, env=self.getenviron())
except Exception, e:
self.warn("Error running hook '%s' for state %s: %s" %
(filename, state, e))