summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-config/mv_common.py
blob: 34d8a58610a7f59a0fab2d24a1e382c9c33859cd (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
# -*- coding: utf-8 -*-
import logging, os, time
import commands
import subprocess
import ConfigParser
import urllib2
config_file = "mv_config"
data_config = __import__(config_file, globals(), locals(), [])

module_config = ConfigParser.RawConfigParser()
try:
    module_config.read('/etc/mythvantage.cfg')
except:
    logging.info("couldn't read mythvantage.cfg")

class config_extra_reader(object):
    def __init__(self,url):
        self.url=url
        self.Config = ConfigParser.ConfigParser()
        req = urllib2.Request(url)
        response = urllib2.urlopen(req)
        self.Config.readfp(response)

    def print_url(self):
        print self.url


    def print_sections(self):
        print self.Config.sections()


    def read_config(self,data):
        rvalue = True
        section = "extra"
        try:
            rvalue = self.Config.get(section, data)
            if rvalue == "False":
                rvalue = False
            if rvalue == "no":
                rvalue = False
        except:
            logging.debug("Couldn't read config_file")
            rvalue = True

        return rvalue



def read_config(module_config,data):
    rvalue = True
    section = "mythvantage"
    try:
        rvalue = module_config.get(section, data)
        if rvalue == "False":
            rvalue = False
        if rvalue == "no":
            rvalue = False


    except:
        logging.debug("Couldn't read mythvantage.cfg")
        rvalue = True

    return rvalue


def runcmd_bg(cmd):
    subprocess.Popen(cmd)

def runcmd(cmd):
    if data_config.NOOPDEBUG=="FALSE":
        pass
    else:
        cmd = "echo "+cmd
    logging.debug("    %s",cmd)
    cmdout = commands.getstatusoutput(cmd)
    logging.debug("    %s",cmdout)
    return cmdout[0]

def runcmd_output(cmd):
    if data_config.NOOPDEBUG=="FALSE":
        pass
    else:
        cmd = "echo "+cmd
    logging.debug("    %s",cmd)
    cmdout = commands.getstatusoutput(cmd)
    logging.debug("    %s",cmdout)
    return cmdout[1]

def services(systemconfig):
    logging.debug("______Start of services______")
    logging.debug("__End services")

def cp_and_log(srcfile,destfile):
    #return
    if not os.path.exists(srcfile):
        logging.info("%s is not present, skipping...",srcfile)
    else:
        cmd = ("rsync -arhp %s %s") %(srcfile,destfile)
        runcmd(cmd)

def link_file(srcfile,link_name):
    logging.info("    Trying to link %s ->  %s",srcfile,link_name)
    if not os.path.exists(srcfile):
        logging.info("    %s is not present, skipping...",srcfile)
    elif os.path.exists(link_name):
        logging.info("    %s is present, skipping...",link_name)
    else:
        try:
            os.symlink(srcfile, link_name)
        except:
            logging.info("    Problem linking files occured")

    return

def mkdir_mythhome(mythhome,user,grp=1000):
     if not os.path.exists(mythhome+"/.mythtv"):
        logging.debug("    Creating %s/.mythtv",mythhome)
        try:
            os.mkdir(mythhome+"/.mythtv")
        except:
            logging.debug("   Couldn't create .mythtv ")

        try:
            #os.chown(mythhome+"/.mythtv", int(user), int(grp))
            #logging.debug("*    Couldn't chown of %s", mythhome)

            cmd = ''' chown -R %s %s/.mythtv''' %(user,mythhome)
            runcmd(cmd)
            cmd = ''' chgrp -R %s %s/.mythtv''' %(grp,mythhome)
            runcmd(cmd)
        except:
            logging.debug("*    Couldn't chown of %s", mythhome)
            pass



def add_service(daemon):
    logging.info("    Adding service %s",daemon)
    cmd = "add_service.sh %s" %daemon
    runcmd(cmd)

def remove_service(daemon):
    logging.info("    Removing service %s",daemon)
    stop_service(daemon)
    cmd = "remove_service.sh %s" %daemon
    runcmd(cmd)

def start_service(daemon):
    logging.info("    start service %s",daemon)
    cmd = "sv start %s" %daemon
    runcmd(cmd)

def stop_service(daemon):
    logging.info("    stop service %s",daemon)
    cmd = "sv stop %s" %daemon
    runcmd(cmd)

def restart_service(daemon):
    logging.info("    Restarting service %s",daemon)
    if daemon == "lcdd":
        stop_service(daemon)
        logging.debug("    killing all lcdd")
        cmd = "killall -9 LCDd"
        runcmd(cmd)
        time.sleep(2)
        start_service(daemon)
    else:
        cmd = "sv restart %s" %daemon
        runcmd(cmd)

def check_service(daemon):
    logging.info("    Checking status of service %s",daemon)
    cmd = "sv status %s" %daemon
    results = runcmd_output(cmd)
    status = results.split()[0]
    if status == "run:":
        return True
    else:
        return False

def hup_service(daemon):
    logging.info("    hup service %s",daemon)
    cmd = "sv hup %s" %daemon
    runcmd(cmd)



def pkg_blacklist_check(pkg):
    cmd = '''grep -q %s /etc/blacklist.package''' %pkg
    rc = runcmd(cmd)
    if rc == 0:
        return True
    else:
        return False

def pkg_installed_check(pkg):
    logging.debug("    Checking if %s is installed",pkg)
    cmd = "pacman -Q %s " %pkg
    rc = runcmd(cmd)
    if rc == 0:
        return True
    else:
        return False


def pacinstall(pkg):
    logging.info("    Checking %s for install",pkg)
    #extra pkg check
    if pkg == "xine":
        pacinstall("xine-ui")
    elif pkg == "dvdcss":
        pacinstall("libdvdcss")
    elif pkg == "webmin":
        pacinstall("webmin")
        add_service("webmin")
    elif pkg == "fuppes":
        pacinstall("fuppes-svn")
    elif pkg == "mupen64":
        pacinstall("mupen64plus-svn")
    elif pkg == "dolphinemu":
        pacinstall("dolphin-emu")
    elif pkg == "webonlinhes":
        pacinstall("web-on-linhes")
    elif pkg == "mame":
         pacinstall("sdlmame")
    elif pkg == "foldingathome":
         add_service("fah")

    elif pkg == "xymonclient":
        if os.path.exists('/home/xymon/client'):
            cmd='''pacman --noconfirm --dbonly -Sdd --force xymonclient'''
            runcmd(cmd)

    if  not pkg_blacklist_check(pkg):
        if pkg_installed_check(pkg):
            logging.info("    %s is already installed, will not install",pkg)
        else:
            logging.info("    Installing %s",pkg)
            cmd ='''pacman --noconfirm -S --force %s ''' %pkg
            runcmd(cmd)
    else:
        logging.info("    %s is blacklisted, will not install",pkg)

def pacremove(pkg):
    logging.info("    Checking %s for removal",pkg)
    if pkg == "xine":
        pacremove("xine-ui")
    elif pkg == "dvdcss":
        pacremove("libdvdcss")
    elif pkg == "webmin":
        remove_service("webmin")
    elif pkg == "fuppes":
        pacremove("fuppes-svn")
    elif pkg == "mupen64":
        pacremove("mupen64plus-svn")
    elif pkg == "dolphinemu":
        pacremove("dolphin-emu")
    elif pkg == "webonlinhes":
        pacremove("web-on-linhes")
    elif pkg == "mame":
        pacremove("sdlmame")
    elif pkg == "foldingathome":
        remove_service("fah")


    if  not pkg_blacklist_check(pkg):
        if not pkg_installed_check(pkg):
            logging.info("    %s is not installed, will not remove",pkg)
        else:
            logging.info("    Removing %s",pkg)
            cmd ='''pacman --noconfirm -R  %s ''' %pkg
            runcmd(cmd)
    else:
        logging.info("    %s is blacklisted, will not remove",pkg)

def getpid(process):
    return commands.getoutput('pidof %s' % process)

def remove_file(filename):
    logging.debug("    Removing %s", filename)
    try:
        os.remove(filename)
    except:
        logging.debug("*     Could not remove %s", filename)


def restartLCD(RESTART_LCD):
    if  RESTART_LCD :
        logging.info("    Restarting lcd server")
        cmd = "killall -9 mythlcdserver"
        runcmd(cmd)
    else:
        logging.debug("    Not restarting MYTHLCD server")


def reloadfe(dbhost,RESTART_LCD):
    logging.debug("____Start of reloadfe____")
    if data_config.SYSTEMTYPE == "MythVantage":
        logging.info("    Clearing Backend cache")
        cmd = '''/usr/bin/backend_control.sh  clearcache  behost %s''' %dbhost
        runcmd(cmd)

    restartLCD(RESTART_LCD)
#    for pid in getpid("mythfrontend"):
    pid = getpid("mythfrontend")
    cmd = ''' kill -s USR1 %s ''' %pid
    runcmd(cmd)
    logging.info("    Reloading frontend with pid of %s",pid)

    logging.debug("__End of reloadfe\n")


def restartfe(RESTART_LCD):
    logging.debug("____Start of restartfe____")
    if data_config.SYSTEMTYPE=="LinHES":
        logging.debug("    LinHES seems to be running will not restartfe")
        return
    logging.info("    Restarting frontend")
    restartLCD(RESTART_LCD)
    cmd="killall -9 mythfrontend"
    runcmd(cmd)
    cmd="killall -9 welcome"
    runcmd(cmd)
    logging.debug("__End of restartfe\n")


def udev_trigger():
    logging.info("    Triggering udev")
    cmd = "udevadm settle"
    runcmd(cmd)
    cmd = "udevadm trigger"
    runcmd(cmd)
    cmd = "udevadm settle"
    runcmd(cmd)
    cmd = "udevadm trigger"
    runcmd(cmd)