1 | import xmlrpclib |
---|
2 | import os |
---|
3 | import shutil |
---|
4 | import subprocess |
---|
5 | |
---|
6 | class LliurexUpCore(object): |
---|
7 | """docstring for LliurexUpCore""" |
---|
8 | def __init__(self): |
---|
9 | super(LliurexUpCore, self).__init__() |
---|
10 | self.defaultMirror = 'llx16' |
---|
11 | self.defaultVersion = 'xenial' |
---|
12 | self.processPath = '/var/run/lliurex-up' |
---|
13 | self.processSourceslist = '/var/run/lliurex-up/sourceslist' |
---|
14 | self.n4d = xmlrpclib.ServerProxy('https://localhost:9779') |
---|
15 | self.haveLliurexMirror = False |
---|
16 | self.flavours = self.n4d.lliurex_version('','LliurexVersion','-v')[1].split(',') |
---|
17 | if len(self.n4d.get_methods('MirrorManager')) > 0: |
---|
18 | self.haveLliurexMirror = True |
---|
19 | |
---|
20 | self.prepareEnvironment() |
---|
21 | |
---|
22 | def prepareEnvironment(self): |
---|
23 | if os.path.exists(self.processPath): |
---|
24 | shutil.rmtree(os.path.join(self.processPath)) |
---|
25 | os.mkdir(self.processPath) |
---|
26 | os.mkdir(self.processSourceslist) |
---|
27 | self.writeDefaultSourceslist() |
---|
28 | |
---|
29 | def writeDefaultSourceslist(self): |
---|
30 | f = open(os.path.join(self.processSourceslist,'default'),'w') |
---|
31 | f.write('deb http://lliurex.net/{version} {version} main'.format(version=self.defaultVersion)) |
---|
32 | f.write('deb http://lliurex.net/{version} {version}-updates main'.format(version=self.defaultVersion)) |
---|
33 | f.write('deb http://lliurex.net/{version} {version}-security main'.format(version=self.defaultVersion)) |
---|
34 | f.close() |
---|
35 | |
---|
36 | def getPackageVersionAvailable(self,package,options=""): |
---|
37 | command = "LANG=C apt-cache policy {package} {options}".format(package=package,options=options) |
---|
38 | p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
39 | installed = None |
---|
40 | candidate = None |
---|
41 | for line in iter(p.stdout.readline,""): |
---|
42 | stripedline = line.strip() |
---|
43 | if stripedline.startswith("Installed") |
---|
44 | installed = stripedline.replace("Installed: ") |
---|
45 | if stripedline.startswith("Candidate") |
---|
46 | candidate = stripedline.replace("Candidate: ") |
---|
47 | return {"installed":installed,"candidate":candidate} |
---|
48 | |
---|
49 | def checkLliurexUpIsUpdated(self): |
---|
50 | sourceslistDefaultPath = os.path.join(self.processSourceslist,'default') |
---|
51 | options = "-o Dir::Etc::sourcelist={sourceslistOnlyLliurex} -o Dir::Etc::sourceparts=/dev/null".format(sourceslistOnlyLliurex=sourceslistDefaultPath) |
---|
52 | command = "LANG=C apt-get update {options}".format(options=options) |
---|
53 | subprocess.Popen(command,shell=True).communicate() |
---|
54 | result = self.getPackageVersionAvailable('lliurex-up',options) |
---|
55 | if result['installed'] != result['candidate']: |
---|
56 | command = "LANG=C apt-get install lliurex-up {options}".format(options=options) |
---|
57 | p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
58 | p.communicate() |
---|
59 | return "reboot" |
---|
60 | return "nothing" |
---|
61 | |
---|
62 | |
---|
63 | def lliurexMirrorIsUpdated(self): |
---|
64 | if self.haveLliurexMirror and 'server' in self.flavours : |
---|
65 | result = self.n4d.is_update_available('','MirrorManager',self.defaultMirror) |
---|
66 | return result |
---|
67 | return None |
---|
68 | |
---|
69 | def lliurexMirrorIsRunning(self): |
---|
70 | if self.haveLliurexMirror and 'server' in self.flavours : |
---|
71 | result = self.n4d.is_alive('','MirrorManager') |
---|
72 | return result['status'] |
---|
73 | return False |
---|
74 | |
---|
75 | def getPercentageLliurexMirror(self): |
---|
76 | if self.haveLliurexMirror and 'server' in self.flavours: |
---|
77 | result = self.n4d.get_percentage('','MirrorManager',self.defaultMirror) |
---|
78 | if result['status']: |
---|
79 | return result['msg'] |
---|
80 | return None |
---|
81 | |
---|
82 | def checkFlavour(self): |
---|
83 | targetMetapackage = None |
---|
84 | if 'None' in self.flavours: |
---|
85 | # get last flavour |
---|
86 | result = self.n4d.lliurex_version('','LliurexVersion','--history') |
---|
87 | if result[0]: |
---|
88 | history = [ x.strip().split('\t')[0].strip() for x in result[1].split('\n') ] |
---|
89 | history = [ x for x in history if not 'lliurex-meta-live' in x ] |
---|
90 | for x in reversed(history): |
---|
91 | if x.startswith('-'): |
---|
92 | targetMetapackage = x[2:] |
---|
93 | break |
---|
94 | if targetMetapackage != '': |
---|
95 | return targetMetapackage |
---|