131 lines
3.2 KiB
Go
131 lines
3.2 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func RunContainer(image string, name string, port int) error {
|
|
|
|
cmd := exec.Command(
|
|
"docker", "run",
|
|
"-d",
|
|
"--name", name,
|
|
"-p", fmt.Sprintf("%d:%d", port, port),
|
|
image,
|
|
)
|
|
|
|
return cmd.Run()
|
|
}
|
|
|
|
func PullImage(image string) (string, error) {
|
|
|
|
cmd := exec.Command("docker", "pull", image)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
return string(out), err
|
|
}
|
|
|
|
func ImageExists(image string) bool {
|
|
cmd := exec.Command("docker", "image", "inspect", image)
|
|
err := cmd.Run()
|
|
|
|
return err == nil
|
|
}
|
|
|
|
func PushFileToContainer(container, filePath, destinationPath string) bool {
|
|
dest := fmt.Sprintf("%s:%s", container, destinationPath)
|
|
|
|
cmd := exec.Command("docker", "cp", filePath, dest)
|
|
|
|
err := cmd.Run()
|
|
return err == nil
|
|
}
|
|
|
|
func RunWireguardDockerContainer(envFilePath string, cv ConfigValues) error {
|
|
containerName := "vproxy"
|
|
|
|
removeExistingContainer(containerName)
|
|
|
|
absPath, err := filepath.Abs(envFilePath)
|
|
if err != nil {
|
|
return fmt.Errorf("erro ao resolver caminho absoluto: %w", err)
|
|
}
|
|
|
|
cmd := exec.Command(
|
|
"docker", "run", "-it", "-d",
|
|
"--name", containerName,
|
|
"--network", "app-dono_app",
|
|
"--restart", "unless-stopped",
|
|
"--cap-add=NET_ADMIN",
|
|
"--device", "/dev/net/tun:/dev/net/tun",
|
|
"--log-opt", "max-size=5m",
|
|
"--log-opt", "max-file=1",
|
|
"--env-file", absPath,
|
|
wireguardImageName,
|
|
)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("docker run falhou: %w\noutput: %s", err, string(out))
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
containerID := strings.TrimSpace(string(out))
|
|
return verifyContainerRunning(containerID)
|
|
}
|
|
|
|
func RunAppClienteContainer(image, containerName, configPath, configDestinationPath string, cv ConfigValues) error {
|
|
removeExistingContainer(containerName)
|
|
|
|
absPath, err := filepath.Abs(configPath)
|
|
if err != nil {
|
|
return fmt.Errorf("erro ao resolver caminho absoluto: %w", err)
|
|
}
|
|
|
|
cmd := exec.Command(
|
|
"docker", "run", "-d",
|
|
"-p", fmt.Sprintf("%s:8080", cv.Server["port"]),
|
|
"--name", containerName,
|
|
"--network", "app-dono_app",
|
|
"--restart", "unless-stopped",
|
|
"-v", fmt.Sprintf("%s:%s", absPath, configDestinationPath), // Config mapping
|
|
"-v", fmt.Sprintf("%s:/app/certs", cv.Cert["cert_dir_path"]), // Certs mapping
|
|
image,
|
|
)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("docker run falhou: %w\noutput: %s", err, string(out))
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
containerID := strings.TrimSpace(string(out))
|
|
return verifyContainerRunning(containerID)
|
|
}
|
|
|
|
func removeExistingContainer(name string) {
|
|
exec.Command("docker", "stop", name).Run()
|
|
exec.Command("docker", "rm", name).Run()
|
|
}
|
|
|
|
func verifyContainerRunning(containerID string) error {
|
|
cmd := exec.Command("docker", "inspect", "--format", "{{.State.Status}}", containerID)
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return fmt.Errorf("erro ao inspecionar container: %w", err)
|
|
}
|
|
|
|
status := strings.TrimSpace(string(out))
|
|
if status != "running" {
|
|
logCmd := exec.Command("docker", "logs", "--tail", "20", containerID)
|
|
logs, _ := logCmd.CombinedOutput()
|
|
return fmt.Errorf("container parou com status %q.\nlogs:\n%s", status, string(logs))
|
|
}
|
|
|
|
return nil
|
|
}
|