ogrepository/bin/checkImage.py

156 lines
5.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Este script comprueba la integridad de la imagen que recibe como parámetro, volviendo a calcular el tamaño del archivo y el hash MD5 del último MB,
y comparándolos con los valores almacenados en el archivo ".size" y en el archivo ".sum", respectivamente.
Parámetros
------------
sys.argv[1] - Nombre completo de la imagen a chequear (con o sin ruta).
- Ejemplo1: image1.img
- Ejemplo2: /opt/opengnsys/ogrepository/images/image1.img
Sintaxis
----------
./checkImage.py image_name|/image_path/image_name
Ejemplos
---------
./checkImage.py image1.img
./checkImage.py /opt/opengnsys/ogrepository/images/image1.img
"""
# --------------------------------------------------------------------------------------------
# IMPORTS
# --------------------------------------------------------------------------------------------
import os
import sys
import hashlib
# --------------------------------------------------------------------------------------------
# VARIABLES
# --------------------------------------------------------------------------------------------
script_name = os.path.basename(__file__)
repo_path = '/opt/opengnsys/ogrepository/images/' # No borrar la barra final
# --------------------------------------------------------------------------------------------
# FUNCTIONS
# --------------------------------------------------------------------------------------------
def show_help():
""" Imprime la ayuda, cuando se ejecuta el script con el parámetro "help".
"""
help_text = f"""
Sintaxis: {script_name} image_name|/image_path/image_name
Ejemplo1: {script_name} image1.img
Ejemplo2: {script_name} /opt/opengnsys/ogrepository/images/image1.img
"""
print(help_text)
def check_params():
""" Comprueba que se haya enviado la cantidad correcta de parámetros, y en el formato correcto.
Si no es así, muestra un mensaje de error, y sale del script.
LLama a la función "show_help" cuando se ejecuta el script con el parámetro "help".
"""
# Si se ejecuta el script con el parámetro "help", se muestra la ayuda, y se sale del script:
if len(sys.argv) == 2 and sys.argv[1] == "help":
show_help()
sys.exit(0)
# Si se ejecuta el script con más o menos de 1 parámetro, se muestra un error y la ayuda, y se sale del script:
elif len(sys.argv) != 2:
print(f"{script_name} Error: Formato incorrecto: Se debe especificar 1 parámetro")
show_help()
sys.exit(1)
def build_file_path():
""" Construye la ruta completa del archivo a chequear
(agregando "/opt/opengnsys/ogrepository/images/" si no se ha especificado en el parámetro).
"""
param_path = sys.argv[1]
# Construimos la ruta completa:
if not param_path.startswith(repo_path):
file_path = os.path.join(repo_path, param_path)
else:
file_path = param_path
return file_path
def get_md5_sum(file_path, megabytes=1):
""" Calcula y retorna el hash MD5 del último MB del archivo de imagen que recibe como parámetro.
Se utiliza para comprobar el valor del archivo ".sum".
"""
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
f.seek(-megabytes * 1024 * 1024, os.SEEK_END)
data = f.read(megabytes * 1024 * 1024)
hash_md5.update(data)
return hash_md5.hexdigest()
# --------------------------------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------------------------------
def main():
"""
"""
# Evaluamos si se ha enviado la cantidad correcta de parámetros, y en el formato correcto:
check_params()
# Obtenemos la ruta completa al archivo a chequear:
file_path = build_file_path()
# Si no existe el archivo de imagen, imprimimos un mensaje de error y salimos del script:
if not os.path.exists(file_path):
print("Image file doesn't exist")
sys.exit(2)
# Comprobamos si existe el archivo ".sum", y si su contenido coincide con el valor real:
if os.path.exists(f"{file_path}.sum"):
sumOK = False
actual_datasum = get_md5_sum(file_path)
with open(f"{file_path}.sum", 'r') as file:
file_datasum = file.read().strip('\n')
if actual_datasum == file_datasum:
sumOK = True
else:
print("Sum file doesn't exist")
sys.exit(3)
# Comprobamos si existe el archivo ".size", y si su contenido coincide con el valor real:
if os.path.exists(f"{file_path}.size"):
sizeOK = False
actual_size = os.path.getsize(file_path)
with open(f"{file_path}.size", 'r') as file:
file_datasize = int(file.read().strip('\n'))
if actual_size == file_datasize:
sizeOK = True
else:
print("Size file doesn't exist")
sys.exit(4)
# Evaluamos los resultados, e imprimimos un mensaje "OK" o "KO":
if sumOK == True and sizeOK == True:
print("Image file passed the Integrity Check correctly")
else:
print("Error: Image file didn't pass the Integrity Check")
# --------------------------------------------------------------------------------------------
if __name__ == "__main__":
main()
# --------------------------------------------------------------------------------------------