summaryrefslogtreecommitdiffstats
path: root/com.gluster.storage.management.server.scripts/src/nodes/ServerUtils.py
blob: 1fec994c311c0b9892ad3f3818adde1f89d3f1e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#  Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com>
#  This file is part of Gluster Storage Platform.
#
#  Gluster Storage Platform is free software; you can redistribute it
#  and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 3 of
#  the License, or (at your option) any later version.
#
#  Gluster Storage Platform is distributed in the hope that it will be
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see
#  <http://www.gnu.org/licenses/>.

import os
import re
import subprocess
import glob
import Globals
from Protocol import *
from Utils import *

def isValidServer(serverName):
    for profile in getProfileList():
        if profile.ProfileName == "default" and profile.Active:
            if serverName == profile.DNS.Hostname:
                return True
    return False

def getHostname():
    for profile in getProfileList():
        if profile.ProfileName == "default" and profile.Active:
            return profile.DNS.Hostname
    return None

def getDomainName():
    try:
        domainName = open(Globals.DOMAINNAME_FILE).read()
    except IOError:
        return None
    return domainName.split()[0]

def replaceServerIp(fileName, findWhat, replaceWith):
    try:
        data = open(fileName).read()
        fp = open(fileName, "w")
        fp.write(re.sub(findWhat, replaceWith, data))
        fp.close()
        return True
    except IOError:
        return False
    except ValueError:
        return False
    except OSError:
        return False

def serverName2IpAddress(serverName):
    command = "dig %s | grep '^%s'" % (serverName, serverName)
    ps = subprocess.Popen(command,
                          shell=True,
                          stdout=subprocess.PIPE,
                          stdin=subprocess.PIPE,
                          stderr=subprocess.PIPE,
                          close_fds=True)
    ipAddress = serverName
    if ps.wait() == 0:
        output = ps.communicate()
        ipAddress = output[0].split()[-1]
    return ipAddress

def getInstallerIp():
    if not os.path.exists(Globals.INSTALLER_INFO_FILE):
        return None
    try:
        for line in open(Globals.INSTALLER_INFO_FILE):
            tokens = line.split("=")
            if tokens[0] == "IP-ADDRESS":
                return tokens[1].split(",")[0].strip()
    except IOError:
        syslog.syslog(syslog.LOG_ERR, "unable to read %s file" % Globals.INSTALLER_INFO_FILE)
    return False

def setInstallerIp(installerIp):
    try:
        open(Globals.INSTALLER_INFO_FILE, "w").write("IP-ADDRESS=%s\n" % installerIp)
        return True
    except IOError:
        log(syslog.LOG_ERR, "unable to create %s file" % Globals.INSTALLER_INFO_FILE)
    return False

def getCurrentServerName():
    try:
        for line in open(Globals.SYSCONFIG_NETWORK_FILE):
            tokens = line.split("=")
            if tokens[0] == "HOSTNAME":
                return tokens[1].strip()
    except IOError:
        syslog.syslog(syslog.LOG_ERR, "unable to read %s file" % Globals.SYSCONFIG_NETWORK_FILE)
    return False

def getLastAccessedNetwork(serverName):
    lastAccessedNetworkFile = ("/%s/servers/%s/%s" %
                               (Globals.GLUSTER_CONF_DIR, serverName, Globals.LAST_ACCESSED_NETWORK_FILE))
    try:
        return open(lastAccessedNetworkFile).read().strip()
    except IOError:
        log(syslog.LOG_ERR, "failed to read last accessed network file %s" % lastAccessedNetworkFile)
        pass
    return False

def setLastAccessedNetwork(serverName, ipAddress):
    lastAccessedNetworkFile = ("/%s/servers/%s/%s" %
                               (Globals.GLUSTER_CONF_DIR, serverName, Globals.LAST_ACCESSED_NETWORK_FILE))
    try:
        open(lastAccessedNetworkFile, "w").write(ipAddress.strip() + "\n")
    except IOError:
        log(syslog.LOG_ERR, "failed to write last accessed network file %s" % lastAccessedNetworkFile)
        return False
    return True

