Revision 8f9a87c5

b/lib/utils/nodesetup.py
45 45

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

  
50 50
  out = StringIO()
51

  
52
  def _write_entry(written):
53
    if not written:
54
      out.write("%s\t%s\n" % (ip, " ".join(names)))
55
    return True
56

  
57
  written = False
51 58
  for line in io.ReadFile(file_name).splitlines(True):
52 59
    fields = line.split()
53 60
    if fields and not fields[0].startswith("#") and ip == fields[0]:
54
      continue
55
    out.write(line)
56

  
57
  out.write("%s\t%s" % (ip, hostname))
58
  if aliases:
59
    out.write(" %s" % " ".join(aliases))
60
  out.write("\n")
61
      written = _write_entry(written)
62
    else:
63
      out.write(line)
64
  _write_entry(written)
61 65

  
62 66
  io.WriteFile(file_name, data=out.getvalue(), mode=0644)
63 67

  
b/test/ganeti.utils.nodesetup_unittest.py
1 1
#!/usr/bin/python
2 2
#
3 3

  
4
# Copyright (C) 2006, 2007, 2010 Google Inc.
4
# Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
76 76
      "198.51.100.4\tmyhost\n")
77 77
    self.assertFileMode(self.tmpname, 0644)
78 78

  
79
  def testSettingOrdering(self):
80
    utils.SetEtcHostsEntry(self.tmpname, "127.0.0.1", "localhost.localdomain",
81
                           ["localhost"])
82

  
83
    self.assertFileContent(self.tmpname,
84
      "# This is a test file for /etc/hosts\n"
85
      "127.0.0.1\tlocalhost.localdomain localhost\n"
86
      "192.0.2.1 router gw\n")
87
    self.assertFileMode(self.tmpname, 0644)
88

  
79 89
  def testRemovingExistingHost(self):
80 90
    utils.RemoveEtcHostsEntry(self.tmpname, "router")
81 91

  

Also available in: Unified diff