Statistics
| Branch: | Tag: | Revision:

root / lib / utils / nodesetup.py @ 8dc76d54

History | View | Annotate | Download (3.3 kB)

1 17b97ab3 Michael Hanselmann
#
2 17b97ab3 Michael Hanselmann
#
3 17b97ab3 Michael Hanselmann
4 17b97ab3 Michael Hanselmann
# Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
5 17b97ab3 Michael Hanselmann
#
6 17b97ab3 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 17b97ab3 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 17b97ab3 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 17b97ab3 Michael Hanselmann
# (at your option) any later version.
10 17b97ab3 Michael Hanselmann
#
11 17b97ab3 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 17b97ab3 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 17b97ab3 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 17b97ab3 Michael Hanselmann
# General Public License for more details.
15 17b97ab3 Michael Hanselmann
#
16 17b97ab3 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 17b97ab3 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 17b97ab3 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 17b97ab3 Michael Hanselmann
# 02110-1301, USA.
20 17b97ab3 Michael Hanselmann
21 17b97ab3 Michael Hanselmann
"""Utility functions for manipulating /etc/hosts.
22 17b97ab3 Michael Hanselmann

23 17b97ab3 Michael Hanselmann
"""
24 17b97ab3 Michael Hanselmann
25 24c855a7 Iustin Pop
from cStringIO import StringIO
26 17b97ab3 Michael Hanselmann
27 17b97ab3 Michael Hanselmann
from ganeti import constants
28 17b97ab3 Michael Hanselmann
29 17b97ab3 Michael Hanselmann
from ganeti.utils import algo
30 17b97ab3 Michael Hanselmann
from ganeti.utils import io
31 17b97ab3 Michael Hanselmann
32 17b97ab3 Michael Hanselmann
33 17b97ab3 Michael Hanselmann
def SetEtcHostsEntry(file_name, ip, hostname, aliases):
34 17b97ab3 Michael Hanselmann
  """Sets the name of an IP address and hostname in /etc/hosts.
35 17b97ab3 Michael Hanselmann

36 17b97ab3 Michael Hanselmann
  @type file_name: str
37 17b97ab3 Michael Hanselmann
  @param file_name: path to the file to modify (usually C{/etc/hosts})
38 17b97ab3 Michael Hanselmann
  @type ip: str
39 17b97ab3 Michael Hanselmann
  @param ip: the IP address
40 17b97ab3 Michael Hanselmann
  @type hostname: str
41 17b97ab3 Michael Hanselmann
  @param hostname: the hostname to be added
42 17b97ab3 Michael Hanselmann
  @type aliases: list
43 17b97ab3 Michael Hanselmann
  @param aliases: the list of aliases to add for the hostname
44 17b97ab3 Michael Hanselmann

45 17b97ab3 Michael Hanselmann
  """
46 17b97ab3 Michael Hanselmann
  # Ensure aliases are unique
47 8f9a87c5 Iustin Pop
  names = algo.UniqueSequence([hostname] + aliases)
48 17b97ab3 Michael Hanselmann
49 24c855a7 Iustin Pop
  out = StringIO()
50 8f9a87c5 Iustin Pop
51 8f9a87c5 Iustin Pop
  def _write_entry(written):
52 8f9a87c5 Iustin Pop
    if not written:
53 8f9a87c5 Iustin Pop
      out.write("%s\t%s\n" % (ip, " ".join(names)))
54 8f9a87c5 Iustin Pop
    return True
55 8f9a87c5 Iustin Pop
56 8f9a87c5 Iustin Pop
  written = False
57 24c855a7 Iustin Pop
  for line in io.ReadFile(file_name).splitlines(True):
58 24c855a7 Iustin Pop
    fields = line.split()
59 24c855a7 Iustin Pop
    if fields and not fields[0].startswith("#") and ip == fields[0]:
60 8f9a87c5 Iustin Pop
      written = _write_entry(written)
61 8f9a87c5 Iustin Pop
    else:
62 8f9a87c5 Iustin Pop
      out.write(line)
63 8f9a87c5 Iustin Pop
  _write_entry(written)
