-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·189 lines (159 loc) · 5.55 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·189 lines (159 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# Dimensigon Docker Entrypoint Script
# Handles initialization, SSL certificates, and flexible command execution
set -e
# Color output for better visibility
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Container-native environment variables
export DM_DISCOVERY_DNS="${DM_DISCOVERY_DNS:-}"
export DM_SECRET_KEY="${DM_SECRET_KEY:-}"
export DM_DATABASE_URL="${DM_DATABASE_URL:-}"
export DM_NODE_NAME="${DM_NODE_NAME:-$(hostname)}"
export DM_AUTO_JOIN="${DM_AUTO_JOIN:-true}"
# Configuration
CONFIG_DIR="${CONFIG_DIR:-/app/.dimensigon}"
SSL_DIR="${CONFIG_DIR}/.ssl"
CERT_FILE="${SSL_DIR}/cert.pem"
KEY_FILE="${SSL_DIR}/key.pem"
log_info "Starting Dimensigon 2.0..."
log_info "Configuration directory: ${CONFIG_DIR}"
# Ensure directories exist
mkdir -p "${SSL_DIR}" /app/data /var/log/dimensigon
# Check for SSL certificates
if [ ! -f "${CERT_FILE}" ] || [ ! -f "${KEY_FILE}" ]; then
log_warn "SSL certificates not found. Generating self-signed certificate..."
# Generate self-signed certificate
openssl req -x509 -nodes -days 3650 -newkey rsa:4096 \
-keyout "${KEY_FILE}" \
-out "${CERT_FILE}" \
-subj "/C=US/ST=State/L=City/O=Dimensigon/OU=IT/CN=dimensigon.local" \
2>/dev/null
chmod 600 "${KEY_FILE}"
chmod 644 "${CERT_FILE}"
log_info "Self-signed certificate generated successfully"
else
log_info "SSL certificates found"
fi
# Validate SSL certificates
if openssl x509 -checkend 86400 -noout -in "${CERT_FILE}" >/dev/null 2>&1; then
log_info "SSL certificate is valid"
else
log_warn "SSL certificate expires within 24 hours or is invalid"
fi
# Set environment defaults if not provided
export FLASK_CONFIG="${FLASK_CONFIG:-production}"
export FLASK_APP="${FLASK_APP:-dimensigon.web}"
export DM_PORT="${DM_PORT:-20194}"
export WORKERS="${WORKERS:-4}"
export GUNICORN_TIMEOUT="${GUNICORN_TIMEOUT:-120}"
log_info "Environment configuration:"
log_info " - FLASK_CONFIG: ${FLASK_CONFIG}"
log_info " - DM_PORT: ${DM_PORT}"
log_info " - WORKERS: ${WORKERS}"
log_info " - Database: ${SQLALCHEMY_DATABASE_URI%%@*}@***"
# Wait for database to be ready (if PostgreSQL)
if [[ "${SQLALCHEMY_DATABASE_URI}" == postgresql* ]]; then
log_info "Waiting for PostgreSQL database..."
# Extract database host and port from connection string
DB_HOST=$(echo "${SQLALCHEMY_DATABASE_URI}" | sed -n 's/.*@\([^:]*\).*/\1/p')
DB_PORT=$(echo "${SQLALCHEMY_DATABASE_URI}" | sed -n 's/.*:\([0-9]*\)\/.*/\1/p')
DB_PORT="${DB_PORT:-5432}"
max_attempts=30
attempt=0
while [ $attempt -lt $max_attempts ]; do
if nc -z "${DB_HOST}" "${DB_PORT}" 2>/dev/null; then
log_info "Database is ready!"
break
fi
attempt=$((attempt + 1))
log_warn "Database not ready yet (attempt ${attempt}/${max_attempts}). Waiting..."
sleep 2
done
if [ $attempt -eq $max_attempts ]; then
log_error "Database connection timeout!"
exit 1
fi
fi
# DNS-based peer discovery for container orchestration
if [ -n "${DM_DISCOVERY_DNS}" ]; then
log_info "Performing DNS discovery for: ${DM_DISCOVERY_DNS}"
PEER_IPS=$(getent hosts "${DM_DISCOVERY_DNS}" 2>/dev/null | awk '{print $1}' || true)
if [ -n "${PEER_IPS}" ]; then
log_info "Discovered peers via DNS:"
for ip in ${PEER_IPS}; do
log_info " - ${ip}"
done
export DM_DISCOVERED_PEERS="${PEER_IPS}"
else
log_warn "No peers discovered via DNS for ${DM_DISCOVERY_DNS}"
fi
fi
# Initialize dimension if needed (check environment variable)
if [ "${DIMENSIGON_INIT_NEW}" = "true" ] && [ -n "${DIMENSION_NAME}" ]; then
log_info "Initializing new dimension: ${DIMENSION_NAME}"
dimensigon new "${DIMENSION_NAME}"
fi
# Join dimension if token provided
if [ "${DIMENSIGON_JOIN}" = "true" ] && [ -n "${JOIN_SERVER}" ] && [ -n "${JOIN_TOKEN}" ]; then
log_info "Joining dimension at ${JOIN_SERVER}"
dimensigon join "${JOIN_SERVER}" "${JOIN_TOKEN}" \
--port "${JOIN_PORT:-20194}" \
${JOIN_SSL:+--ssl} \
${JOIN_VERIFY:+--verify}
fi
# Handle special commands
case "${1}" in
bash|sh)
log_info "Starting interactive shell"
exec "$@"
;;
dimensigon)
log_info "Executing Dimensigon command: ${*}"
exec "$@"
;;
gunicorn)
log_info "Starting Gunicorn with ${WORKERS} workers"
exec "$@"
;;
test)
log_info "Running tests"
exec pytest tests/
;;
*)
# Default: Start gunicorn if no command provided
if [ $# -eq 0 ]; then
log_info "Starting Dimensigon with Gunicorn (${WORKERS} workers)"
exec gunicorn \
--bind "0.0.0.0:${DM_PORT}" \
--workers "${WORKERS}" \
--threads 2 \
--worker-class sync \
--worker-tmp-dir /dev/shm \
--timeout "${GUNICORN_TIMEOUT}" \
--keepalive 5 \
--max-requests 1000 \
--max-requests-jitter 100 \
--access-logfile - \
--error-logfile - \
--log-level "${LOG_LEVEL:-info}" \
--capture-output \
--enable-stdio-inheritance \
"dimensigon.web:create_app('${FLASK_CONFIG}')"
else
log_info "Executing command: $*"
exec "$@"
fi
;;
esac