summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.gateway/WebContent/scripts/Utils.py
blob: 82f8b7b6e974252c71f82e4e01d47512ac325525 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
#  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 sys
p1 = os.path.abspath(os.path.dirname(sys.argv[0]))
p2 = "%s/common" % os.path.dirname(p1)
if not p1 in sys.path:
    sys.path.append(p1)
if not p2 in sys.path:
    sys.path.append(p2)
import re
import socket
import struct
import syslog
import subprocess
#import spwd
import time
#import uuid
import tempfile
import grp
import pwd
import inspect
from datetime import datetime
import urllib

import Globals
import Protocol

RUN_COMMAND_ERROR = -1024
LOG_SYSLOG = 1
SYSLOG_REQUIRED = False
LOG_FILE_NAME = None
LOG_FILE_OBJ = None


def _getLogCode(priority):
    if syslog.LOG_EMERG == priority:
        return "M"
    elif syslog.LOG_ALERT == priority:
        return "A"
    elif syslog.LOG_CRIT == priority:
        return "C"
    elif syslog.LOG_ERR == priority:
        return "E"
    elif syslog.LOG_WARNING == priority:
        return "W"
    elif syslog.LOG_NOTICE == priority:
        return "N"
    elif syslog.LOG_INFO == priority:
        return "I"
    elif syslog.LOG_DEBUG == priority:
        return "D"
    else: # UNKNOWN
        return "X"


def setLogFile(fileName):
    global LOG_FILE_NAME

    if fileName:
        LOG_FILE_NAME = fileName
        return True
    return False


def closeLog():
    global LOG_FILE_OBJ
    global SYSLOG_REQUIRED

    if SYSLOG_REQUIRED:
        syslog.closelog()
        SYSLOG_REQUIRED = False
        return True

    if LOG_FILE_OBJ:
        try:
            LOG_FILE_OBJ.close()
            LOG_FILE_OBJ = None
        except IOError, e:
            sys.stderr.write("Failed to close file: %s\n" % e)
            return False
    return True


def openLog(fileName=None):
    global LOG_FILE_NAME
    global LOG_FILE_OBJ
    global SYSLOG_REQUIRED

    if fileName == LOG_SYSLOG:
        syslog.openlog(os.path.basename(sys.argv[0]))
        SYSLOG_REQUIRED = True
        return True

    if fileName:
        LOG_FILE_NAME = fileName

    if not LOG_FILE_NAME:
        return False

    closeLog()

    try:
        LOG_FILE_OBJ = open(LOG_FILE_NAME, "a")
    except IOError, e:
        sys.stderr.write("Failed to open file %s: %s\n" % (LOG_FILE_NAME, e))
        return False
    return True

def record(priority, message=None):
    global LOG_FILE_OBJ
    global SYSLOG_REQUIRED

    stack = inspect.stack()[1]
    if stack[3] == "<module>":
        prefix = "%s:%s:%s" % (stack[1], stack[2], stack[3])
    else:
        prefix = "%s:%s:%s()" % (stack[1], stack[2], stack[3])

    if type(priority) == type("") or type(priority) == type(u""):
        logPriority = syslog.LOG_INFO
        logMessage = priority
    else:
        logPriority = priority
        logMessage = message

    if SYSLOG_REQUIRED:
        syslog.syslog(logPriority, "[%s]: %s" % (prefix, logMessage))
        return

    fp = sys.stderr
    if LOG_FILE_OBJ:
        fp = LOG_FILE_OBJ

    fp.write("[%s] %s [%s]: %s" % (str(datetime.now()), _getLogCode(logPriority), prefix, logMessage))
    if logMessage[-1] != '\n':
        fp.write("\n")
    fp.flush()
    return


def trace(message):
    if message:
        log(syslog.LOG_DEBUG, message)


def isString(value):
    return (type(value) == type("") or type(value) == type(u""))


def getTempFileName():
    filedesc, filename = tempfile.mkstemp(prefix="GSP_")
    os.close(filedesc)
    return filename


