1 | import subprocess |
---|
2 | import datetime |
---|
3 | import sys |
---|
4 | from DiskLib import * |
---|
5 | from CacheLib import * |
---|
6 | from SystemLib import * |
---|
7 | from SystemLib import OG_ERR_CACHESIZE, OG_ERR_NOTCACHE, OG_ERR_NOTWRITE, OG_ERR_FILESYS, OG_ERR_REPO, OG_ERR_NOTOS, OG_ERR_NOGPT, OG_ERR_OUTOFLIMIT, OG_ERR_IMAGE, OG_ERR_CACHE, OGLOGSESSION, OGLOGCOMMAND, OGLOGFILE, OG_ERR_LOCKED, OG_ERR_PARTITION, OG_ERR_FORMAT, OG_ERR_NOTEXEC, OG_ERR_NOTFOUND |
---|
8 | |
---|
9 | def ogEcho(*args): |
---|
10 | # Variables locales |
---|
11 | CONT = 1 |
---|
12 | LOGS = "" |
---|
13 | LOGLEVEL = "" |
---|
14 | DATETIME = "" |
---|
15 | |
---|
16 | # Selección de ficheros de registro de incidencias. |
---|
17 | while CONT: |
---|
18 | arg = args.pop(0).lower() |
---|
19 | if arg == "log": |
---|
20 | LOGS += " " + OGLOGFILE |
---|
21 | elif arg == "command": |
---|
22 | LOGS += " " + OGLOGCOMMAND |
---|
23 | elif arg == "session": |
---|
24 | LOGS += " " + OGLOGSESSION |
---|
25 | else: |
---|
26 | CONT = 0 |
---|
27 | |
---|
28 | # Selección del nivel de registro (opcional). |
---|
29 | arg = args.pop(0).lower() |
---|
30 | if arg == "help": |
---|
31 | pass |
---|
32 | elif arg == "info" or arg == "warning" or arg == "error": |
---|
33 | LOGLEVEL = arg |
---|
34 | |
---|
35 | if LOGLEVEL: |
---|
36 | DATETIME = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
---|
37 | # Registrar mensajes en fichero de log si la depuración no está desactivada. |
---|
38 | if DEBUG.lower() != "no": |
---|
39 | LOGS += " " + OGLOGFILE |
---|
40 | logger.info(f"OpenGnsys {LOGLEVEL} {DATETIME} {' '.join(args)}") |
---|
41 | else: |
---|
42 | print(' '.join(args)) |
---|
43 | |
---|
44 | def ogExecAndLog(*args): |
---|
45 | # Variables locales |
---|
46 | ISCOMMAND = False |
---|
47 | ISLOG = False |
---|
48 | ISSESSION = False |
---|
49 | COMMAND = "" |
---|
50 | CONTINUE = 1 |
---|
51 | FILES = "" |
---|
52 | REDIREC = "" |
---|
53 | |
---|
54 | # Si se solicita, mostrar ayuda. |
---|
55 | if len(args) > 0 and args[0] == "help": |
---|
56 | ogHelp(f"{FUNCNAME} str_logfile ... str_command ...", |
---|
57 | f"{FUNCNAME} COMMAND ls -al /") |
---|
58 | return |
---|
59 | |
---|
60 | # Procesar parámetros. |
---|
61 | while CONTINUE: |
---|
62 | arg = args.pop(0).lower() |
---|
63 | if arg == "command": |
---|
64 | ISCOMMAND = True |
---|
65 | continue |
---|
66 | elif arg == "log": |
---|
67 | ISLOG = True |
---|
68 | continue |
---|
69 | elif arg == "session": |
---|
70 | ISSESSION = True |
---|
71 | continue |
---|
72 | else: |
---|
73 | COMMAND = " ".join(args) |
---|
74 | CONTINUE = 0 |
---|
75 | |
---|
76 | # Error si no se recibe un comando que ejecutar. |
---|
77 | if not COMMAND: |
---|
78 | ogRaiseError(OG_ERR_FORMAT) |
---|
79 | return |
---|
80 | |
---|
81 | # Componer lista de ficheros de registro. |
---|
82 | if ISCOMMAND: |
---|
83 | FILES = OGLOGCOMMAND |
---|
84 | open(FILES, "w").close() |
---|
85 | REDIREC = "2>&1" |
---|
86 | if ISLOG: |
---|
87 | FILES += " " + OGLOGFILE |
---|
88 | if ISSESSION: |
---|
89 | FILES += " " + OGLOGSESSION |
---|
90 | |
---|
91 | # Ejecutar comando. |
---|
92 | subprocess.call(f"{COMMAND} {REDIREC} | tee -a {FILES}", shell=True) |
---|
93 | # Salida de error del comando ejecutado. |
---|
94 | return subprocess.PIPESTATUS[0] |
---|
95 | |
---|
96 | def ogGetCaller(): |
---|
97 | # Obtener el nombre del programa o del script que ha llamado al proceso actual. |
---|
98 | output = subprocess.check_output(["ps", "hp", str(os.getppid()), "-o", "args"]).decode("utf-8") |
---|
99 | lines = output.split("\n") |
---|
100 | caller = "" |
---|
101 | for line in lines: |
---|
102 | if "bash" in line and line.split()[1] != "": |
---|
103 | caller = line.split()[1] |
---|
104 | else: |
---|
105 | caller = line.split()[0].lstrip("-") |
---|
106 | return os.path.basename(caller) |
---|
107 | |
---|
108 | def ogHelp(*args): |
---|
109 | # Variables locales |
---|
110 | FUNC = "" |
---|
111 | MSG = "" |
---|
112 | |
---|
113 | # Mostrar función, descripción y formato. |
---|
114 | FUNC = args[0] if len(args) > 0 else FUNCNAME[-1] |
---|
115 | MSG = f"MSG_HELP_{FUNC}" |
---|
116 | ogEcho("help", f"{MSG_FUNCTION} {FUNC}: {globals()[MSG]}") |
---|
117 | if len(args) > 1: |
---|
118 | ogEcho("help", f" {MSG_FORMAT}: {args[1]}") |
---|
119 | |
---|
120 | # Mostrar ejemplos (si existen). |
---|
121 | for example in args[2:]: |
---|
122 | ogEcho("help", f" {MSG_EXAMPLE}: {example}") |
---|
123 | |
---|
124 | def ogRaiseError(*args): |
---|
125 | # Variables locales |
---|
126 | CONT = 1 |
---|
127 | LOGS = "" |
---|
128 | MSG = "" |
---|
129 | CODE = "" |
---|
130 | FUNCS = "" |
---|
131 | |
---|
132 | # Si se solicita, mostrar ayuda. |
---|
133 | if len(args) > 0 and args[0] == "help": |
---|
134 | ogHelp(f"{FUNCNAME}", f"{FUNCNAME} [str_logfile ...] int_errorcode str_errormessage") |
---|
135 | return |
---|
136 | |
---|
137 | # Selección de registros de incidencias. |
---|
138 | while CONT: |
---|
139 | arg = args.pop(0).lower() |
---|
140 | if arg == "log" or arg == "command" or arg == "session": |
---|
141 | LOGS += " " + arg |
---|
142 | else: |
---|
143 | CONT = 0 |
---|
144 | |
---|
145 | # Obtener código y mensaje de error. |
---|
146 | CODE = args.pop(0) |
---|
147 | if CODE == OG_ERR_FORMAT: |
---|
148 | MSG = f"{MSG_ERR_FORMAT} \"{args.pop(0)}\"" |
---|
149 | elif CODE == OG_ERR_NOTFOUND: |
---|
150 | MSG = f"{MSG_ERR_NOTFOUND} \"{args.pop(0)}\"" |
---|
151 | elif CODE == OG_ERR_OUTOFLIMIT: |
---|
152 | MSG = f"{MSG_ERR_OUTOFLIMIT} \"{args.pop(0)}\"" |
---|
153 | elif CODE == OG_ERR_PARTITION: |
---|
154 | MSG = f"{MSG_ERR_PARTITION} \"{args.pop(0)}\"" |
---|
155 | elif CODE == OG_ERR_LOCKED: |
---|
156 | MSG = f"{MSG_ERR_LOCKED} \"{args.pop(0)}\"" |
---|
157 | elif CODE == OG_ERR_CACHE: |
---|
158 | MSG = f"{MSG_ERR_CACHE} \"{args.pop(0)}\"" |
---|
159 | elif CODE == OG_ERR_NOGPT: |
---|
160 | MSG = f"{MSG_ERR_NOGPT} \"{args.pop(0)}\"" |
---|
161 | elif CODE == OG_ERR_REPO: |
---|
162 | MSG = f"{MSG_ERR_REPO} \"{args.pop(0)}\"" |
---|
163 | elif CODE == OG_ERR_FILESYS: |
---|
164 | MSG = f"{MSG_ERR_FILESYS} \"{args.pop(0)}\"" |
---|
165 | elif CODE == OG_ERR_IMAGE: |
---|
166 | MSG = f"{MSG_ERR_IMAGE} \"{args.pop(0)}\"" |
---|
167 | elif CODE == OG_ERR_NOTOS: |
---|
168 | MSG = f"{MSG_ERR_NOTOS} \"{args.pop(0)}\"" |
---|
169 | elif CODE == OG_ERR_NOTEXEC: |
---|
170 | MSG = f"{MSG_ERR_NOTEXEC} \"{args.pop(0)}\"" |
---|
171 | elif CODE == OG_ERR_NOTWRITE: |
---|
172 | MSG = f"{MSG_ERR_NOTWRITE} \"{args.pop(0)}\"" |
---|
173 | elif CODE == OG_ERR_NOTCACHE: |
---|
174 | MSG = f"{MSG_ERR_NOTCACHE} \"{args.pop(0)}\"" |
---|
175 | elif CODE == OG_ERR_CACHESIZE: |
---|
176 | MSG = f"{MSG_ERR_CACHESIZE} \"{args.pop(0)}\"" |
---|
177 | elif CODE == OG_ERR_REDUCEFS: |
---|
178 | MSG = f"{MSG_ERR_REDUCEFS} \"{args.pop(0)}\"" |
---|
179 | elif CODE == OG_ERR_EXTENDFS: |
---|
180 | MSG = f"{MSG_ERR_EXTENDFS} \"{args.pop(0)}\"" |
---|
181 | elif CODE == OG_ERR_IMGSIZEPARTITION: |
---|
182 | MSG = f"{MSG_ERR_IMGSIZEPARTITION} \"{args.pop(0)}\"" |
---|
183 | elif CODE == OG_ERR_UPDATECACHE: |
---|
184 | MSG = f"{MSG_ERR_UPDATECACHE} \"{args.pop(0)}\"" |
---|
185 | elif CODE == OG_ERR_DONTFORMAT: |
---|
186 | MSG = f"{MSG_ERR_DONTFORMAT} \"{args.pop(0)}\"" |
---|
187 | elif CODE == OG_ERR_IMAGEFILE: |
---|
188 | MSG = f"{MSG_ERR_IMAGEFILE} \"{args.pop(0)}\"" |
---|
189 | elif CODE == OG_ERR_UCASTSYNTAXT: |
---|
190 | MSG = f"{MSG_ERR_UCASTSYNTAXT} \"{args.pop(0)}\"" |
---|
191 | elif CODE == OG_ERR_UCASTSENDPARTITION: |
---|
192 | MSG = f"{MSG_ERR_UCASTSENDPARTITION} \"{args.pop(0)}\"" |
---|
193 | elif CODE == OG_ERR_UCASTSENDFILE: |
---|
194 | MSG = f"{MSG_ERR_UCASTSENDFILE} \"{args.pop(0)}\"" |
---|
195 | elif CODE == OG_ERR_UCASTRECEIVERPARTITION: |
---|
196 | MSG = f"{MSG_ERR_UCASTRECEIVERPARTITION} \"{args.pop(0)}\"" |
---|
197 | elif CODE == OG_ERR_UCASTRECEIVERFILE: |
---|
198 | MSG = f"{MSG_ERR_UCASTRECEIVERFILE} \"{args.pop(0)}\"" |
---|
199 | elif CODE == OG_ERR_MCASTSYNTAXT: |
---|
200 | MSG = f"{MSG_ERR_MCASTSYNTAXT} \"{args.pop(0)}\"" |
---|
201 | elif CODE == OG_ERR_MCASTSENDFILE: |
---|
202 | MSG = f"{MSG_ERR_MCASTSENDFILE} \"{args.pop(0)}\"" |
---|
203 | elif CODE == OG_ERR_MCASTRECEIVERFILE: |
---|
204 | MSG = f"{MSG_ERR_MCASTRECEIVERFILE} \"{args.pop(0)}\"" |
---|
205 | elif CODE == OG_ERR_MCASTSENDPARTITION: |
---|
206 | MSG = f"{MSG_ERR_MCASTSENDPARTITION} \"{args.pop(0)}\"" |
---|
207 | elif CODE == OG_ERR_MCASTRECEIVERPARTITION: |
---|
208 | MSG = f"{MSG_ERR_MCASTRECEIVERPARTITION} \"{args.pop(0)}\"" |
---|
209 | elif CODE == OG_ERR_PROTOCOLJOINMASTER: |
---|
210 | MSG = f"{MSG_ERR_PROTOCOLJOINMASTER} \"{args.pop(0)}\"" |
---|
211 | elif CODE == OG_ERR_DONTMOUNT_IMAGE: |
---|
212 | MSG = f"{MSG_ERR_DONTMOUNT_IMAGE} \"{args.pop(0)}\"" |
---|
213 | elif CODE == OG_ERR_DONTUNMOUNT_IMAGE: |
---|
214 | MSG = f"{MSG_ERR_DONTUNMOUNT_IMAGE} \"{args.pop(0)}\"" |
---|
215 | elif CODE == OG_ERR_DONTSYNC_IMAGE: |
---|
216 | MSG = f"{MSG_ERR_DONTSYNC_IMAGE} \"{args.pop(0)}\"" |
---|
217 | elif CODE == OG_ERR_NOTDIFFERENT: |
---|
218 | MSG = f"{MSG_ERR_NOTDIFFERENT} \"{args.pop(0)}\"" |
---|
219 | elif CODE == OG_ERR_SYNCHRONIZING: |
---|
220 | MSG = f"{MSG_ERR_SYNCHRONIZING} \"{args.pop(0)}\"" |
---|
221 | elif CODE == OG_ERR_NOTUEFI: |
---|
222 | MSG = f"{MSG_ERR_NOTUEFI} \"{args.pop(0)}\"" |
---|
223 | elif CODE == OG_ERR_NOMSDOS: |
---|
224 | MSG = f"{MSG_ERR_NOMSDOS} \"{args.pop(0)}\"" |
---|
225 | elif CODE == OG_ERR_NOTBIOS: |
---|
226 | MSG = f"{MSG_ERR_NOTBIOS} \"{args.pop(0)}\"" |
---|
227 | else: |
---|
228 | MSG = MSG_ERR_GENERIC |
---|
229 | CODE = OG_ERR_GENERIC |
---|
230 | |
---|
231 | # Obtener lista de funciones afectadas, incluyendo el script que las llama. |
---|
232 | FUNCS = " ".join(FUNCNAME[1:]) |
---|
233 | FUNCS = FUNCS.replace("main", os.path.basename(sys.argv[0])) |
---|
234 | |
---|
235 | # Mostrar mensaje de error si es función depurable y salir con el código indicado. |
---|
236 | if CODE == OG_ERR_FORMAT or ogCheckStringInGroup(FUNCS, NODEBUGFUNCTIONS) or not ogCheckStringInGroup(FUNCS.split()[0], NODEBUGFUNCTIONS): |
---|
237 | ogEcho(LOGS, "error", f"{FUNCS.replace(' ', '<-')}: {MSG}", file=sys.stderr) |
---|
238 | |
---|
239 | return CODE |
---|
240 | |
---|
241 | def ogIsRepoLocked(): |
---|
242 | # Variables locales |
---|
243 | FILES = "" |
---|
244 | |
---|
245 | # Si se solicita, mostrar ayuda. |
---|
246 | if len(sys.argv) > 1 and sys.argv[1] == "help": |
---|
247 | ogHelp(f"{FUNCNAME}", f"{FUNCNAME}", f"if {FUNCNAME}(): ...") |
---|
248 | |
---|
249 | # No hacer nada, si no está definido el punto de montaje del repositorio. |
---|
250 | if not OGIMG: |
---|
251 | return 1 |
---|
252 | |
---|
253 | # Comprobar si alguno de los ficheros abiertos por los procesos activos está en el |
---|
254 | # punto de montaje del repositorio de imágenes. |
---|
255 | FILES = subprocess.check_output(["find", "/proc", "-maxdepth", "2", "-type", "f", "-lname", f"{OGIMG}/*"]).decode("utf-8") |
---|
256 | return bool(FILES) |
---|
257 | |
---|
258 | def ogCheckProgram(*args): |
---|
259 | # Si se solicita, mostrar ayuda. |
---|
260 | if len(args) > 0 and args[0] == "help": |
---|
261 | ogHelp(f"{FUNCNAME} \"str_program ...\"", |
---|
262 | f"{FUNCNAME} \"partimage partclone mbuffer\"") |
---|
263 | return |
---|
264 | |
---|
265 | # Error si no se recibe 1 parámetro. |
---|
266 | if len(args) != 1: |
---|
267 | ogRaiseError(OG_ERR_FORMAT) |
---|
268 | return |
---|
269 | |
---|
270 | PERROR = 0 |
---|
271 | PLOG = " " |
---|
272 | for i in args[0].split(): |
---|
273 | if not shutil.which(i): |
---|
274 | PERROR = 1 |
---|
275 | PLOG += f" {i}" |
---|
276 | |
---|
277 | if PERROR == 1: |
---|
278 | ogRaiseError(OG_ERR_NOTEXEC, PLOG) |
---|
279 | return |
---|
280 | else: |
---|
281 | return 0 |
---|
282 | |
---|
283 | def ogIsVirtualMachine(): |
---|
284 | output = subprocess.check_output(["dmidecode", "-s", "system-product-name"]).decode("utf-8") |
---|
285 | if "KVM" in output or "VirtualBox" in output: |
---|
286 | return 1 |
---|
287 | else: |
---|
288 | return 0 |
---|