Skip to content

Commit e3bd851

Browse files
committed
feat(bin): add container management scripts
Added new utility scripts to compose/bin: - bash: Launch an interactive bash session inside the container - cli: Execute CLI commands inside the php-fpm container - clinotty: Execute CLI commands inside php-fpm without allocating a TTY - docker-compose: Wrapper for managing docker-compose commands - restart: Restart all running containers - setup-ssl: Generate and install SSL certificates - setup-ssl-ca: Configure a Certificate Authority (CA) for local SSL - start: Start all containers in detached mode - stop: Stop all running containers These scripts improve usability and streamline container operations.
1 parent 19375e5 commit e3bd851

File tree

9 files changed

+269
-0
lines changed

9 files changed

+269
-0
lines changed

compose/bin/bash

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Check if `bin/cli` exists
4+
if [ ! -f "bin/cli" ]; then
5+
echo "Error: 'bin/cli' not found."
6+
echo "Please ensure the file exists in the 'bin/' directory."
7+
exit 1
8+
fi
9+
10+
# Check if `bin/cli` is executable
11+
if [ ! -x "bin/cli" ]; then
12+
echo "Warning: 'bin/cli' is not executable."
13+
echo "Attempting to grant execute permissions..."
14+
15+
# Try to fix permissions
16+
chmod +x bin/cli 2>/dev/null
17+
18+
# Re-check if the permission fix was successful
19+
if [ ! -x "bin/cli" ]; then
20+
echo "Error: Failed to grant execute permissions."
21+
echo "Please manually run the following command:"
22+
echo " chmod +x bin/cli"
23+
exit 1
24+
fi
25+
echo "Permissions fixed. Proceeding..."
26+
fi
27+
28+
# Execute the CLI Bash command
29+
echo "Starting CLI Bash session..."
30+
bin/cli bash

compose/bin/cli

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# Ensure a CLI command is provided as an argument
4+
if [ -z "$1" ]; then
5+
echo "Error: No CLI command specified."
6+
echo "Usage: bin/cli <command>"
7+
echo "Example: bin/cli ls"
8+
exit 1
9+
fi
10+
11+
# Execute the command inside the php-fpm container
12+
echo "Executing command inside php-fpm container: $@"
13+
bin/docker-compose exec php-fpm "$@"

compose/bin/clinotty

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# Ensure a CLI command is provided as an argument
4+
if [ -z "$1" ]; then
5+
echo "Error: No CLI command specified."
6+
echo "Usage: bin/clinotty <command>"
7+
echo "Example: bin/clinotty ls"
8+
exit 1
9+
fi
10+
11+
# Execute the command inside the php-fpm container
12+
echo "Executing command inside php-fpm container: $@"
13+
bin/docker-compose exec php-fpm "$@"

compose/bin/docker-compose

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
# Check if `docker compose` is available; otherwise, fallback to `docker-compose`.
4+
# This ensures compatibility with different Docker versions.
5+
if docker compose version > /dev/null 2>&1; then
6+
DOCKER_COMPOSE="docker compose"
7+
else
8+
DOCKER_COMPOSE="docker-compose"
9+
fi
10+
11+
# Define the compose file(s) to use.
12+
# This script supports multiple compose files if needed in the future.
13+
COMPOSE_FILES=("compose.yaml")
14+
15+
# Prefix each compose file with `-f` to ensure correct command syntax.
16+
# Using an array prevents issues with file paths that contain spaces.
17+
COMPOSE_ARGS=()
18+
for file in "${COMPOSE_FILES[@]}"; do
19+
COMPOSE_ARGS+=("-f" "$file")
20+
done
21+
22+
# Execute the Docker Compose command with the specified files and additional arguments.
23+
${DOCKER_COMPOSE} "${COMPOSE_ARGS[@]}" "$@"

compose/bin/restart

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
# Ensure that `bin/start` and `bin/stop` exist and are executable
4+
if [ ! -x "bin/stop" ] || [ ! -x "bin/start" ]; then
5+
echo "Error: Required scripts 'bin/stop' or 'bin/start' are missing or not executable."
6+
echo "Please ensure they exist and have execute permissions:"
7+
echo " chmod +x bin/stop bin/start"
8+
exit 1
9+
fi
10+
11+
# Display restart message
12+
echo "Restarting services..."
13+
14+
# Stop services
15+
bin/stop "$@"
16+
17+
# Start services
18+
bin/start "$@"
19+
20+
echo "Services restarted successfully."