def runCommandBG(command, stdinFileObj=None, stdoutFileObj=None, stderrFileObj=None,
                 shell=False, root=None):
    log("runCommandBG(): Trying to execute command [%s]" % command)

    if shell:
        if not isString(command):
            return None
    else:
        if isString(command):
            command = command.split()

    if root == True:
        if shell:
            command = "sudo " + command
        else:
            command = ['sudo'] + command
    elif isString(root):
        if shell:
            command = "sudo -u " + root + " " + command
        else:
            command = ['sudo', '-u', root] + command

    if not stdinFileObj:
        stdinFileObj=subprocess.PIPE
    if not stdoutFileObj:
        stdoutFileObj=subprocess.PIPE
    if not stderrFileObj:
        stderrFileObj=subprocess.PIPE

    try:
        process = subprocess.Popen(command,
                                   bufsize=-1,
                                   stdin=stdinFileObj,
                                   stdout=stdoutFileObj,
                                   stderr=stderrFileObj,
                                   shell=shell)
        return process
    except OSError, e:
        log("runCommandBG(): Failed to run command [%s]: %s" % (command, e))
    return None


def runCommand(command,
               input='', output=False,
               shell=False, root=None):
    rv = {}
    rv["Status"] = RUN_COMMAND_ERROR
    rv["Stdout"] = None
    rv["Stderr"] = None

    try:
        stdinFileName = getTempFileName()
        stdinFileObj = open(stdinFileName, "w")
        stdinFileObj.write(input)
        stdinFileObj.close()
        stdinFileObj = open(stdinFileName, "r")

        stdoutFileName = getTempFileName()
        stdoutFileObj = open(stdoutFileName, "w")

        stderrFileName = getTempFileName()
        stderrFileObj = open(stderrFileName, "w")
    except IOError, e:
        log("Failed to create temporary file for executing command [%s]: %s" % (command, e))
        if output:
            return rv
        return rv["Status"]

    stdoutContent = None
    stderrContent = None

    process = runCommandBG(command,
                           stdinFileObj=stdinFileObj,
                           stdoutFileObj=stdoutFileObj,
                           stderrFileObj=stderrFileObj,
                           shell=shell, root=root)
    if process:
        rv['Status'] = process.wait()
        rv['Stdout'] = open(stdoutFileName).read()
        rv['Stderr'] = open(stderrFileName).read()

    os.remove(stdinFileName)
    os.remove(stdoutFileName)
    os.remove(stderrFileName)

    log("runCommand(): execution status of command [%s] = [%s]" % (command, rv))

    if output:
        return rv
    return rv["Status"]


def runCommandFG(command, stdout=False, stderr=False,
                 shell=False, root=None):
    if stdout or stderr:
        output = True
    else:
        output = False
    return runCommand(command, output=output, shell=shell, root=root)


def IP2Number(ipString):
    try:
        return socket.htonl(struct.unpack("I", socket.inet_aton(ipString))[0])
    except socket.error:
        return None
    except TypeError:
        return None
    except struct.error:
        return None


def Number2IP(number):
    try:
        return socket.inet_ntoa(struct.pack("I", socket.ntohl(number)))
    except socket.error:
        return None
    except AttributeError:
        return None
    except ValueError:
        return None


def computeHostName(hostName):
    if not hostName:
        return False

    hostPrefix = ""
    for i in range(len(hostName), 0, -1):
        pos = i - 1
        if hostName[pos].isdigit():
            continue
        break
    hostPrefix = hostName[:pos+1]
    try:
        hostIndex = int(hostName[pos+1:]) 
    except ValueError:
        hostIndex = 0
    # TODO: Check the availablity of the (server) name
    return "%s%s" % (hostPrefix, hostIndex + 1)


