Add initial setup scripts and configuration files for development environment
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import os
|
||||
import uuid
|
||||
import shutil
|
||||
import json
|
||||
import subprocess
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
|
||||
# Archivo JSON para guardar los datos de los contenedores
|
||||
CONTAINER_DATA_FILE = 'containers.json'
|
||||
# Nombre y etiqueta para la imagen base
|
||||
BASE_IMAGE_TAG = 'alpine_vscode:latest'
|
||||
|
||||
CONTENEDOR_IDENTIFICADOR = "easily-sound-ant"
|
||||
|
||||
# -------------------------------
|
||||
# Función: Eliminar un contenedor por ID o nombre y actualizar el JSON
|
||||
# -------------------------------
|
||||
def delete_container(identifier):
|
||||
if not os.path.exists(CONTAINER_DATA_FILE):
|
||||
print("❌ No hay archivo JSON de contenedores.")
|
||||
return
|
||||
|
||||
with open(CONTAINER_DATA_FILE, 'r') as file:
|
||||
containers = json.load(file)
|
||||
|
||||
# Determinar si el identificador es un ID o un nombre
|
||||
container = next((c for c in containers if c['id'] == identifier or c['name'] == identifier), None)
|
||||
if not container:
|
||||
print(f"❌ No se encontró ningún contenedor con ID o nombre: {identifier} en el JSON.")
|
||||
return
|
||||
|
||||
try:
|
||||
# Comprobar si el contenedor existe realmente en Docker
|
||||
result = subprocess.getoutput(f"docker ps -a -q -f id={container['id']}")
|
||||
if not result:
|
||||
print(f"⚠️ El contenedor {container['name']} ya no existe en Docker. Eliminando del JSON...")
|
||||
else:
|
||||
# Eliminar el contenedor y su red asociada
|
||||
print(f"➤ Eliminando contenedor: {container['name']} con ID: {container['id']}")
|
||||
os.system(f"docker rm -f {container['id']}")
|
||||
print(f"➤ Eliminando red: {container['network']}")
|
||||
os.system(f"docker network rm {container['network']}")
|
||||
|
||||
# Actualizar el JSON excluyendo el contenedor eliminado
|
||||
containers = [c for c in containers if c['id'] != container['id']]
|
||||
with open(CONTAINER_DATA_FILE, 'w') as file:
|
||||
json.dump(containers, file, indent=4)
|
||||
|
||||
print(f"✔ Contenedor {container['name']} eliminado correctamente.")
|
||||
except Exception as e:
|
||||
print(f"❌ Error eliminando el contenedor {container['name']}: {e}")
|
||||
|
||||
# -------------------------------
|
||||
# Función: Limpiar imágenes huérfanas
|
||||
# -------------------------------
|
||||
def clean_unused_images():
|
||||
print("➤ Eliminando imágenes huérfanas...")
|
||||
os.system('docker image prune -f')
|
||||
|
||||
# -------------------------------
|
||||
# Función: Limpiar redes huérfanas
|
||||
# -------------------------------
|
||||
def clean_unused_networks():
|
||||
print("➤ Eliminando redes huérfanas...")
|
||||
os.system('docker network prune -f')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
identifier = CONTENEDOR_IDENTIFICADOR
|
||||
|
||||
if identifier:
|
||||
delete_container(identifier)
|
||||
|
||||
# Limpiar imágenes y redes huérfanas
|
||||
clean_unused_images()
|
||||
clean_unused_networks()
|
||||
|
||||
else:
|
||||
print("❌ No se ingresó ningún ID o nombre.")
|
||||
Reference in New Issue
Block a user