rlib2: Convert /2/groups to OpcodeResource
[ganeti-local] / lib / utils / nodesetup.py
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 from cStringIO import StringIO
26
27 from ganeti import constants
28
29 from ganeti.utils import algo
30 from ganeti.utils import io
31
32
33 def SetEtcHostsEntry(file_name, ip, hostname, aliases):
34   """Sets the name of an IP address and hostname in /etc/hosts.
35
36   @type file_name: str
37   @param file_name: path to the file to modify (usually C{/etc/hosts})
38   @type ip: str
39   @param ip: the IP address
40   @type hostname: str
41   @param hostname: the hostname to be added
42   @type aliases: list
43   @param aliases: the list of aliases to add for the hostname
44
45   """
46   # Ensure aliases are unique
47   names = algo.UniqueSequence([hostname] + aliases)
48
49   out = StringIO()
50
51   def _write_entry(written):
52     if not written:
53       out.write("%s\t%s\n" % (ip, " ".join(names)))
54     return True
55
56   written = False
57   for line in io.ReadFile(file_name).splitlines(True):
58     fields = line.split()
59     if fields and not fields[0].startswith("#") and ip == fields[0]:
60       written = _write_entry(written)
61     else:
62       out.write(line)
63   _write_entry(written)
64
65   io.WriteFile(file_name, data=out.getvalue(), mode=0644)
66
67
68 def AddHostToEtcHosts(hostname, ip):
69   """Wrapper around SetEtcHostsEntry.
70
71   @type hostname: str
72   @param hostname: a hostname that will be resolved and added to
73       L{constants.ETC_HOSTS}
74   @type ip: str
75   @param ip: The ip address of the host
76
77   """
78   SetEtcHostsEntry(constants.ETC_HOSTS, ip, hostname, [hostname.split(".")[0]])
79
80
81 def RemoveEtcHostsEntry(file_name, hostname):
82   """Removes a hostname from /etc/hosts.
83
84   IP addresses without names are removed from the file.
85
86   @type file_name: str
87   @param file_name: path to the file to modify (usually C{/etc/hosts})
88   @type hostname: str
89   @param hostname: the hostname to be removed
90
91   """
92   out = StringIO()
93
94   for line in io.ReadFile(file_name).splitlines(True):
95     fields = line.split()
96     if len(fields) > 1 and not fields[0].startswith("#"):
97       names = fields[1:]
98       if hostname in names:
99         while hostname in names:
100           names.remove(hostname)
101         if names:
102           out.write("%s %s\n" % (fields[0], " ".join(names)))
103         continue
104
105     out.write(line)
106
107   io.WriteFile(file_name, data=out.getvalue(), mode=0644)
108
109
110 def RemoveHostFromEtcHosts(hostname):
111   """Wrapper around RemoveEtcHostsEntry.
112
113   @type hostname: str
114   @param hostname: hostname that will be resolved and its
115       full and shot name will be removed from
116       L{constants.ETC_HOSTS}
117
118   """
119   RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname)
120   RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname.split(".")[0])