Revision 13998ef2

b/lib/backend.py
51 51
from ganeti import ssconf
52 52

  
53 53

  
54
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
55

  
56

  
54 57
class RPCFail(Exception):
55 58
  """Class denoting RPC failure.
56 59

  
......
58 61

  
59 62
  """
60 63

  
64

  
61 65
def _Fail(msg, *args, **kwargs):
62 66
  """Log an error and the raise an RPCFail exception.
63 67

  
......
364 368
  try:
365 369
    priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
366 370

  
367
    f = open(pub_key, 'r')
368
    try:
369
      utils.RemoveAuthorizedKey(auth_keys, f.read(8192))
370
    finally:
371
      f.close()
371
    utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
372 372

  
373 373
    utils.RemoveFile(priv_key)
374 374
    utils.RemoveFile(pub_key)
......
406 406
  if hyp_info is not None:
407 407
    outputarray.update(hyp_info)
408 408

  
409
  f = open("/proc/sys/kernel/random/boot_id", 'r')
410
  try:
411
    outputarray["bootid"] = f.read(128).rstrip("\n")
412
  finally:
413
    f.close()
409
  outputarray["bootid"] = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
414 410

  
415 411
  return outputarray
416 412

  
b/lib/bdev.py
780 780

  
781 781
    """
782 782
    try:
783
      stat = open(filename, "r")
784
      try:
785
        data = stat.read().splitlines()
786
      finally:
787
        stat.close()
783
      data = utils.ReadFile(filename).splitlines()
788 784
    except EnvironmentError, err:
789 785
      if err.errno == errno.ENOENT:
790 786
        _ThrowError("The file %s cannot be opened, check if the module"
b/lib/bootstrap.py
63 63
    raise errors.OpExecError("Could not generate ssh keypair, error %s" %
64 64
                             result.output)
65 65

  
66
  f = open(pub_key, 'r')
67
  try:
68
    utils.AddAuthorizedKey(auth_keys, f.read(8192))
69
  finally:
70
    f.close()
66
  utils.AddAuthorizedKey(auth_keys, utils.ReadFile(auth_keys))
71 67

  
72 68

  
73 69
def _GenerateSelfSignedSslCert(file_name, validity=(365 * 5)):
......
240 236
  _InitGanetiServerSetup()
241 237

  
242 238
  # set up ssh config and /etc/hosts
243
  f = open(constants.SSH_HOST_RSA_PUB, 'r')
244
  try:
245
    sshline = f.read()
246
  finally:
247
    f.close()
239
  sshline = utils.ReadFile(constants.SSH_HOST_RSA_PUB)
248 240
  sshkey = sshline.split(" ")[1]
249 241

  
250 242
  if modify_etc_hosts:
b/lib/cmdlib.py
2799 2799
                priv_key, pub_key]
2800 2800

  
2801 2801
    for i in keyfiles:
2802
      f = open(i, 'r')
2803
      try:
2804
        keyarray.append(f.read())
2805
      finally:
2806
        f.close()
2802
      keyarray.append(utils.ReadFile(i))
