1 | import subprocess |
---|
2 | import os |
---|
3 | import urllib |
---|
4 | import apt |
---|
5 | import apt.debfile |
---|
6 | import types |
---|
7 | |
---|
8 | class LliureXRemoteInstallerClient: |
---|
9 | |
---|
10 | dir_tmp="/tmp/.LLXRemoteInstallerClient" |
---|
11 | N4D_VAR="LLX_REMOTE_INSTALLER" |
---|
12 | N4D_INSTALLED="LLX_REMOTE_INSTALLER_INSTALLED" |
---|
13 | |
---|
14 | #REPO ADDAPLICATION_SOURCES value |
---|
15 | dir_sources="/etc/apt/sources.list.d/" |
---|
16 | file_sources="llxremoteinstaller_sources.list" |
---|
17 | file_sources=str(dir_sources)+str(file_sources) |
---|
18 | |
---|
19 | #Dict values |
---|
20 | DEB='deb' |
---|
21 | APT='apt' |
---|
22 | SH='sh' |
---|
23 | LIST='packages' |
---|
24 | URL='url' |
---|
25 | |
---|
26 | #Essential package for provides |
---|
27 | pack_provide="dctrl-tools" |
---|
28 | |
---|
29 | #Installed apps |
---|
30 | LIST_APP_FINAL=[] |
---|
31 | |
---|
32 | def __init__(self): |
---|
33 | self.dbg=0 |
---|
34 | pass |
---|
35 | #def __init__ |
---|
36 | |
---|
37 | def _print_info(self,message): |
---|
38 | print("[RemoteInstallerClient] "+str(message)) |
---|
39 | |
---|
40 | def _debug(self,message): |
---|
41 | if self.dbg==1: |
---|
42 | print("RemoteInstallerClient: "+str(message)) |
---|
43 | self._log(message) |
---|
44 | |
---|
45 | def _log(self, message): |
---|
46 | logFile="/tmp/remoteInstall.log" |
---|
47 | if os.path.exists(logFile): |
---|
48 | f=open (logFile,"a") |
---|
49 | else: |
---|
50 | f=open (logFile,"w") |
---|
51 | f.write(str(message)+"\n") |
---|
52 | f.close() |
---|
53 | |
---|
54 | def startup(self,options): |
---|
55 | if os.path.exists(self.file_sources): |
---|
56 | os.remove(self.file_sources) |
---|
57 | |
---|
58 | if options["boot"]: |
---|
59 | if options["controlled"]: |
---|
60 | if os.system("host server")==0: |
---|
61 | objects["LliureXRemoteInstallerClient"].test_system() |
---|
62 | #def startup |
---|
63 | |
---|
64 | def create_dict (self,mode=[]): |
---|
65 | try: |
---|
66 | #Installed apps dict |
---|
67 | dict={} |
---|
68 | for mod in mode: |
---|
69 | dict[mod]=[] |
---|
70 | COMMENT="[LLXRemoteInstallerClient] (create_dict) Dictionary is created %s"%(dict) |
---|
71 | self._debug(COMMENT) |
---|
72 | return [True,str(COMMENT),dict] |
---|
73 | except Exception as e: |
---|
74 | print e |
---|
75 | return[False,str(e)] |
---|
76 | #def_create_dict |
---|
77 | |
---|
78 | def read_var (self,namevar=None,localhost=None): |
---|
79 | try: |
---|
80 | # if localhost in ["",None]: |
---|
81 | if not localhost: |
---|
82 | proxy="https://server:9779" |
---|
83 | else: |
---|
84 | proxy="https://localhost:9779" |
---|
85 | import xmlrpclib as x |
---|
86 | c=x.ServerProxy(proxy) |
---|
87 | DICT=c.get_variable("","VariablesManager",namevar) |
---|
88 | COMMENT="[LLXRemoteInstallerClient] (read_var) Value of N4D var %s of %s is %s"%(namevar,proxy,DICT) |
---|
89 | self._debug(COMMENT) |
---|
90 | return [True,str(COMMENT),DICT] |
---|
91 | except Exception as e: |
---|
92 | return [False,str(e)] |
---|
93 | #def_read_list |
---|
94 | |
---|
95 | def initialize_n4dvar(self,list_dict): |
---|
96 | self._debug("FNC: initialize_n4dvar") |
---|
97 | try: |
---|
98 | dict={} |
---|
99 | for x in list_dict: |
---|
100 | dict[x]={} |
---|
101 | if x == self.APT: |
---|
102 | self._debug("Apt mode") |
---|
103 | dict[x]['lliurex']={} |
---|
104 | dict[x]['lliurex'][self.LIST]=[] |
---|
105 | dict[x]['lliurex'][self.URL]=[] |
---|
106 | else: |
---|
107 | dict[x][self.URL]=[] |
---|
108 | dict[x][self.LIST]=[] |
---|
109 | COMMENT=("[LLXRemoteInstallerClient] (initialize_n4dvar) Dict initialized") |
---|
110 | self._debug(COMMENT) |
---|
111 | return [True,str(COMMENT),dict] |
---|
112 | except Exception as e: |
---|
113 | return [False,str(e)] |
---|
114 | |
---|
115 | #def_initialize_var |
---|
116 | |
---|
117 | #def test_var (self,namevar=None,localhost=None,user=None,passwd=None): |
---|
118 | def test_var (self,namevar=None,localhost=None): |
---|
119 | self._debug("test_var") |
---|
120 | try: |
---|
121 | # if localhost in ["",None]: |
---|
122 | if not localhost: |
---|
123 | proxy="https://server:9779" |
---|
124 | else: |
---|
125 | proxy="https://localhost:9779" |
---|
126 | self._debug("Proxy: "+proxy) |
---|
127 | self._debug("Localhost: "+str(localhost)) |
---|
128 | self._debug("Namevar: "+str(namevar)) |
---|
129 | import xmlrpclib as x |
---|
130 | c=x.ServerProxy(proxy) |
---|
131 | VALOR=c.get_variable("","VariablesManager",namevar) |
---|
132 | self._debug("[LLXRemoteInstallerClient] (test_var) Value of N4d var "+str(namevar)+" is: "+str(VALOR)) |
---|
133 | # if VALOR in [None,'','None']: |
---|
134 | if not VALOR: |
---|
135 | list_dict=[self.APT,self.DEB,self.SH] |
---|
136 | VALOR=self.create_dict ([self.APT,self.DEB,self.SH])[2] |
---|
137 | if objects["VariablesManager"].add_variable(namevar,VALOR,"",namevar,[],False,False)[0]: |
---|
138 | COMMENT = ("[LLXRemoteInstallerClient] (test_var) Added variable %s to VariablesManager with valor %s" %(namevar,VALOR)) |
---|
139 | self._debug(COMMENT) |
---|
140 | return [True,str(COMMENT)] |
---|
141 | else: |
---|
142 | COMMENT = ("[LLXRemoteInstallerClient] (test_var) Cannot create %s again in VariablesManager" %namevar) |
---|
143 | self._debug(COMMENT) |
---|
144 | return [True,str(COMMENT)] |
---|
145 | else: |
---|
146 | COMMENT=("[LLXRemoteInstallerClient] (test_var) %s Variable exists in your system, it hasn't been created again" %namevar) |
---|
147 | self._debug(COMMENT) |
---|
148 | return [True,str(COMMENT)] |
---|
149 | |
---|
150 | except Exception as e: |
---|
151 | self._debug("ERROR test_var" + str(e)) |
---|
152 | return [False,str(e)] |
---|
153 | #def_test_var |
---|
154 | |
---|
155 | def reset_var (self,namevar=None,localhost=None): |
---|
156 | self._debug("reset_var") |
---|
157 | try: |
---|
158 | # if localhost in ["",None]: |
---|
159 | if not localhost: |
---|
160 | proxy="https://server:9779" |
---|
161 | else: |
---|
162 | proxy="https://localhost:9779" |
---|
163 | import xmlrpclib as x |
---|
164 | c=x.ServerProxy(proxy) |
---|
165 | data=None |
---|
166 | objects["VariablesManager"].set_variable(namevar,data) |
---|
167 | COMMENT=("[LLXRemoteInstallerClient] (reset_var) %s has been updated" %namevar) |
---|
168 | self._debug(COMMENT) |
---|
169 | return [True,str(COMMENT)] |
---|
170 | |
---|
171 | except Exception as e: |
---|
172 | return [False,str(e)] |
---|
173 | |
---|
174 | #def_reset_var |
---|
175 | |
---|
176 | def update_var_dict (self,namevar=None,dict={},localhost=None): |
---|
177 | self._debug("update_var dict") |
---|
178 | try: |
---|
179 | if self.test_var(namevar,localhost)[0]: |
---|
180 | if localhost in ["",None]: |
---|
181 | proxy="https://server:9779" |
---|
182 | else: |
---|
183 | proxy="https://localhost:9779" |
---|
184 | import xmlrpclib as x |
---|
185 | c=x.ServerProxy(proxy) |
---|
186 | objects["VariablesManager"].set_variable(namevar,dict) |
---|
187 | COMMENT="[LLXRemoteInstallerClient] (update_var_list) %s has been updated with this list of APP %s" %(namevar,dict) |
---|
188 | self._debug(COMMENT) |
---|
189 | return [True,str(COMMENT)] |
---|
190 | else: |
---|
191 | COMMENT="[LLXRemoteInstallerClient] (update_var_list) Can't update variable" |
---|
192 | self._debug(COMMENT) |
---|
193 | return [False,str(COMMENT)] |
---|
194 | except Exception as e: |
---|
195 | return [False,str(e)] |
---|
196 | |
---|
197 | #def_add_list |
---|
198 | |
---|
199 | def download(self, apps=[],url=None,dir=None): |
---|
200 | try: |
---|
201 | #CREATE AUX_SOURCES IF IT'S NECESSARY |
---|
202 | if not os.path.exists(dir): |
---|
203 | os.makedirs(dir) |
---|
204 | for app in apps: |
---|
205 | file_app=str(dir)+"/"+app |
---|
206 | url_complete=str(url)+str(app) |
---|
207 | if os.path.exists(file): |
---|
208 | self._debug("(download) The FILE: "+file+" has been donwloaded before, it will be deleted now.") |
---|
209 | os.remove(file) |
---|
210 | self._debug("(download) The FILE: "+app+" is downloading now to directory "+file_app+" .....") |
---|
211 | urllib.urlretrieve(url_complete,file_app) |
---|
212 | os.chmod(file_app,0755) |
---|
213 | |
---|
214 | COMMENT="Your FILES: %s has been downloaded in %s"%(apps,dir) |
---|
215 | return [True,str(COMMENT)] |
---|
216 | except Exception as e: |
---|
217 | return[False,str(e)] |
---|
218 | |
---|
219 | #def_download |
---|
220 | |
---|
221 | |
---|
222 | def repo_add (self,sources_private=None): |
---|
223 | try: |
---|
224 | if sources_private not in ["",None,[]]: |
---|
225 | COMMENT="(repo_add) REPO IS PARTICULAR %s" %sources_private |
---|
226 | self._debug(COMMENT) |
---|
227 | mode = 'a' if os.path.exists(self.file_sources) else 'w' |
---|
228 | f_used=open(self.file_sources,mode) |
---|
229 | self._debug("open("+self.file_sources+","+mode+")") |
---|
230 | f_used.write(sources_private+'\n') |
---|
231 | f_used.close() |
---|
232 | self._debug("[LLXRemoteInstaller](repo_add) File created now read it") |
---|
233 | #NOW READ THE NEW SOURCES.LIST |
---|
234 | sources=[] |
---|
235 | file=open(self.file_sources) |
---|
236 | f=file.read().splitlines() |
---|
237 | for line in f: |
---|
238 | sources.append(line) |
---|
239 | file.close() |
---|
240 | |
---|
241 | COMMENT="[LLXRemoteInstallerClient](repo_add) Your repo LLXRemoteInstallerClient has new lines %s"%sources |
---|
242 | self._debug(COMMENT) |
---|
243 | |
---|
244 | return [True,str(COMMENT),sources] |
---|
245 | except Exception as e: |
---|
246 | return [False,str(e)] |
---|
247 | |
---|
248 | #def_repo_add |
---|
249 | |
---|
250 | def repo_restore (self,file=None): |
---|
251 | self._debug("repo_restore") |
---|
252 | try: |
---|
253 | |
---|
254 | COMMENT="(repo_restore) Repo %s to test APT Aplications is deleted and restore to initial state"%file |
---|
255 | self._debug(COMMENT) |
---|
256 | if os.path.exists(file): |
---|
257 | os.remove(file) |
---|
258 | self.repo_update() |
---|
259 | #Delete proxy settings |
---|
260 | if os.path.exists("/etc/apt/apt.conf.d/98proxySettings"): |
---|
261 | os.remove("/etc/apt/apt.conf.d/98proxySettings") |
---|
262 | COMMENT="[LLXRemoteInstallerClient](repo_restore) Repo from AddApplications has been deleted" |
---|
263 | #self._debug(COMMENT) |
---|
264 | return [True,str(COMMENT)] |
---|
265 | |
---|
266 | except Exception as e: |
---|
267 | return [False,str(e)] |
---|
268 | |
---|
269 | #def_repo_restore |
---|
270 | |
---|
271 | def repo_update (self): |
---|
272 | self._debug("repo_update") |
---|
273 | try: |
---|
274 | self._debug("(repo_restore) Updating indices, please wait........") |
---|
275 | proc = subprocess.Popen('apt-get update', shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash") |
---|
276 | proc.wait() |
---|
277 | #self.cache.update() |
---|
278 | self.cache=apt.Cache() |
---|
279 | COMMENT="[LLXRemoteInstallerClient](repo_restore) Your APT CACHE has updated with new indices" |
---|
280 | #self._debug(COMMENT) |
---|
281 | return [True,str(COMMENT)] |
---|
282 | |
---|
283 | except Exception as e: |
---|
284 | return [False,str(e)] |
---|
285 | |
---|
286 | #def_repo_update |
---|
287 | |
---|
288 | def repo_customize_apt(self,repos=[]): |
---|
289 | pinFile="/etc/apt/preferences.d/lliurex-pinning" |
---|
290 | if os.path.exists(pinFile): |
---|
291 | f=open(pinFile,'a') |
---|
292 | f.write("###\n") |
---|
293 | for repo in repos: |
---|
294 | arrayRepoDir=repo.split(' ') |
---|
295 | repoOrig=arrayRepoDir[1] |
---|
296 | pinLine="Package: *\nPin: origin "+repoOrig+"\nPin-Priority:700\n" |
---|
297 | f.write(pinLine) |
---|
298 | f.close() |
---|
299 | #Setup proxy in apt.config.d |
---|
300 | prefFile=open("/etc/apt/apt.conf.d/98proxySettings","w") |
---|
301 | prefFile.write('Acquire::http::proxy "http://proxy:3128";') |
---|
302 | prefFile.close() |
---|
303 | #def repo_customize_apt |
---|
304 | |
---|
305 | def repo_restore_config(self): |
---|
306 | pinFile="/etc/apt/preferences.d/lliurex-pinning" |
---|
307 | if os.path.exists(pinFile): |
---|
308 | f=open(pinFile,'r') |
---|
309 | lines=f.readlines() |
---|
310 | f.close() |
---|
311 | defaultPin=[] |
---|
312 | continueReading=1 |
---|
313 | for line in lines: |
---|
314 | if continueReading: |
---|
315 | if line!="###\n": |
---|
316 | defaultPin.append(line) |
---|
317 | else: |
---|
318 | continueReading=0 |
---|
319 | else: |
---|
320 | break |
---|
321 | f=open(pinFile,'w') |
---|
322 | f.writelines(defaultPin) |
---|
323 | f.close() |
---|
324 | if os.path.exists("/etc/apt/apt.conf.d/98proxySettings"): |
---|
325 | os.remove("/etc/apt/apt.conf.d/98proxySettings") |
---|
326 | self._debug("Default config restored") |
---|
327 | #def repo_restore_config |
---|
328 | |
---|
329 | def deb_solvedDependency (self,deb=None,list=[]): |
---|
330 | try: |
---|
331 | for s in list: |
---|
332 | name_deb=s[0] |
---|
333 | version_deb=s[1] |
---|
334 | test_deb=s[2] |
---|
335 | self._debug("(deb_solvedDependency) DEB: "+deb+" depends on package: "+name_deb+" -- version: "+test_deb+" "+version_deb+" -- ") |
---|
336 | #Check if exists in cache. If not abort process |
---|
337 | if name_deb in self.cache: |
---|
338 | #self._debug("[LLXRemoteInstallerClient](deb_solvedDependency) Dependence is avaible") |
---|
339 | pkg=self.cache[name_deb] |
---|
340 | #Check if installed or install dependency |
---|
341 | if pkg.is_installed: |
---|
342 | self._debug("(deb_solvedDependency) Dependency IS INSTALLED in your system DO NOTHING WITH IT") |
---|
343 | else: |
---|
344 | self._debug("(deb_solvedDependency) Dependency "+name_deb+" is being installed ......") |
---|
345 | list_deb_aux=[] |
---|
346 | list_deb_aux.append(name_deb) |
---|
347 | self.apt_install(list_deb_aux) |
---|
348 | else: |
---|
349 | #Check if it's a virtual package from a provides |
---|
350 | self._debug("(deb_solvedDependency) Testing if dependency "+name_deb+" is a virtual package") |
---|
351 | result=subprocess.Popen(["LAGUAGE=en_EN; grep-status -FProvides,Package -sPackage,Provides,Status %s"%name_deb],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0] |
---|
352 | virtual_deb=False |
---|
353 | for line in result.splitlines(): |
---|
354 | if "install" in line: |
---|
355 | self._debug("(deb_solvedDependency) Dependence: "+name_deb+" is a virtual package installed in my system") |
---|
356 | virtual_deb=True |
---|
357 | #Abort the process only if dependencies doesn't resolv and it's not a virtual package |
---|
358 | if not virtual_deb: |
---|
359 | COMMENT="[LLXRemoteInstallerClient](deb_solvedDependency) Your DEB: %s has unreliable dependencies: %s can not been installed"%(deb,name_deb) |
---|
360 | #self._debug(COMMENT) |
---|
361 | return[False,str(COMMENT)] |
---|
362 | |
---|
363 | COMMENT="[LLXRemoteInstallerClient](deb_solvedDependency) Your DEPENDENCIES has been resolved. Now will continue installing: %s"%(deb) |
---|
364 | #self._debug(COMMENT) |
---|
365 | return[True,str(COMMENT)] |
---|
366 | except Exception as e: |
---|
367 | return [False,str(e)] |
---|
368 | |
---|
369 | #def_debsolvedependence |
---|
370 | |
---|
371 | |
---|
372 | def deb_testDependencies(self,deb=None,dependsList=[]): |
---|
373 | try: |
---|
374 | #Check if dependencies are installed |
---|
375 | self._debug("(deb_testDependencies) Testing your DEB: "+deb+" against this dependencies list "+str(dependsList)) |
---|
376 | for x in dependsList: |
---|
377 | self._debug("[LLXRemoteInstallerClient](deb_testDependencies) -------------- Testing this tupla "+str(x)+" ------------------") |
---|
378 | #If dependency has more than one element is an "OR" dependency. Must resolv one of them |
---|
379 | if len(x)<2: |
---|
380 | if self.deb_solvedDependency(deb,x)[0] == 'False': |
---|
381 | name_deb=x[0] |
---|
382 | COMMENT="[LLXRemoteInstallerClient](deb_testDependencies) Your DEB: %s has dependences without solution with your actual repos, APP: %s can not been installed"%(deb,name_deb) |
---|
383 | #self._debug(COMMENT) |
---|
384 | return[False,str(COMMENT)] |
---|
385 | else: |
---|
386 | #"OR" Dependency, one "True" is enough |
---|
387 | OK=False |
---|
388 | self._debug("(deb_testDependencies) Initializing test for *OR* tupla") |
---|
389 | for s in x: |
---|
390 | s=[s] |
---|
391 | name_deb=s[0][0] |
---|
392 | if self.deb_solvedDependency(deb,s)[0]: |
---|
393 | self._debug("(deb_testDependencies) Testing OR tupla: this dependency "+str(name_deb)+" can be installed, as solves the conflict") |
---|
394 | OK=True |
---|
395 | ok_solved=s |
---|
396 | if not OK: |
---|
397 | COMMENT="[LLXRemoteInstallerClient](deb_testDependencies) Testing OR tupla: can not resolve this OR dependency for %s"%(x) |
---|
398 | #self._debug(COMMENT) |
---|
399 | return[False,str(COMMENT)] |
---|
400 | else: |
---|
401 | pass |
---|
402 | self._debug("(deb_testDependencies) Testing OR tupla, can install this dependency: "+str(ok_solved)+" and can resolve OR dependency for "+str(x)) |
---|
403 | |
---|
404 | COMMENT="[LLXRemoteInstallerClient](deb_testDependencies) Dependencies are resolved. Now you can install your DEB: %s"%(deb) |
---|
405 | #self._debug(COMMENT) |
---|
406 | return[True,str(COMMENT)] |
---|
407 | except Exception as e: |
---|
408 | self._debug("ERROR deb_testDependencies: "+str(e)) |
---|
409 | return [False,str(e)] |
---|
410 | #deb_testDependencies |
---|
411 | |
---|
412 | def deb_install(self,list_deb=[],dir_deb=None): |
---|
413 | try: |
---|
414 | list_ok=[] |
---|
415 | list_not=[] |
---|
416 | #Load apt cache |
---|
417 | self.cache=apt.Cache() |
---|
418 | self._debug("(deb_install) CACHE updated") |
---|
419 | #Check each deb marked to install |
---|
420 | for deb in list_deb: |
---|
421 | file_deb=str(dir_deb)+"/"+str(deb) |
---|
422 | self._debug("(deb_install) Test first deb to install: "+deb+" in dir: "+file_deb) |
---|
423 | app=apt.debfile.DebPackage(file_deb,self.cache) |
---|
424 | #Check if it's installable |
---|
425 | if app.check(): |
---|
426 | self._debug("(deb_install) The deb can be installed, now will proceed to check dependencies, please wait....") |
---|
427 | if self.deb_testDependencies(deb,app.depends)[0]: |
---|
428 | #Install if all is OK |
---|
429 | self._debug("(deb_install) The system are ready to install the DEB: "+deb) |
---|
430 | app.install() |
---|
431 | list_ok.append(deb) |
---|
432 | else: |
---|
433 | #Failed dependencies |
---|
434 | self._debug("(deb_install) The system cannot resolve the dependencies for your deb, review your repos or your DEB if you want to install it: "+deb) |
---|
435 | |
---|
436 | else: |
---|
437 | #There's any reason that makes deb uninstallable |
---|
438 | self._debug("(deb_install) Your DEB: "+deb+" cannot be installed in your system") |
---|
439 | list_not.append(deb) |
---|
440 | |
---|
441 | COMMENT="DEBS installed: %s . DEBS with problems:%s"%(list_ok, list_not) |
---|
442 | return [True,str(COMMENT),list_ok,list_not] |
---|
443 | except Exception as e: |
---|
444 | self._debug("(deb_install) ERROR: "+str(e)) |
---|
445 | return[False,str(e)] |
---|
446 | #deb_install |
---|
447 | |
---|
448 | def sh_install(self,list_sh=[],dir=""): |
---|
449 | try: |
---|
450 | list_ok=[] |
---|
451 | list_not=[] |
---|
452 | for app in list_sh: |
---|
453 | self._debug("(sh_install) Working with SCRIPT: "+app) |
---|
454 | file_app=str(dir)+"/"+app |
---|
455 | if os.path.exists(file_app): |
---|
456 | self._debug("(sh_install) Executing it, please wait..........") |
---|
457 | proc = subprocess.Popen(file_app, shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash") |
---|
458 | proc.wait() |
---|
459 | lines=subprocess.Popen(["LAGUAGE=en_EN; md5sum %s | awk '{print $1}'"%file_app],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0] |
---|
460 | for line in lines.splitlines(): |
---|
461 | md5=line |
---|
462 | list_ok.append([app,md5]) |
---|
463 | else: |
---|
464 | self._debug("(sh_install) The script "+file_app+" not exists in your system.") |
---|
465 | list_not.append(app) |
---|
466 | |
---|
467 | # if list_ok not in ["",None,[]]: |
---|
468 | if list_ok: |
---|
469 | COMMENT="Your SCRIPTS: %s had been executed"%(list_ok) |
---|
470 | return [True,str(COMMENT),list_ok,list_not] |
---|
471 | else: |
---|
472 | COMMENT="Some scripts failed to execute, please check them %s"%(list_not) |
---|
473 | return [True,str(COMMENT),list_ok,list_not] |
---|
474 | |
---|
475 | except Exception as e: |
---|
476 | return[False,str(e)] |
---|
477 | |
---|
478 | #sh_install |
---|
479 | |
---|
480 | def apt_install(self,list_apt=[]): |
---|
481 | self._debug("App List: "+str(list_apt)) |
---|
482 | try: |
---|
483 | list_apt_ok=[] |
---|
484 | list_apt_not=[] |
---|
485 | list_apt_system=[] |
---|
486 | self._debug("(apt_install) Apps list: "+str(list_apt)) |
---|
487 | for app in list_apt: |
---|
488 | self._debug("(apt_install) Checking if app "+app+" is available and installable") |
---|
489 | if app in self.cache: |
---|
490 | pkg=self.cache[app] |
---|
491 | if pkg.is_installed: |
---|
492 | self._debug("(apt_install) The APP: "+app+" is intalled in your system") |
---|
493 | list_apt_system.append(app) |
---|
494 | else: |
---|
495 | self._debug("(apt_install) The APP: "+app+" wil be installed soon") |
---|
496 | pkg.mark_install() |
---|
497 | list_apt_ok.append(app) |
---|
498 | # list_apt_system.append(app) |
---|
499 | else: |
---|
500 | list_apt_not.append(app) |
---|
501 | |
---|
502 | if list_apt_ok: |
---|
503 | # self._debug("(apt_install) Please wait while installing: "+str(list_apt_ok)) |
---|
504 | self._debug("(apt_install) Please wait while installing: "+str(list_apt_ok)) |
---|
505 | self.cache.commit() |
---|
506 | # if list_apt_not in ["",None,[]]: |
---|
507 | if not list_apt_not: |
---|
508 | COMMENT="[LLXRemoteInstallerClient](apt_install) The system has been updated with this APP list: %s"%(list_apt_system) |
---|
509 | else: |
---|
510 | COMMENT="[LLXRemoteInstallerClient](apt_install) The system has been updated with this APP list: %s but this list cann't be installed:%s"%(list_apt_ok, list_apt_not) |
---|
511 | else: |
---|
512 | # if list_apt_not in ["",None,[]]: |
---|
513 | if not list_apt_not: |
---|
514 | COMMENT="[LLXRemoteInstallerClient](apt_install) Do nothing, because your system has installed all APP in list: %s"%(list_apt) |
---|
515 | else: |
---|
516 | COMMENT="[LLXRemoteInstallerClient](apt_install) Do nothing, because your system has installed all APP in list: %s and this list cannot be installed because your REPO do not have it:%s"%(list_apt_system, list_apt_not) |
---|
517 | |
---|
518 | # if list_apt_system not in ["",None,[]]: |
---|
519 | if list_apt_ok: |
---|
520 | for app in list_apt_ok: |
---|
521 | self._debug("[LLXRemoteInstallerClient](apt_install) The APP: "+app+" is intalled in your system.....Adding to installed list") |
---|
522 | # list_apt_ok.append(app) |
---|
523 | self._debug(COMMENT) |
---|
524 | self._debug(str(list_apt_ok)+" --- "+str(list_apt_not)+" --- "+str(list_apt_system)) |
---|
525 | return [True,str(COMMENT),list_apt_ok,list_apt_not,list_apt_system] |
---|
526 | |
---|
527 | except Exception as e: |
---|
528 | return[False,str(e)] |
---|
529 | |
---|
530 | #apt_install |
---|
531 | |
---|
532 | def deb_test(self,appDict,dictOrig): |
---|
533 | self._debug("deb_test") |
---|
534 | #Get dict values |
---|
535 | list_deb=appDict[self.LIST] |
---|
536 | url_deb=appDict[self.URL] |
---|
537 | |
---|
538 | #Create download path |
---|
539 | dir_deb=str(self.dir_tmp)+"/"+"deb" |
---|
540 | #Check if the deb is installed |
---|
541 | deb_aux=[] |
---|
542 | self._debug("(test_system) Checking if any deb on the list is already installed "+str(list_deb)) |
---|
543 | for deb in list_deb: |
---|
544 | if deb not in dictOrig: |
---|
545 | self._debug("(test_system)DEB: "+deb+" marked for install") |
---|
546 | deb_aux.append(deb) |
---|
547 | else: |
---|
548 | pass |
---|
549 | self._debug("(test_system)DEB: "+deb+" is already installed") |
---|
550 | list_deb=deb_aux |
---|
551 | #Download needed debs |
---|
552 | # if list_deb not in ["",None,[]]: |
---|
553 | if list_deb: |
---|
554 | self._debug("(test_system) Debs list is "+str(list_deb)+" Download path is: "+url_deb) |
---|
555 | self.download(list_deb,url_deb,dir_deb) |
---|
556 | result_deb=self.deb_install(list_deb,dir_deb) |
---|
557 | |
---|
558 | else: |
---|
559 | self._debug("(test_system) Deb list is empty") |
---|
560 | result_deb=["","","","",""] |
---|
561 | return(result_deb) |
---|
562 | #def deb_test |
---|
563 | |
---|
564 | def sh_test(self,appDict,dictOrig): |
---|
565 | self._debug("sh_test") |
---|
566 | #Get dict values |
---|
567 | list_sh=appDict[self.LIST] |
---|
568 | url_sh=appDict[self.URL] |
---|
569 | #Create donwload path and download needed scripts |
---|
570 | dir_sh=str(self.dir_tmp)+"/"+"scripts" |
---|
571 | self._debug("Created tmp dir "+dir_sh) |
---|
572 | # if list_sh not in ["",None,[]]: |
---|
573 | if list_sh: |
---|
574 | sh_aux=[] |
---|
575 | for sh_tupla in list_sh: |
---|
576 | sh=sh_tupla[0] |
---|
577 | md5=sh_tupla[1] |
---|
578 | if sh_tupla not in dictOrig: |
---|
579 | self._debug("(test_system) SH: must install "+sh) |
---|
580 | sh_aux.append(sh) |
---|
581 | else: |
---|
582 | self._debug("(test_system) SH: "+sh+" already installed") |
---|
583 | #Download and execute the scripts |
---|
584 | list_sh=sh_aux |
---|
585 | self._debug("(test_system) Script list is "+str(list_sh)+" Download to: "+url_sh) |
---|
586 | self.download(list_sh,url_sh,dir_sh) |
---|
587 | if list_sh not in ["",None,[]]: |
---|
588 | result_sh=self.sh_install(list_sh,dir_sh) |
---|
589 | else: |
---|
590 | self._debug("(test_system) Script list is empty") |
---|
591 | result_sh=["","","","",""] |
---|
592 | |
---|
593 | else: |
---|
594 | self._debug("(test_system) Script list is empty") |
---|
595 | result_sh=["","","","",""] |
---|
596 | return(result_sh) |
---|
597 | #def sh_test |
---|
598 | |
---|
599 | def _refine_apt_repoList(self,appDict,dictOrig): |
---|
600 | self._debug("_refine_apt_repoList") |
---|
601 | repoList=[] |
---|
602 | list_apt=[] |
---|
603 | lliurex_net=["deb http://lliurex.net/xenial xenial main restricted universe multiverse","deb http://lliurex.net/xenial xenial-security main restricted universe multiverse","deb http://lliurex.net/xenial xenial-updates main restricted universe multiverse"] |
---|
604 | lliurex_mirror=["deb http://mirror/llx16 xenial main restricted universe multiverse","deb http://mirror/llx16 xenial-security main restricted universe multiverse","deb http://mirror/llx16 xenial-updates main restricted universe multiverse"] |
---|
605 | for source in appDict: |
---|
606 | self._debug("[LLXRemoteInstallerClient](_refine_apt_repoList) Adding applist from: "+str(source)) |
---|
607 | aux_list_apt=appDict[source][self.LIST] |
---|
608 | self._debug("[LLXRemoteInstallerClient](_refine_apt_repoList) Adding PPA: "+str(source)) |
---|
609 | url=appDict[source][self.URL] |
---|
610 | apt_aux=[] |
---|
611 | self._debug("(test_system) Checking if apt list is installed "+str(aux_list_apt)) |
---|
612 | for apt in aux_list_apt: |
---|
613 | if apt not in dictOrig: |
---|
614 | self._debug("(test_system) Must install APT: "+apt) |
---|
615 | apt_aux.append(apt) |
---|
616 | else: |
---|
617 | self._debug("(test_system) "+apt+" is installed") |
---|
618 | #Add non installed debs to list |
---|
619 | list_apt.extend(apt_aux) |
---|
620 | #configure pinning for new repos |
---|
621 | if source in ["LliureX"]: |
---|
622 | repoList.extend(lliurex_net) |
---|
623 | elif source in ["Mirror"]: |
---|
624 | repoList.extend(lliurex_mirror) |
---|
625 | else: |
---|
626 | repoList.append(url) |
---|
627 | appsRepoDict={'apt':list_apt,'repos':repoList} |
---|
628 | return appsRepoDict |
---|
629 | #def _refine_apt_repoList |
---|
630 | |
---|
631 | def apt_test(self,appDict,dict_orig): |
---|
632 | self._debug("apt_test") |
---|
633 | #Get dict values |
---|
634 | list_apt_resume=[] |
---|
635 | ubuntu=["deb http://archive.ubuntu.com/ubuntu xenial main restricted universe multiverse","deb http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse","deb http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse"] |
---|
636 | list_apt=[] |
---|
637 | result_apt=["","","","",""] |
---|
638 | #List with repos for pinning customize |
---|
639 | repoList=ubuntu |
---|
640 | #Get debs and repos |
---|
641 | appsRepoDict=self._refine_apt_repoList(appDict[self.APT],dict_orig[self.APT]) |
---|
642 | repoList.extend(appsRepoDict['repos']) |
---|
643 | list_apt=appsRepoDict['apt'] |
---|
644 | self.repo_customize_apt(repoList) |
---|
645 | if list_apt: |
---|
646 | for url in repoList: |
---|
647 | if self.repo_add(url): |
---|
648 | self._debug("(test_system) New REPO has been added to your system") |
---|
649 | if self.repo_update(): |
---|
650 | self._debug("[LLXRemoteInstallerClient](test_system) Your CACHE has been updated") |
---|
651 | else: |
---|
652 | # return [False,"failed repo_update"] |
---|
653 | return result_apt |
---|
654 | else: |
---|
655 | # return [False,"failed repo_add"] |
---|
656 | return result_apt |
---|
657 | #Proceed with the list, repos are updated |
---|
658 | self._debug("[LLXRemoteInstallerClient](test_system) Calling apt_install with "+str(list_apt)) |
---|
659 | result_apt=self.apt_install(list_apt)[2] |
---|
660 | if result_apt: |
---|
661 | for app in result_apt: |
---|
662 | list_apt_resume.append(app) |
---|
663 | #Delete repo if was created by us |
---|
664 | self._debug("Call repo_restore") |
---|
665 | self.repo_restore(self.file_sources) |
---|
666 | else: |
---|
667 | self._debug( "[LLXRemoteInstallerClient](test_system) No apps installed") |
---|
668 | result_apt=["","","","",""] |
---|
669 | |
---|
670 | #Restore default config |
---|
671 | self._debug("Call repo_restore_config") |
---|
672 | self.repo_restore_config() |
---|
673 | result_apt=list_apt_resume |
---|
674 | return(result_apt) |
---|
675 | #def apt_test |
---|
676 | |
---|
677 | def _update_results(self,dict_orig,result_deb,result_sh,result_apt): |
---|
678 | if not dict_orig: |
---|
679 | #Create dict if doesn't exists |
---|
680 | self._debug("[LLXRemoteInstallerClient](test_system) Creando el diccionario.......") |
---|
681 | dict_new=self.create_dict ([self.APT,self.DEB,self.SH])[2] |
---|
682 | dict_new[self.APT]=list(result_apt) |
---|
683 | dict_new[self.DEB]=list(result_deb[2]) |
---|
684 | dict_new[self.SH]=list(result_sh[2]) |
---|
685 | self.update_var_dict (self.N4D_INSTALLED,dict_new,"localhost") |
---|
686 | else: |
---|
687 | #Update dict |
---|
688 | dict_new=dict_orig |
---|
689 | log="[LLXRemoteInstallerClient](test_system) Will add APT: %s ** DEBS: %s ** SH: %s "%(result_apt,result_deb[2],result_sh[2]) |
---|
690 | self._debug(log) |
---|
691 | #Check the dict against a tuple |
---|
692 | dict_help=self.create_dict ([self.APT,self.DEB,self.SH])[2] |
---|
693 | dict_help[self.APT]=list(result_apt) |
---|
694 | dict_help[self.DEB]=list(result_deb[2]) |
---|
695 | dict_help[self.SH]=list(result_sh[2]) |
---|
696 | self._debug("[LLXRemoteInstallerClient](test_system) dict to compare is "+str(dict_help)) |
---|
697 | #Check values |
---|
698 | for valor_dict in dict_help: |
---|
699 | self._debug("[LLXRemoteInstallerClient](test_system) Test APP from: "+valor_dict) |
---|
700 | try: |
---|
701 | for app_installed in dict_help[valor_dict]: |
---|
702 | ok=False |
---|
703 | self._debug("[LLXRemoteInstallerClient](test_system) Check if app is installed: "+str(app_installed)) |
---|
704 | for app_history in dict_orig[valor_dict]: |
---|
705 | log="[LLXRemoteInstallerClient](test_system) APP Installed: %s -- TESTING -- APP System: %s"%(app_installed,app_history) |
---|
706 | self._debug(log) |
---|
707 | if app_history == app_installed: |
---|
708 | self._debug("[LLXRemoteInstallerClient](test_system) App exists, don't add to dict") |
---|
709 | ok=True |
---|
710 | if not ok: |
---|
711 | log="[LLXRemoteInstallerClient](test_system) Adding app to list: %s"%app_installed |
---|
712 | self._debug(log) |
---|
713 | dict_new[valor_dict].append(app_installed) |
---|
714 | else: |
---|
715 | log="[LLXRemoteInstallerClient](test_system) App hasn't been added to dict: %s"%app_installed |
---|
716 | self._debug(log) |
---|
717 | except Exception as e: |
---|
718 | self._debug("ERROR: "+str(e)) |
---|
719 | return (dict_new) |
---|
720 | #def _update_results |
---|
721 | |
---|
722 | def test_system(self): |
---|
723 | try: |
---|
724 | |
---|
725 | #Get installed apps dict |
---|
726 | self.test_var(self.N4D_INSTALLED,"localhost") |
---|
727 | dict_orig=self.read_var(self.N4D_INSTALLED,"localhost")[2] |
---|
728 | |
---|
729 | #Get server dict |
---|
730 | appDict=self.read_var(self.N4D_VAR)[2] |
---|
731 | #Check the server's dict for install |
---|
732 | # if appDict in ["",None,"None"]: |
---|
733 | if not appDict: |
---|
734 | COMMENT="[LLXRemoteInstallerClient](test_system) Variable %s do not exist in your server, do nothing"%self.N4D_VAR |
---|
735 | self._debug(COMMENT) |
---|
736 | return [True,str(COMMENT)] |
---|
737 | |
---|
738 | self._debug("[LLXRemoteInstallerClient] (test_system) The DICTIONARY to use is: "+str(dict)) |
---|
739 | self._debug("[LLXRemoteInstallerClient] (test_system) The DICTIONARY installed in your system is: "+str(dict_orig)) |
---|
740 | dict_orig_aux=dict_orig |
---|
741 | |
---|
742 | #TEST Debs |
---|
743 | self._debug("Call deb_test") |
---|
744 | result_deb=self.deb_test(appDict[self.DEB],dict_orig[self.DEB]) |
---|
745 | #TEST SH |
---|
746 | self._debug("Call sh_test") |
---|
747 | result_sh=self.sh_test(appDict[self.SH],dict_orig[self.SH]) |
---|
748 | #TEST Apt |
---|
749 | self._debug("Call apt_test") |
---|
750 | result_apt=self.apt_test(appDict,dict_orig) |
---|
751 | |
---|
752 | #Check that it's a list |
---|
753 | sh_installed=list(result_sh[2]) |
---|
754 | #Add results to N4D dict |
---|
755 | dict_new=self._update_results(dict_orig,result_deb,result_sh,result_apt) |
---|
756 | # if dict_orig in ["",None,{}]: |
---|
757 | log="Dict now is %s"%dict_new |
---|
758 | self._debug(log) |
---|
759 | self._debug("Updating.......") |
---|
760 | #Add installed apps to N4D |
---|
761 | self.update_var_dict (self.N4D_INSTALLED,dict_new,"localhost") |
---|
762 | COMMENT="The system has been configured with the APPS: %s * has executed the scripts: %s * Installed new DEBS: %s"%(result_apt,sh_installed,result_deb[2]) |
---|
763 | self._debug(COMMENT) |
---|
764 | return [True,str(COMMENT),result_apt,result_sh[2],result_deb[2],dict_new] |
---|
765 | except Exception as e: |
---|
766 | return[False,str(e)] |
---|
767 | |
---|