split_config.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. {% extends "admin/base.html" %}
  2. {% block title %}{{ _("Dividir Configuración Squid") }}{% endblock %}
  3. {% block content %}
  4. <div class="max-w-7xl mx-auto">
  5. <!-- Header -->
  6. <div class="bg-gradient-to-r from-blue-600 to-blue-700 rounded-xl shadow-lg p-6 mb-6 text-white">
  7. <div class="flex items-center space-x-4">
  8. <i class="fas fa-code-branch text-4xl"></i>
  9. <div>
  10. <h2 class="text-3xl font-bold">{{ _("Dividir Configuración Squid") }}</h2>
  11. <p class="text-blue-100 mt-1">{{ _("Modulariza tu archivo squid.conf para una gestión más sencilla") }}</p>
  12. </div>
  13. </div>
  14. </div>
  15. <!-- Info Cards -->
  16. <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
  17. <div class="bg-white rounded-lg shadow-md p-6">
  18. <div class="flex items-center space-x-3">
  19. <i class="fas fa-file-code text-blue-600 text-2xl"></i>
  20. <div>
  21. <p class="text-gray-600 text-sm">{{ _("Archivo de entrada") }}</p>
  22. <p class="font-semibold text-gray-800 text-sm">{{ input_file }}</p>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="bg-white rounded-lg shadow-md p-6">
  27. <div class="flex items-center space-x-3">
  28. <i class="fas fa-folder text-emerald-600 text-2xl"></i>
  29. <div>
  30. <p class="text-gray-600 text-sm">{{ _("Directorio de salida") }}</p>
  31. <p class="font-semibold text-gray-800 text-sm">{{ output_dir }}</p>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="bg-white rounded-lg shadow-md p-6">
  36. <div class="flex items-center space-x-3">
  37. <i class="fas fa-layer-group text-amber-600 text-2xl"></i>
  38. <div>
  39. <p class="text-gray-600 text-sm">{{ _("Estado") }}</p>
  40. {% if output_exists %}
  41. <p class="font-semibold text-emerald-600 text-sm">{{ files_count }} {{ _("archivos generados") }}</p>
  42. {% else %}
  43. <p class="font-semibold text-gray-600 text-sm">{{ _("No dividido aún") }}</p>
  44. {% endif %}
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. <!-- Description -->
  50. <div class="bg-white rounded-lg shadow-md p-6 mb-6">
  51. <h3 class="text-xl font-bold text-gray-800 mb-3 flex items-center">
  52. <i class="fas fa-info-circle text-blue-600 mr-2"></i>
  53. {{ _("¿Qué hace esta funcionalidad?") }}
  54. </h3>
  55. <p class="text-gray-700 mb-4">
  56. {{ _("Esta herramienta divide tu archivo monolítico") }} <code class="bg-gray-100 px-2 py-1 rounded">squid.conf</code>
  57. {{ _("en múltiples archivos modulares organizados por categorías. Esto facilita:") }}
  58. </p>
  59. <ul class="list-disc list-inside text-gray-700 space-y-2 ml-4">
  60. <li>{{ _("Gestión y mantenimiento más sencillo de la configuración") }}</li>
  61. <li>{{ _("Mejor organización de directivas por funcionalidad") }}</li>
  62. <li>{{ _("Facilita la edición desde la interfaz web") }}</li>
  63. <li>{{ _("Reduce errores al editar configuraciones complejas") }}</li>
  64. </ul>
  65. </div>
  66. <!-- Files to be generated -->
  67. <div class="bg-white rounded-lg shadow-md p-6 mb-6">
  68. <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
  69. <i class="fas fa-list text-blue-600 mr-2"></i>
  70. {{ _("Archivos que se generarán") }}
  71. </h3>
  72. <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
  73. {% for filename, description in split_info.items() %}
  74. <div class="border border-gray-200 rounded-lg p-4 hover:shadow-md transition-shadow">
  75. <div class="flex items-start space-x-3">
  76. <i class="fas fa-file-alt text-blue-500 mt-1"></i>
  77. <div class="flex-1">
  78. <p class="font-semibold text-gray-800 text-sm">{{ filename }}</p>
  79. <p class="text-gray-600 text-xs mt-1">{{ description }}</p>
  80. </div>
  81. </div>
  82. </div>
  83. {% endfor %}
  84. </div>
  85. </div>
  86. <!-- Actions -->
  87. <div class="bg-white rounded-lg shadow-md p-6">
  88. <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
  89. <i class="fas fa-cogs text-blue-600 mr-2"></i>
  90. {{ _("Acciones") }}
  91. </h3>
  92. <!-- RC -->
  93. <div class="mb-6 bg-red-50 border-2 border-red-300 rounded-lg p-6 flex items-start space-x-4">
  94. <i class="fas fa-exclamation-triangle text-red-600 text-3xl mt-1 animate-pulse"></i>
  95. <div class="flex-1">
  96. <p class="font-bold text-red-800 text-lg">{{ _("¡MUY IMPORTANTE!") }}</p>
  97. <p class="text-red-700 mt-2 font-medium">
  98. {{ _("Esta funcionalidad se encuentra en") }} <strong>{{ _("RC") }}</strong>. Puede contener errores y comportamientos inesperados.
  99. </p>
  100. <p class="text-red-700 mt-2">
  101. <strong>{{ _("Por favor, no la uses en producción de momento. Úsala únicamente en un entorno de prueba.") }}</strong>
  102. </p>
  103. </div>
  104. </div>
  105. <div class="space-y-4">
  106. <!-- Strict mode toggle -->
  107. <div class="flex items-center space-x-3 p-4 bg-gray-50 rounded-lg">
  108. <input type="checkbox" id="strictMode" class="w-5 h-5 text-blue-600 rounded focus:ring-blue-500">
  109. <div class="flex-1">
  110. <label for="strictMode" class="font-medium text-gray-800 cursor-pointer">{{ _("Modo estricto") }}</label>
  111. <p class="text-sm text-gray-600">{{ _("Abortar si se encuentran directivas desconocidas") }}</p>
  112. </div>
  113. </div>
  114. <!-- Action buttons -->
  115. <div class="flex flex-col sm:flex-row gap-4">
  116. <button onclick="confirmSplit()" title="{{ _("Divide squid.conf en archivos modulares y crea respaldo") }}" class="flex-1 bg-gradient-to-r from-blue-600 to-blue-700 hover:from-blue-700 hover:to-blue-800 text-white font-semibold px-6 py-3 rounded-lg shadow-lg hover:shadow-xl transform hover:scale-105 transition-all duration-200 flex items-center justify-center">
  117. <i class="fas fa-code-branch mr-2"></i>
  118. {{ _("Dividir Configuración") }}
  119. </button>
  120. {% if output_exists %}
  121. <button onclick="showFilesModal()" title="{{ _("Abre un modal con los archivos generados por la división") }}" class="flex-1 bg-gradient-to-r from-emerald-600 to-emerald-700 hover:from-emerald-700 hover:to-emerald-800 text-white font-semibold px-6 py-3 rounded-lg shadow-lg hover:shadow-xl transform hover:scale-105 transition-all duration-200 flex items-center justify-center">
  122. <i class="fas fa-folder-open mr-2"></i>
  123. {{ _("Ver Archivos Generados") }}
  124. </button>
  125. {% endif %}
  126. </div>
  127. </div>
  128. <!-- Warning -->
  129. <div class="mt-6 bg-amber-50 border border-amber-200 rounded-lg p-4 flex items-start space-x-3">
  130. <i class="fas fa-exclamation-triangle text-amber-600 text-xl mt-1"></i>
  131. <div class="flex-1">
  132. <p class="font-semibold text-amber-800">{{ _("Advertencia") }}</p>
  133. <p class="text-sm text-amber-700 mt-1">
  134. {{ _("Esta operación creará múltiples archivos en el directorio") }} <code class="bg-amber-100 px-2 py-0.5 rounded">{{ output_dir }}</code>.
  135. Si ya existen archivos en ese directorio, serán sobrescritos.
  136. </p>
  137. </div>
  138. </div>
  139. </div>
  140. <!-- Results area (hidden by default) -->
  141. <div id="resultsArea" class="hidden mt-6 bg-white rounded-lg shadow-md p-6">
  142. <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
  143. <i class="fas fa-check-circle text-emerald-600 mr-2"></i>
  144. {{ _("Resultados") }}
  145. </h3>
  146. <div id="resultsContent" class="space-y-3"></div>
  147. </div>
  148. </div>
  149. <!-- Modal para ver archivos generados -->
  150. <div id="filesModal" class="hidden fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center p-4">
  151. <div class="bg-white rounded-lg shadow-2xl max-w-4xl w-full max-h-[90vh] flex flex-col">
  152. <!-- Modal Header -->
  153. <div class="bg-gradient-to-r from-emerald-600 to-emerald-700 text-white px-6 py-4 rounded-t-lg flex items-center justify-between">
  154. <div class="flex items-center space-x-3">
  155. <i class="fas fa-folder-open text-2xl"></i>
  156. <h3 class="text-xl font-bold">{{ _("Archivos Generados") }}</h3>
  157. </div>
  158. <button onclick="closeFilesModal()" class="text-white hover:text-gray-200 transition-colors">
  159. <i class="fas fa-times text-2xl"></i>
  160. </button>
  161. </div>
  162. <!-- Modal Body -->
  163. <div class="flex-1 overflow-y-auto p-6">
  164. <div id="filesModalContent" class="space-y-4">
  165. <!-- Loading spinner -->
  166. <div class="flex items-center justify-center py-8">
  167. <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-emerald-600"></div>
  168. </div>
  169. </div>
  170. </div>
  171. <!-- Modal Footer -->
  172. <div class="bg-gray-50 px-6 py-4 rounded-b-lg flex items-center justify-between border-t">
  173. <p class="text-sm text-gray-600">
  174. <i class="fas fa-info-circle mr-1"></i>
  175. {{ _("Directorio:") }} <code class="bg-gray-200 px-2 py-1 rounded text-xs">{{ output_dir }}</code>
  176. </p>
  177. <button onclick="closeFilesModal()" class="bg-gray-600 hover:bg-gray-700 text-white font-semibold px-4 py-2 rounded-lg transition-colors">
  178. {{ _("Cerrar") }}
  179. </button>
  180. </div>
  181. </div>
  182. </div>
  183. <!-- Toastr CSS -->
  184. <link rel="stylesheet" href="{{ url_for('static', filename='css/toastr.min.css') }}">
  185. <!-- jQuery (required for Toastr) -->
  186. <script src="{{ url_for('static', filename='js/jquery-4.0.0.min.js') }}"></script>
  187. <!-- Toastr JS -->
  188. <script src="{{ url_for('static', filename='js/toastr.min.js') }}"></script>
  189. <script>
  190. // Configure toastr
  191. toastr.options = {
  192. closeButton: true,
  193. progressBar: true,
  194. positionClass: "toast-top-right",
  195. timeOut: "5000",
  196. };
  197. // Get CSRF token
  198. function getCsrfToken() {
  199. return document.querySelector('meta[name="csrf-token"]').getAttribute('content');
  200. }
  201. // Get current date for backup filename
  202. function getCurrentDate() {
  203. const now = new Date();
  204. const year = now.getFullYear();
  205. const month = String(now.getMonth() + 1).padStart(2, '0');
  206. const day = String(now.getDate()).padStart(2, '0');
  207. const hours = String(now.getHours()).padStart(2, '0');
  208. const minutes = String(now.getMinutes()).padStart(2, '0');
  209. return `${year}${month}${day}_${hours}${minutes}`;
  210. }
  211. function confirmSplit() {
  212. const backupFilename = `squid.conf.bak${getCurrentDate()}`;
  213. const msgEl = document.getElementById('confirmMessage');
  214. msgEl.replaceChildren();
  215. const wrapper = document.createElement('div');
  216. wrapper.className = 'space-y-3';
  217. const title = document.createElement('div');
  218. title.className = 'text-base text-gray-900 font-semibold mb-1';
  219. title.textContent = '¿Está seguro de que desea dividir la configuración de Squid?';
  220. wrapper.appendChild(title);
  221. const infoBox = document.createElement('div');
  222. infoBox.className = 'bg-blue-50 border border-blue-200 rounded-lg p-3 mt-2 text-left';
  223. const p1 = document.createElement('p');
  224. p1.className = 'text-sm text-blue-900 mb-2';
  225. const icon = document.createElement('i');
  226. icon.className = 'fas fa-info-circle mr-2';
  227. const strong = document.createElement('strong');
  228. strong.textContent = 'Esta operación es reversible';
  229. p1.appendChild(icon);
  230. p1.appendChild(strong);
  231. const p2 = document.createElement('p');
  232. p2.className = 'text-xs text-blue-800';
  233. p2.append('Se creará automáticamente una copia de seguridad de tu archivo ');
  234. const code = document.createElement('code');
  235. code.className = 'bg-blue-100 px-1 py-0.5 rounded';
  236. code.textContent = 'squid.conf';
  237. p2.appendChild(code);
  238. p2.append(' con el nombre:');
  239. const p3 = document.createElement('p');
  240. p3.className = 'text-xs text-blue-900 font-mono mt-1 bg-blue-100 px-2 py-1 rounded';
  241. p3.textContent = backupFilename;
  242. infoBox.appendChild(p1);
  243. infoBox.appendChild(p2);
  244. infoBox.appendChild(p3);
  245. wrapper.appendChild(infoBox);
  246. msgEl.appendChild(wrapper);
  247. openConfirmModal(executeSplit);
  248. }
  249. async function executeSplit() {
  250. const strictMode = document.getElementById('strictMode').checked;
  251. const resultsArea = document.getElementById('resultsArea');
  252. const resultsContent = document.getElementById('resultsContent');
  253. try {
  254. // Show loading toast
  255. toastr.info('Dividiendo configuración...', 'Procesando');
  256. const response = await fetch('/admin/api/split-config', {
  257. method: 'POST',
  258. headers: {
  259. 'Content-Type': 'application/json',
  260. 'X-CSRFToken': getCsrfToken()
  261. },
  262. body: JSON.stringify({ strict: strictMode })
  263. });
  264. const data = await response.json();
  265. if (data.status === 'success') {
  266. toastr.success(data.message, '{{ _('Éxito') }}');
  267. // Show results
  268. resultsArea.classList.remove('hidden');
  269. resultsContent.innerHTML = `
  270. <div class="bg-emerald-50 border border-emerald-200 rounded-lg p-4">
  271. <p class="font-semibold text-emerald-800 mb-2">
  272. <i class="fas fa-check-circle mr-2"></i>
  273. {{ _('Configuración dividida exitosamente') }}
  274. </p>
  275. <p class="text-sm text-emerald-700">
  276. {{ _('Se generaron') }} <strong>${data.data.total_files}</strong> {{ _('archivos en') }}
  277. <code class="bg-emerald-100 px-2 py-0.5 rounded">${data.data.output_dir}</code>
  278. </p>
  279. </div>
  280. <div class="mt-4">
  281. <p class="font-semibold text-gray-800 mb-2">{{ _('Archivos generados:') }}</p>
  282. <div class="grid grid-cols-1 md:grid-cols-2 gap-2">
  283. ${Object.entries(data.data.files).map(([filename, lines]) => `
  284. <div class="flex justify-between items-center bg-gray-50 px-3 py-2 rounded">
  285. <span class="text-sm text-gray-700">${filename}</span>
  286. <span class="text-xs text-gray-500">${lines} líneas</span>
  287. </div>
  288. `).join('')}
  289. </div>
  290. </div>
  291. `;
  292. // Reload page after 3 seconds to update the stats
  293. setTimeout(() => {
  294. window.location.reload();
  295. }, 3000);
  296. } else {
  297. toastr.error(data.message || 'Error al dividir la configuración', 'Error');
  298. resultsArea.classList.add('hidden');
  299. }
  300. } catch (error) {
  301. console.error('Error:', error);
  302. toastr.error('Error de conexión al dividir la configuración', 'Error');
  303. resultsArea.classList.add('hidden');
  304. }
  305. }
  306. // Función para mostrar el modal de archivos
  307. async function showFilesModal() {
  308. const modal = document.getElementById('filesModal');
  309. const content = document.getElementById('filesModalContent');
  310. // Mostrar modal
  311. modal.classList.remove('hidden');
  312. // Mostrar loading
  313. content.innerHTML = `
  314. <div class="flex items-center justify-center py-8">
  315. <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-emerald-600"></div>
  316. </div>
  317. `;
  318. try {
  319. const response = await fetch('/admin/api/get-split-files', {
  320. method: 'GET',
  321. headers: {
  322. 'X-CSRFToken': getCsrfToken()
  323. }
  324. });
  325. const data = await response.json();
  326. if (data.status === 'success') {
  327. const files = data.data.files;
  328. if (files.length === 0) {
  329. content.innerHTML = `
  330. <div class="text-center py-8">
  331. <i class="fas fa-folder-open text-gray-400 text-6xl mb-4"></i>
  332. <p class="text-gray-600">No se encontraron archivos en el directorio</p>
  333. </div>
  334. `;
  335. return;
  336. }
  337. content.innerHTML = `
  338. <div class="mb-4 bg-emerald-50 border border-emerald-200 rounded-lg p-4">
  339. <p class="text-emerald-800">
  340. <i class="fas fa-check-circle mr-2"></i>
  341. Se encontraron <strong>${data.data.total_files}</strong> archivo${data.data.total_files !== 1 ? 's' : ''} de configuración
  342. </p>
  343. </div>
  344. <div class="space-y-3">
  345. ${files.map(file => `
  346. <div class="border border-gray-200 rounded-lg p-4 hover:shadow-md transition-shadow bg-white">
  347. <div class="flex items-start justify-between">
  348. <div class="flex items-start space-x-3 flex-1">
  349. <i class="fas fa-file-code text-blue-500 text-xl mt-1"></i>
  350. <div class="flex-1">
  351. <p class="font-semibold text-gray-800">${file.filename}</p>
  352. <div class="mt-2 flex flex-wrap gap-3 text-xs text-gray-600">
  353. <span class="flex items-center">
  354. <i class="fas fa-align-left mr-1"></i>
  355. ${file.lines} línea${file.lines !== 1 ? 's' : ''}
  356. </span>
  357. <span class="flex items-center">
  358. <i class="fas fa-weight-hanging mr-1"></i>
  359. ${file.size_human}
  360. </span>
  361. <span class="flex items-center">
  362. <i class="fas fa-clock mr-1"></i>
  363. ${file.modified}
  364. </span>
  365. </div>
  366. </div>
  367. </div>
  368. </div>
  369. </div>
  370. `).join('')}
  371. </div>
  372. `;
  373. } else {
  374. content.innerHTML = `
  375. <div class="bg-red-50 border border-red-200 rounded-lg p-4">
  376. <p class="text-red-800">
  377. <i class="fas fa-exclamation-circle mr-2"></i>
  378. ${data.message || 'Error al obtener los archivos'}
  379. </p>
  380. </div>
  381. `;
  382. }
  383. } catch (error) {
  384. console.error('Error:', error);
  385. content.innerHTML = `
  386. <div class="bg-red-50 border border-red-200 rounded-lg p-4">
  387. <p class="text-red-800">
  388. <i class="fas fa-exclamation-circle mr-2"></i>
  389. Error de conexión al obtener los archivos
  390. </p>
  391. </div>
  392. `;
  393. }
  394. }
  395. // Función para cerrar el modal
  396. function closeFilesModal() {
  397. const modal = document.getElementById('filesModal');
  398. modal.classList.add('hidden');
  399. }
  400. // Cerrar modal al hacer clic fuera de él
  401. document.getElementById('filesModal')?.addEventListener('click', function(e) {
  402. if (e.target === this) {
  403. closeFilesModal();
  404. }
  405. });
  406. // Cerrar modal con la tecla ESC
  407. document.addEventListener('keydown', function(e) {
  408. if (e.key === 'Escape') {
  409. closeFilesModal();
  410. }
  411. });
  412. </script>
  413. {% endblock %}