def daemonize():
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit first parent
            sys.exit(0) 
    except OSError, e: 
        #sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
        return False
	
    # decouple from parent environment
    os.chdir("/") 
    os.setsid() 
    os.umask(0) 
	
    # do second fork
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit from second parent
            sys.exit(0) 
    except OSError, e: 
        #sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
        return False 
	
    # redirect standard file descriptors
    sys.stdout.flush()
    sys.stderr.flush()
    si = file("/dev/null", 'r')
    so = file("/dev/null", 'a+')
    se = file("/dev/null", 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    return True


def getDownloadStatus(fileName):
    try:
        lines = [line for line in open(fileName)
                 if "saved" in line or "%" in line]
    except IOError:
        return 0
    if not lines:
        return 0
    if "saved" in lines[-1]:
        return 100
    return lines[-1].split("%")[0].split()[-1]


def getMeminfo():
    """-> dict of data from meminfo (str:int).
    Values are in kilobytes.
    """
    import re
    re_parser = re.compile(r'^(?P<key>\S*):\s*(?P<value>\d*)\s*kB' )
    result = {}
    for line in open('/proc/meminfo'):
        match = re_parser.match(line)
        if not match:
            continue # skip lines that don't parse
        key, value = match.groups(['key', 'value'])
        result[key] = int(value)
    result['MemUsed'] = (result['MemTotal'] - result['MemFree'])
    return result


def getCpuUsage():
    """-> dict of cpuid : (usertime, nicetime, systemtime, idletime)
    cpuid "cpu" means the total for all CPUs.
    cpuid "cpuN" means the value for CPU N.
    """
    wanted_records = [line for line in open('/proc/stat') if
                      line.startswith('cpu')]
    result = {}
    for cpuline in wanted_records:
        fields = cpuline.split()[:5]
        data = map(int, fields[1:])
        result[fields[0]] = tuple(data)
    return result

def _getCpuStatList():
    try:
        fp = open("/proc/stat")
        line = fp.readline()
        fp.close()
        return map(float, line.split()[1:5])
    except IOError, e:
        Utils.log("Failed to open /proc/stat: %s" % str(e))
    return None

def getCpuUsageAvg():
    st1 = _getCpuStatList()
    #time1 = time.time()
    time.sleep(1)
    st2 = _getCpuStatList()
    #time2 = time.time()
    if not (st1 and st2):
        return None
    usageTime = (st2[0] - st1[0]) + (st2[1] - st1[1]) + (st2[2] - st1[2])
    try:
        return (100.0 * usageTime) / (usageTime + (st2[3] - st1[3]))
    except ZeroDivisionError, e:
        return 0

def getLoadavg():
    try:
        loadavgstr = open('/proc/loadavg', 'r').readline().strip()
    except IOError:
        syslog.syslog(syslog.LOG_ERR, "failed to find cpu load")
        return None

    data = map(float, loadavgstr.split()[1:])
    # returns 1 minute load average
    return data[0]


def getInfinibandPortStatus():

    """ Check for availability of infiniband port 
    and return which port is active in a key pair value
    """

    # Check for existence of infiniband ports
    value = os.popen ("ls /sys/class/infiniband").readline().strip()

    if not value:
        return None

    portlist = os.popen ("echo /sys/class/infiniband/*/ports/*").readline().split()
    
    portkeys = {}
    
    for port in portlist:
        value = os.popen ("cat %s/state" % 
                          port.strip()).readline().split(':')[1].strip()
        portkeys[port.strip()] = value
        
    return portkeys
        

def getServerFqdn():
    rv = runCommand("hostname --fqdn", output=True, root=True)
    if not rv["Stderr"]:
        return stripEmptyLines(rv["Stdout"])
    return socket.gethostname()


def getServerCount():
    try:
        return int(open(Globals.SERVER_COUNT_FILE).read().strip())
    except IOError:
        log("failed to read file %s" % Globals.SERVER_COUNT_FILE)
        return 1
    except ValueError:
        log("invalid number format in file %s" % Globals.SERVER_COUNT_FILE)
        return 1


def setServerCount(count):
    try:
        open(Globals.SERVER_COUNT_FILE, "w").write("%s\n" % count)
        return True
    except IOError:
        log("failed to write file %s" % Globals.SERVER_COUNT_FILE)
        return False


def getInstalledServerCount():
    try:
        return int(open(Globals.INSTALLED_SERVER_COUNT_FILE).read().strip())
    except IOError:
        log("failed to read file %s" % Globals.INSTALLED_SERVER_COUNT_FILE)
        return 1
    except ValueError:
        log("invalid number format in file %s" % Globals.INSTALLED_SERVER_COUNT_FILE)
        return 1


def setInstalledServerCount(count):
    try:
        open(Globals.INSTALLED_SERVER_COUNT_FILE, "w").write("%s\n" % count)
        return True
    except IOError:
        log("failed to write file %s" % Globals.INSTALLED_SERVER_COUNT_FILE)
        return False


def getLastInstalledServerIpList():
    ipList = {}
    networkDom = Protocol.XDOM()
    if not networkDom.parseFile(Globals.GLOBAL_NETWORK_FILE):
        log("failed to parse file %s" % Globals.GLOBAL_NETWORK_FILE)
    for tagE in networkDom.getElementsByTagRoute("server.interface"):
        interfaceDom = Protocol.XDOM()
        interfaceDom.setDomObj(tagE)
        ipAddress = interfaceDom.getTextByTagRoute("ipaddr")
        if ipAddress:
            ipList[interfaceDom.getTextByTagRoute("device")] = ipAddress
    return ipList


def getFreeIpAddress(device=None):
    serverCount = getServerCount()
    installedServerCount = getInstalledServerCount()
    if serverCount == installedServerCount:
        return None

    availableServerCount = serverCount - installedServerCount
    ipList = getLastInstalledServerIpList()
    
    if not ipList:
        return None

    if device:
        if device not in ipList.keys():
            return None
        deviceIpAddress = ipList[device]
    else:
        deviceIpAddress = ipList.values()[0]
    ipNumber = IP2Number(deviceIpAddress)

    for i in range((ipNumber + availableServerCount), ipNumber, -1):
        ipAddress = Number2IP(i)
        if runCommandFG(["ping", "-qnc", "1", ipAddress]) != 0:
            return ipAddress
    return None


def getPasswordHash(userName):
    try:
        #return spwd.getspnam(userName).sp_pwd
        return "Not implimented"
    except KeyError:
        return None


def getTransactionKey():
    try:
        tokens = open(Globals.TRANSACTION_KEY_FILE).read().split(',')
    except IOError:
        return None, None
    return tokens


def generateSignature():
    #return str(uuid.uuid4()) + ('--%f' % time.time())
    return ('--%f' % time.time())


def getSignature():
    try:
        return open(Globals.SIGNATURE_FILE).read().strip()
    except IOError:
        log(syslog.LOG_ERR, "unable to read signaure from %s file" % Globals.SIGNATURE_FILE)
        return False


def storeSignature(signature, fileName=Globals.SIGNATURE_FILE):
    try:
        open(fileName, "w").write(signature + "\n")
    except IOError:
        log(syslog.LOG_ERR, "unable to write signature %s to %s file" % (signature, fileName))
        return False
    return True


def isUserExist(userName):
    try:
        grp.getgrnam(userName).gr_gid
        return True
    except KeyError:
        pass
    try:
        pwd.getpwnam(userName).pw_uid
        return True
    except KeyError:
        pass
    return False


def getGsnUserInfo(fileName=Globals.GSN_USER_INFO_FILE):
    userInfo = {}
    userInfo["UserId"] = None
    userInfo["Password"] = None
    try:
        for line in open(fileName):
            line = line.strip()
            k = line[:line.index("=")]
            v = line[line.index("=") + 1:]
            if v[0] == "'" or v[0] == '"':
                v = v[1:]
            if v[-1] == "'" or v[-1] == '"':
                v = v[:-1]
            if k.upper() == "GSN_ID":
                userInfo["UserId"] = v
            if k.upper() == "GSN_PASSWORD":
                userInfo["Password"] = v
    except IOError, e:
        log("Failed to read file %s: %s" % (fileName, e))
    return userInfo


def setGsnUserInfo(userInfo, fileName=Globals.GSN_USER_INFO_FILE):
    try:
        fp = open(fileName, "w")
        fp.write("GSN_ID=%s\n" % userInfo["UserId"])
        fp.write("GSN_PASSWORD=%s\n" % userInfo["Password"])
        fp.close()
        return True
    except IOError, e:
        log("Failed to write file %s: %s" % (fileName, e))
    return False


def getPlatformVersion(fileName=Globals.GLUSTER_VERSION_FILE):
    versionInfo = {}
    versionInfo["Version"] = None
    versionInfo["Update"] = None
    try:
        lines = open(Globals.GLUSTER_VERSION_FILE).readlines()
        for line in open(fileName):
            line = line.strip()
            k = line[:line.index("=")]
            v = line[line.index("=") + 1:]
            if v[0] == "'" or v[0] == '"':
                v = v[1:]
            if v[-1] == "'" or v[-1] == '"':
                v = v[:-1]
            if k.upper() == "VERSION":
                versionInfo["Version"] = v
            if k.upper() == "UPDATE":
                versionInfo["Update"] = v
    except IOError, e:
        log("Failed to read file %s: %s" % (fileName, e))
    return versionInfo


def setPlatformVersion(versionInfo, fileName=Globals.GLUSTER_VERSION_FILE):
    if isString(versionInfo):
        tokens = versionInfo.strip().split(".")
        if len(tokens) < 2:
            log("Invalid version format %s. Expecting <MAJOR>.<MINOR>.<PATCHLEVEL>" % versionInfo)
            return False
        version = ".".join(tokens[:2])
        update = ".".join(tokens[2:])
        if not update:
            update = "0"
    else:
        version = versionInfo["Version"]
        update = versionInfo["Update"]
    try:
        fp = open(fileName, "w")
        fp.write("VERSION=%s\n" % version)
        fp.write("UPDATE=%s\n" % update)
        fp.close()
        return True
    except IOError, e:
        log("Failed to write file %s: %s" % (fileName, e))
    return False


def getGlusterUpdateDom(serverVersion):
    errorMessage = ""
    updateInfoDom = None
    try:
        baseUrl = open(Globals.GLUSTER_UPDATE_SITE_FILE).read().strip()
    except IOError, e:
        log("Failed to read file %s: %s" % (Globals.GLUSTER_UPDATE_SITE_FILE, e))
        errorMessage = "Failed to read update site file"
        return updateInfoDom, errorMessage

    try:
        url = "%s/%s/%s" % (baseUrl, serverVersion, Globals.GLUSTER_UPDATES_FILE)
        connection = urllib.urlopen(url)
        if connection.getcode() != 200:
            connection.close()
            errorMessage = "Error received from server to open URL %s" % url
            return updateInfoDom, errorMessage
        updateInfoString = connection.read()
        connection.close()
    except IOError, e:
        log("Failed to get update information from URL %s: %s" % (url, e))
        errorMessage = "Error getting update information"
        return updateInfoDom, errorMessage

    updateInfoDom = Protocol.XDOM()
    if not updateInfoDom.parseString(updateInfoString):
        log("XML parse error on update information content [%s]" % updateInfoString)
        errorMessage = "Parse error on update information"
        updateInfoDom = None
    return updateInfoDom, errorMessage


def removeFile(fileName, root=False):
    if root:
        if runCommand("rm %s" % fileName, root=True) == 0:
            return True
        return False
    try:
        os.remove(fileName)
        return True
    except OSError, e:
        log("Failed to remove file %s: %s" % (fileName, e))
    return False


def isLiveMode():
    return os.path.exists(Globals.LIVE_MODE_FILE)

def convertKbToMb(kb):
    return kb / 1024.0


def getIPIndex(indexFile):
    try:
        fp = open(indexFile)
        line = fp.readline()
        fp.close()
        index = int(line)
    except IOError:
        index = 0
    except ValueError:
        index = False
    return index

def setIPIndex(index, indexFile):
    try:
        fp = open(indexFile, "w")
        fp.write(str(index))
        fp.close()
    except IOError:
        return False
    return True

def IP2Number(ipString):
    try:
        return socket.htonl(struct.unpack("I", socket.inet_aton(ipString))[0])
    except socket.error:
        return None
    except TypeError:
        return None
    except struct.error:
        return None

def Number2IP(number):
    try:
        return socket.inet_ntoa(struct.pack("I", socket.ntohl(number)))
    except socket.error:
        return None
    except AttributeError:
        return None
    except ValueError:
        return None

def hasEntryFoundInFile(searchString, dnsEntryFileName):
    try:
        addServerEntryList = open(dnsEntryFileName).read().split()
    except IOError:
        return None
    if searchString in addServerEntryList:
        return True
    return False


def computeIpAddress(ipAddress, startIp, endIp):
    startIpNumber = IP2Number(startIp)
    endIpNumber = IP2Number(endIp)
    if not ipAddress:
        return startIp
    nextIpNumber = IP2Number(ipAddress)
    while True:  
        nextIpNumber = nextIpNumber + 1
        ipAddress = Number2IP(nextIpNumber)
        rv = runCommandFG(["ping", "-qnc", "1", ipAddress])
        if type(rv) == type(True):
            return False
        if rv != 0:
            break

    if nextIpNumber >= startIpNumber and nextIpNumber <= endIpNumber:
        return ipAddress

    nextIpNumber = IP2Number(startIp)
    while True:
        ipAddress = Number2IP(nextIpNumber)
        nextIpNumber = nextIpNumber + 1
        rv = runCommandFG(["ping", "-qnc", "1", ipAddress])
        if type(rv) == type(True):
            return False
        if rv != 0:
            break

    if IP2Number(ipAddress) >= startIpNumber and IP2Number(ipAddress) <= endIpNumber:
        return ipAddress
    return False


def setHostNameAndIp(hostName, ipAddress, lastAddServerDetailFile):
    try:
        fp = open(lastAddServerDetailFile, "w")
        fp.write("HOSTNAME=" + hostName + "\n")
        fp.write("IPADDRESS=" + ipAddress);
        fp.close()
    except IOError:
        return False
    return True

def getPort():
    try:
        fd = open(Globals.PORT_FILE, "r")
        portString = fd.readline()
        fd.close()
        port = int(portString)
    except IOError:
        port = Globals.DEFAULT_PORT - 2
    except ValueError:
        port = Globals.DEFAULT_PORT - 2
    return port

def setPort(port):
    try:
        fd = open(Globals.PORT_FILE, "w")
        fd.write(str(port))
        fd.close()
    except IOError:
        return False
    return True

def getServerAgentCredentials():
    try:
        lines = open(Globals.SERVERAGENT_AUTH_FILE).readlines()
    except IOError:
        return None,None

    userName = None
    password = None

    for l in lines:
        if l[-1] == '\n':
            l = l[:-1]
        k = l[:l.index('=')]
        v = l[l.index('=') + 1:]
        if v[0] == "'" or v[0] == '"':
            v = v[1:]
        if v[-1] == "'" or v[-1] == '"':
            v = v[:-1]
        if k.upper() == "AGENT_ID":
            userName = v
        if k.upper() == "AGENT_PASSWORD":
            password = v

    return userName, password

def getGatewayAgentCredentials():
    try:
        lines = open(Globals.GATEWAYAGENT_AUTH_FILE).readlines()
    except IOError:
        return None

    #userName = None
    password = None

    for l in lines:
        if l[-1] == '\n':
            l = l[:-1]
        k = l[:l.index('=')]
        v = l[l.index('=') + 1:]
        if v[0] == "'" or v[0] == '"':
            v = v[1:]
        if v[-1] == "'" or v[-1] == '"':
            v = v[:-1]
        #if k.upper() == "AGENT_ID":
        #    userName = v
        if k.upper() == "AGENT_PASSWORD":
            password = v

    return password

def getWebAgentCredentials():
    try:
        lines = open(Globals.WEBAGENT_AUTH_FILE).readlines()
    except IOError:
        return None,None

    userName = None
    password = None

    for l in lines:
        if l[-1] == '\n':
            l = l[:-1]
        k = l[:l.index('=')]
        v = l[l.index('=') + 1:]
        if v[0] == "'" or v[0] == '"':
            v = v[1:]
        if v[-1] == "'" or v[-1] == '"':
            v = v[:-1]
        if k.upper() == "AGENT_ID":
            userName = v
        if k.upper() == "AGENT_PASSWORD":
            password = v

    return userName, password

def daemonize():
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit first parent
            sys.exit(0) 
    except OSError, e: 
        #sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
        return False
	
    # decouple from parent environment
    os.chdir("/") 
    os.setsid() 
    os.umask(0) 
	
    # do second fork
    try: 
        pid = os.fork() 
        if pid > 0:
            # exit from second parent
            sys.exit(0) 
    except OSError, e: 
        #sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
        return False 
	
    # redirect standard file descriptors
    sys.stdout.flush()
    sys.stderr.flush()
    si = file("/dev/null", 'r')
    so = file("/dev/null", 'a+')
    se = file("/dev/null", 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    return True

def getFreeIpAddress():
    startRange, endRange = getStoragePoolInfo()
    if not (startRange and endRange):
        return None

    startIpNumber = IP2Number(startRange)
    endIpNumber = IP2Number(endRange)

    for ipNumber in range(endIpNumber, startIpNumber, -1):
        rv = runCommandFG(["ping", "-qnc", "1", Number2IP(ipNumber)])
        if type(rv) == type(True):
            return None
        if rv != 0:
            return Number2IP(ipNumber)
    return None

def getDhcpServerStatus():
    status = runCommandFG(["sudo", "service", "dnsmasq", " status"])
    if type(status) == type(True) or 0 != status:
        return False
    return True

def startDhcpServer():
    status = runCommandFG(["sudo", "service", "dnsmasq", " start"])
    if type(status) == type(True) or 0 != status:
        return False
    return True

def stopDhcpServer():
    status = runCommandFG(["sudo", "service", "dnsmasq", " stop"])
    if type(status) == type(True) or 0 != status:
        return False
    return True

def getStoragePoolInfo():
    startRange = None
    endRange = None
    try:
        for line in open(Globals.GLUSTER_SERVER_POOL_FILE):
            tokens = line.split("=")
            if tokens[0] == "STARTRANGE":
                startRange = tokens[1].strip()
            if tokens[0] == "ENDRANGE":
                endRange = tokens[1].strip()
    except IOError:
        log(syslog.LOG_ERR, "unable to read %s file" % Globals.GLUSTER_SERVER_POOL_FILE)
    return startRange, endRange

def configureDnsmasq(serverIpAddress, dhcpIpAddress):
    dnsmasqConfFile = Globals.GLUSTER_CONF_CONF_DIR + "/dnsmasq.conf"
    serverPortString = "68"
    try:
        for arg in open("/proc/cmdline").read().strip().split():
            token = arg.split("=")
            if token[0] == "dhcp":
                serverPortString = token[1]
                break
    except IOError:
        log(syslog.LOG_ERR, "Failed to read /proc/cmdline.  Continuing with default port 68")
    try:
        serverPort = int(serverPortString)
    except ValueError:
        log(syslog.LOG_ERR, "Invalid dhcp port '%s' in /proc/cmdline.  Continuing with default port 68" % serverPortString)
        serverPort = 68

    try:
        fp = open(dnsmasqConfFile, "w")
        fp.write("no-hosts\n")
        #fp.write("addn-hosts=%s\n" % Globals.GLUSTER_DNS_ENTRIES)
        fp.write("bind-interfaces\n")
        fp.write("except-interface=lo\n")
        fp.write("dhcp-range=%s,%s\n" % (dhcpIpAddress, dhcpIpAddress))
        fp.write("dhcp-lease-max=1\n")
        #fp.write("dhcp-option=option:router,%s\n" % serverIp)
        #fp.write("dhcp-option=option:ntp-server,%s\n" % serverIp)
        fp.write("dhcp-alternate-port=%s\n" % serverPort)
        fp.write("server=%s\n" % serverIpAddress)
        fp.write("dhcp-script=/usr/sbin/server-info\n")
        fp.close()
    except IOError:
        log(syslog.LOG_ERR, "unable to write dnsmasq configuration %s" % dnsmasqConfFile)
        return False
    status = runCommandFG(["sudo", "cp", "-f", Globals.GLUSTER_CONF_CONF_DIR + "/dnsmasq.conf", Globals.DNSMASQ_CONF_FILE])
    if type(status) == type(True) or 0 != status:
        log(syslog.LOG_ERR, "unable to copy dnsmasq configuration to " + Globals.DNSMASQ_CONF_FILE)
        return False
    return True

def configureDhcpServer(serverIpAddress, dhcpIpAddress):
    return configureDnsmasq(serverIpAddress, dhcpIpAddress)

def log(priority, message=None):
    if type(priority) == type(""):
        logPriority = syslog.LOG_INFO
        logMessage = priority
    else:
        logPriority = priority
        logMessage = message
    if not logMessage:
        return
    #if Globals.DEBUG:
    #    sys.stderr.write(logMessage)
    else:
        syslog.syslog(logPriority, logMessage)
    return


def stripEmptyLines(content):
    ret = ""
    for line in content.split("\n"):
        if line.strip() != "":
            ret += line
    return ret


def getDeviceFormatStatusFile(device):
    return "/var/tmp/format_%s.status" % device.replace('/', '_')

def getDeviceFormatLockFile(device):
    return "/var/lock/format_%s.lock" % device.replace('/', '_')

def getDeviceFormatOutputFile(device):
    return "/var/tmp/format_%s.out" % device.replace('/', '_')

def getGlusterVersion():
    rv = runCommand("/usr/sbin/gluster --version", output=True)
    if rv["Stderr"]:
        return None
    if rv["Status"] != 0:
        return None
    if not rv["Stdout"]:
        return None
    return rv["Stdout"].strip().split()[1]