2025-01-07 16:01:49 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
2025-01-08 19:29:05 +01:00
|
|
|
"strings"
|
|
|
|
"bufio"
|
2025-01-07 16:01:49 +01:00
|
|
|
|
|
|
|
"git.keyzox.me/42_adjoly/inception/internal/env"
|
|
|
|
"git.keyzox.me/42_adjoly/inception/internal/log"
|
|
|
|
)
|
|
|
|
|
2025-01-08 19:29:05 +01:00
|
|
|
func removeSkipNetworking(filePath string) error {
|
|
|
|
// Open the file for reading
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open file: %v", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Create a temporary slice to store updated lines
|
|
|
|
var updatedLines []string
|
|
|
|
|
|
|
|
// Read the file line by line
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
// Skip lines that contain "skip-networking"
|
|
|
|
if !strings.Contains(line, "skip-networking") {
|
|
|
|
updatedLines = append(updatedLines, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for scanner errors
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return fmt.Errorf("error reading file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file for writing (overwrite mode)
|
|
|
|
file, err = os.Create(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open file for writing: %v", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Write the updated lines back to the file
|
|
|
|
writer := bufio.NewWriter(file)
|
|
|
|
for _, line := range updatedLines {
|
|
|
|
_, err := writer.WriteString(line + "\n")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write to file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return writer.Flush()
|
|
|
|
}
|
|
|
|
|
2025-01-07 16:01:49 +01:00
|
|
|
func createDBDir(dataDir string) {
|
|
|
|
if dataDir == "/var/lib/mysql" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := os.Mkdir(dataDir, 750)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2025-01-09 10:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func anyFileExists(folderPath string) (bool, error) {
|
|
|
|
// Open the folder
|
|
|
|
dir, err := os.Open(folderPath)
|
2025-01-07 16:01:49 +01:00
|
|
|
if err != nil {
|
2025-01-09 10:07:36 +01:00
|
|
|
return false, err
|
2025-01-07 16:01:49 +01:00
|
|
|
}
|
2025-01-09 10:07:36 +01:00
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
// Read directory contents
|
|
|
|
files, err := dir.Readdir(1) // Read at most 1 file
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we read at least one file, it exists
|
|
|
|
return len(files) > 0, nil
|
2025-01-07 16:01:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkOlderDB(dataDir string) bool {
|
2025-01-09 10:07:36 +01:00
|
|
|
exist, err := anyFileExists(dataDir)
|
|
|
|
if err != nil || exist == false {
|
2025-01-07 16:01:49 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2025-01-08 19:29:05 +01:00
|
|
|
func waitForMariaDB(rootPass string) {
|
2025-01-07 16:01:49 +01:00
|
|
|
for i := 0; i < 30; i++ {
|
2025-01-08 19:29:05 +01:00
|
|
|
cmd := exec.Command("mariadb", "-uroot", "-p"+escapePassword(rootPass), "-e", "SELECT 1")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2025-01-07 16:01:49 +01:00
|
|
|
if err := cmd.Run(); err == nil {
|
2025-01-08 19:29:05 +01:00
|
|
|
fmt.Println("MariaDB is ready.")
|
2025-01-07 16:01:49 +01:00
|
|
|
return
|
2025-01-08 19:29:05 +01:00
|
|
|
} else {
|
|
|
|
fmt.Printf("MariaDB init process in progress... (%d/%d): %v\n", i+1, 30, err)
|
2025-01-07 16:01:49 +01:00
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
fmt.Println("MariaDB init process failed.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2025-01-08 19:29:05 +01:00
|
|
|
func escapeIdentifier(identifier string) string {
|
|
|
|
// Replace backticks with double backticks to safely escape identifiers
|
|
|
|
return fmt.Sprintf("`%s`", strings.ReplaceAll(identifier, "'", "\""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func escapePassword(password string) string {
|
|
|
|
// Escape single quotes in passwords
|
|
|
|
return strings.ReplaceAll(password, "'", "\\'")
|
|
|
|
}
|
|
|
|
|
2025-01-07 16:01:49 +01:00
|
|
|
func configureMariaDB(rootPassword, user, password, database string) {
|
2025-01-08 19:29:05 +01:00
|
|
|
cmd := exec.Command("mariadb", "-uroot", "-p"+rootPassword, "-e", fmt.Sprintf(`
|
2025-01-07 16:01:49 +01:00
|
|
|
ALTER USER 'root'@'localhost' IDENTIFIED BY '%s';
|
|
|
|
CREATE DATABASE IF NOT EXISTS %s;
|
|
|
|
CREATE USER IF NOT EXISTS '%s'@'%%' IDENTIFIED BY '%s';
|
|
|
|
GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%';
|
|
|
|
FLUSH PRIVILEGES;
|
2025-01-08 19:29:05 +01:00
|
|
|
`, escapePassword(rootPassword), escapeIdentifier(database), escapeIdentifier(user), escapePassword(password), escapeIdentifier(database), escapeIdentifier(user)))
|
2025-01-07 16:01:49 +01:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fmt.Printf("Error configuring MariaDB: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := os.Args
|
|
|
|
|
|
|
|
|
|
|
|
if args[1] == "mariadbd" || args[1] == "mysqld" {
|
|
|
|
_log.Log("note", "Entrypoint script for MariaDB Server started")
|
|
|
|
|
|
|
|
rootPass := env.FileEnv("MYSQL_ROOT_PASSWORD", "default")
|
|
|
|
pass := env.FileEnv("MYSQL_PASSWORD", "default")
|
|
|
|
user := env.FileEnv("MYSQL_USER", "mariadb")
|
|
|
|
dbName := env.EnvCheck("MYSQL_DATABASE", "default")
|
|
|
|
dataDir := env.EnvCheck("DATADIR", "/var/lib/mysql")
|
2025-01-08 19:29:05 +01:00
|
|
|
filePath := "/etc/my.cnf.d/mariadb-server.cnf"
|
2025-01-07 16:01:49 +01:00
|
|
|
|
|
|
|
oldDB := checkOlderDB(dataDir)
|
|
|
|
if oldDB == false {
|
|
|
|
createDBDir(dataDir)
|
|
|
|
// Init DB
|
|
|
|
cmd := exec.Command("mariadb-install-db", "--user=mysql", "--ldata="+dataDir)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
_log.Log("error", "Error initializing MariaDB")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting temp mariadb server for config
|
|
|
|
cmd = exec.Command("mariadbd-safe", "--skip-networking")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
fmt.Printf("Error starting MariaDB: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
// Wait for mariadb to start
|
2025-01-08 19:29:05 +01:00
|
|
|
waitForMariaDB(rootPass)
|
2025-01-07 16:01:49 +01:00
|
|
|
|
|
|
|
configureMariaDB(rootPass, user, pass, dbName)
|
|
|
|
|
2025-01-08 19:29:05 +01:00
|
|
|
|
|
|
|
cmd = exec.Command("mysqladmin", "-uroot", "-p"+rootPass, "shutdown")
|
|
|
|
if err := cmd.Run(); err != nil {
|
2025-01-07 16:01:49 +01:00
|
|
|
fmt.Printf("Error stopping MariaDB: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
2025-01-09 10:11:18 +01:00
|
|
|
if err := removeSkipNetworking(filePath); err != nil {
|
|
|
|
fmt.Printf("Error: %v\n", err)
|
|
|
|
} else {
|
|
|
|
_log.Log("note", "Successfully removed 'skip-networking' from the configuration file.")
|
|
|
|
}
|
2025-01-07 16:01:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(args[1], args[2:]...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fmt.Printf("Error running MariaDB: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|