summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-config/timezip.py
blob: ad9bbd00a6b503c0f2a5130b5a3fcdda3ed23c9b (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
#!/usr/bin/python
# import MySQL module
#used to import the zipcode/timezone/nfs info fromMBE
import MySQLdb
import sys
import getopt
import socket


def usage():
    print " -h --help"
    print " -d  --installdb"
    print " -m --masterdb"
    print " -n  --hostname of current machine"

def printvars():
    print "masterdb: " + masterdb
    print "installdb:" + installdb
    print "hostname: " + thishostname
    print "BEhostname:" + BEhostname
    print "zipcode:" + zipcode
    print "timezone:" + tz
    print "nfsip:" + nfsip
    print "nfstoggle:" + nfstoggle
    print "nfsmount:"  + nfsmount
    print "hobbitclient:" + hobbitclient
# connect
def selectvars():

    global BEhostname
    global zipcode
    global tz
    global nfsip
    global nfstoggle
    global nfsmount
    global hobbitclient

    db = MySQLdb.connect(host=masterdb, user="mythtv", passwd="mythtv", db="mythconverg")
    # create a cursor
    cursor = db.cursor()
    # execute SQL statement
    cursor.execute("select hostname from settings where value='BackendServerIP' and data=(%s)",(masterdb))
    result = cursor.fetchone()
    try:
        BEhostname=result[0]
    except TypeError:
        BEhostname=""

    cursor.execute("select data from settings where value='HostZipcode' and hostname=(%s)",(BEhostname))
    result = cursor.fetchone()
    try:
        zipcode =  result[0]
    except TypeError:
        zipcode=""

    cursor.execute("select data from settings where value='HostCentralNFSIP' and hostname=(%s)",(BEhostname))
    result = cursor.fetchone()
    try:
        nfsip=result[0]
    except TypeError:
        nfsip=""

    cursor.execute("select data from settings where value='HostHaveCentralNFS' and hostname=(%s)",(BEhostname))
    result = cursor.fetchone()
    try:
        nfstoggle=result[0]
    except TypeError:
        nfstoggle=""

    cursor.execute("select data from settings where value='HostNFSmountpoint' and hostname=(%s)",(BEhostname))
    result = cursor.fetchone()
    try:
        nfsmount=result[0]
    except TypeError:
        nfsmount=""

    cursor.execute("select data from settings where value='HostTimeZone' and hostname=(%s)",(BEhostname))
    result = cursor.fetchone()
    try:
        tz=result[0]
    except TypeError:
        tz=""

    if  ( thishostname != BEhostname ):
        cursor.execute("select data from settings where value='GlobalServiceHobbitserver'")
        result = cursor.fetchone()
        try:
            hobbitclient=result[0]
        except TypeError:
            hobbitclient="0"



def insertvars():

    db = MySQLdb.connect(host=installdb, user="mythtv", passwd="mythtv", db="mythconverg")
    cursor = db.cursor()
#inserting the zipcode
    cursor.execute("select * from settings where value='HostZipcode' and hostname=(%s)",(thishostname))
    result = cursor.fetchone()
    if ( zipcode!="" ):
        if (result == None):
            cursor.execute("INSERT INTO  settings (value,data,hostname) VALUES ('HostZipcode',(%s),(%s))",(zipcode,thishostname))
            print "inserting zipcode"
        else:
            cursor.execute("update settings set data=(%s) where hostname=(%s) and value='HostZipcode'",(zipcode,thishostname))
            print "updating zipcode"

#inserting the timezone
    cursor.execute("select *  from settings where value='HostTimeZone' and hostname=(%s)",(thishostname))
    result = cursor.fetchone()
    if ( tz != ""):
         if (result == None):
            cursor.execute("INSERT INTO  settings (value,data,hostname) VALUES ('HostTimeZone',(%s),(%s))",(tz,thishostname))
            print "inserting timezone"
         else:
            cursor.execute("update settings set data=(%s) where hostname=(%s) and value='HostTimeZone'",(tz,thishostname))
            print "updating timezone"


#start of NFSIP
    cursor.execute("select data from settings where value='HostCentralNFSIP' and hostname=(%s)",(thishostname))
    result = cursor.fetchone()
    if ( nfsip != ""):
        if (result == None):
            cursor.execute("INSERT INTO  settings (value,data,hostname) VALUES ('HostCentralNFSIP',(%s),(%s))",(nfsip,thishostname))
            print "inserting nfsip"

    if (nfstoggle != ""):
        cursor.execute("select data from settings where value='HostHaveCentralNFS' and hostname=(%s)",(thishostname))
        result = cursor.fetchone()
        if (result == None):
            cursor.execute("INSERT INTO  settings (value,data,hostname) VALUES ('HostHaveCentralNFS',(%s),(%s))",(nfstoggle,thishostname))
            print "inserting nfstoggle"

    if ( nfsmount !="") :
        cursor.execute("select data from settings where value='HostNFSmountpoint' and hostname=(%s)",(thishostname))
        result = cursor.fetchone()
        if (result == None):
            cursor.execute("INSERT INTO  settings (value,data,hostname) VALUES ('HostNFSmountpoint',(%s),(%s))",(nfsmount,thishostname))
            print "inserting nfsmountpoint"

#hobbitclient
    cursor.execute("select *  from settings where value='HostServiceHobbitclient' and hostname=(%s)",(thishostname))
    result = cursor.fetchone()
    if ( tz != ""):
         if (result == None):
            cursor.execute("INSERT INTO  settings (value,data,hostname) VALUES ('HostServiceHobbitclient',(%s),(%s))",(hobbitclient,thishostname))
            print "inserting hobbitcleint"
         else:
            cursor.execute("update settings set data=(%s) where hostname=(%s) and value='HostServiceHobbitclient'",(hobbitclient,thishostname))
            print "updating hobbitclient"


def main(argv):
    global masterdb
    global installdb
    global thishostname
    thishostname=""
    global BEhostname
    global zipcode
    global tz
    global nfsip
    global hobbitclient
    try:
        opts, args = getopt.getopt(argv, "hm:d:n:", ["help", "masterdb=", "installdb=" , "hostname=" ] )
    except getopt.GetoptError:
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
             usage()
             sys.exit()
        elif opt in ( "-d" , "--installdb"):
             installdb = arg
        elif opt in ("-m", "--masterdb"):
             masterdb = arg
        elif opt in ("-n", "--hostname"):
             thishostname = arg

    if ( thishostname == "" ):
         thishostname = socket.gethostname()

    selectvars()
    printvars()
    insertvars()
if __name__ == "__main__":
    main(sys.argv[1:])