1 | #!/usr/bin/env python3 |
---|
2 | |
---|
3 | import os |
---|
4 | import subprocess |
---|
5 | import sys |
---|
6 | import html2text |
---|
7 | import syslog |
---|
8 | |
---|
9 | import epi.epimanager as EpiManager |
---|
10 | import signal |
---|
11 | signal.signal(signal.SIGINT,signal.SIG_IGN) |
---|
12 | |
---|
13 | class EPIC(object): |
---|
14 | |
---|
15 | def __init__(self,app): |
---|
16 | |
---|
17 | self.epicore=EpiManager.EpiManager() |
---|
18 | self.epicore.read_conf(app) |
---|
19 | signal.signal(signal.SIGINT,self.handler_signal) |
---|
20 | |
---|
21 | if len(self.epicore.epiFiles)==0: |
---|
22 | msg_log='APP epi file not exist' |
---|
23 | print (' [EPIC]: '+msg_log) |
---|
24 | self.write_log(msg_log) |
---|
25 | sys.exit(1) |
---|
26 | else: |
---|
27 | msg_log='APP epi file loaded by EPIC: ' + app |
---|
28 | self.write_log(msg_log) |
---|
29 | |
---|
30 | |
---|
31 | #def __init__ |
---|
32 | |
---|
33 | def get_info(self): |
---|
34 | |
---|
35 | #self.epicore.get_pkg_info() |
---|
36 | |
---|
37 | order=len(self.epicore.epiFiles) |
---|
38 | depends="" |
---|
39 | pkgs="" |
---|
40 | |
---|
41 | for item in self.epicore.epiFiles: |
---|
42 | order=order-1 |
---|
43 | tmp=self.epicore.epiFiles[order] |
---|
44 | #if tmp["type"]!="file": |
---|
45 | for item in tmp["pkg_list"]: |
---|
46 | if order>0: |
---|
47 | depends=depends+item["name"]+" " |
---|
48 | else: |
---|
49 | pkgs=pkgs+item["name"] +" " |
---|
50 | ''' |
---|
51 | else: |
---|
52 | if order>0: |
---|
53 | depends=depends+tmp["name"]+" " |
---|
54 | else: |
---|
55 | pkgs=pkgs+tmp["name"] |
---|
56 | ''' |
---|
57 | |
---|
58 | return depends,pkgs |
---|
59 | |
---|
60 | #def get_info |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | def showInfo(self,checked=None): |
---|
66 | |
---|
67 | checksystem=True |
---|
68 | if not checked: |
---|
69 | print (' [EPIC]: Searching information...') |
---|
70 | self.epicore.get_pkg_info() |
---|
71 | #checksystem=self.checking_system() |
---|
72 | |
---|
73 | if checksystem: |
---|
74 | depends,pkgs=self.get_info() |
---|
75 | |
---|
76 | epi_conf=self.epicore.epiFiles[0] |
---|
77 | status=epi_conf["status"] |
---|
78 | |
---|
79 | try: |
---|
80 | if epi_conf["script"]["remove"]: |
---|
81 | self.uninstall="Yes" |
---|
82 | except Exception as e: |
---|
83 | self.uninstall="No" |
---|
84 | |
---|
85 | print (" [EPIC]: Information availabled:") |
---|
86 | print (" Application: " + pkgs) |
---|
87 | print (" Status: " + status) |
---|
88 | print (" Uninstall process availabled: " + self.uninstall) |
---|
89 | if len(depends)>0: |
---|
90 | print (" Additional application required: " + depends) |
---|
91 | |
---|
92 | |
---|
93 | return 0 |
---|
94 | else: |
---|
95 | return 1 |
---|
96 | |
---|
97 | #def showInfo |
---|
98 | |
---|
99 | def checking_system(self,action=None): |
---|
100 | |
---|
101 | check=True |
---|
102 | print (' [EPIC]: Checking system...') |
---|
103 | |
---|
104 | self.connection=self.epicore.check_connection() |
---|
105 | |
---|
106 | if self.connection: |
---|
107 | self.epicore.check_root() |
---|
108 | self.epicore.get_pkg_info() |
---|
109 | self.required_root=self.epicore.required_root() |
---|
110 | self.required_x=self.check_required_x() |
---|
111 | if self.required_root: |
---|
112 | msg_log="You need root privileges to " + action + " the application" |
---|
113 | print (' [EPIC]: '+msg_log) |
---|
114 | self.write_log(msg_log) |
---|
115 | check=False |
---|
116 | if self.required_x: |
---|
117 | msg_log="Can not " + action + " the application via terminal. Use epi-gtk for this" |
---|
118 | print (' [EPIC]: '+ msg_log) |
---|
119 | self.write_log(msg_log) |
---|
120 | check=False |
---|
121 | else: |
---|
122 | msg_log="Internet connection not detected" |
---|
123 | print (' [EPIC]: '+msg_log) |
---|
124 | self.write_log(msg_log) |
---|
125 | check=False |
---|
126 | |
---|
127 | return check |
---|
128 | |
---|
129 | #def checking_system |
---|
130 | |
---|
131 | def check_required_x(self): |
---|
132 | |
---|
133 | cont=0 |
---|
134 | for item in self.epicore.epiFiles: |
---|
135 | if self.epicore.epiFiles[item]["required_x"]: |
---|
136 | cont=cont+1 |
---|
137 | if cont>0: |
---|
138 | return True |
---|
139 | else: |
---|
140 | return False |
---|
141 | |
---|
142 | def add_repository_keys(self,order): |
---|
143 | |
---|
144 | print(' [EPIC]: Gathering information....') |
---|
145 | |
---|
146 | cmd=self.epicore.add_repository_keys(order) |
---|
147 | if cmd !="": |
---|
148 | p=subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE) |
---|
149 | output=p.communicate() |
---|
150 | error=self.readErrorOutput(output[1]) |
---|
151 | if error: |
---|
152 | msg_log='Installation aborted. Error gathering information: ' +'\n'+str(output[1]) |
---|
153 | print(' [EPIC]: '+msg_log) |
---|
154 | self.write_log(msg_log) |
---|
155 | return False |
---|
156 | else: |
---|
157 | msg_log='Gathering information: OK' |
---|
158 | self.write_log(msg_log) |
---|
159 | return True |
---|
160 | |
---|
161 | else: |
---|
162 | return True |
---|
163 | |
---|
164 | #def add_repository_keys |
---|
165 | |
---|
166 | def download_app(self): |
---|
167 | |
---|
168 | cmd=self.epicore.download_app() |
---|
169 | |
---|
170 | if cmd !="": |
---|
171 | print(' [EPIC]: Downloading application....') |
---|
172 | os.system(cmd) |
---|
173 | result=self.epicore.check_download() |
---|
174 | if result: |
---|
175 | msg_log='Downloading application: OK' |
---|
176 | self.write_log(msg_log) |
---|
177 | return True |
---|
178 | else: |
---|
179 | msg_log='Installation aborted. Error downloading application' |
---|
180 | print(' [EPIC]: '+msg_log) |
---|
181 | self.write_log(msg_log) |
---|
182 | return False |
---|
183 | else: |
---|
184 | return True |
---|
185 | |
---|
186 | #def download_app |
---|
187 | |
---|
188 | |
---|
189 | def preinstall_app(self): |
---|
190 | |
---|
191 | cmd=self.epicore.preinstall_app() |
---|
192 | |
---|
193 | if cmd !="": |
---|
194 | print(' [EPIC]: Preparing installation...') |
---|
195 | p=subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE) |
---|
196 | output=p.communicate() |
---|
197 | error=self.readErrorOutput(output[1]) |
---|
198 | if error: |
---|
199 | msg_log='Installation aborted. Error preparing system:' '\n' + str(output[1]) |
---|
200 | print(' [EPIC]: ' +msg_log) |
---|
201 | self.write_log(msg_log) |
---|
202 | return False |
---|
203 | else: |
---|
204 | result=self.epicore.check_preinstall() |
---|
205 | if result: |
---|
206 | msg_log='Preparing installation: OK' |
---|
207 | self.write_log(msg_log) |
---|
208 | return True |
---|
209 | else: |
---|
210 | msg_log='Installation aborted. Error preparing system' |
---|
211 | print(' [EPIC]: '+msg_log) |
---|
212 | self.write_log(msg_log) |
---|
213 | |
---|
214 | return False |
---|
215 | else: |
---|
216 | return True |
---|
217 | |
---|
218 | #def preinstall_app |
---|
219 | |
---|
220 | |
---|
221 | def install_app(self): |
---|
222 | |
---|
223 | cmd=self.epicore.install_app() |
---|
224 | |
---|
225 | if cmd !="": |
---|
226 | print(' [EPIC]: Installing application...') |
---|
227 | p=subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE) |
---|
228 | output=p.communicate() |
---|
229 | error=self.readErrorOutput(output[1]) |
---|
230 | if error: |
---|
231 | msg_log='Installation aborted. Error installing application:' +'\n'+str(output[1]) |
---|
232 | print(' [EPIC]: '+msg_log) |
---|
233 | self.write_log(msg_log) |
---|
234 | return False |
---|
235 | else: |
---|
236 | dpkg_status,result=self.epicore.check_install_remove("install") |
---|
237 | if result: |
---|
238 | msg_log='Installing application: OK' |
---|
239 | self.write_log(msg_log) |
---|
240 | return True |
---|
241 | else: |
---|
242 | msg_log='Installation aborted. Error installing application' |
---|
243 | print(' [EPIC]: '+msg_log) |
---|
244 | self.write_log(msg_log) |
---|
245 | return False |
---|
246 | else: |
---|
247 | return True |
---|
248 | |
---|
249 | #def install_app |
---|
250 | |
---|
251 | def postinstall_app(self): |
---|
252 | |
---|
253 | cmd=self.epicore.postinstall_app() |
---|
254 | |
---|
255 | if cmd !="": |
---|
256 | print(' [EPIC]: Ending installation...') |
---|
257 | p=subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE) |
---|
258 | output=p.communicate() |
---|
259 | error=self.readErrorOutput(output[1]) |
---|
260 | if error: |
---|
261 | msg_log='Installation aborted. Error ending installation:' +'\n'+str(output[1]) |
---|
262 | print(' [EPIC]: '+msg_log) |
---|
263 | self.write_log(msg_log) |
---|
264 | return False |
---|
265 | else: |
---|
266 | result=self.epicore.check_postinstall() |
---|
267 | if result: |
---|
268 | msg_log='Ending installation. OK' |
---|
269 | self.write_log(msg_log) |
---|
270 | return True |
---|
271 | else: |
---|
272 | msg_log='Installation aborted. Error ending installation' |
---|
273 | print(' [EPIC]: '+msg_log) |
---|
274 | self.write_log(msg_log) |
---|
275 | return False |
---|
276 | else: |
---|
277 | return True |
---|
278 | |
---|
279 | #def postinstall_app |
---|
280 | |
---|
281 | |
---|
282 | def install(self,mode): |
---|
283 | |
---|
284 | checksystem=self.checking_system('install') |
---|
285 | error=False |
---|
286 | |
---|
287 | if checksystem: |
---|
288 | self.showInfo(True) |
---|
289 | if not mode: |
---|
290 | response=input(' [EPIC]: Do you want to install the application (yes/no)): ').lower() |
---|
291 | else: |
---|
292 | response='yes' |
---|
293 | |
---|
294 | if response.startswith('y'): |
---|
295 | msg_log='Installing application by CLI' |
---|
296 | self.write_log(msg_log) |
---|
297 | order=len(self.epicore.epiFiles) |
---|
298 | if order>1: |
---|
299 | print ('****************************************************************') |
---|
300 | print ('*********************** INSTALLING DEPENDS *********************') |
---|
301 | print ('****************************************************************') |
---|
302 | |
---|
303 | for item in self.epicore.epiFiles: |
---|
304 | order=order-1 |
---|
305 | self.epicore.zerocenter_feedback(order,'init') |
---|
306 | if order==0: |
---|
307 | print ('****************************************************************') |
---|
308 | print ('******************** INSTALLING APPLICATION ********************') |
---|
309 | print ('****************************************************************') |
---|
310 | |
---|
311 | result=self.add_repository_keys(order) |
---|
312 | if result: |
---|
313 | result=self.download_app() |
---|
314 | if result: |
---|
315 | result=self.preinstall_app() |
---|
316 | if result: |
---|
317 | result=self.install_app() |
---|
318 | if result: |
---|
319 | result=self.postinstall_app() |
---|
320 | if result: |
---|
321 | self.epicore.zerocenter_feedback(order,'install',result) |
---|
322 | self.epicore.remove_repo_keys() |
---|
323 | else: |
---|
324 | error=True |
---|
325 | |
---|
326 | else: |
---|
327 | error=True |
---|
328 | else: |
---|
329 | error=True |
---|
330 | |
---|
331 | else: |
---|
332 | error=True |
---|
333 | else: |
---|
334 | error=True |
---|
335 | |
---|
336 | if error: |
---|
337 | self.epicore.zerocenter_feedback(order,'install',result) |
---|
338 | self.epicore.remove_repo_keys() |
---|
339 | return 1 |
---|
340 | |
---|
341 | msg_log='Installation completed successfully' |
---|
342 | print(' [EPIC]: '+msg_log) |
---|
343 | self.write_log(msg_log) |
---|
344 | self.epicore.remove_repo_keys() |
---|
345 | |
---|
346 | |
---|
347 | else: |
---|
348 | msg_log='Installation cancelled' |
---|
349 | print (' [EPIC]: '+msg_log) |
---|
350 | self.write_log(msg_log) |
---|
351 | self.epicore.remove_repo_keys() |
---|
352 | return 0 |
---|
353 | |
---|
354 | else: |
---|
355 | return 1 |
---|
356 | |
---|
357 | #def install |
---|
358 | |
---|
359 | |
---|
360 | def uninstall_process(self): |
---|
361 | |
---|
362 | cmd=self.epicore.unistall_app(0) |
---|
363 | |
---|
364 | if cmd !="": |
---|
365 | print(' [EPIC]: Uninstall application...') |
---|
366 | p=subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE) |
---|
367 | output=p.communicate() |
---|
368 | error=self.readErrorOutput(output[1]) |
---|
369 | if error: |
---|
370 | msg_log='Uninstalled process ending with errors:' +'\n'+str(output[1]) |
---|
371 | print(' [EPIC]: '+msg_log) |
---|
372 | self.write_log(msg_log) |
---|
373 | return False |
---|
374 | else: |
---|
375 | dpkg_status,result=self.epicore.check_install_remove("uninstall") |
---|
376 | if result: |
---|
377 | return True |
---|
378 | else: |
---|
379 | msg_log='Uninstalled process ending with errors' |
---|
380 | print(' [EPIC]: '+msg_log) |
---|
381 | self.write_log(msg_log) |
---|
382 | return False |
---|
383 | else: |
---|
384 | return True |
---|
385 | |
---|
386 | #def uninstall_process |
---|
387 | |
---|
388 | def uninstall(self,mode): |
---|
389 | |
---|
390 | checksystem=self.checking_system('uninstall') |
---|
391 | error=False |
---|
392 | |
---|
393 | if checksystem: |
---|
394 | self.showInfo(True) |
---|
395 | if self.uninstall=='Yes': |
---|
396 | if not mode: |
---|
397 | response=input(' [EPIC]: Do you want to uninstall the application (yes/no)): ').lower() |
---|
398 | else: |
---|
399 | response='yes' |
---|
400 | |
---|
401 | if response.startswith('y'): |
---|
402 | |
---|
403 | msg_log='Uninstall application by CLI' |
---|
404 | self.write_log(msg_log) |
---|
405 | |
---|
406 | result=self.uninstall_process() |
---|
407 | if result: |
---|
408 | self.epicore.zerocenter_feedback(0,'uninstall',result) |
---|
409 | msg_log='Application successfully uninstalled' |
---|
410 | print(' [EPIC]: '+msg_log) |
---|
411 | self.write_log(msg_log) |
---|
412 | return 0 |
---|
413 | else: |
---|
414 | self.epicore.zerocenter_feedback(0,'uninstall',result) |
---|
415 | return 1 |
---|
416 | else: |
---|
417 | msg_log='Uninstall process canceled' |
---|
418 | print (' [EPIC]: '+msg_log) |
---|
419 | self.write_log(msg_log) |
---|
420 | return 0 |
---|
421 | |
---|
422 | else: |
---|
423 | msg_log='Uninstall process not availabled' |
---|
424 | print (' [EPIC]: '+msg_log) |
---|
425 | self.write_log(msg_log) |
---|
426 | return 0 |
---|
427 | |
---|
428 | else: |
---|
429 | return 1 |
---|
430 | |
---|
431 | #def uninstall |
---|
432 | |
---|
433 | |
---|
434 | def readErrorOutput(self,output): |
---|
435 | |
---|
436 | cont=0 |
---|
437 | if type(output) is bytes: |
---|
438 | output=output.decode() |
---|
439 | lines=output.split('\n') |
---|
440 | for line in lines: |
---|
441 | if "E: " in line: |
---|
442 | cont=cont+1 |
---|
443 | |
---|
444 | if cont>0: |
---|
445 | return True |
---|
446 | else: |
---|
447 | return False |
---|
448 | |
---|
449 | # def readErrorOutput |
---|
450 | |
---|
451 | def handler_signal(self,signal,frame): |
---|
452 | |
---|
453 | print("\n [EPIC]: Cancel process with Ctrl+C signal") |
---|
454 | self.epicore.remove_repo_keys() |
---|
455 | msg_log="Cancel process with Ctrl+C signal" |
---|
456 | self.write_log(msg_log) |
---|
457 | sys.exit(0) |
---|
458 | |
---|
459 | #def handler_signal |
---|
460 | |
---|
461 | def write_log(self,msg): |
---|
462 | |
---|
463 | syslog.openlog("EPI") |
---|
464 | syslog.syslog(msg) |
---|
465 | |
---|
466 | return |
---|
467 | |
---|
468 | #def write_log |
---|