compose/bin/setup-ssl

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
# Ensure a domain is provided as an argument
4+
if [ -z "$1" ]; then
5+
echo "Error: Please specify a domain (e.g., mydomain.test)"
6+
exit 1
7+
fi
8+
9+
# Check if the Certificate Authority (CA) is already set up, if not, set it up
10+
if ! bin/docker-compose exec -T -u root nginx cat /root/.local/share/mkcert/rootCA.pem | grep -q 'BEGIN CERTIFICATE'; then
11+
echo "Setting up Certificate Authority (CA)..."
12+
bin/setup-ssl-ca
13+
fi
14+
15+
# Initialize an empty array to store domain names without port numbers
16+
DOMAINS_CLEAN=()
17+
18+
# Process each provided domain, stripping out any port numbers
19+
for DOMAIN in "$@"; do
20+
CLEAN_DOMAIN=$(echo "$DOMAIN" | cut -d ':' -f1)
21+
DOMAINS_CLEAN+=("$CLEAN_DOMAIN")
22+
done
23+
24+
# Generate SSL certificate for the provided domains
25+
echo "Generating SSL certificates for: ${DOMAINS_CLEAN[*]}..."
26+
bin/docker-compose exec -T -u root nginx mkcert -key-file nginx.key -cert-file nginx.crt "${DOMAINS_CLEAN[@]}"
27+
28+
# Move generated certificates to the appropriate location
29+
echo "Moving SSL certificate and key to /etc/nginx/certs/..."
30+
bin/docker-compose exec -T -u root nginx chown app:app nginx.key nginx.crt
31+
bin/docker-compose exec -T -u root nginx mv nginx.key nginx.crt /etc/nginx/certs/
32+
33+
# Restart nginx to apply SSL changes
34+
echo "Restarting Nginx and related services to apply SSL changes..."
35+
bin/restart
36+
37+
echo "SSL setup completed successfully."