64 17b97ab3 Michael Hanselmann
65 24c855a7 Iustin Pop
  io.WriteFile(file_name, data=out.getvalue(), mode=0644)
66 17b97ab3 Michael Hanselmann
67 17b97ab3 Michael Hanselmann
68 17b97ab3 Michael Hanselmann
def AddHostToEtcHosts(hostname, ip):
69 17b97ab3 Michael Hanselmann
  """Wrapper around SetEtcHostsEntry.
70 17b97ab3 Michael Hanselmann

71 17b97ab3 Michael Hanselmann
  @type hostname: str
72 17b97ab3 Michael Hanselmann
  @param hostname: a hostname that will be resolved and added to
73 17b97ab3 Michael Hanselmann
      L{constants.ETC_HOSTS}
74 17b97ab3 Michael Hanselmann
  @type ip: str
75 17b97ab3 Michael Hanselmann
  @param ip: The ip address of the host
76 17b97ab3 Michael Hanselmann

77 17b97ab3 Michael Hanselmann
  """
78 17b97ab3 Michael Hanselmann
  SetEtcHostsEntry(constants.ETC_HOSTS, ip, hostname, [hostname.split(".")[0]])
79 17b97ab3 Michael Hanselmann
80 17b97ab3 Michael Hanselmann
81 17b97ab3 Michael Hanselmann
def RemoveEtcHostsEntry(file_name, hostname):
82 17b97ab3 Michael Hanselmann
  """Removes a hostname from /etc/hosts.
83 17b97ab3 Michael Hanselmann

84 17b97ab3 Michael Hanselmann
  IP addresses without names are removed from the file.
85 17b97ab3 Michael Hanselmann

86 17b97ab3 Michael Hanselmann
  @type file_name: str
87 17b97ab3 Michael Hanselmann
  @param file_name: path to the file to modify (usually C{/etc/hosts})
88 17b97ab3 Michael Hanselmann
  @type hostname: str
89 17b97ab3 Michael Hanselmann
  @param hostname: the hostname to be removed
90 17b97ab3 Michael Hanselmann

91 17b97ab3 Michael Hanselmann
  """
92 24c855a7 Iustin Pop
  out = StringIO()
93 24c855a7 Iustin Pop
94 24c855a7 Iustin Pop
  for line in io.ReadFile(file_name).splitlines(True):
95 24c855a7 Iustin Pop
    fields = line.split()
96 24c855a7 Iustin Pop
    if len(fields) > 1 and not fields[0].startswith("#"):
97 24c855a7 Iustin Pop
      names = fields[1:]
98 24c855a7 Iustin Pop
      if hostname in names:
99 24c855a7 Iustin Pop
        while hostname in names:
100 24c855a7 Iustin Pop
          names.remove(hostname)
101 24c855a7 Iustin Pop
        if names:
102 24c855a7 Iustin Pop
          out.write("%s %s\n" % (fields[0], " ".join(names)))
103 24c855a7 Iustin Pop
        continue
104 24c855a7 Iustin Pop
105 24c855a7 Iustin Pop
    out.write(line)
106 24c855a7 Iustin Pop
107 24c855a7 Iustin Pop
  io.WriteFile(file_name, data=out.getvalue(), mode=0644)
108 17b97ab3 Michael Hanselmann
109 17b97ab3 Michael Hanselmann
110 17b97ab3 Michael Hanselmann
def RemoveHostFromEtcHosts(hostname):
111 17b97ab3 Michael Hanselmann
  """Wrapper around RemoveEtcHostsEntry.
112 17b97ab3 Michael Hanselmann

113 17b97ab3 Michael Hanselmann
  @type hostname: str
114 17b97ab3 Michael Hanselmann
  @param hostname: hostname that will be resolved and its
115 17b97ab3 Michael Hanselmann
      full and shot name will be removed from
116 17b97ab3 Michael Hanselmann
      L{constants.ETC_HOSTS}
117 17b97ab3 Michael Hanselmann

118 17b97ab3 Michael Hanselmann
  """
119 17b97ab3 Michael Hanselmann
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname)
120 17b97ab3 Michael Hanselmann
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname.split(".")[0])