vRouter: prevent fh leakage and use buffered writes in DataBags

This commit is contained in:
Ronald van Zantvoort 2017-05-08 13:13:51 +02:00
parent 52232b92a0
commit c10c3245d1
3 changed files with 16 additions and 15 deletions

View File

@ -26,7 +26,7 @@ class CsConfig(object):
A class to cache all the stuff that the other classes need
"""
__LOG_FILE = "/var/log/cloud.log"
__LOG_LEVEL = "DEBUG"
__LOG_LEVEL = "INFO"
__LOG_FORMAT = "%(asctime)s %(levelname)-8s %(message)s"
cl = None

View File

@ -47,28 +47,29 @@ class DataBag:
data = self.bdata
if not os.path.exists(self.DPATH):
os.makedirs(self.DPATH)
self.fpath = self.DPATH + '/' + self.key + '.json'
self.fpath = os.path.join(self.DPATH, self.key + '.json')
try:
handle = open(self.fpath)
with open(self.fpath, 'r') as _fh:
logging.debug("Loading data bag type %s", self.key)
data = json.load(_fh)
except IOError:
logging.debug("Creating data bag type %s", self.key)
data.update({"id": self.key})
else:
logging.debug("Loading data bag type %s", self.key)
data = json.load(handle)
handle.close()
self.dbag = data
finally:
self.dbag = data
def save(self, dbag):
try:
handle = open(self.fpath, 'w')
with open(self.fpath, 'w') as _fh:
logging.debug("Writing data bag type %s", self.key)
json.dump(
dbag, _fh,
sort_keys=True,
indent=2
)
except IOError:
logging.error("Could not write data bag %s", self.key)
else:
logging.debug("Writing data bag type %s", self.key)
logging.debug(dbag)
jsono = json.dumps(dbag, indent=4, sort_keys=True)
handle.write(jsono)
def getDataBag(self):
return self.dbag

View File

@ -27,7 +27,7 @@ import configure
import json
from cs.CsVmPassword import *
logging.basicConfig(filename='/var/log/cloud.log', level=logging.DEBUG, format='%(asctime)s %(filename)s %(funcName)s:%(lineno)d %(message)s')
logging.basicConfig(filename='/var/log/cloud.log', level=logging.INFO, format='%(asctime)s %(filename)s %(funcName)s:%(lineno)d %(message)s')
# first commandline argument should be the file to process
if (len(sys.argv) != 2):