summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-config/systemconfig.py
blob: bd02408b06ade785a09f7add8e82feeb11dd7f85 (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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import sys,  os,  commands,  glob,  time
from time import time, localtime, strftime
import getopt,  re,  MySQLdb
import logging
import mv_common , mv_network
import inspect
#____________________________________________setup the logging______________________________________________________
LOG_FILENAME = '/tmp/systemconfig.log'
DEBUGLOG = '/tmp/systemconfig_debug.log'
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename=DEBUGLOG,
                    filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
#formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
formatter = logging.Formatter("%(message)s")
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

#infoformatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
infoformatter = logging.Formatter("%(message)s")
infofile = logging.FileHandler(LOG_FILENAME, 'w')
infofile.setFormatter(infoformatter)
infofile.setLevel(logging.INFO)
logging.getLogger('').addHandler(infofile)


def usage():
    logging.info("List of available modules")
    logging.info("more then one module by be called")
    logging.info("Ex:  misc,sleep,audio")
    cmd_list = ["misc",
                "sleep",
               "hostype",
               "hostypec" ,
               "advanced" ,
               "audio" ,
               "network" ,
               "advancedX" ,
               "webuser" ,
               "restartfe" ,
               "reloadfe" ,
               "ddns" ,
               "screensaver" ,
               "ir" ,
               "user",
               "software",
               "plugins",
               "smolt",
               "all",
               "func",
               "supplemental",
               "vnc",
               "fileshare",
               "config_xml"]
    for i in cmd_list:
        logging.info(i)

def setup_x(systemconfig):
    if  mv_common.read_config(mv_common.module_config,"xorg")  == False  :
        logging.info("____Skipping of Xorg, config disabled____")
    else:
        if not os.path.exists('/usr/share/icons/default'):
            if os.path.exists('/usr/share/icons/Vanilla-DMZ-AA'):
                mv_common.link_file('/usr/share/icons/Vanilla-DMZ-AA','/usr/share/icons/default');
        if systemconfig.get("UseXLargeMouse") == "1" :
            cmd = "echo Xcursor.size: 48 > %s/.Xdefaults" %data_config.MYTHHOME
        else:
            cmd = "echo Xcursor.size: 16 > %s/.Xdefaults" %data_config.MYTHHOME
        mv_common.runcmd(cmd)

        cmd = "%s/bin/xconfig.sh" %systemconfig.get("MVROOT")
        mv_common.runcmd(cmd)


def generate_config_xml(uuid,dbhost):
    configxml_t='''
<Configuration>
  <LocalHostName>my-unique-identifier-goes-here</LocalHostName>
  <Database>
    <PingHost>1</PingHost>
    <Host>%s</Host>
    <UserName>mythtv</UserName>
    <Password>mythtv</Password>
    <DatabaseName>mythconverg</DatabaseName>
    <Port>3306</Port>
  </Database>
  <WakeOnLAN>
    <Enabled>0</Enabled>
    <SQLReconnectWaitTime>0</SQLReconnectWaitTime>
    <SQLConnectRetry>5</SQLConnectRetry>
    <Command>echo 'WOLsqlServerCommand not set'</Command>
  </WakeOnLAN>
  <UPnP>
    <UDN>
      <MediaRenderer>%s</MediaRenderer>
    </UDN>
  </UPnP>
</Configuration>
    '''



    configxml= configxml_t %(dbhost,uuid)
    return configxml

def gen_uuid():
    cmd = "/usr/bin/uuidgen"
    uuid = mv_common.runcmd_output(cmd)
    return uuid

def setup_config_xml(systemconfig):
    #setup mysql.txt and config.xml
    logging.info("   setup config.xml")

    mv_root = systemconfig.get("MVROOT")
    mysqltxt = "/usr/share/mythtv/mysql.txt"
    #mythfrontend will delete and create config.xml in the mythhome dir
    #this breaks the link to /usr/share/mythtv/config.xml and causes other
    #users (root) to be out of sync mythtv user
    configxml_file = data_config.MYTHHOME + "/.mythtv/config.xml"
    configxml_share_link = "/usr/share/mythtv/config.xml"
    configxml_root_link = "/root/.mythtv/config.xml"

    templates = systemconfig.get("TEMPLATES")
    mysqltemplate = templates + "/mysql.txt"
    dbhost = systemconfig.get("dbhost")
    uuid = gen_uuid()
    config_xml=generate_config_xml(uuid,dbhost)
    mv_common.mkdir_mythhome(data_config.MYTHHOME,"mythtv","mythtv")
    mv_common.mkdir_mythhome("/root","root","root")

    delfile = data_config.MYTHHOME + "/.mythtv/mysql.txt"
    mv_common.remove_file(delfile)
    delfile = mv_root + "/bin/mythtv/.mythtv/mysql.txt"
    mv_common.remove_file(delfile)

    #backup config.xml files and delete
    currenttime = strftime("%b-%d-%Y-%H:%M:%S", localtime())
    for i in [ configxml_file , configxml_share_link, configxml_root_link ]:
        if os.path.islink(i):
            # path is a link, delete it
            logging.info("    Removing link %s ", i)
            mv_common.remove_file(i)
        else:
            # path is a file, backup and delete unless it is mythhome
            if configxml_file != i:
                cmd = "mv %s %s-%s" %(i,i,currenttime)
                mv_common.runcmd_output(cmd)

    #create config.xml
    try:
        logging.info("    Writing %s", configxml_file)
        f = open(configxml_file,  'w')

        for outline in config_xml:
            f.write(outline)
        f.close()
    except:
        logging.debug("    Couldn't write config.xml")

    #link /usr/share/mythtv/config.xml and /root/.mythtv/config.xml
    mv_common.link_file(configxml_file,configxml_share_link)
    mv_common.link_file(configxml_file,configxml_root_link)




def main(argv):
    try:
        MVROOT = os.environ["MV_ROOT"]
    except:
        logging.debug("MVROOT was not defined, using the default value")
        MVROOT = "/usr/MythVantage"

    global cmdmodule
    global systemconfig
    systemconfig["MVROOT"]=MVROOT
    restartlcd = False
    cmdmodule={"misc":False , "sleep":False ,
               "hostype":False , "hostypec":False ,
               "advanced":False , "audio":False ,
               "network":False , "advancedX":False ,
               "webuser":False , "restartfe":False ,
               "reloadfe":False , "ddns":False ,
               "screensaver":False , "ir":False ,
               "this_is_install":False ,
               "user":False, "software":False,
               "plugins":False, "smolt":False,
               "all":False, "dhcp_request":False ,
               "func":False, "supplemental":False,
               "vnc":False, "fileshare":False,
               "config_xml":False,
               "locale":False }
    try:
        opts,  args = getopt.getopt(argv,  'c:hm:d:',  ["help","modules" ] )
    except getopt.GetoptError, why:
        print why
        usage()
        print "exception happened"
        sys.exit(2)
    for opt,  arg in opts:
        if opt in ("-h",  "--help"):
            usage()
            sys.exit(0)
        elif opt in ("-m"):
            for i in arg.split(","):
                cmdmodule[i]=True
        elif opt in ("-d"):
            dhcp_dev = arg


    if cmdmodule["this_is_install"] or cmdmodule["hostypec"]:
        systemconfig["hostypec"] = True
        #cmdmodule["smolt"] = True

    if cmdmodule["hostypec"] == True :
        cmdmodule["fileshare"] = True
        cmdmodule["hostype"] = True

    systemconfig["mythip"] = mv_network.setup_MYTH_IP(systemconfig)
    systemconfig["mythdhcp"] = mv_network.setup_MYTH_DHCP(systemconfig)
    systemconfig["mythgw"] = mv_network.setup_MYTH_DHCP(systemconfig)
    systemconfig["mythdns"] = mv_network.setup_MYTH_DHCP(systemconfig)

    if cmdmodule["all"]:
        logging.info("***    WILL RUN ALL MODULES   ***")
        cmdmodule["network"] = True
        cmdmodule["hostype"] = True
        cmdmodule["ir"]= True
        #cmdmodule["network"] = True
        cmdmodule["audio"] = True
        cmdmodule["misc"] = True
        cmdmodule["sleep"] = True
        cmdmodule["webuser"] = True
        cmdmodule["ddns"] = True
        cmdmodule["screensaver"] = True
        cmdmodule["advanced"] = True
        cmdmodule["software"] = True
        cmdmodule["advancedX"] = True
        #cmdmodule["smolt"] = True
        cmdmodule["user"] = True
        cmdmodule["vnc"] = True
        cmdmodule["fileshare"] = True
        cmdmodule["config_xml"] = True
        cmdmodule["locale"] = True


    ##putting this here until I can create the seperate share module
    #if cmdmodule["fileshare"]:
        #cmdmodule["advanced"] = True
        #cmdmodule["misc"] = True

    if cmdmodule["network"]:
        restartfe = mv_network.setup_network(systemconfig, cmdmodule["this_is_install"])
        systemconfig["mythip"] = mv_network.setup_MYTH_IP(systemconfig)
        systemconfig["mythdhcp"] = mv_network.setup_MYTH_DHCP(systemconfig)
        systemconfig["mythgw"] = mv_network.setup_MYTH_DHCP(systemconfig)
        systemconfig["mythdns"] = mv_network.setup_MYTH_DHCP(systemconfig)
        cmdmodule["hostype"] = True

        if restartfe == True :
            logging.debug("    Setting the frontend to restart due to network change")
            cmdmodule["restartfe"] = True

    if cmdmodule["config_xml"]:
        setup_config_xml(systemconfig)


    if cmdmodule["func"]:
        from mv_hostype import setup_func_minion
        setup_func_minion(systemconfig.get("dbhost"),systemconfig.get("hostname"))




    if cmdmodule["hostype"]:
        import mv_hostype
        cmdmodule["advancedX"] = True
        setup_x(systemconfig)
        cmdmodule["ir"] = True
        mv_hostype.hostypeprint(systemconfig)
        #cmdmodule["smolt"] = True

    #if cmdmodule["network"]:
        #restartfe = mv_network.setup_network(systemconfig, cmdmodule["this_is_install"])
        #if restartfe == True :
            #logging.debug("    Setting the frontend to restart due to network change")
            #cmdmodule["restartfe"] = True



    if cmdmodule["ir"] == True:
        import mv_ir
        restart = mv_ir.setup_ir(systemconfig,data_config,cmdmodule["this_is_install"])
        restartfe = restart[0]
        restartlcd = restart[1]
        if restartfe == True :
            logging.debug("    Setting the frontend to restart due to lirc change")
            cmdmodule["restartfe"] = True
        cmdmodule["smolt"] = True


    if cmdmodule["audio"] == True:
        if  mv_common.read_config(mv_common.module_config,"audio")  == False  :
            logging.info("____Skipping of Audio, config disabled____")
        else:
            logging.debug("______Start of Audio Configuration______")
            if cmdmodule["this_is_install"]:
                if systemconfig.get("Audiotype") == "OSS":
                    mv_common.pacinstall("oss")
                    mv_common.add_service("oss")

            cmd="%s/bin/soundconfig.sh -t real -i %s -d %s" %(MVROOT,
                                            systemconfig.get("Audiotype"),
                                            systemconfig["SoundDevice"])
            mv_common.runcmd(cmd)
            mv_common.reloadfe(systemconfig.get("dbhost"),restartlcd)
        logging.debug("__End of audio")

    if cmdmodule["misc"]:
        if  mv_common.read_config(mv_common.module_config,"misc")  == False  :
            logging.info("____Skipping of misc, config disabled____")
        else:
            import mv_misc
	    #zipcode is no longer used
            #mv_misc.setup_zip(MVROOT,systemconfig.get("zipcode"))
            mv_misc.setup_tz(systemconfig.get("timezone"),systemconfig["TEMPLATES"])
            #mv_misc.setup_nfs(systemconfig)

    if cmdmodule["sleep"]:
        if  mv_common.read_config(mv_common.module_config,"sleep")  == False  :
            logging.info("____Skipping of sleep, config disabled____")
        else:
            import mv_misc
            mv_misc.setup_sleep(systemconfig)

    if cmdmodule["webuser"]:
        import mv_webuser
        mv_webuser.setup_web_auth(systemconfig.get("UseMythWEB_auth"))

    if cmdmodule["ddns"]:
        import mv_ddns
        mv_ddns.setup_ddns(systemconfig)

    if cmdmodule["screensaver"]:
        import mv_screensaver
        mv_screensaver.setup_screensaver(systemconfig, data_config)

    if cmdmodule["advanced"]:
        import mv_advanced
        mv_advanced.setup_advanced(systemconfig, data_config)

    if cmdmodule["fileshare"]:
        import mv_fileshare
        mv_fileshare.setup_fileshare(systemconfig, data_config)


    if cmdmodule["software"] or cmdmodule["plugins"]:
        import mv_software
        mv_software.setup_software(systemconfig, data_config)

    if cmdmodule["advancedX"]:
        if  mv_common.read_config(mv_common.module_config,"xorg")  == False  :
            logging.info("____Skipping of advancedX, config disabled____")
        else:
            setup_x(systemconfig)

    if cmdmodule["restartfe"]:
        mv_common.restartfe(restartlcd)

    if cmdmodule["reloadfe"]:
        mv_common.reloadfe(systemconfig.get("dbhost"),restartlcd)

    #if cmdmodule["smolt"]:
        #import mv_smolt
        #mv_smolt.setup_smolt_type(systemconfig.get("SystemType"),
                                  #systemconfig.get("Remotetype"),
                                  #systemconfig.get("RunFrontend"),
                                  #data_config.MYTHHOME,
                                  #data_config.SYSTEMTYPE)

    if cmdmodule["user"]:
        logging.info("____Start of ssh user____")
        if systemconfig.get("rootSSH") == "1" :
            logging.info("    Allowing ssh access for root")
            cmd = '''sed -i "s/^.*PermitRootLogin.*$/PermitRootLogin yes/" /etc/ssh/sshd_config'''
            mv_common.runcmd(cmd)
        else:
            logging.info("    Removing ssh access for root")
            cmd = '''sed -i "s/^.*PermitRootLogin.*$/PermitRootLogin no/" /etc/ssh/sshd_config'''
            mv_common.runcmd(cmd)
        logging.info("__End of ssh user\n")

    if cmdmodule["dhcp_request"]:
        cmd = '''dhcpcd -t4 -h' ' -Td %s > /tmp/mvnetwork.dhcpinfo ''' %dhcp_dev
        mv_common.runcmd(cmd)

    if cmdmodule["supplemental"] or cmdmodule["this_is_install"]:
        import mv_supplemental
        mv_supplemental.windowmanager(systemconfig)
        mv_supplemental.supplemental(systemconfig,cmdmodule["this_is_install"])

    if cmdmodule["vnc"]:
        import mv_vnc
        mv_vnc.setupvnc(systemconfig,data_config)

    if cmdmodule["fileshare"]:
        import mv_fileshare
        mv_fileshare.setup_fileshare(systemconfig, data_config)

    if cmdmodule["locale"]:
        import mv_locale
        mv_locale.setup_locale(systemconfig)

if __name__ == "__main__":

    config_file = "mv_config"
    data_config = __import__(config_file,  globals(),  locals(),  [])
    #Read in systemconfig
    global systemconfig
    systemconfig = {"hostypec":False}
    file_name = "/etc/systemconfig"
    try:
        config_file = open(file_name)
    except:
        logging.critical("%s could not be opened", file_name)
        sys.exit(1)

    for line in config_file:
        line = line.strip()
        if line and line[0] is not "#" and line[-1] is not "=":
            var, val = line.rsplit("=", 1)
            val = val.strip('"')
            systemconfig[var.strip()] = val.strip()
    try:
        systemconfig["TEMPLATES"] = os.environ["TEMPLATES"]
    except:
        logging.critical("    *TEMPLATES is not defined")
        logging.critical("    *using default value")
        systemconfig["TEMPLATES"] = "/usr/MythVantage/templates"

    logging.debug("______START OF DEBUG______")
    main(sys.argv[1:])
    logging.debug("______cmd line options______")
    for i in cmdmodule.items():
        logging.debug (i)
    logging.debug("______systemconfig______")
    for i in systemconfig.items():
        logging.debug(i)
    #pass
    logging.debug("______END OF DEBUG______")