compose/bin/setup-ssl-ca

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
# Exit script on error
3+
set -o errexit
4+
5+
# Generate a new local CA inside the Nginx container
6+
echo "Generating a new local Certificate Authority (CA)..."
7+
bin/docker-compose exec -T -u root nginx mkcert -install
8+
9+
# Copy the generated CA certificate to the host
10+
CONTAINER_ID=$(bin/docker-compose ps -q nginx | awk '{print $1}')
11+
docker cp "$CONTAINER_ID":/root/.local/share/mkcert/rootCA.pem .
12+
13+
echo "System password required to install the CA certificate on the host..."
14+
15+
# Determine the operating system and install CA accordingly
16+
if [ "$(uname)" == "Darwin" ]; then
17+
echo "Installing CA certificate on macOS..."
18+
19+
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain rootCA.pem
20+
21+
# Check if Firefox is installed
22+
FIREFOX_BIN="/Applications/Firefox.app/Contents/MacOS/firefox-bin"
23+
if [ -f "$FIREFOX_BIN" ]; then
24+
echo "Configuring Firefox to trust the CA certificate..."
25+
26+
# Create Firefox policy for trusting enterprise certificates
27+
echo "{\"policies\": {\"Certificates\": {\"ImportEnterpriseRoots\": true}}}" | sudo tee policies.json
28+
29+
# Ensure the distribution directory exists
30+
FIREFOX_DIST_DIR="/Applications/Firefox.app/Contents/Resources/distribution"
31+
[ ! -d "$FIREFOX_DIST_DIR" ] && sudo mkdir -p "$FIREFOX_DIST_DIR"
32+
33+
# Move policy file to Firefox distribution directory
34+
sudo mv policies.json "$FIREFOX_DIST_DIR/policies.json"
35+
36+
# Ensure the Mozilla Certificates directory exists
37+
MOZILLA_CERT_DIR="/Library/Application Support/Mozilla/Certificates"
38+
[ ! -d "$MOZILLA_CERT_DIR" ] && sudo mkdir -p "$MOZILLA_CERT_DIR"
39+
40+
# Move the CA certificate to the Mozilla Certificates directory
41+
sudo mv rootCA.pem "$MOZILLA_CERT_DIR/rootCA.pem"
42+
else
43+
sudo rm rootCA.pem
44+
fi
45+
46+
else
47+
echo "Installing CA certificate on Linux..."
48+
49+
# Ensure required package `libnss3-tools` is installed
50+
REQUIRED_PKG="libnss3-tools"
51+
if ! dpkg-query -W --showformat='${Status}\n' "$REQUIRED_PKG" | grep -q "install ok installed"; then
52+
echo "Installing required package: $REQUIRED_PKG..."
53+
sudo apt-get --yes install "$REQUIRED_PKG"
54+
fi
55+
56+
# Define certificate variables
57+
CERT_FILE="rootCA.pem"
58+
CERT_NAME="Root CA"
59+
60+
# Install certificate for legacy cert8 (DBM format)
61+
echo "Configuring legacy certificate database (cert8.db)..."
62+
find ~/ -name "cert8.db" -print0 | while IFS= read -r -d '' CERT_DB; do
63+
CERT_DIR=$(dirname "$CERT_DB")
64+
certutil -D -n "$CERT_NAME" -i "$CERT_FILE" -d dbm:"$CERT_DIR"
65+
certutil -A -n "$CERT_NAME" -t "TCu,Cu,Tu" -i "$CERT_FILE" -d dbm:"$CERT_DIR"
66+
done
67+
68+
# Install certificate for modern cert9 (SQL format)
69+
echo "Configuring modern certificate database (cert9.db)..."
70+
find ~/ -name "cert9.db" -print0 | while IFS= read -r -d '' CERT_DB; do
71+
CERT_DIR=$(dirname "$CERT_DB")
72+
certutil -D -n "$CERT_NAME" -i "$CERT_FILE" -d sql:"$CERT_DIR"
73+
certutil -A -n "$CERT_NAME" -t "TCu,Cu,Tu" -i "$CERT_FILE" -d sql:"$CERT_DIR"
74+
done
75+
76+
# Move CA certificate to system-wide directory and update certificates
77+
sudo mv rootCA.pem /usr/local/share/ca-certificates/rootCA.crt
78+
sudo update-ca-certificates
79+
fi
80+
81+
echo "SSL CA setup completed successfully."

compose/bin/start

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Ensure that Docker and Docker Compose are available before starting
4+
if ! command -v docker &> /dev/null; then
5+
echo "Error: Docker is not installed or not in the system's PATH."
6+
echo "Please install Docker and try again."
7+
exit 1
8+
fi
9+
10+
if ! command -v bin/docker-compose &> /dev/null; then
11+
echo "Error: 'docker-compose' not found in 'bin/'."
12+
echo "Please ensure Docker Compose is installed and available."
13+
exit 1
14+
fi
15+
16+
# Start the Docker containers
17+
echo "Starting Docker services..."
18+
bin/docker-compose up -d --remove-orphans "$@"
19+
20+
# Check if the containers started successfully
21+
if [ $? -eq 0 ]; then
22+
echo "Services started successfully."
23+
else
24+
echo "Error: Failed to start services. Check logs for details."
25+
exit 1
26+
fi

compose/bin/stop

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Ensure that Docker and Docker Compose are available before stopping services
4+
if ! command -v docker &> /dev/null; then
5+
echo "Error: Docker is not installed or not in the system's PATH."
6+
echo "Please install Docker and try again."
7+
exit 1
8+
fi
9+
10+
if ! command -v bin/docker-compose &> /dev/null; then
11+
echo "Error: 'docker-compose' not found in 'bin/'."
12+
echo "Please ensure Docker Compose is installed and available."
13+
exit 1
14+
fi
15+
16+
# Stop the Docker containers
17+
echo "Stopping Docker services..."
18+
bin/docker-compose stop "$@"
19+
20+
# Check if the containers stopped successfully
21+
if [ $? -eq 0 ]; then
22+
echo "Services stopped successfully."
23+
else
24+
echo "Error: Failed to stop services. Check logs for details."
25+
exit 1
26+
fi

0 commit comments

Comments
 (0)