diff --git a/templates/deployment.yaml b/templates/deployment.yaml index b23ae61..518e151 100644 --- a/templates/deployment.yaml +++ b/templates/deployment.yaml @@ -27,26 +27,135 @@ spec: set -e echo "=== WordPress Initialization Started ===" - echo "Preparing wp-content in persistent storage..." - if [ ! -d /var/www/html-persistent/wp-content ]; then + if [ ! -d /var/www/html/wp-content ]; then echo "Initializing wp-content directory..." - mkdir -p /var/www/html-persistent/wp-content - cp -rp /usr/src/wordpress/wp-content/* /var/www/html-persistent/wp-content/ - echo "wp-content initialized in persistent storage" + mkdir -p /var/www/html/wp-content + cp -rp /usr/src/wordpress/wp-content/* /var/www/html/wp-content/ + echo "wp-content initialized" else - echo "wp-content already exists in persistent storage, preserving user data" + echo "wp-content already exists, preserving user data" fi - echo "Copying WordPress core files to ephemeral storage (excluding wp-content)..." - cd /usr/src/wordpress - find . -maxdepth 1 ! -name wp-content ! -name . -exec cp -rp {} /var/www/html/ \; + echo "Generating wp-config.php..." + cat > /var/www/html/wp-config.php << 'EOF' + /dev/null || true + chmod +x /tmp/wp-cli.phar + + echo "Waiting for database connection..." + max_attempts=30 + attempt=0 + while [ $attempt -lt $max_attempts ]; do + if /tmp/wp-cli.phar --path=/usr/src/wordpress db check 2>/dev/null; then + echo "Database connection successful" + break + fi + attempt=$((attempt + 1)) + echo "Waiting for database... ($attempt/$max_attempts)" + sleep 2 + done + + echo "Checking if WordPress is already installed..." + TABLES=$(/tmp/wp-cli.phar --path=/usr/src/wordpress db query "SHOW TABLES LIKE '${WORDPRESS_TABLE_PREFIX}options';" 2>/dev/null || echo "") + + if [ -z "$TABLES" ]; then + echo "=== Starting WordPress Installation ===" + + if [ -z "$WP_ADMIN_PASSWORD" ]; then + WP_ADMIN_PASSWORD=$(tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 16) + echo "Generated admin password: $WP_ADMIN_PASSWORD" + echo "$WP_ADMIN_PASSWORD" > /var/www/html/.initial-admin-password + fi + + INSTALL_URL="http://127.0.0.1" + + echo "Installing WordPress..." + /tmp/wp-cli.phar --path=/usr/src/wordpress core install \ + --url="$INSTALL_URL" \ + --title="$WP_SITE_TITLE" \ + --admin_user="$WP_ADMIN_USER" \ + --admin_password="$WP_ADMIN_PASSWORD" \ + --admin_email="$WP_ADMIN_EMAIL" \ + --skip-email + + echo "=== WordPress Installation Completed ===" + echo "Admin User: $WP_ADMIN_USER" + if [ -f /var/www/html/.initial-admin-password ]; then + echo "Admin Password: $(cat /var/www/html/.initial-admin-password)" + fi + echo "Note: Site URL is dynamic and will be set based on HTTP_HOST" + else + echo "WordPress is already installed, skipping installation" + + if /tmp/wp-cli.phar --path=/usr/src/wordpress core version 2>/dev/null; then + CURRENT_VERSION=$(cat /usr/src/wordpress/wp-includes/version.php | grep "wp_version = " | cut -d "'" -f 2) + echo "Checking for WordPress updates... Target version: $CURRENT_VERSION" + /tmp/wp-cli.phar --path=/usr/src/wordpress core update --version=$CURRENT_VERSION 2>/dev/null || true + /tmp/wp-cli.phar --path=/usr/src/wordpress core update-db 2>/dev/null || true + fi + fi + + {{- if .Values.wordpress.adsTxt.enabled }} + echo "Deploying ads.txt..." + cat > /var/www/html/ads.txt << 'ADSTXT' + {{ .Values.wordpress.adsTxt.content }} + ADSTXT + echo "ads.txt deployed" + {{- end }} + + echo "=== WordPress Initialization Completed ===" + volumeMounts: + - name: wordpress-persistent + mountPath: /var/www/html echo "Generating wp-config.php from Secret..." cat > /var/www/html/wp-config.php << 'EOF' @@ -247,8 +356,15 @@ spec: containerPort: 80 protocol: TCP volumeMounts: - - name: wordpress-core + - name: wordpress-source mountPath: /var/www/html + subPath: wordpress + readOnly: true + - name: wordpress-persistent + mountPath: /var/www/html/wp-content + - name: wordpress-persistent + mountPath: /var/www/html/wp-config.php + subPath: wp-config.php - name: nginx-config mountPath: /etc/nginx/conf.d/default.conf subPath: default.conf @@ -282,14 +398,15 @@ spec: - name: WORDPRESS_TABLE_PREFIX value: {{ .Values.wordpress.tablePrefix | quote }} volumeMounts: - - name: wordpress-core - mountPath: /var/www/html - name: wordpress-persistent - mountPath: /var/www/html-persistent + mountPath: /var/www/html/wp-content + - name: wordpress-persistent + mountPath: /var/www/html/wp-config.php + subPath: wp-config.php resources: {{- toYaml .Values.resources.wordpress | nindent 12 }} volumes: - - name: wordpress-core + - name: wordpress-source emptyDir: {} - name: wordpress-persistent {{- if .Values.persistence.enabled }}