summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-config/mv_advanced.py
blob: 2b53d04dd88c9dfc36e2b1e21fa514cf1e187f2c (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
# -*- coding: utf-8 -*-
import logging, os, re
import mv_common

def setup_pacman(create_link):
    if create_link:
        logging.info("    Creating pacman link")
        #LOOK
        #This is not implmented yet, below is the bash code for reference
        #if [ ! x$1 = x ]
    #then
        #ln -s ${BASE}/data/var ${BASE}/data/srv/httpd/htdocs/repo
        #echo "creating the link for the pacman repo mirror"
    #fi

    #for i in mv-core mv-core-testing mv-extra mv-extra-testing
    #do
        #echo "[$i]"  > ${BASE}/etc/pacman.d/$i
        ##add mirror if needed
        #if [ x$PKG_MIRROR = x1 ]
        #then
            #echo " Server = http://$dbhost/repo/$i " >> ${BASE}/etc/pacman.d/$i
        #fi
        ##add user templates
        #USERTEMPLATES="/data/home/mythtv/templates"
        #if [ -f $USERTEMPLATES/sources/$i ]
        #then
            #echo "Adding user  $i"
            #cat $USERTEMPLATES/sources/$i  >> ${BASE}/etc/pacman.d/$i
        #fi
        #cat $TEMPLATES/sources/$i  >> ${BASE}/etc/pacman.d/$i
        #echo "setting local mirror to $dbhost for $i "
    #done
    else:
        logging.info("    Pacman link will not be created")
    return

def setup_ncid_client(dbhost,templatefile):
    #This always runs
    logging.debug("    Configuring ncid client with server %s",dbhost)
    cmd = '''sed -e "s/^set Host.*$/set Host %s /" %s >/etc/ncid/ncid.conf''' %(dbhost, templatefile)
    return

def setup_ncid_daemon(port,templatefile,Runncidd):
    logging.info("    Configuring callerid daemon")
    if Runncidd == "1":
        logging.debug("    Port: %s",port)
        mv_common.cp_and_log("/etc/ncid/ncidd.conf",templatefile)
        cmd = '''sed -e "s/.*set ttyport.*$/set ttyport =  \/dev\/%s /" %s >/etc/ncid/ncidd.conf''' %(port,templatefile)
        mv_common.runcmd(cmd)
        mv_common.pacinstall("ncidd")
        mv_common.add_service("ncidd")
    else:
        logging.debug("    Callerid will not run")
        mv_common.pacremove("ncidd")
        mv_common.remove_service("ncidd")
    return

def setup_splash(Usebootsplash):
    if Usebootsplash == "1":
        logging.info("    Enabling splash")
        cmd = ''' sed -i "s/^default.*0/default 1/g" /boot/grub/menu.lst'''
        mv_common.runcmd(cmd)
    else:
        logging.info("    Disabling splash")
        cmd = ''' sed -i "s/^default.*1/default 0/g" /boot/grub/menu.lst'''
        mv_common.runcmd(cmd)
    return
def setup_rrd(UseRRD):
    if UseRRD == "1":
        logging.info("    Enabling rrd_stats")
        mv_common.pacinstall("rrd_stats")
    else:
        logging.info("    Disabling rrd_stats")
        mv_common.pacremove("rrd_stats")


def setup_hobbit(UseHobbit,SystemType,dbhost):
    if UseHobbit == "1":
        if SystemType == "Standalone" or SystemType == "Master_backend":
            logging.info("    Installing hobbit server")
            mv_common.pacinstall("hobbitserver")
            mv_common.add_service("hobbit")
        else:
            logging.info("    Installing hobbit client")
            mv_common.pacinstall("hobbit-client")
            setup_hobbit_client(dbhost)
            mv_common.add_service("hobbit-client")

    else:
            logging.info("    Removing Hobbit")
            mv_common.pacremove("hobbit")
            mv_common.pacremove("hobbit-client")
            mv_common.remove_service("hobbit-client")
            mv_common.remove_service("hobbitserver")
            cmd = "rm -f /data/srv/httpd/htdocs/hobbit/index.html"
            mv_common.runcmd(cmd)
    return

def setup_hobbit_client(dbhost):
    logging.info("    Configuring hobbit client")
    logging.debug("   hobbit server is %s:", dbhost)
    cmd='''sed -i "s/^BBDISP.*$/BBDISP=%s/g" /data/srv/hobbit/client/etc/hobbitclient.cfg ''' %dbhost
    mv_common.runcmd(cmd)
    return

def setup_evrouter(UseEvrouter,EvrouterConfig,template):
    if UseEvrouter == "1":
        logging.info("    Configuring everouter")
        logging.debug("    EvrouterConfig is %s:",EvrouterConfig)
        if EvrouterConfig == "tinker":
            logging.debug("    tinker mode, not changing the config")
            return
        mv_common.pacinstall("evrouter")
        mv_common.pacinstall("Xvfb")
        mv_common.cp_and_log(template+"/evrouter/"+EvrouterConfig,"/etc/evrouter.cfg")
        mv_common.add_service("evrouter")
        mv_common.add_service("Xvfb")
    else:
        logging.info("    Not using evrouter")
        mv_common.pacremove("evrouter")
        mv_common.remove_service("evrouter")
        mv_common.remove_service("Xvfb")

    return

def setup_DNSmasq(RunDHCP,ip,gw,nameserver):
    if RunDHCP == "1":
        logging.info("    Configuring dhcp server(dnsmasq)")
        logging.debug("    ip: %s", ip)
        logging.debug("    gw: %s", gw)
        logging.debug("    ns: %s", nameserver)
        mv_common.pacinstall("dnsmasq")
        mv_common.pacinstall("mvpmc")
        mv_common.add_service("dnsmasq")
        logging.debug("    Setting default route to my gw: %s",gw)
        try:
            f = open("/etc/dnsmasq.conf",'r')
            dnsconf = f.readlines()
            f.close()
        except:
                logging.info("    Couldn't open dnsmasq.conf")
                return
        for line in dnsconf:
            outline = line
            if re.match("^dhcp-option=3",line):
                logging.debug("    Setting default route to my gw: %s",gw)
                outline = "dhcp-option=3,%s\n" %gw
            if re.match("^dhcp-option=6",line):
                logging.debug("    Setting dns to my ip: %s",ip)
                outline = "dhcp-option=6,%s\n" %ip
            f.write(outline)
        f.close()

        logging.debug("    change nfsroot to my ip:%s",ip)
        cmd = '''sed -i "s/nfsroot=.*:/nfsroot=%s:/g" /data/srv/tftp/pxelinux.cfg/default''' %ip
        mv_common.runcmd(cmd)

        logging.debug("    Adding 127.0.0.1 to resolv.conf")
        logging.debug("    other nameserver is %s",nameserver)
        try:
            f = open("/etc/resolv.conf",'r')
            dns = f.readlines()
            f.close()
        except:
                dns=''
                logging.debug("   Couldn't open resolv.conf for reading")
        try:
            f = open("/etc/resolv.conf",'w')
            line="search lan\n"
            f.write(line)
            line="nameserver 127.0.0.1\n"
            f.write(line)
            line="nameserver %s\n" %nameserver
            f.write(line)
            for line in dns:
                f.write(line)
            f.close()
        except:
                logging.debug("   Couldn't open resolv.conf for writing")

        logging.debug("    setup dongle.config")
        cmd = ''' sed -i "s/mvpmc -f .*/mvpmc -f \/etc\/helvR10.fon -s %s \& /" /data/srv/tftp/dongle.bin.config ''' %ip
        #COMMAND="%s/mvpmc -f .*/mvpmc -f \/etc\/helvR10.fon -s ${ip} \& /"
        #ex ${BASE}/data/srv/tftp/dongle.bin.config <<EOF
        mv_common.runcmd(cmd)
    else:
        logging.info("    removing dhcp server(dnsmasq)")
        mv_common.pacremove("dnsmasq")
        mv_common.pacremove("mvpmc")
        mv_common.remove_service("dnsmasq")
    return

def setup_mythweb(UseMythWEB):
    if UseMythWEB == "1":
        logging.info("    Activating mythweb")
        mv_common.pacinstall("lighttpd")
        mv_common.pacinstall("mythweb")
        mv_common.add_service("lighttpd")
    else:
        logging.info("    Removing mythweb")
        mv_common.pacremove("lighttpd")
        mv_common.pacremove("mythweb")
        mv_common.remove_service("lighttpd")
    return

def setup_samba(systemconfig,data_config):
    mythhome = data_config.MYTHHOME
    if systemconfig["UseSamba"] == "1":
        logging.info("    Activating windows file sharing")
        usersamba=mythhome+"/templates/smb.conf"
        mv_common.pacinstall("samba")
        if not os.path.exists("/etc/samba"):
            logging.debug("    Creating directory /etc/samba")
            try:
                os.makedirs("/etc/samba")
            except:
                pass

        if os.path.exists(usersamba):
            logging.debug("    Using user provided config file %s",usersamba)
            cmd = "install -D -m755  %s /etc/samba/smb.conf" %usersamba
            mv_common.runcmd(cmd)
        else:
            Samba_media = systemconfig["Samba_media"]
            Samba_home = systemconfig["Samba_home"]
            if systemconfig["Samba_mediareadonly"] == "1":
                smreadonly = "yes"
            else:
                smreadonly = "no"

            if systemconfig["Samba_homereadonly"] == "1":
                shreadonly = "yes"
            else:
                shreadonly = "no"
            domain = systemconfig["Samba_domain"]
            servername = systemconfig["hostname"]

            try:
                f = open(systemconfig["TEMPLATES"]+"/samba/smb.conf.template",'r')
                t_smbconf = f.readlines()
                f.close()
            except:
                logging.info("   Couldn't open samba template file")
                return

            try:
                f = open("/etc/samba/smb.conf",'w')
            except:
                logging.info("   Couldn't open samba file")
                return

            for line in t_smbconf:
                outline = line
                if re.match("^.*workgroup", line):
                    logging.debug("    Setting workgroup to %s",domain)
                    outline="workgroup = %s\n" %domain
                    logging.debug("     %s",outline)
                if re.match("^.* server string",line):
                    logging.debug("    Setting server name to %s",servername)
                    outline="server string = %s\n" %servername
                    logging.debug("     %s",outline)
                f.write(outline)
            outline="include =  %s/templates/user.shares \n" %mythhome
            f.write(outline)
            if Samba_media == "1":
                outline="include =  /etc/samba/smb.conf.media\n"
                f.write(outline)
            if Samba_home == "1":
                outline="include =  /etc/samba/smb.conf.home\n"
                f.write(outline)
            f.close()
            logging.info("    Writing smb.conf.media")
            try:
                f = open("/etc/samba/smb.conf.media","w")
            except:
                logging.info("    Couldn't open smb.conf.media")
                return
            medialines='''
[%s]
path = %s
public = yes
only guest = yes
writeable  = %s
printable = no
force user = mythtv
force group = mythtv
create mask = 0755''' %(data_config.SMEDIA,data_config.DATAMOUNT,smreadonly)
            f.write(medialines)
            f.close
            logging.debug("     %s",medialines)
            logging.info("    Writing smb.conf.home")
            try:
                    f = open("/etc/samba/smb.conf.home","w")
            except:
                logging.info("    Couldn't open smb.conf.home")
                return
            homelines='''
[home]
path = %s
public = yes
only guest = yes
writeable  = %s
printable = no
force user = mythtv
force group = mythtv
create mask = 0755 ''' %(data_config.MYTHHOME,shreadonly)
            f.write(homelines)
            f.close
            logging.debug("     %s",homelines)


        mv_common.add_service("nmbd")
        mv_common.add_service("smbd")
    else:
        logging.info("  Removing windows file sharing")
        mv_common.remove_service("smbd")
        mv_common.remove_service("nmbd")
        mv_common.pacremove("samba")
        mv_common.pacinstall("smbclient")
    return

def setup_NFSshares(UseNFS,templatefile):
    if UseNFS == "1":
        logging.info("    Activating NFS server")
        mv_common.pacinstall("nfs-utils")
        mv_common.pacinstall("portmap")
        cmd = '''sed -e "s/REPLACEME/*/g" %s  >/etc/exports''' %templatefile
        mv_common.add_service("nfsd")
        mv_common.add_service("nfs-utils")
        mv_common.add_service("portmap")
    else:
        logging.info("    Removing NFS server")
        mv_common.remove_service("nfsd")
    return

def setup_dyndns(DDnsEnable):
    if DDnsEnable == "1":
        logging.info("    Installing Dynamic DNS client")
        mv_common.pacinstall("inadyn")
        mv_common.add_service("inadyn")
    else:
        logging.info("    Removing Dynamic DNS client")
        mv_common.pacremove("inadyn")
        mv_common.remove_service("inadyn")


def setup_advanced(systemconfig,data_config):
    logging.info("____Start of advanced configuration____")
    create_link = False
    if systemconfig["SystemType"] == "Master_backend":
        create_link = True
    setup_pacman(create_link)
    setup_ncid_client(systemconfig["dbhost"],
                      systemconfig["TEMPLATES"]+"/ncid.conf.template")

    setup_ncid_daemon(systemconfig["nciddSerialPort"],
                      systemconfig["TEMPLATES"]+"/ncidd.conf.template",
                      systemconfig["Runncidd"])

    setup_splash(systemconfig["Usebootsplash"])
    if data_config.SYSTEMTYPE == "MythVantage":
        setup_hobbit(systemconfig["UseHobbit"],
                    systemconfig["SystemType"],
                    systemconfig["dbhost"])
    if data_config.SYSTEMTYPE == "LinHES":
        setup_rrd(systemconfig["UseRRD"])

    setup_evrouter(systemconfig["UseEvrouter"],
                   systemconfig["EvrouterConfig"],
                   systemconfig["TEMPLATES"])

    setup_DNSmasq(systemconfig["RunDHCP"],
                  systemconfig["mythip"],
                  systemconfig["mythgw"],
                  systemconfig["mythdns"])

    setup_mythweb(systemconfig["UseMythWEB"])
    setup_samba(systemconfig,data_config)
    setup_NFSshares(systemconfig["UseNFS"],
                systemconfig["TEMPLATES"]+"/exports.template")

    setup_dyndns(systemconfig["DDnsEnable"])
    logging.info("__End of advanced configuration\n")