Update logs setup.
Add clean dockers env. Fix Dockerfile with the latest step.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import logging
|
||||
import os
|
||||
import json
|
||||
|
||||
from pathlib import Path
|
||||
from subprocess import run, CalledProcessError
|
||||
|
||||
# def clean_all_dockers():
|
||||
# # bash docker context use default && docker system prune -a -f
|
||||
# # bash docker context use podman && docker system prune -a -f
|
||||
# # bash docker context use desktop-linux && docker system prune -a -f
|
||||
# docker_client = docker.DockerClient(base_url=f'unix:///var/run/docker.sock')
|
||||
|
||||
|
||||
def clean_all_dockers():
|
||||
"""
|
||||
Nettoie Docker pour tous les contextes disponibles (default, podman, ...).
|
||||
Equivalent a, pour chaque contexte :
|
||||
docker context use <context>
|
||||
docker system prune -a -f
|
||||
Puis remet le contexte d'origine en place a la fin.
|
||||
"""
|
||||
logs_dir = Path(os.getcwd(), 'logs')
|
||||
logs_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
logger = logging.getLogger('blender_docker.clean')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.propagate = False
|
||||
if not logger.handlers:
|
||||
handler = logging.FileHandler(logs_dir / 'clean.log')
|
||||
handler.setFormatter(logging.Formatter(
|
||||
'%(asctime)s|%(name)s [%(levelname)s] %(message)s'
|
||||
))
|
||||
logger.addHandler(handler)
|
||||
|
||||
# Contexte courant, pour le restaurer a la fin
|
||||
original_context = run(
|
||||
['docker', 'context', 'show'],
|
||||
capture_output=True, text=True, check=True
|
||||
).stdout.strip()
|
||||
logger.info(f'Contexte d\'origine: {original_context}')
|
||||
|
||||
# Liste de tous les contextes disponibles (format json, un objet par ligne)
|
||||
contexts_raw = run(
|
||||
['docker', 'context', 'ls', '--format', '{{json .}}'],
|
||||
capture_output=True, text=True, check=True
|
||||
).stdout.strip().splitlines()
|
||||
contexts = [json.loads(line)['Name'] for line in contexts_raw if line]
|
||||
|
||||
print(f'\tContextes trouves: {contexts}')
|
||||
logger.info(f'Contextes trouves: {contexts}')
|
||||
|
||||
for context in contexts:
|
||||
try:
|
||||
print(f'\tNettoyage du contexte: {context}')
|
||||
logger.info(f'Nettoyage du contexte: {context}')
|
||||
|
||||
run(['docker', 'context', 'use', context], check=True)
|
||||
|
||||
result = run(
|
||||
['docker', 'system', 'prune', '-a', '-f'],
|
||||
capture_output=True, text=True, check=True
|
||||
)
|
||||
|
||||
print(f'\t{result.stdout.strip()}')
|
||||
logger.info(result.stdout.strip())
|
||||
|
||||
except CalledProcessError as e:
|
||||
print(f'\tErreur sur le contexte {context}: {e.stderr.strip()}')
|
||||
logger.error(f'Erreur sur le contexte {context}: {e.stderr.strip()}')
|
||||
|
||||
# On remet le contexte d'origine
|
||||
run(['docker', 'context', 'use', original_context], check=True)
|
||||
print(f'\tContexte restaure: {original_context}')
|
||||
logger.info(f'Contexte restaure: {original_context}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
clean_all_dockers()
|
||||
Reference in New Issue
Block a user