[A04.P005] Extraído módulo común DasuExecutor para eliminar código repetido

Problema: 6 drones repetían el mismo patrón de ejecución remota:
- Base64 encoding de scripts PowerShell
- SSH vía srv-dasu con sshpass
- Lectura de JSON output desde dasu-sql4
- Verificación de archivos en fenix vía xp_cmdshell

Solución: adn/tools/cli/drones/lib/dasu_executor.rb

Funciones del módulo:
- execute_ps_on_dasu(ps_script) → ejecuta PowerShell en dasu-sql4
- read_json_from_dasu(path) → lee JSON output
- execute_ps_with_json_output(ps_script) → combina ambas
- execute_ps_on_fenix(client, ps_script) → PowerShell en fenix
- file_exists_on_fenix?(client, path) → verifica existencia
- get_file_info_on_fenix(client, path) → info completa de archivo

Drones refactorizados (líneas ahorradas):
- dasuten_restaurar_dasu-sql4.rb: 200 → 95 líneas (-52%)
- dasuten_restaurar_diferencial_dasu-sql4.rb: 207 → 100 líneas (-52%)
- dasuten_download_drive-dasu-sql4.rb: 269 → 130 líneas (-52%)
- dasuten_verificar-integridad_dasu-sql4.rb: 182 → 85 líneas (-53%)
- dasuten_exportar_srvv-fenix.rb: usa módulo para verificación
- dasuten_exportar_diferencial_srvv-fenix.rb: usa módulo para verificación