def getServerIpList(serverName, preferredNetworkOnly=False):
    networkXmlFile = ("%s/servers/%s/network.xml" % (Globals.GLUSTER_CONF_DIR, serverName))
    configDom = XDOM()
    if not configDom.parseFile(networkXmlFile):
        log(syslog.LOG_ERR, "failed to read %s file" % networkXmlFile)
        return None
    preferredNetwork = configDom.getTextByTagRoute("preferred-network")
    ipList = []
    interfaceDom = XDOM()
    for tagE in configDom.getElementsByTagName("interface"):
        interfaceDom.setDomObj(tagE)
        deviceName = interfaceDom.getTextByTagRoute("device")
        hostIp = interfaceDom.getTextByTagRoute("ipaddr")
        if not hostIp:
            continue
        if preferredNetworkOnly:
            if preferredNetwork.upper() == "ANY" or preferredNetwork.upper() == deviceName.upper():
                ipList.append(hostIp)
        else:
            ipList.append(hostIp)
    if preferredNetworkOnly:
        lastAccessedNetworkIp = getLastAccessedNetwork(serverName)
        if lastAccessedNetworkIp in ipList:
            ipList.remove(lastAccessedNetworkIp)
            ipList = [lastAccessedNetworkIp] + ipList
    return ipList

def getServerPreferredIpList(serverName):
    return getServerIpList(serverName, True)

def getExecuteServerList(serverList):
    executeServerList = {}
    for serverName in serverList:
        if serverName == Globals.INSTALLER_SERVER_NAME:
            installerIp = getInstallerIp()
            if installerIp:
                executeServerList[serverName] = [installerIp]
            continue
        executeServerList[serverName] = getServerPreferredIpList(serverName)
    return executeServerList

def getAllServerList():
    serverList = []
    for filePath in glob.glob("%s/servers/*" % Globals.GLUSTER_CONF_DIR):
        if os.path.isdir(filePath):
            serverList.append(os.path.basename(filePath))
    try:
        serverList.remove(Globals.INSTALLER_SERVER_NAME)
    except ValueError:
        pass
    return serverList

def getServerNetworkConfigFromLocalFile(serverName):
    configDom = XDOM()
    configDom.parseFile("%s/servers/%s/network.xml" % (Globals.GLUSTER_CONF_DIR, serverName))
    return configDom

def updateServerNetworkConfigXmlFile(serverName, serverNetworkDom):
    configDom = XDOM()
    serverTag = serverNetworkDom.getElementsByTagRoute("server")[0]
    configDom.setDomObj(serverTag)
    if not configDom.writexml("%s/%s/network.xml" % (Globals.SERVER_VOLUME_CONF_DIR, serverName)):
        log("Faild to write xml file %s/%s/network.xml" % (Globals.SERVER_VOLUME_CONF_DIR, serverName))

