Revision 17b97ab3 lib/utils/__init__.py
b/lib/utils/__init__.py | ||
---|---|---|
62 | 62 |
from ganeti.utils.filelock import * # pylint: disable-msg=W0401 |
63 | 63 |
from ganeti.utils.io import * # pylint: disable-msg=W0401 |
64 | 64 |
from ganeti.utils.x509 import * # pylint: disable-msg=W0401 |
65 |
from ganeti.utils.nodesetup import * # pylint: disable-msg=W0401 |
|
65 | 66 |
|
66 | 67 |
|
67 | 68 |
#: when set to True, L{RunCmd} is disabled |
... | ... | |
1076 | 1077 |
return cpu_list |
1077 | 1078 |
|
1078 | 1079 |
|
1079 |
def SetEtcHostsEntry(file_name, ip, hostname, aliases): |
|
1080 |
"""Sets the name of an IP address and hostname in /etc/hosts. |
|
1081 |
|
|
1082 |
@type file_name: str |
|
1083 |
@param file_name: path to the file to modify (usually C{/etc/hosts}) |
|
1084 |
@type ip: str |
|
1085 |
@param ip: the IP address |
|
1086 |
@type hostname: str |
|
1087 |
@param hostname: the hostname to be added |
|
1088 |
@type aliases: list |
|
1089 |
@param aliases: the list of aliases to add for the hostname |
|
1090 |
|
|
1091 |
""" |
|
1092 |
# Ensure aliases are unique |
|
1093 |
aliases = UniqueSequence([hostname] + aliases)[1:] |
|
1094 |
|
|
1095 |
def _WriteEtcHosts(fd): |
|
1096 |
# Duplicating file descriptor because os.fdopen's result will automatically |
|
1097 |
# close the descriptor, but we would still like to have its functionality. |
|
1098 |
out = os.fdopen(os.dup(fd), "w") |
|
1099 |
try: |
|
1100 |
for line in ReadFile(file_name).splitlines(True): |
|
1101 |
fields = line.split() |
|
1102 |
if fields and not fields[0].startswith("#") and ip == fields[0]: |
|
1103 |
continue |
|
1104 |
out.write(line) |
|
1105 |
|
|
1106 |
out.write("%s\t%s" % (ip, hostname)) |
|
1107 |
if aliases: |
|
1108 |
out.write(" %s" % " ".join(aliases)) |
|
1109 |
out.write("\n") |
|
1110 |
out.flush() |
|
1111 |
finally: |
|
1112 |
out.close() |
|
1113 |
|
|
1114 |
WriteFile(file_name, fn=_WriteEtcHosts, mode=0644) |
|
1115 |
|
|
1116 |
|
|
1117 |
def AddHostToEtcHosts(hostname, ip): |
|
1118 |
"""Wrapper around SetEtcHostsEntry. |
|
1119 |
|
|
1120 |
@type hostname: str |
|
1121 |
@param hostname: a hostname that will be resolved and added to |
|
1122 |
L{constants.ETC_HOSTS} |
|
1123 |
@type ip: str |
|
1124 |
@param ip: The ip address of the host |
|
1125 |
|
|
1126 |
""" |
|
1127 |
SetEtcHostsEntry(constants.ETC_HOSTS, ip, hostname, [hostname.split(".")[0]]) |
|
1128 |
|
|
1129 |
|
|
1130 |
def RemoveEtcHostsEntry(file_name, hostname): |
|
1131 |
"""Removes a hostname from /etc/hosts. |
|
1132 |
|
|
1133 |
IP addresses without names are removed from the file. |
|
1134 |
|
|
1135 |
@type file_name: str |
|
1136 |
@param file_name: path to the file to modify (usually C{/etc/hosts}) |
|
1137 |
@type hostname: str |
|
1138 |
@param hostname: the hostname to be removed |
|
1139 |
|
|
1140 |
""" |
|
1141 |
def _WriteEtcHosts(fd): |
|
1142 |
# Duplicating file descriptor because os.fdopen's result will automatically |
|
1143 |
# close the descriptor, but we would still like to have its functionality. |
|
1144 |
out = os.fdopen(os.dup(fd), "w") |
|
1145 |
try: |
|
1146 |
for line in ReadFile(file_name).splitlines(True): |
|
1147 |
fields = line.split() |
|
1148 |
if len(fields) > 1 and not fields[0].startswith("#"): |
|
1149 |
names = fields[1:] |
|
1150 |
if hostname in names: |
|
1151 |
while hostname in names: |
|
1152 |
names.remove(hostname) |
|
1153 |
if names: |
|
1154 |
out.write("%s %s\n" % (fields[0], " ".join(names))) |
|
1155 |
continue |
|
1156 |
|
|
1157 |
out.write(line) |
|
1158 |
|
|
1159 |
out.flush() |
|
1160 |
finally: |
|
1161 |
out.close() |
|
1162 |
|
|
1163 |
WriteFile(file_name, fn=_WriteEtcHosts, mode=0644) |
|
1164 |
|
|
1165 |
|
|
1166 |
def RemoveHostFromEtcHosts(hostname): |
|
1167 |
"""Wrapper around RemoveEtcHostsEntry. |
|
1168 |
|
|
1169 |
@type hostname: str |
|
1170 |
@param hostname: hostname that will be resolved and its |
|
1171 |
full and shot name will be removed from |
|
1172 |
L{constants.ETC_HOSTS} |
|
1173 |
|
|
1174 |
""" |
|
1175 |
RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname) |
|
1176 |
RemoveEtcHostsEntry(constants.ETC_HOSTS, hostname.split(".")[0]) |
|
1177 |
|
|
1178 |
|
|
1179 | 1080 |
def GetHomeDir(user, default=None): |
1180 | 1081 |
"""Try to get the homedir of the given user. |
1181 | 1082 |
|
Also available in: Unified diff