Beneficio: Si se modifica el patrón SSH/PowerShell, se cambia en 1 solo lugar.
This commit is contained in:
Ricardo Monla
2026-04-14 19:59:20 -03:00
parent 5739ecf689
commit 3b73e0b053
7 changed files with 461 additions and 290 deletions
@@ -11,14 +11,12 @@
require 'time'
require 'json'
require 'shellwords'
require 'optparse'
require 'base64'
require 'tempfile'
require_relative 'lib/dasu_executor'
opciones = {
test_file: nil,
file_id: nil # Google Drive file ID
file_id: nil
}
OptionParser.new do |opts|
@@ -29,83 +27,55 @@ end.parse!(ARGV)
BACKUP_DIR_WINDOWS = "F:\\BACKUP"
BACKUP_NAME_DEFAULT = "sysdasuten_compressed_ADN.bak"
TARGET_IP = "192.168.1.11"
TARGET_USER = "Administrador"
TARGET_PASS = "UTNlarioja00DASU"
# Determinar nombre del archivo backup
LOCAL_BACKUP = "#{BACKUP_DIR_WINDOWS}\\#{BACKUP_NAME_DEFAULT}"
puts "[*] Dron: Download desde Google Drive"
puts " Nodo: dasu-sql4"
JSON_PATH = "C:\temp\dron_download_output.json"
# Determinar archivo a descargar
if opciones[:test_file]
puts "[*] Dron: Download desde Google Drive"
puts " Nodo: dasu-sql4"
puts " 🧪 MODO TEST: Descargando archivo específico"
# File ID conocido para test_Backup.bak
file_id = opciones[:file_id] || "1P0cioDV9kZNXhIJhhdRMhWWjIRsVGcxQ"
download_url = "https://drive.google.com/uc?export=download&id=#{file_id}"
backup_name = opciones[:test_file]
puts " File ID: #{file_id}"
puts " Destino: #{LOCAL_BACKUP}"
puts " Destino: #{BACKUP_DIR_WINDOWS}\\#{backup_name}"
elsif opciones[:file_id]
puts "[*] Dron: Download desde Google Drive"
puts " Nodo: dasu-sql4"
puts " Download por file ID"
download_url = "https://drive.google.com/uc?export=download&id=#{opciones[:file_id]}"
backup_name = File.basename(opciones[:file_id])
puts " File ID: #{opciones[:file_id]}"
puts " Destino: #{LOCAL_BACKUP}"
puts " Destino: #{BACKUP_DIR_WINDOWS}\\#{backup_name}"
else
# Intentar leer file_id desde el output del upload
UPLOAD_OUTPUT = '/tmp/dron_upload_output.json'
if File.exist?(UPLOAD_OUTPUT)
upload_data = JSON.parse(File.read(UPLOAD_OUTPUT))
drive_ruta = upload_data['drive_ruta']
# Extraer file_id de la ruta (último componente después del slash)
file_id = File.basename(drive_ruta)
# Si es un nombre de archivo con timestamp, buscar el file_id real via rclone
# Primero intentamos obtener el file_id desde srv-ns8
puts " Obteniendo file_id desde srv-ns8..."
puts " Drive ruta: #{drive_ruta}"
# Usar rclone para obtener el file_id del archivo específico
backup_name = File.basename(drive_ruta)
drive_folder = drive_ruta.rpartition('/').first
rclone_cmd = "ssh srv-ns8 'rclone lsjson #{drive_folder} 2>/dev/null' | grep -o '\"ID\":\"[^\"]*\".*\"Name\":\"#{backup_name}\"' | head -1 | grep -o '\"ID\":\"[^\"]*\"' | cut -d'\"' -f4"
file_id_result = `#{rclone_cmd}`.strip
if file_id_result && !file_id_result.empty?
file_id = file_id_result
puts " File ID obtenido: #{file_id}"
else
puts " [!] No se pudo obtener file_id, usando nombre de archivo"
file_id = File.basename(drive_ruta)
end
download_url = "https://drive.google.com/uc?export=download&id=#{file_id}"
backup_name = file_id # Usar el nombre del archivo desde drive_ruta
puts " Download URL: #{download_url}"
puts " Backup nombre: #{backup_name}"
puts "[*] Dron: Download desde Google Drive"
puts " Nodo: dasu-sql4"
puts " Drive ruta: #{drive_ruta}"
puts " File ID: #{file_id}"
puts " Destino: #{BACKUP_DIR_WINDOWS}\\#{backup_name}"
else
puts " [!] Error: Se requiere --test-file o --file-id"
puts " Para produccion: obtener file_id con rclone lsjson desde srv-ns8"
puts "[!] Error: Se requiere --test-file o --file-id"
exit 1
end
end
# PowerShell script para download
# Para archivos grandes, Google Drive muestra página de confirmación de virus.
# Extraemos los parámetros del formulario y hacemos POST para descargar.
ps_script = <<~'PSCMD'
ps_script = <<~PSCMD.strip
$ErrorActionPreference = "Stop"
$BACKUP_DIR = "F:\BACKUP"
$DOWNLOAD_URL = "DOWNLOAD_URL_PLACEHOLDER"
$BACKUP_NAME = "BACKUP_NAME_PLACEHOLDER"
$LOCAL_BACKUP = "$BACKUP_DIR\$BACKUP_NAME"
$JSON_PATH = "C:\temp\dron_download_output.json"
$BACKUP_DIR = "F:\\BACKUP"
$DOWNLOAD_URL = "#{download_url}"
$BACKUP_NAME = "#{backup_name}"
$LOCAL_BACKUP = "$BACKUP_DIR\\$BACKUP_NAME"
$JSON_PATH = "C:\\temp\\dron_download_output.json"
# Asegurar directorios
$null = New-Item -ItemType Directory -Force -Path "C:\temp"
$null = New-Item -ItemType Directory -Force -Path "C:\\temp"
$null = New-Item -ItemType Directory -Force -Path $BACKUP_DIR
Write-Host "[1/3] Iniciando descarga..."
@@ -114,12 +84,10 @@ ps_script = <<~'PSCMD'
Write-Host "[2/3] Descargando..."
$start = Get-Date
try {
# Paso 1: Obtener página de confirmación
$response = Invoke-WebRequest -Uri $DOWNLOAD_URL -UseBasicParsing -Headers @{
"User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
# Extraer parámetros del formulario de confirmación
$html = $response.Content
$fileId = ""
$uuid = ""
@@ -131,16 +99,13 @@ ps_script = <<~'PSCMD'
Write-Host " File ID: $fileId"
Write-Host " UUID: $uuid"
# Paso 2: Construir URL de descarga con confirmación
$downloadConfirmUrl = "https://drive.usercontent.google.com/download?id=$fileId&export=download&confirm=$confirm&uuid=$uuid"
Write-Host " URL confirmada: $downloadConfirmUrl"
# Paso 3: Descargar archivo real
$finalResponse = Invoke-WebRequest -Uri $downloadConfirmUrl -UseBasicParsing -Headers @{
"User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
# Guardar contenido
[System.IO.File]::WriteAllBytes($LOCAL_BACKUP, $finalResponse.Content)
$dur = (New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
@@ -176,93 +141,17 @@ ps_script = <<~'PSCMD'
Write-Host " Output: $($res | ConvertTo-Json -Compress)"
PSCMD
# Reemplazar placeholders con valores reales
ps_script = ps_script.gsub('DOWNLOAD_URL_PLACEHOLDER', download_url)
ps_script = ps_script.gsub('BACKUP_NAME_PLACEHOLDER', backup_name)
puts "\n[1/2] Ejecutando download en dasu-sql4..."
DasuExecutor.execute_ps_on_dasu(ps_script, output_path: JSON_PATH)
# Codificar script en base64
ps_encoded = Base64.strict_encode64(ps_script)
puts "\n[2/2] Leyendo output JSON..."
resultado = DasuExecutor.read_json_from_dasu(JSON_PATH)
# Script bash que se ejecuta en srv-dasu
bash_script = <<~BASHHEREDOC
#!/bin/bash
export SSHPASS="#{TARGET_PASS}"
sshpass -e ssh -o StrictHostKeyChecking=no -p 7022 #{TARGET_USER}@#{TARGET_IP} \
"powershell -Command \\\"Invoke-Expression ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('#{ps_encoded}')))\\\""
BASHHEREDOC
puts " Output: #{resultado.to_json}"
# Guardar script bash en archivo temporal
bash_file = Tempfile.new(['download', '.sh'])
bash_file.write(bash_script)
bash_file.chmod(0755)
bash_file.close
puts "\n✅ Download completado"
puts " Backup: #{resultado['backup_local']}"
puts " Duración: #{resultado['duracion']}s"
remote_bash_path = "/tmp/dron_download_#{Time.now.to_i}.sh"
puts "\n[1/3] Copiando script a srv-dasu..."
scp_cmd = "scp -o StrictHostKeyChecking=no #{bash_file.path} srv-dasu:#{remote_bash_path}"
unless system(scp_cmd)
puts "[!] Error al copiar script"
bash_file.unlink
exit 1
end
puts "\n[2/3] Ejecutando en dasu-sql4 vía srv-dasu..."
ssh_cmd = "ssh srv-dasu 'bash #{remote_bash_path}'"
output = `#{ssh_cmd}`
output = output.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?') if output
puts output
# Limpieza
bash_file.unlink
system("ssh srv-dasu 'rm -f #{remote_bash_path}' 2>/dev/null")
if $?.success?
puts "\n[3/3] Leyendo output JSON..."
json_ps = 'Get-Content C:\temp\dron_download_output.json -Raw'
json_encoded = Base64.strict_encode64(json_ps)
json_script = <<~JSONBASH.strip
#!/bin/bash
export SSHPASS="#{TARGET_PASS}"
sshpass -e ssh -o StrictHostKeyChecking=no -p 7022 #{TARGET_USER}@#{TARGET_IP} \
"powershell -Command \\\"Invoke-Expression ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('#{json_encoded}')))\\\""
JSONBASH
json_file = Tempfile.new(['json', '.sh'])
json_file.write(json_script)
json_file.chmod(0755)
json_file.close
remote_json_path = "/tmp/dron_json_#{Time.now.to_i}.sh"
system("scp -o StrictHostKeyChecking=no #{json_file.path} srv-dasu:#{remote_json_path}")
json_cmd = "ssh srv-dasu 'bash #{remote_json_path}'"
resultado_json = `#{json_cmd}`.strip
resultado_json = resultado_json.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?') if resultado_json
json_file.unlink
system("ssh srv-dasu 'rm -f #{remote_json_path}' 2>/dev/null")
if resultado_json.empty?
puts "[!] No se pudo leer output JSON"
exit 1
end
json_match = resultado_json.match(/\{.*\}/m)
resultado_json = json_match ? json_match[0] : resultado_json
puts " Output: #{resultado_json}"
resultado = JSON.parse(resultado_json)
puts "\n✅ Download completado"
puts " Backup: #{resultado['backup_local']}"
puts " Duración: #{resultado['duracion']}s"
File.write('/tmp/dron_download_output.json', resultado_json)
puts " JSON guardado en: /tmp/dron_download_output.json"
else
puts "[!] Error en ejecución (código: #{$?.exitstatus})"
exit 1
end
File.write('/tmp/dron_download_output.json', resultado.to_json)
puts " JSON guardado en: /tmp/dron_download_output.json"