def compareServerNetworkDom(serverNetworkDomA, serverNetworkDomB, requestFlag=True):
    command = "command.server."
    if not requestFlag:
        command = ""
    sourceServer = {}
    tagText = serverNetworkDomA.getTextByTagRoute("name")
    if not tagText:
        taxText = None
    sourceServer["name"] = tagText
    tagText = serverNetworkDomA.getTextByTagRoute("domain-name")
    if not tagText:
        tagText = None
    sourceServer["domain-name"]   = tagText
    tagText = serverNetworkDomA.getTextByTagRoute("search-domain")
    if not tagText:
        tagText = None
    sourceServer["search-domain"] = tagText
    tagText = serverNetworkDomA.getTextByTagRoute("dns1")
    if not tagText:
        tagText = None
    sourceServer["dns1"] = tagText
    tagText = serverNetworkDomA.getTextByTagRoute("dns2")
    if not tagText:
        tagText = None
    sourceServer["dns2"] = tagText
    tagText = serverNetworkDomA.getTextByTagRoute("dns3")
    if not tagText:
        tagText = None
    sourceServer["dns3"] = tagText
    for tagE in serverNetworkDomA.getElementsByTagRoute("interface"):
        interfaceDom = XDOM()
        interfaceDom.setDomObj(tagE)
        sourceServerList = {}
        tagText = interfaceDom.getTextByTagRoute("description")
        if not tagText:
            tagText = None
        sourceServerList["description"] = tagText
        tagText = interfaceDom.getTextByTagRoute("hwaddr")
        if not tagText:
            tagText = None
        sourceServerList["hwaddr"] = tagText
        tagText = interfaceDom.getTextByTagRoute("onboot")
        if not tagText:
            tagText = None
        sourceServerList["onboot"] = tagText
        tagText = interfaceDom.getTextByTagRoute("bootproto")
        if not tagText:
            tagText = None
        sourceServerList["bootproto"] = tagText
        tagText = interfaceDom.getTextByTagRoute("ipaddr")
        if not tagText:
            tagText = None
        sourceServerList["ipaddr"]  = tagText
        tagText = interfaceDom.getTextByTagRoute("netmask")
        if not tagText:
            tagText = None
        sourceServerList["netmask"] = tagText
        tagText = interfaceDom.getTextByTagRoute("gateway")
        if not tagText:
            tagText = None
        sourceServerList["gateway"] = tagText
        sourceServer[interfaceDom.getTextByTagRoute("device")] = sourceServerList
    objServer = {}
    tagText = serverNetworkDomB.getTextByTagRoute(command + "name")
    if not tagText:
        taxText = None
    objServer["name"] = tagText
    tagText = serverNetworkDomB.getTextByTagRoute(command + "domain-name")
    if not tagText:
        tagText = None
    objServer["domain-name"]   = tagText
    tagText = serverNetworkDomB.getTextByTagRoute(command + "search-domain")
    if not tagText:
        tagText = None
    objServer["search-domain"] = tagText
    tagText = serverNetworkDomB.getTextByTagRoute(command + "dns1")
    if not tagText:
        tagText = None
    objServer["dns1"] = tagText
    tagText = serverNetworkDomB.getTextByTagRoute(command + "dns2")
    if not tagText:
        tagText = None
    objServer["dns2"] = tagText
    tagText = serverNetworkDomB.getTextByTagRoute(command + "dns3")
    if not tagText:
        tagText = None
    objServer["dns3"] = tagText
    for tagE in serverNetworkDomB.getElementsByTagRoute(command + "interface"):
        interfaceDom = XDOM()
        interfaceDom.setDomObj(tagE)
        objServerList = {}
        tagText = interfaceDom.getTextByTagRoute("description")
        if not tagText:
            tagText = None
        objServerList["description"] = tagText
        tagText = interfaceDom.getTextByTagRoute("hwaddr")
        if not tagText:
            tagText = None
        objServerList["hwaddr"] = tagText
        tagText = interfaceDom.getTextByTagRoute("onboot")
        if not tagText:
            tagText = None
        objServerList["onboot"] = tagText
        tagText = interfaceDom.getTextByTagRoute("bootproto")
        if not tagText:
            tagText = None
        objServerList["bootproto"] = tagText
        tagText = interfaceDom.getTextByTagRoute("ipaddr")
        if not tagText:
            tagText = None
        objServerList["ipaddr"]  = tagText
        tagText = interfaceDom.getTextByTagRoute("netmask")
        if not tagText:
            tagText = None
        objServerList["netmask"] = tagText
        tagText = interfaceDom.getTextByTagRoute("gateway")
        if not tagText:
            tagText = None
        objServerList["gateway"] = tagText
        objServer[interfaceDom.getTextByTagRoute("device")] = objServerList
    return sourceServer == objServer