feat: funciones infra — Docker, deploy, build y health check
Funciones impuras para gestión de contenedores: docker_build_image, docker_compose_up/down, docker_volume_create/list/remove, generate_dockerfile, write_dockerfile, go_build_binary, health_check_http, deploy_app y stop_app. Todas con tests unitarios donde aplica. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DeployApp orquesta el deploy completo de una app Go en Docker.
|
||||
// Pasos: genera Dockerfile → lo escribe → build image → run container (detach, port mapping).
|
||||
// Retorna el container ID del contenedor lanzado.
|
||||
func DeployApp(appDir string, imageName string, port int, envVars map[string]string) (string, error) {
|
||||
// 1. Generar Dockerfile (puro)
|
||||
content := GenerateDockerfile(imageName, port, envVars)
|
||||
|
||||
// 2. Escribir Dockerfile a disco
|
||||
_, err := WriteDockerfile(appDir, content)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("deploy_app: escribir Dockerfile: %w", err)
|
||||
}
|
||||
|
||||
// 3. Build de la imagen Docker
|
||||
tag := imageName + ":latest"
|
||||
_, err = DockerBuildImage(appDir, tag, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("deploy_app: build imagen %s: %w", tag, err)
|
||||
}
|
||||
|
||||
// 4. Ejecutar el contenedor en modo detach con port mapping
|
||||
portMapping := fmt.Sprintf("%d:%d", port, port)
|
||||
opts := DockerRunOpts{
|
||||
Name: imageName,
|
||||
Ports: []string{portMapping},
|
||||
Env: envVars,
|
||||
Detach: true,
|
||||
}
|
||||
containerID, err := DockerRunContainer(tag, opts)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("deploy_app: run contenedor %s: %w", imageName, err)
|
||||
}
|
||||
|
||||
return containerID, nil
|
||||
}
|
||||
Reference in New Issue
Block a user