Commit 13998ef2 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Use ReadFile/WriteFile in more places



This survived QA, burnin and unittests.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarLuca Bigliardi <shammash@google.com>
parent a2442bf9
......@@ -51,6 +51,9 @@ from ganeti import objects
from ganeti import ssconf
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
class RPCFail(Exception):
"""Class denoting RPC failure.
......@@ -58,6 +61,7 @@ class RPCFail(Exception):
"""
def _Fail(msg, *args, **kwargs):
"""Log an error and the raise an RPCFail exception.
......@@ -364,11 +368,7 @@ def LeaveCluster():
try:
priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
f = open(pub_key, 'r')
try:
utils.RemoveAuthorizedKey(auth_keys, f.read(8192))
finally:
f.close()
utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
utils.RemoveFile(priv_key)
utils.RemoveFile(pub_key)
......@@ -406,11 +406,7 @@ def GetNodeInfo(vgname, hypervisor_type):
if hyp_info is not None:
outputarray.update(hyp_info)
f = open("/proc/sys/kernel/random/boot_id", 'r')
try:
outputarray["bootid"] = f.read(128).rstrip("\n")
finally:
f.close()
outputarray["bootid"] = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
return outputarray
......
......@@ -780,11 +780,7 @@ class BaseDRBD(BlockDev):
"""
try:
stat = open(filename, "r")
try:
data = stat.read().splitlines()
finally:
stat.close()
data = utils.ReadFile(filename).splitlines()
except EnvironmentError, err:
if err.errno == errno.ENOENT:
_ThrowError("The file %s cannot be opened, check if the module"
......
......@@ -63,11 +63,7 @@ def _InitSSHSetup():
raise errors.OpExecError("Could not generate ssh keypair, error %s" %
result.output)
f = open(pub_key, 'r')
try:
utils.AddAuthorizedKey(auth_keys, f.read(8192))
finally:
f.close()
utils.AddAuthorizedKey(auth_keys, utils.ReadFile(auth_keys))
def _GenerateSelfSignedSslCert(file_name, validity=(365 * 5)):
......@@ -240,11 +236,7 @@ def InitCluster(cluster_name, mac_prefix,
_InitGanetiServerSetup()
# set up ssh config and /etc/hosts
f = open(constants.SSH_HOST_RSA_PUB, 'r')
try:
sshline = f.read()
finally:
f.close()
sshline = utils.ReadFile(constants.SSH_HOST_RSA_PUB)
sshkey = sshline.split(" ")[1]
if modify_etc_hosts:
......
......@@ -2799,11 +2799,7 @@ class LUAddNode(LogicalUnit):
priv_key, pub_key]
for i in keyfiles:
f = open(i, 'r')
try:
keyarray.append(f.read())
finally:
f.close()
keyarray.append(utils.ReadFile(i))
result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
keyarray[2],
......
......@@ -1041,14 +1041,12 @@ class ConfigWriter:
"""Read the config data from disk.
"""
f = open(self._cfg_file, 'r')
raw_data = utils.ReadFile(self._cfg_file)
try:
try:
data = objects.ConfigData.FromDict(serializer.Load(f.read()))
except Exception, err:
raise errors.ConfigurationError(err)
finally:
f.close()
data = objects.ConfigData.FromDict(serializer.Load(raw_data))
except Exception, err:
raise errors.ConfigurationError(err)
# Make sure the configuration has the right version
_ValidateConfig(data)
......@@ -1113,16 +1111,9 @@ class ConfigWriter:
destination = self._cfg_file
self._BumpSerialNo()
txt = serializer.Dump(self._config_data.ToDict())
dir_name, file_name = os.path.split(destination)
fd, name = tempfile.mkstemp('.newconfig', file_name, dir_name)
f = os.fdopen(fd, 'w')
try:
f.write(txt)
os.fsync(f.fileno())
finally:
f.close()
# we don't need to do os.close(fd) as f.close() did it
os.rename(name, destination)
utils.WriteFile(destination, data=txt)
self.write_count += 1
# and redistribute the config file to master candidates
......
......@@ -661,11 +661,7 @@ class JobQueue(object):
for file_name in files:
# Read file content
fd = open(file_name, "r")
try:
content = fd.read()
finally:
fd.close()
content = utils.ReadFile(file_name)
result = rpc.RpcRunner.call_jobqueue_update([node_name],
[node.primary_ip],
......@@ -917,15 +913,13 @@ class JobQueue(object):
filepath = self._GetJobPath(job_id)
logging.debug("Loading job from %s", filepath)
try:
fd = open(filepath, "r")
raw_data = utils.ReadFile(filepath)
except IOError, err:
if err.errno in (errno.ENOENT, ):
return None
raise
try:
data = serializer.LoadJson(fd.read())
finally:
fd.close()
data = serializer.LoadJson(raw_data)
try:
job = _QueuedJob.Restore(self, data)
......
......@@ -63,6 +63,8 @@ debug_locks = False
#: when set to True, L{RunCmd} is disabled
no_fork = False
_RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid"
class RunResult(object):
"""Holds the result of running external programs.
......@@ -472,14 +474,14 @@ def ReadPidFile(pidfile):
"""
try:
pf = open(pidfile, 'r')
raw_data = ReadFile(pidfile)
except EnvironmentError, err:
if err.errno != errno.ENOENT:
logging.exception("Can't read pid file?!")
logging.exception("Can't read pid file")
return 0
try:
pid = int(pf.read())
pid = int(raw_data)
except ValueError, err:
logging.info("Can't parse pid file contents", exc_info=True)
return 0
......@@ -1150,11 +1152,7 @@ def NewUUID():
@rtype: str
"""
f = open("/proc/sys/kernel/random/uuid", "r")
try:
return f.read(128).rstrip("\n")
finally:
f.close()
return ReadFile(_RANDOM_UUID_FILE, size=128).rstrip("\n")
def GenerateSecret(numbytes=20):
......
......@@ -72,7 +72,7 @@ def GenericOpCodes(opts, args):
jex = cli.JobExecutor(cl=cl)
for fname in args:
op_data = simplejson.loads(open(fname).read())
op_data = simplejson.loads(utils.ReadFile(fname))
op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
jex.QueueJob("file %s" % fname, *op_list)
......
......@@ -443,9 +443,7 @@ def BatchCreate(opts, args):
json_filename = args[0]
try:
fd = open(json_filename, 'r')
instance_data = simplejson.load(fd)
fd.close()
instance_data = simplejson.loads(utils.ReadFile(json_filename))
except Exception, err:
ToStderr("Can't parse the instance definition file: %s" % str(err))
return 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment