「✨」 feat: Changed folder structure to comply with the dumb subject
This commit is contained in:
210
srcs/cmd/mariadb/entrypoint/entrypoint.go
Normal file
210
srcs/cmd/mariadb/entrypoint/entrypoint.go
Normal file
@ -0,0 +1,210 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
"strings"
|
||||
"bufio"
|
||||
|
||||
"git.keyzox.me/42_adjoly/inception/internal/env"
|
||||
"git.keyzox.me/42_adjoly/inception/internal/log"
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func createDBDir(dataDir string) {
|
||||
if dataDir == "/var/lib/mysql" {
|
||||
return
|
||||
}
|
||||
err := os.Mkdir(dataDir, 750)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func anyFileExists(folderPath string) (bool, error) {
|
||||
// Open the folder
|
||||
dir, err := os.Open(folderPath)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func checkOlderDB(dataDir string) bool {
|
||||
exist, err := anyFileExists(dataDir)
|
||||
if err != nil || exist == false {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func waitForMariaDB(rootPass string) {
|
||||
for i := 0; i < 30; i++ {
|
||||
cmd := exec.Command("mariadb", "-uroot", "-p"+env.EscapeEnv(rootPass), "-e", "SELECT 1")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err == nil {
|
||||
fmt.Println("MariaDB is ready.")
|
||||
return
|
||||
} else {
|
||||
fmt.Printf("MariaDB init process in progress... (%d/%d): %v\n", i+1, 30, err)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
fmt.Println("MariaDB init process failed.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
//func escapeIdentifier(identifier string) string {
|
||||
// // Replace backticks with double backticks to safely escape identifiers
|
||||
// return fmt.Sprintf("%s", strings.ReplaceAll(identifier, "'", "\""))
|
||||
//}
|
||||
|
||||
|
||||
func configureMariaDB(rootPassword, user, password, database string) {
|
||||
cmd := exec.Command("mariadb", "-uroot", "-e", fmt.Sprintf(`
|
||||
ALTER USER 'root'@'localhost' IDENTIFIED BY '%s';
|
||||
CREATE DATABASE IF NOT EXISTS %s;
|
||||
CREATE USER IF NOT EXISTS '%s'@'%%';
|
||||
GRANT ALL PRIVILEGES ON %s.* TO '%s'@'localhost' IDENTIFIED BY '%s';
|
||||
GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%';
|
||||
FLUSH PRIVILEGES;
|
||||
`,
|
||||
env.EscapeEnv(rootPassword),
|
||||
env.EscapeEnv(database),
|
||||
env.EscapeEnv(user),
|
||||
env.EscapeEnv(database),
|
||||
env.EscapeEnv(user),
|
||||
env.EscapeEnv(password),
|
||||
env.EscapeEnv(database),
|
||||
env.EscapeEnv(user),
|
||||
))
|
||||
|
||||
// Capture standard output and error
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Printf("Error configuring MariaDB: %v\n", err)
|
||||
fmt.Printf("Command Output: %s\n", string(output))
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("MariaDB configured successfully.")
|
||||
}
|
||||
|
||||
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")
|
||||
filePath := "/etc/my.cnf.d/mariadb-server.cnf"
|
||||
|
||||
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", fmt.Sprintf("Error initializing MariaDB: %v", err))
|
||||
}
|
||||
|
||||
// Starting temp mariadb server for config
|
||||
cmd = exec.Command("mariadbd-safe", "--datadir="+dataDir, "--skip-networking")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Start(); err != nil {
|
||||
_log.Log("error", fmt.Sprintf("Error starting MariaDB: %v", err))
|
||||
}
|
||||
|
||||
// Wait for mariadb to start
|
||||
waitForMariaDB(rootPass)
|
||||
|
||||
configureMariaDB(rootPass, user, pass, dbName)
|
||||
|
||||
cmd_stop := exec.Command("mariadb-admin", "-uroot", "-p"+env.EscapeEnv(rootPass), "shutdown")
|
||||
cmd_stop.Stdout = os.Stdout
|
||||
cmd_stop.Stderr = os.Stderr
|
||||
if err := cmd_stop.Run(); err != nil {
|
||||
fmt.Printf("Error stopping MariaDB: %v\n", err)
|
||||
}
|
||||
|
||||
}
|
||||
if err := removeSkipNetworking(filePath); err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
} else {
|
||||
_log.Log("note", "Successfully removed 'skip-networking' from the configuration file.")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
38
srcs/cmd/mariadb/healthcheck/healthcheck.go
Normal file
38
srcs/cmd/mariadb/healthcheck/healthcheck.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
|
||||
"git.keyzox.me/42_adjoly/inception/internal/env"
|
||||
)
|
||||
|
||||
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()
|
||||
if err != nil {
|
||||
fmt.Printf("Health check failed: %v\n", err)
|
||||
return false
|
||||
}
|
||||
fmt.Println("MariaDB is healthy")
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Configuration
|
||||
user := env.EscapeEnv(env.FileEnv("MYSQL_USER", "mariadb"))
|
||||
password := env.EscapeEnv(env.FileEnv("MYSQL_PASSWORD", "default"))
|
||||
host := "127.0.0.1"
|
||||
port := "3306"
|
||||
|
||||
if checkMariaDB(user, password, host, port) {
|
||||
os.Exit(0) // Success
|
||||
}
|
||||
|
||||
fmt.Println("MariaDB health check failed")
|
||||
os.Exit(1) // Failure
|
||||
}
|
9
srcs/cmd/wordpress/wordpress-entry.go
Normal file
9
srcs/cmd/wordpress/wordpress-entry.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
14
srcs/configs/mariadb/docker.cnf
Normal file
14
srcs/configs/mariadb/docker.cnf
Normal file
@ -0,0 +1,14 @@
|
||||
[mariadb]
|
||||
host-cache-size=0
|
||||
skip-name-resolve
|
||||
port=3306
|
||||
|
||||
|
||||
expire_logs_days=10
|
||||
|
||||
!includedir /etc/mysql/conf.d
|
||||
|
||||
[mysqld]
|
||||
port=3306
|
||||
socket=/run/mysqld/mysqld.sock
|
||||
bind-address=0.0.0.0
|
14
srcs/configs/nginx/wp.conf
Normal file
14
srcs/configs/nginx/wp.conf
Normal file
@ -0,0 +1,14 @@
|
||||
server {
|
||||
listen 80;
|
||||
|
||||
location / {
|
||||
root /usr/share/nginx/html;
|
||||
index index.html index.htm;
|
||||
}
|
||||
|
||||
location /health {
|
||||
access_log off;
|
||||
return 200 'healthy';
|
||||
add_header Content-Type text/plain;
|
||||
}
|
||||
}
|
62
srcs/docker-compose.yml
Normal file
62
srcs/docker-compose.yml
Normal file
@ -0,0 +1,62 @@
|
||||
name: inception
|
||||
|
||||
volumes:
|
||||
wp-db:
|
||||
wp-site:
|
||||
|
||||
networks:
|
||||
inception:
|
||||
external: false
|
||||
|
||||
services:
|
||||
nginx:
|
||||
container_name: inception-nginx
|
||||
build:
|
||||
context: .
|
||||
dockerfile: docker/nginx/dockerfile
|
||||
networks:
|
||||
- inception
|
||||
environment:
|
||||
- TZ=Europe/Paris
|
||||
depends_on:
|
||||
- wordpress-php
|
||||
- db
|
||||
volumes:
|
||||
- wp-site:/var/www/html
|
||||
ports:
|
||||
- "443:443"
|
||||
restart: unless-stopped
|
||||
|
||||
wordpress-php:
|
||||
container_name: inception-wordp-php
|
||||
build:
|
||||
context: .
|
||||
dockerfile: docker/wordpress/dockerfile
|
||||
networks:
|
||||
- inception
|
||||
environment:
|
||||
- PHP_MEMORY_LIMIT="512M"
|
||||
- PHP_MAX_UPLOAD="50M"
|
||||
- TZ=Europe/Paris
|
||||
volumes:
|
||||
- wp-site:/var/www/html
|
||||
depends_on:
|
||||
- db
|
||||
restart: unless-stopped
|
||||
|
||||
db:
|
||||
container_name: inception-db
|
||||
build:
|
||||
context: .
|
||||
dockerfile: docker/mariadb/Dockerfile
|
||||
networks:
|
||||
- inception
|
||||
environment:
|
||||
- MYSQL_ROOT_PASSWORD="alpine"
|
||||
- MYSQL_PASSWORD="alpine"
|
||||
- MYSQL_USER="kanel"
|
||||
- MYSQL_DATABASE="knl"
|
||||
- TZ=Europe/Paris
|
||||
volumes:
|
||||
- wp-db:/var/lib/mysql
|
||||
restart: unless-stopped
|
BIN
srcs/docker/alpine/alpine-minirootfs-3.21.2-x86_64.tar.gz
Normal file
BIN
srcs/docker/alpine/alpine-minirootfs-3.21.2-x86_64.tar.gz
Normal file
Binary file not shown.
42
srcs/docker/mariadb/Dockerfile
Normal file
42
srcs/docker/mariadb/Dockerfile
Normal file
@ -0,0 +1,42 @@
|
||||
FROM scratch
|
||||
ADD docker/alpine/alpine-minirootfs-3.21.2-x86_64.tar.gz /
|
||||
|
||||
LABEL version="0.1"
|
||||
LABEL maintainer="KeyZox"
|
||||
|
||||
RUN addgroup mysql \
|
||||
&& adduser -S -G mysql mysql -h /var/lib/mysql
|
||||
|
||||
RUN mkdir /build
|
||||
|
||||
COPY go.mod /build/go.mod
|
||||
COPY cmd /build/cmd
|
||||
COPY internal /build/internal
|
||||
|
||||
RUN apk add --no-cache go mariadb tzdata mariadb-client \
|
||||
&& cd /build \
|
||||
&& go build git.keyzox.me/42_adjoly/inception/cmd/mariadb/entrypoint \
|
||||
&& go build git.keyzox.me/42_adjoly/inception/cmd/mariadb/healthcheck \
|
||||
&& cp /build/entrypoint /docker-entrypoint \
|
||||
&& cp /build/healthcheck /docker-healthcheck \
|
||||
&& chmod +x /docker-healthcheck \
|
||||
&& chmod +x /docker-entrypoint \
|
||||
&& apk del go \
|
||||
&& mkdir -p /etc/mysql/conf.d /etc/mysql/mariadb.conf.d/ /run/mariadb /run/mysqld \
|
||||
&& chmod ugo+rwx,o+t /run/mariadb \
|
||||
&& chown -R mysql:mysql /var/lib/mysql /run/mariadb /run/mysqld \
|
||||
&& rm -Rf /build \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
ENV LANG=C.UTF-8
|
||||
|
||||
COPY --chmod=0644 configs/mariadb/docker.cnf /etc/my.cnf.d/mariadb-server.cnf.d
|
||||
|
||||
VOLUME /var/lib/mysql
|
||||
|
||||
ENTRYPOINT [ "/docker-entrypoint" ]
|
||||
WORKDIR /var/lib/mysql
|
||||
|
||||
EXPOSE 3306
|
||||
CMD [ "mariadbd", "--user=mysql" ]
|
||||
HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD /docker-healthcheck
|
24
srcs/docker/nginx/Dockerfile
Normal file
24
srcs/docker/nginx/Dockerfile
Normal file
@ -0,0 +1,24 @@
|
||||
FROM alpine:3.21
|
||||
|
||||
LABEL version="0.1"
|
||||
LABEL maintainer="KeyZox"
|
||||
|
||||
RUN set -x \
|
||||
&& addgroup -g 101 -S nginx\
|
||||
&& adduser -S -D -H -u 101 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx \
|
||||
&& apk add --no-cache nginx tzdata \
|
||||
&& ln -sf /dev/stdout /var/log/nginx/access.log \
|
||||
&& ln -sf /dev/stderr /var/log/nginx/error.log
|
||||
|
||||
VOLUME /etc/nginx
|
||||
RUN mkdir -p /etc/nginx/sites-available
|
||||
|
||||
COPY docker-healthcheck.sh /docker-healthcheck.sh
|
||||
RUN chmod +x /docker-healthcheck.sh
|
||||
|
||||
WORKDIR /etc/nginx
|
||||
STOPSIGNAL SIGQUIT
|
||||
|
||||
EXPOSE 80
|
||||
CMD [ "nginx", "-g", "daemon off;" ]
|
||||
HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD /docker-healthcheck.sh
|
3
srcs/docker/nginx/docker-healthcheck.sh
Normal file
3
srcs/docker/nginx/docker-healthcheck.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
curl -f http://localhost/health || exit 1
|
33
srcs/docker/nginx/nginx-health.conf
Normal file
33
srcs/docker/nginx/nginx-health.conf
Normal file
@ -0,0 +1,33 @@
|
||||
user nginx;
|
||||
worker_processes 1;
|
||||
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
sendfile on;
|
||||
access_log /var/log/nginx/access.log;
|
||||
keepalive_timeout 3000;
|
||||
server {
|
||||
listen 80;
|
||||
root /www;
|
||||
index index.php index.html index.htm;
|
||||
server_name localhost;
|
||||
client_max_body_size 32m;
|
||||
error_page 500 502 503 504 /50x.html;
|
||||
location = /50x.html {
|
||||
root /var/lib/nginx/html;
|
||||
}
|
||||
location ~ \.php$ {
|
||||
fastcgi_pass 127.0.0.1:9000;
|
||||
fastcgi_index index.php;
|
||||
include fastcgi.conf;
|
||||
}
|
||||
}
|
||||
}
|
25
srcs/docker/wordpress/Dockerfile
Normal file
25
srcs/docker/wordpress/Dockerfile
Normal file
@ -0,0 +1,25 @@
|
||||
FROM alpine:3.21
|
||||
|
||||
LABEL version="0.1"
|
||||
LABEL maintainer="KeyZox"
|
||||
|
||||
RUN set -x \
|
||||
&& adduser -u 82 -D -S -G www-data www-data \
|
||||
&& apk add --no-cache php84-fpm wget
|
||||
|
||||
VOLUME /var/www/html
|
||||
|
||||
WORKDIR /var/www/html
|
||||
RUN wget https://wordpress.org/wordpress-6.7.1.tar.gz \
|
||||
&& tar -xzvf wordpress-6.7.1.tar.gz \
|
||||
&& rm workpress-6.7.1.tar.gz
|
||||
|
||||
#COPY docker-entrypoint.sh /
|
||||
#RUN [ "chmod", "+x", "/docker-entrypoint.sh" ]
|
||||
|
||||
#ENTRYPOINT [ "/docker-entrypoint.sh" ]
|
||||
WORKDIR /var/www/html
|
||||
STOPSIGNAL SIGQUIT
|
||||
|
||||
EXPOSE 9000
|
||||
CMD [ "php-fpm84", "-F" ]
|
6
srcs/go.mod
Normal file
6
srcs/go.mod
Normal file
@ -0,0 +1,6 @@
|
||||
module git.keyzox.me/42_adjoly/inception
|
||||
|
||||
go 1.23.4
|
||||
|
||||
require (
|
||||
)
|
45
srcs/internal/env/env_util.go
vendored
Normal file
45
srcs/internal/env/env_util.go
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.keyzox.me/42_adjoly/inception/internal/log"
|
||||
)
|
||||
|
||||
func FileEnv(Value string, Default string) string {
|
||||
val, is := os.LookupEnv(Value)
|
||||
|
||||
if is {
|
||||
return val
|
||||
} else {
|
||||
val, is := os.LookupEnv(Value + "_FILE")
|
||||
if is {
|
||||
content, err := os.ReadFile(val)
|
||||
if err != nil {
|
||||
_log.Log("error", "Error reading file")
|
||||
}
|
||||
return string(content)
|
||||
}
|
||||
}
|
||||
return Default
|
||||
}
|
||||
|
||||
func EnvCheck(Value, Default string) string {
|
||||
val, is := os.LookupEnv(Value)
|
||||
|
||||
if is {
|
||||
return val
|
||||
}
|
||||
return Default
|
||||
}
|
||||
|
||||
func EscapeEnv(str string) string {
|
||||
if str[0] == '"' && str[len(str) - 1] == '"' {
|
||||
return strings.TrimPrefix(strings.TrimSuffix(str, "\""), "\"")
|
||||
} else if str[0] == '"' && str[len(str) - 1] == '"' {
|
||||
return strings.TrimPrefix(strings.TrimSuffix(str, "'"), "'")
|
||||
} else {
|
||||
return str
|
||||
}
|
||||
}
|
20
srcs/internal/log/log.go
Normal file
20
srcs/internal/log/log.go
Normal file
@ -0,0 +1,20 @@
|
||||
package _log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Log(Type string, Log string) {
|
||||
t := time.Now()
|
||||
t.Format(time.RFC3339)
|
||||
if Type == "note" {
|
||||
fmt.Printf("%s-[%s] [Entrypoint]: %s\n", t, Type, Log)
|
||||
} else if Type == "warn" {
|
||||
fmt.Fprintf(os.Stderr ,"%s-[%s] [Entrypoint]: %s\n", t, Type, Log)
|
||||
} else if Type == "error" {
|
||||
fmt.Fprintf(os.Stderr ,"%s-[%s] [Entrypoint]: %s\n", t, Type, Log)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user