Statistics
| Branch: | Tag: | Revision:

root / lib / utils / nodesetup.py @ 364c350f

History | View | Annotate | Download (3.4 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 ee045466 Michael Hanselmann
from ganeti import pathutils
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 eea3b572 Bernardo Dal Seno
  io.WriteFile(file_name, data=out.getvalue(), uid=0, gid=0, mode=0644,
66 eea3b572 Bernardo Dal Seno
               keep_perms=io.KP_IF_EXISTS)
67 17b97ab3 Michael Hanselmann
68 17b97ab3 Michael Hanselmann
69 17b97ab3 Michael Hanselmann
def AddHostToEtcHosts(hostname, ip):
70 17b97ab3 Michael Hanselmann
  """Wrapper around SetEtcHostsEntry.
71 17b97ab3 Michael Hanselmann

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

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

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

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

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

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

120 17b97ab3 Michael Hanselmann
  """
121 ee045466 Michael Hanselmann
  RemoveEtcHostsEntry(pathutils.ETC_HOSTS, hostname)
122 ee045466 Michael Hanselmann
  RemoveEtcHostsEntry(pathutils.ETC_HOSTS, hostname.split(".")[0])