Advertisement
sergio_educacionit

apache_tools-jueves-p2.py

Jun 8th, 2023
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. # Importar libreria para capturar argumentos pasados al script.
  4.  
  5. import sys
  6. import os
  7. import subprocess
  8.  
  9.  
  10. # Diccionario con mensajes de error
  11.  
  12. mensajes_error = {
  13.     "err_no_command":"Debe ingresar un comando.",
  14.     "err_invalid_command":"Debe ingresar un comando valido.",
  15.     "err_no_servername":"Debe ingresar un nombre para el vhost."
  16. }
  17.  
  18.  
  19.  
  20. # Lista con los comandos que utiliza este programa
  21.  
  22.  
  23. comandos = [ 'newhost', 'modhost', 'delhost' , 'test']
  24.  
  25. argumentos = sys.argv
  26.  
  27. # Validar que haya al menos un argumento pasado al script
  28.  
  29. if len(argumentos) < 2:
  30.  
  31.     print(mensajes_error["err_no_command"])
  32.     exit(1)
  33.  
  34.  
  35. if argumentos[1] == comandos[0]:
  36.    
  37.     if len(argumentos) < 3:
  38.  
  39.         print(mensajes_error["err_no_servername"])
  40.         exit(1)
  41.  
  42.     puerto = 80
  43.  
  44.     server_name = argumentos[-1]
  45.  
  46.     document_root = f"/var/www/{server_name}"
  47.  
  48.  
  49.     # Procesar parametros
  50.  
  51.     contador = 0
  52.  
  53.     for arg in argumentos:
  54.  
  55.         if arg == "-p":
  56.  
  57.             puerto = argumentos[contador + 1]
  58.  
  59.  
  60.         elif arg == "-d":
  61.  
  62.             document_root = argumentos[contador + 1]
  63.  
  64.         contador += 1
  65.  
  66.  
  67.     output = f"<VirtualHost *:{puerto}>\n\n\tDocumentRoot {document_root}\n\tServerName {server_name}\n\n</VirtualHost>\n"
  68.    
  69.     # Escritura de archivo
  70.  
  71.     try:
  72.         with open(f"/etc/apache2/sites-available/{server_name}.conf", "w") as vhost:
  73.  
  74.             vhost.write(output)
  75.             print(output)
  76.  
  77.     except Exception as error:
  78.  
  79.         print(error)
  80.  
  81.  
  82. # modhost
  83. elif argumentos[1] == comandos[1]:
  84.  
  85.     # abrir un archivo '<server_name>.conf'
  86.     # crear la cadena para escribir el fichero con los parametros para puerto y document root
  87.     # sobreescribir el fichero
  88.  
  89.  
  90.     print(argumentos[1])
  91.  
  92. # delhost
  93. elif argumentos[1] == comandos[2]:
  94.  
  95.  
  96.     # comprobamos la canitdad de argumentos
  97.  
  98.     if len(argumentos) < 3:
  99.  
  100.         print(mensajes_error["err_no_servername"])
  101.         exit(1)
  102.  
  103.     # Si existe el argumento '--delete' se elimnan los ficheros available y enabled
  104.     if  argumentos[2] == "--delete":
  105.  
  106.         try:
  107.             os.remove(f"/etc/apache2/sites-available/{argumentos[-1]}.conf")
  108.             os.remove(f"/etc/apache2/sites-enabled/{argumentos[-1]}.conf")
  109.  
  110.         except Exception as error:
  111.  
  112.             print(error)
  113.  
  114.         finally:
  115.  
  116.              process_output = subprocess.run(["systemctl", "reload", "apache2"], capture_output=True, text=True)
  117.  
  118.              process_error = process_output.stderr
  119.              
  120.              print(process_error)
  121.  
  122.  
  123.     else:
  124.         # de lo contrario solo el enabled
  125.         try:
  126.             os.remove(f"/etc/apache2/sites-enabled/{argumentos[-1]}.conf")
  127.  
  128.         except Exception as error:
  129.        
  130.             print(error)
  131.  
  132.         finally:
  133.  
  134.              process_output = subprocess.run(["systemctl", "reload", "apache2"], capture_output=True, text=True)
  135.  
  136.              process_error = process_output.stderr
  137.              
  138.              print(process_error)
  139.  
  140.  
  141.  
  142.  
  143.  
  144. elif argumentos[1] == comandos[3]:
  145.    
  146.    
  147.     process_output = subprocess.run(["apache2ctl", "configtest"], capture_output=True, text=True)
  148.  
  149.     process_error = process_output.stderr
  150.  
  151.     print(process_error)
  152.  
  153.  
  154. else:
  155.  
  156.     print(mensajes_error["err_invalid_command"])
  157.  
  158.  
  159.  
  160.  
  161.  
  162. print("\nFin\n")
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement