2025-01-07 16:01:49 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2025-01-08 19:29:05 +01:00
|
|
|
"os"
|
2025-01-09 17:58:51 +01:00
|
|
|
"os/exec"
|
2025-01-09 17:29:36 +01:00
|
|
|
"strings"
|
2025-01-09 17:58:51 +01:00
|
|
|
"time"
|
2025-01-07 16:01:49 +01:00
|
|
|
|
|
|
|
"git.keyzox.me/42_adjoly/inception/internal/env"
|
|
|
|
)
|
|
|
|
|
2025-01-09 17:58:51 +01:00
|
|
|
func checkMariaDB(user, password, host, port string) bool {
|
|
|
|
// Create the command to run mariadb client
|
|
|
|
cmd := exec.Command("mariadb", "-u"+user, "-p"+password, "-h"+host, "-P"+port, "-e", "SELECT 1;")
|
|
|
|
|
|
|
|
// Run the command
|
|
|
|
err := cmd.Run()
|
2025-01-07 16:01:49 +01:00
|
|
|
if err != nil {
|
2025-01-09 17:58:51 +01:00
|
|
|
fmt.Printf("Health check failed: %v\n", err)
|
2025-01-09 17:29:36 +01:00
|
|
|
return false
|
2025-01-07 16:01:49 +01:00
|
|
|
}
|
2025-01-09 17:58:51 +01:00
|
|
|
fmt.Println("MariaDB is healthy")
|
2025-01-07 16:01:49 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2025-01-09 17:58:51 +01:00
|
|
|
func escapePassword(password string) string {
|
|
|
|
// Escape single quotes in passwords
|
|
|
|
new := strings.ReplaceAll(password, "\"", "")
|
|
|
|
return strings.ReplaceAll(new, "'", "\\'")
|
|
|
|
}
|
2025-01-07 16:01:49 +01:00
|
|
|
|
2025-01-09 17:58:51 +01:00
|
|
|
func main() {
|
|
|
|
// Configuration
|
|
|
|
user := escapePassword(env.FileEnv("MYSQL_USER", "mariadb"))
|
|
|
|
password := escapePassword(env.FileEnv("MYSQL_PASSWORD", "default"))
|
|
|
|
host := "127.0.0.1"
|
|
|
|
port := "3306"
|
|
|
|
|
|
|
|
// Retry health check for MariaDB
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if checkMariaDB(user, password, host, port) {
|
|
|
|
os.Exit(0) // Success
|
|
|
|
}
|
|
|
|
fmt.Println("Waiting for MariaDB to become ready...")
|
|
|
|
time.Sleep(2 * time.Second)
|
2025-01-07 16:01:49 +01:00
|
|
|
}
|
2025-01-09 17:29:36 +01:00
|
|
|
|
2025-01-09 17:58:51 +01:00
|
|
|
fmt.Println("MariaDB health check failed")
|
|
|
|
os.Exit(1) // Failure
|
2025-01-07 16:01:49 +01:00
|
|
|
}
|