summaryrefslogtreecommitdiffstats
path: root/com.gluster.storage.management.server.scripts/src/GetServerNetworkConfig.py
blob: 065916f236503efcad941d48f621038076a1fb2a (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
#  Copyright (C) 2009 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 sys
import syslog
import socket
import Globals
import re
from ServerUtils import *
from XmlHandler import *

from netconfpkg.NCHardwareList import getHardwareList
from netconfpkg.NCProfileList import getProfileList
from netconfpkg.NCDevice import Device
from GetNetDeviceList import getNetDeviceList

def getServerNetworkConfig():
    serverName = socket.gethostname()

    if not serverName:
        return ResponseXml.errorResponse("Unable to get server name!")

    ProfileList = getProfileList()
    DNS = None
    for profile in ProfileList:
        if profile.ProfileName == "default" and profile.Active:
            DNS = profile.DNS
        break

    if not serverName == DNS.Hostname:
        return ResponseXml.errorResponse("Unable to get server details")

    responseDom = ResponseXml()
    responseDom.appendTagRoute("status.code", "0"); 
    responseDom.appendTagRoute("status.message", "SUCCESS")
    serverTag = responseDom.appendTagRoute("server.name", serverName)
    networkInterfaces  = responseDom.appendTagRoute("server.networkInterfaces", None)

    #hardwareList = getHardwareList()
    #serverTag.appendChild(responseDom.createTag("domainname", getDomainName()))
    #serverTag.appendChild(responseDom.createTag("dns1", DNS.PrimaryDNS))
    #serverTag.appendChild(responseDom.createTag("dns2", DNS.SecondaryDNS))
    #serverTag.appendChild(responseDom.createTag("dns3", DNS.TertiaryDNS))

    deviceList = {}
    for device in getNetDeviceList():
        deviceList[device["device"]] = device
    for device in getHardwareList():
        try:
            macAddress = open("/sys/class/net/%s/address" % device.Name).read().strip()
        except IOError:
            continue
        interfaceTag = responseDom.createTag("networkInterface", device.Name)
        if deviceList[device.Name]:
            interfaceTag.appendChild(responseDom.createTag("ipAddress", deviceList[device.Name]["ipaddr"]))
            interfaceTag.appendChild(responseDom.createTag("netMask", deviceList[device.Name]["netmask"]))
            interfaceTag.appendChild(responseDom.createTag("defaultGateway", deviceList[device.Name]["gateway"]))
            interfaceTag.appendChild(responseDom.createTag("isPreferred", None))
        serverTag.appendChild(interfaceTag)
    responseDom.appendTag(serverTag)
    return responseDom