2807 2803

  
2808 2804
    result = self.rpc.call_node_add(node, keyarray[0], keyarray[1],
2809 2805
                                    keyarray[2],
b/lib/config.py
1041 1041
    """Read the config data from disk.
1042 1042

  
1043 1043
    """
1044
    f = open(self._cfg_file, 'r')
1044
    raw_data = utils.ReadFile(self._cfg_file)
1045

  
1045 1046
    try:
1046
      try:
1047
        data = objects.ConfigData.FromDict(serializer.Load(f.read()))
1048
      except Exception, err:
1049
        raise errors.ConfigurationError(err)
1050
    finally:
1051
      f.close()
1047
      data = objects.ConfigData.FromDict(serializer.Load(raw_data))
1048
    except Exception, err:
1049
      raise errors.ConfigurationError(err)
1052 1050

  
1053 1051
    # Make sure the configuration has the right version
1054 1052
    _ValidateConfig(data)
......
1113 1111
      destination = self._cfg_file
1114 1112
    self._BumpSerialNo()
1115 1113
    txt = serializer.Dump(self._config_data.ToDict())
1116
    dir_name, file_name = os.path.split(destination)
1117
    fd, name = tempfile.mkstemp('.newconfig', file_name, dir_name)
1118
    f = os.fdopen(fd, 'w')
1119
    try:
1120
      f.write(txt)
1121
      os.fsync(f.fileno())
1122
    finally:
1123
      f.close()
1124
    # we don't need to do os.close(fd) as f.close() did it
1125
    os.rename(name, destination)
1114

  
1115
    utils.WriteFile(destination, data=txt)
1116

  
1126 1117
    self.write_count += 1
1127 1118

  
1128 1119
    # and redistribute the config file to master candidates
b/lib/jqueue.py
661 661

  
662 662
    for file_name in files:
663 663
      # Read file content
664
      fd = open(file_name, "r")
665
      try:
666
        content = fd.read()
667
      finally:
668
        fd.close()
664
      content = utils.ReadFile(file_name)
669 665

  
670 666
      result = rpc.RpcRunner.call_jobqueue_update([node_name],
671 667
                                                  [node.primary_ip],
......
917 913
    filepath = self._GetJobPath(job_id)
918 914
    logging.debug("Loading job from %s", filepath)
919 915
    try:
920
      fd = open(filepath, "r")
916
      raw_data = utils.ReadFile(filepath)
921 917
    except IOError, err:
922 918
      if err.errno in (errno.ENOENT, ):
923 919
        return None
924 920
      raise
925
    try:
926
      data = serializer.LoadJson(fd.read())
927
    finally:
928
      fd.close()
921

  
922
    data = serializer.LoadJson(raw_data)
929 923

  
930 924
    try:
931 925
      job = _QueuedJob.Restore(self, data)
b/lib/utils.py
63 63
#: when set to True, L{RunCmd} is disabled
64 64
no_fork = False
65 65

  
66
_RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid"
67

  
66 68

  
67 69
class RunResult(object):
68 70
  """Holds the result of running external programs.
......
472 474

  
473 475
  """
474 476
  try:
475
    pf = open(pidfile, 'r')
477
    raw_data = ReadFile(pidfile)
476 478
  except EnvironmentError, err:
477 479
    if err.errno != errno.ENOENT:
478
      logging.exception("Can't read pid file?!")
480
      logging.exception("Can't read pid file")
479 481
    return 0
480 482

  
481 483
  try:
482
    pid = int(pf.read())
484
    pid = int(raw_data)
483 485
  except ValueError, err:
484 486
    logging.info("Can't parse pid file contents", exc_info=True)
485 487
    return 0
......
1150 1152
  @rtype: str
1151 1153

  
1152 1154
  """
1153
  f = open("/proc/sys/kernel/random/uuid", "r")
1154
  try:
1155
    return f.read(128).rstrip("\n")
1156
  finally:
1157
    f.close()
1155
  return ReadFile(_RANDOM_UUID_FILE, size=128).rstrip("\n")
1158 1156

  
1159 1157

  
1160 1158
def GenerateSecret(numbytes=20):
b/scripts/gnt-debug
72 72
  jex = cli.JobExecutor(cl=cl)
73 73

  
74 74
  for fname in args:
75
    op_data = simplejson.loads(open(fname).read())
75
    op_data = simplejson.loads(utils.ReadFile(fname))
76 76
    op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
77 77
    jex.QueueJob("file %s" % fname, *op_list)
78 78

  
b/scripts/gnt-instance
443 443

  
444 444
  json_filename = args[0]
445 445
  try:
446
    fd = open(json_filename, 'r')
447
    instance_data = simplejson.load(fd)
448
    fd.close()
446
    instance_data = simplejson.loads(utils.ReadFile(json_filename))
449 447
  except Exception, err:
450 448
    ToStderr("Can't parse the instance definition file: %s" % str(err))
451 449
    return 1

Also available in: Unified diff