Skip to content

Commit c10c324

Browse files
author
Ronald van Zantvoort
committed
vRouter: prevent fh leakage and use buffered writes in DataBags
1 parent 52232b9 commit c10c324

3 files changed

Lines changed: 16 additions & 15 deletions

File tree

systemvm/patches/debian/config/opt/cloud/bin/cs/CsConfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CsConfig(object):
2626
A class to cache all the stuff that the other classes need
2727
"""
2828
__LOG_FILE = "/var/log/cloud.log"
29-
__LOG_LEVEL = "DEBUG"
29+
__LOG_LEVEL = "INFO"
3030
__LOG_FORMAT = "%(asctime)s %(levelname)-8s %(message)s"
3131
cl = None
3232

systemvm/patches/debian/config/opt/cloud/bin/merge.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,29 @@ def load(self):
4747
data = self.bdata
4848
if not os.path.exists(self.DPATH):
4949
os.makedirs(self.DPATH)
50-
self.fpath = self.DPATH + '/' + self.key + '.json'
50+
self.fpath = os.path.join(self.DPATH, self.key + '.json')
51+
5152
try:
52-
handle = open(self.fpath)
53+
with open(self.fpath, 'r') as _fh:
54+
logging.debug("Loading data bag type %s", self.key)
55+
data = json.load(_fh)
5356
except IOError:
5457
logging.debug("Creating data bag type %s", self.key)
5558
data.update({"id": self.key})
56-
else:
57-
logging.debug("Loading data bag type %s", self.key)
58-
data = json.load(handle)
59-
handle.close()
60-
self.dbag = data
59+
finally:
60+
self.dbag = data
6161

6262
def save(self, dbag):
6363
try:
64-
handle = open(self.fpath, 'w')
64+
with open(self.fpath, 'w') as _fh:
65+
logging.debug("Writing data bag type %s", self.key)
66+
json.dump(
67+
dbag, _fh,
68+
sort_keys=True,
69+
indent=2
70+
)
6571
except IOError:
6672
logging.error("Could not write data bag %s", self.key)
67-
else:
68-
logging.debug("Writing data bag type %s", self.key)
69-
logging.debug(dbag)
70-
jsono = json.dumps(dbag, indent=4, sort_keys=True)
71-
handle.write(jsono)
7273

7374
def getDataBag(self):
7475
return self.dbag

systemvm/patches/debian/config/opt/cloud/bin/update_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import json
2828
from cs.CsVmPassword import *
2929

30-
logging.basicConfig(filename='/var/log/cloud.log', level=logging.DEBUG, format='%(asctime)s %(filename)s %(funcName)s:%(lineno)d %(message)s')
30+
logging.basicConfig(filename='/var/log/cloud.log', level=logging.INFO, format='%(asctime)s %(filename)s %(funcName)s:%(lineno)d %(message)s')
3131

3232
# first commandline argument should be the file to process
3333
if (len(sys.argv) != 2):

0 commit comments

Comments
 (0)