Statistics
| Branch: | Tag: | Revision:

root / lib / utils / nodesetup.py @ 24c855a7

History | View | Annotate | Download (3.2 kB)

1
#
2
#
3

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

    
21
"""Utility functions for manipulating /etc/hosts.
22

23
"""
24

    
25
import os
26
from cStringIO import StringIO
27

    
28
from ganeti import constants
29

    
30
from ganeti.utils import algo
31
from ganeti.utils import io
32

    
33

    
34
def SetEtcHostsEntry(file_name, ip, hostname, aliases):
35
  """Sets the name of an IP address and hostname in /etc/hosts.
36

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

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

    
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)
56

    
57
  out.write("%s\t%s" % (ip, hostname))
58
  if aliases:
59
    out.write(" %s" % " ".join(aliases))
60
  out.write("\n")
61

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

    
64

    
65
def AddHostToEtcHosts(hostname, ip):
66
  """Wrapper around SetEtcHostsEntry.
67

68
  @type hostname: str
69
  @param hostname: a hostname that will be resolved and added to
70
      L{constants.ETC_HOSTS}
71
  @type ip: str
72
  @param ip: The ip address of the host
73

74
  """
75
  SetEtcHostsEntry(constants.ETC_HOSTS, ip, hostname, [hostname.split(".")[0]])
76

    
77

    
78
def RemoveEtcHostsEntry(file_name, hostname):
79
  """Removes a hostname from /etc/hosts.
80

81
  IP addresses without names are removed from the file.
82

83
  @type file_name: str
84
  @param file_name: path to the file to modify (usually C{/etc/hosts})
85
  @type hostname: str
86
  @param hostname: the hostname to be removed
87

88
  """
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)
105

    
106

    
107
def RemoveHostFromEtcHosts(hostname):
108
  """Wrapper around RemoveEtcHostsEntry.
109

110
  @type hostname: str
111
  @param hostname: hostname that will be resolved and its
112
      full and shot name will be removed from
113
      L{constants.ETC_HOSTS}
114

115
  """
116
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname)
117
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname.split(".")[0])