Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.3 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
  names = algo.UniqueSequence([hostname] + aliases)
49

    
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
58
  for line in io.ReadFile(file_name).splitlines(True):
59
    fields = line.split()
60
    if fields and not fields[0].startswith("#") and ip == fields[0]:
61
      written = _write_entry(written)
62
    else:
63
      out.write(line)
64
  _write_entry(written)
65

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

    
68

    
69
def AddHostToEtcHosts(hostname, ip):
70
  """Wrapper around SetEtcHostsEntry.
71

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

78
  """
79
  SetEtcHostsEntry(constants.ETC_HOSTS, ip, hostname, [hostname.split(".")[0]])
80

    
81

    
82
def RemoveEtcHostsEntry(file_name, hostname):
83
  """Removes a hostname from /etc/hosts.
84

85
  IP addresses without names are removed from the file.
86

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

92
  """
93
  out = StringIO()
94

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

    
106
    out.write(line)
107

    
108
  io.WriteFile(file_name, data=out.getvalue(), mode=0644)
109

    
110

    
111
def RemoveHostFromEtcHosts(hostname):
112
  """Wrapper around RemoveEtcHostsEntry.
113

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

119
  """
120
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname)
121
  RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname.split(".")[0])