1 | import xmlrpclib |
---|
2 | import os |
---|
3 | import shutil |
---|
4 | import subprocess |
---|
5 | import socket |
---|
6 | import distutils.dir_util |
---|
7 | |
---|
8 | class LliurexUpCore(object): |
---|
9 | """docstring for LliurexUpCore""" |
---|
10 | def __init__(self): |
---|
11 | super(LliurexUpCore, self).__init__() |
---|
12 | self.defaultMirror = 'llx16' |
---|
13 | self.defaultVersion = 'xenial' |
---|
14 | self.processPath = '/var/run/lliurex-up' |
---|
15 | self.changelogsPath = os.path.join(self.processPath,'changelogs') |
---|
16 | self.processSourceslist = os.path.join(self.processPath,'sourceslist') |
---|
17 | self.targetMetapackagePath=os.path.join(self.processPath,"targetMetapackage") |
---|
18 | self.previousflavourspath = os.path.join(self.processPath,'previousflavours') |
---|
19 | self.preActionsPath = '/usr/share/lliurex-up/preActions' |
---|
20 | self.postActionsPath = '/usr/share/lliurex-up/postActions' |
---|
21 | |
---|
22 | self.n4d = xmlrpclib.ServerProxy('https://localhost:9779') |
---|
23 | self.haveLliurexMirror = False |
---|
24 | self.metapackageRef=[] |
---|
25 | self.previuosFlavours = [] |
---|
26 | self.getTargetMetapackage() |
---|
27 | self.flavours = [] |
---|
28 | self.getPreviuosFlavours() |
---|
29 | if len(self.n4d.get_methods('MirrorManager')) > 0: |
---|
30 | self.haveLliurexMirror = True |
---|
31 | self.prepareEnvironment() |
---|
32 | |
---|
33 | def getPreviuosFlavours(self): |
---|
34 | |
---|
35 | if os.path.exists(self.previousflavourspath): |
---|
36 | aux = open(self.previousflavourspath,'r') |
---|
37 | lines = aux.readlines() |
---|
38 | for x in lines: |
---|
39 | self.previuosFlavours.append(x.strip()) |
---|
40 | aux.close() |
---|
41 | |
---|
42 | |
---|
43 | def getTargetMetapackage(self): |
---|
44 | |
---|
45 | if os.path.exists(self.targetMetapackagePath): |
---|
46 | aux = open(self.targetMetapackagePath,'r') |
---|
47 | lines = aux.readlines() |
---|
48 | for x in lines: |
---|
49 | self.metapackageRef.append(x.strip()) |
---|
50 | aux.close() |
---|
51 | |
---|
52 | def saveTargetMetapackage(self,targetMetapackage): |
---|
53 | |
---|
54 | aux=open(self.targetMetapackagePath,'w') |
---|
55 | x=targetMetapackage.split("-")[2] |
---|
56 | aux.write(x+"\n") |
---|
57 | x="edu" |
---|
58 | aux.write(x+"\n") |
---|
59 | aux.close() |
---|
60 | |
---|
61 | |
---|
62 | def updateFlavoursList(self): |
---|
63 | self.flavours = [ x.strip() for x in self.n4d.lliurex_version('','LliurexVersion','-v')[1].split(',') ] |
---|
64 | if len(self.flavours) > 0: |
---|
65 | aux = open(self.previousflavourspath,'w') |
---|
66 | for x in self.flavours: |
---|
67 | aux.write(x+"\n") |
---|
68 | aux.close() |
---|
69 | |
---|
70 | def writeDefaultSourceslist(self): |
---|
71 | f = open(os.path.join(self.processSourceslist,'default'),'w') |
---|
72 | f.write('deb http://lliurex.net/{version} {version} main\n'.format(version=self.defaultVersion)) |
---|
73 | f.write('deb http://lliurex.net/{version} {version}-updates main\n'.format(version=self.defaultVersion)) |
---|
74 | f.write('deb http://lliurex.net/{version} {version}-security main\n'.format(version=self.defaultVersion)) |
---|
75 | f.close() |
---|
76 | |
---|
77 | def prepareEnvironment(self): |
---|
78 | ''' |
---|
79 | This funcion delete all environment and rebuild environment |
---|
80 | ''' |
---|
81 | if not os.path.exists(self.processPath): |
---|
82 | os.mkdir(self.processPath) |
---|
83 | if not os.path.exists(self.processSourceslist): |
---|
84 | os.mkdir(self.processSourceslist) |
---|
85 | if not os.path.exists(self.changelogsPath): |
---|
86 | os.mkdir(self.changelogsPath) |
---|
87 | self.writeDefaultSourceslist() |
---|
88 | |
---|
89 | def cleanEnvironment(self): |
---|
90 | if os.path.exists(self.processPath): |
---|
91 | shutil.rmtree(os.path.join(self.processPath)) |
---|
92 | |
---|
93 | |
---|
94 | def updateCacheApt(self,options=""): |
---|
95 | command = "LANG=C apt-get update {options}".format(options=options) |
---|
96 | subprocess.Popen(command,shell=True).communicate() |
---|
97 | |
---|
98 | |
---|
99 | def getPackageVersionAvailable(self,package,options=""): |
---|
100 | ''' |
---|
101 | Args : |
---|
102 | package String |
---|
103 | options String |
---|
104 | |
---|
105 | return dictionary => result |
---|
106 | result : {'installed':String,'candidate':String} |
---|
107 | |
---|
108 | Options are Apt options |
---|
109 | ''' |
---|
110 | command = "LANG=C apt-cache policy {package} {options}".format(package=package,options=options) |
---|
111 | p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
112 | installed = None |
---|
113 | candidate = None |
---|
114 | for line in iter(p.stdout.readline,""): |
---|
115 | stripedline = line.strip() |
---|
116 | if stripedline.startswith("Installed"): |
---|
117 | installed = stripedline.replace("Installed: ","") |
---|
118 | if stripedline.startswith("Candidate"): |
---|
119 | candidate = stripedline.replace("Candidate: ","") |
---|
120 | return {"installed":installed,"candidate":candidate} |
---|
121 | |
---|
122 | def isLliurexUpIsUpdated(self): |
---|
123 | ''' |
---|
124 | return Boolean |
---|
125 | ''' |
---|
126 | sourceslistDefaultPath = os.path.join(self.processSourceslist,'default') |
---|
127 | options = "" |
---|
128 | if self.canConnectToLliurexNet(): |
---|
129 | options = "-o Dir::Etc::sourcelist={sourceslistOnlyLliurex} -o Dir::Etc::sourceparts=/dev/null".format(sourceslistOnlyLliurex=sourceslistDefaultPath) |
---|
130 | |
---|
131 | self.updateCacheApt(options) |
---|
132 | result = self.getPackageVersionAvailable('lliurex-up',options) |
---|
133 | |
---|
134 | if result['installed'] != result['candidate']: |
---|
135 | return False |
---|
136 | return True |
---|
137 | |
---|
138 | def installLliurexUp(self,options=""): |
---|
139 | ''' |
---|
140 | Args : |
---|
141 | options String |
---|
142 | return dictionary => result |
---|
143 | result : {'returncode':Int,'stdout':String,'stderr':String} |
---|
144 | |
---|
145 | options are Apt options |
---|
146 | |
---|
147 | |
---|
148 | This function install lliurex-up |
---|
149 | ''' |
---|
150 | command = "LANG=C apt-get install testdisk {options}".format(options=options) |
---|
151 | p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
152 | poutput,perror = p.communicate() |
---|
153 | return {'returncode':p.returncode,'stdout':poutput,'stderrs':perror} |
---|
154 | |
---|
155 | def lliurexMirrorIsUpdated(self): |
---|
156 | ''' |
---|
157 | return None | dictionary => result |
---|
158 | result : {'status':Boolean,'msg':String,'action':String} |
---|
159 | result.msg : message of status |
---|
160 | result.action : Action to launch |
---|
161 | ''' |
---|
162 | if self.haveLliurexMirror and 'server' in self.flavours : |
---|
163 | result = self.n4d.is_update_available('','MirrorManager',self.defaultMirror) |
---|
164 | return result |
---|
165 | return None |
---|
166 | |
---|
167 | def lliurexMirrorIsRunning(self): |
---|
168 | ''' |
---|
169 | return Boolean |
---|
170 | ''' |
---|
171 | if self.haveLliurexMirror and 'server' in self.flavours : |
---|
172 | result = self.n4d.is_alive('','MirrorManager') |
---|
173 | return result['status'] |
---|
174 | return False |
---|
175 | |
---|
176 | def getPercentageLliurexMirror(self): |
---|
177 | ''' |
---|
178 | return int | None |
---|
179 | ''' |
---|
180 | if self.haveLliurexMirror and 'server' in self.flavours: |
---|
181 | result = self.n4d.get_percentage('','MirrorManager',self.defaultMirror) |
---|
182 | if result['status']: |
---|
183 | return result['msg'] |
---|
184 | return None |
---|
185 | |
---|
186 | def checkFlavour(self): |
---|
187 | ''' |
---|
188 | return None|String |
---|
189 | If metapackages has been uninstalled, this function return |
---|
190 | package to must install. If return None, you are ok and don't need |
---|
191 | install anything. |
---|
192 | ''' |
---|
193 | self.updateFlavoursList() |
---|
194 | targetMetapackage = None |
---|
195 | if 'None' in self.flavours: |
---|
196 | # get last flavour |
---|
197 | result = self.n4d.lliurex_version('','LliurexVersion','--history') |
---|
198 | if result[0]: |
---|
199 | history = [ x.strip().split('\t')[0].strip() for x in result[1].split('\n') ] |
---|
200 | history = [ x for x in history if not 'lliurex-meta-live' in x ] |
---|
201 | for x in reversed(history): |
---|
202 | if x.startswith('-'): |
---|
203 | targetMetapackage = x[2:] |
---|
204 | break |
---|
205 | |
---|
206 | if targetMetapackage !=None: |
---|
207 | self.saveTargetMetapackage(targetMetapackage) |
---|
208 | return targetMetapackage |
---|
209 | |
---|
210 | def canConnectToLliurexNet(self): |
---|
211 | ''' |
---|
212 | return Boolean |
---|
213 | ''' |
---|
214 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
215 | host = socket.gethostbyname('lliurex.net') |
---|
216 | result = s.connect_ex((host, 80)) |
---|
217 | s.close() |
---|
218 | if result: |
---|
219 | return False |
---|
220 | return True |
---|
221 | |
---|
222 | def getLliurexVersionLliurexNet(self): |
---|
223 | ''' |
---|
224 | return dictionary => result |
---|
225 | result : {'installed':String,'candidate':String} |
---|
226 | ''' |
---|
227 | sourceslistDefaultPath = os.path.join(self.processSourceslist,'default') |
---|
228 | options = "" |
---|
229 | if self.canConnectToLliurexNet(): |
---|
230 | options = "-o Dir::Etc::sourcelist={sourceslistOnlyLliurex} -o Dir::Etc::sourceparts=/dev/null".format(sourceslistOnlyLliurex=sourceslistDefaultPath) |
---|
231 | self.updateCacheApt(options) |
---|
232 | return self.getPackageVersionAvailable('lliurex-version-timestamp',options) |
---|
233 | |
---|
234 | def getLliurexVersionLocal(self): |
---|
235 | self.updateCacheApt('') |
---|
236 | return self.getPackageVersionAvailable('lliurex-version-timestamp','') |
---|
237 | |
---|
238 | def preActionsScript(self): |
---|
239 | return 'run-parts --arg="preActions" ' + self.preActionsPath |
---|
240 | |
---|
241 | def postActionsScript(self): |
---|
242 | return 'run-parts --arg="postActions" ' + self.postActionsPath |
---|
243 | |
---|
244 | |
---|
245 | def installInitialFlavour(self,flavourToInstall,options=""): |
---|
246 | ''' |
---|
247 | Args : |
---|
248 | flavourToInstall String |
---|
249 | options String |
---|
250 | return dictionary => result |
---|
251 | result : {'returncode':Int,'stdout':String,'stderr':String} |
---|
252 | |
---|
253 | options are Apt options |
---|
254 | |
---|
255 | |
---|
256 | This function install lliurex-up |
---|
257 | ''' |
---|
258 | command = "LANG=C apt-get install " + flavourToInstall + "{options} ".format(options=options) |
---|
259 | p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
260 | poutput,perror = p.communicate() |
---|
261 | return {'returncode':p.returncode,'stdout':poutput,'stderrs':perror} |
---|
262 | |
---|
263 | def getPackagesToUpdate(self): |
---|
264 | ''' |
---|
265 | packageInfo definition |
---|
266 | { |
---|
267 | 'PACKAGENAME' : { |
---|
268 | 'install' : 'INSTALLEDVERSION', |
---|
269 | 'candidate' : 'CANDIDATEVERSION', |
---|
270 | 'icon' : 'ICONNAME', |
---|
271 | 'changelog' : 'CHANGELOGTEXT' |
---|
272 | } |
---|
273 | } |
---|
274 | ''' |
---|
275 | packageInfo = {} |
---|
276 | self.updateCacheApt("") |
---|
277 | psimulate = subprocess.Popen('LANG=C apt-get dist-upgrade -sV',shell=True,stdout=subprocess.PIPE) |
---|
278 | rawoutputpsimulate = psimulate.stdout.readlines() |
---|
279 | rawpackagestoinstall = [ aux.strip() for aux in rawoutputpsimulate if aux.startswith('Inst') ] |
---|
280 | r = [ aux.replace('Inst ','') for aux in rawpackagestoinstall ] |
---|
281 | for allinfo in r : |
---|
282 | packageInfo[allinfo.split(' ')[0]] = {} |
---|
283 | packageInfo[allinfo.split(' ')[0]]['raw'] = ' '.join(allinfo.split(' ')[1:]) |
---|
284 | |
---|
285 | for package in packageInfo: |
---|
286 | raw = packageInfo[package]['raw'].split(' ') |
---|
287 | if raw[0].startswith('['): |
---|
288 | packageInfo[package]['install'] = raw[0][1:-1] |
---|
289 | packageInfo[package]['candidate'] = raw[1][1:] |
---|
290 | elif raw[0].startswith('('): |
---|
291 | packageInfo[package]['install'] = None |
---|
292 | packageInfo[package]['candidate'] = raw[0][1:] |
---|
293 | packageInfo[package].pop('raw') |
---|
294 | #packageInfo[package]['changelog'] = os.path.join(self.changelogsPath,package) |
---|
295 | #os.system('LANG=C apt-get changelog %s > %s%s'%(package,self.changelogsPath,package)) |
---|
296 | #packageInfo[package]['icon'] = |
---|
297 | return packageInfo |
---|
298 | |
---|
299 | |
---|
300 | def distUpgradeProcess(self): |
---|
301 | |
---|
302 | return 'apt-get dist-upgrade --yes --force-yes' |
---|
303 | |
---|
304 | |
---|
305 | def installFinalFlavour(self,flavourToInstall): |
---|
306 | |
---|
307 | return 'apt-get install ' + flavourToInstall + ' --yes --force-yes' |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | if __name__ == '__main__': |
---|
312 | x = LliurexUpCore() |
---|