Revision 24c855a7

b/lib/utils/nodesetup.py
23 23
"""
24 24

  
25 25
import os
26
from cStringIO import StringIO
26 27

  
27 28
from ganeti import constants
28 29

  
......
46 47
  # Ensure aliases are unique
47 48
  aliases = algo.UniqueSequence([hostname] + aliases)[1:]
48 49

  
49
  def _WriteEtcHosts(fd):
50
    # Duplicating file descriptor because os.fdopen's result will automatically
51
    # close the descriptor, but we would still like to have its functionality.
52
    out = os.fdopen(os.dup(fd), "w")
53
    try:
54
      for line in io.ReadFile(file_name).splitlines(True):
55
        fields = line.split()
56
        if fields and not fields[0].startswith("#") and ip == fields[0]:
57
          continue
58
        out.write(line)
50
  out = StringIO()
51
  for line in io.ReadFile(file_name).splitlines(True):
52
    fields = line.split()
53
    if fields and not fields[0].startswith("#") and ip == fields[0]:
54
      continue
55
    out.write(line)
59 56

  
60
      out.write("%s\t%s" % (ip, hostname))
61
      if aliases:
62
        out.write(" %s" % " ".join(aliases))
63
      out.write("\n")
64
      out.flush()
65
    finally:
66
      out.close()
57
  out.write("%s\t%s" % (ip, hostname))
58
  if aliases:
59
    out.write(" %s" % " ".join(aliases))
60
  out.write("\n")
67 61

  
68
  io.WriteFile(file_name, fn=_WriteEtcHosts, mode=0644)
62
  io.WriteFile(file_name, data=out.getvalue(), mode=0644)
69 63

  
70 64

  
71 65
def AddHostToEtcHosts(hostname, ip):
......
92 86
  @param hostname: the hostname to be removed
93 87

  
94 88
  """
95
  def _WriteEtcHosts(fd):
96
    # Duplicating file descriptor because os.fdopen's result will automatically
97
    # close the descriptor, but we would still like to have its functionality.
98
    out = os.fdopen(os.dup(fd), "w")
99
    try:
100
      for line in io.ReadFile(file_name).splitlines(True):
101
        fields = line.split()
102
        if len(fields) > 1 and not fields[0].startswith("#"):
103
          names = fields[1:]
104
          if hostname in names:
105
            while hostname in names:
106
              names.remove(hostname)
107
            if names:
108
              out.write("%s %s\n" % (fields[0], " ".join(names)))
109
            continue
110

  
111
        out.write(line)
112

  
113
      out.flush()
114
    finally:
115
      out.close()
116

  
117
  io.WriteFile(file_name, fn=_WriteEtcHosts, mode=0644)
89
  out = StringIO()
90

  
91
  for line in io.ReadFile(file_name).splitlines(True):
92
    fields = line.split()
93
    if len(fields) > 1 and not fields[0].startswith("#"):
94
      names = fields[1:]
95
      if hostname in names:
96
        while hostname in names:
97
          names.remove(hostname)
98
        if names:
99
          out.write("%s %s\n" % (fields[0], " ".join(names)))
100
        continue
101

  
102
    out.write(line)
103

  
104
  io.WriteFile(file_name, data=out.getvalue(), mode=0644)
118 105

  
119 106

  
120 107
def RemoveHostFromEtcHosts(hostname):

Also available in: Unified diff