templates/deployment.yaml を更新

This commit is contained in:
2025-11-22 13:43:43 +00:00
parent 2ba161fb5b
commit c1a1dc04d5

View File

@@ -21,20 +21,15 @@ spec:
- name: wordpress-init - name: wordpress-init
image: "{{ .Values.image.wordpress.registry }}/{{ .Values.image.wordpress.repository }}:{{ .Values.image.wordpress.tag }}" image: "{{ .Values.image.wordpress.registry }}/{{ .Values.image.wordpress.repository }}:{{ .Values.image.wordpress.tag }}"
imagePullPolicy: {{ .Values.image.wordpress.pullPolicy }} imagePullPolicy: {{ .Values.image.wordpress.pullPolicy }}
command: ["/bin/sh"] command: ["/bin/sh", "-c"]
args: args:
- -c
- | - |
#!/bin/sh
set -e set -e
echo "=== WordPress Initialization Started ===" echo "=== WordPress Initialization Started ==="
# WordPress本体ファイルをemptyDirにコピー使い捨て
echo "Copying WordPress core files to ephemeral storage..." echo "Copying WordPress core files to ephemeral storage..."
cp -rp /usr/src/wordpress/* /var/www/html/ cp -rp /usr/src/wordpress/* /var/www/html/
# wp-content ディレクトリをPVCに配置永続化
if [ ! -d /var/www/html-persistent/wp-content ]; then if [ ! -d /var/www/html-persistent/wp-content ]; then
echo "Initializing wp-content directory..." echo "Initializing wp-content directory..."
mkdir -p /var/www/html-persistent/wp-content mkdir -p /var/www/html-persistent/wp-content
@@ -43,90 +38,64 @@ spec:
echo "wp-content already exists, preserving user data" echo "wp-content already exists, preserving user data"
fi fi
# wp-content をシンボリックリンクで接続
rm -rf /var/www/html/wp-content rm -rf /var/www/html/wp-content
ln -sf /var/www/html-persistent/wp-content /var/www/html/wp-content ln -sf /var/www/html-persistent/wp-content /var/www/html/wp-content
# wp-config.php を Secretから生成毎回再作成
echo "Generating wp-config.php from Secret..." echo "Generating wp-config.php from Secret..."
cat > /var/www/html/wp-config.php <<'WPCONFIG' cat > /var/www/html/wp-config.php << 'EOF'
<?php <?php
/** define('DB_NAME', getenv('WORDPRESS_DB_NAME'));
* WordPress Configuration File define('DB_USER', getenv('WORDPRESS_DB_USER'));
* Generated by Helm Chart define('DB_PASSWORD', getenv('WORDPRESS_DB_PASSWORD'));
*/ define('DB_HOST', getenv('WORDPRESS_DB_HOST'));
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
// ** Database settings ** // $table_prefix = getenv('WORDPRESS_TABLE_PREFIX') ?: 'wp_';
define('DB_NAME', getenv('WORDPRESS_DB_NAME'));
define('DB_USER', getenv('WORDPRESS_DB_USER'));
define('DB_PASSWORD', getenv('WORDPRESS_DB_PASSWORD'));
define('DB_HOST', getenv('WORDPRESS_DB_HOST'));
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
// ** Table prefix ** // define('AUTH_KEY', getenv('WP_AUTH_KEY'));
$table_prefix = getenv('WORDPRESS_TABLE_PREFIX') ?: 'wp_'; define('SECURE_AUTH_KEY', getenv('WP_SECURE_AUTH_KEY'));
define('LOGGED_IN_KEY', getenv('WP_LOGGED_IN_KEY'));
define('NONCE_KEY', getenv('WP_NONCE_KEY'));
define('AUTH_SALT', getenv('WP_AUTH_SALT'));
define('SECURE_AUTH_SALT', getenv('WP_SECURE_AUTH_SALT'));
define('LOGGED_IN_SALT', getenv('WP_LOGGED_IN_SALT'));
define('NONCE_SALT', getenv('WP_NONCE_SALT'));
// ** Authentication Unique Keys and Salts ** // if ( defined( 'WP_CLI' ) ) {
define('AUTH_KEY', getenv('WP_AUTH_KEY'));
define('SECURE_AUTH_KEY', getenv('WP_SECURE_AUTH_KEY'));
define('LOGGED_IN_KEY', getenv('WP_LOGGED_IN_KEY'));
define('NONCE_KEY', getenv('WP_NONCE_KEY'));
define('AUTH_SALT', getenv('WP_AUTH_SALT'));
define('SECURE_AUTH_SALT', getenv('WP_SECURE_AUTH_SALT'));
define('LOGGED_IN_SALT', getenv('WP_LOGGED_IN_SALT'));
define('NONCE_SALT', getenv('WP_NONCE_SALT'));
// ** WordPress Site URLs - Dynamic based on HTTP_HOST ** //
if ( defined( 'WP_CLI' ) ) {
$_SERVER['HTTP_HOST'] = '127.0.0.1'; $_SERVER['HTTP_HOST'] = '127.0.0.1';
} }
// Determine protocol (HTTP or HTTPS) $protocol = 'http';
$protocol = 'http'; if ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
if ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
$protocol = 'https'; $protocol = 'https';
} elseif ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ) { } elseif ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ) {
$protocol = 'https'; $protocol = 'https';
} }
define( 'WP_HOME', $protocol . '://' . $_SERVER['HTTP_HOST'] . '/' ); define( 'WP_HOME', $protocol . '://' . $_SERVER['HTTP_HOST'] . '/' );
define( 'WP_SITEURL', $protocol . '://' . $_SERVER['HTTP_HOST'] . '/' ); define( 'WP_SITEURL', $protocol . '://' . $_SERVER['HTTP_HOST'] . '/' );
define( 'WP_AUTO_UPDATE_CORE', false );
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false);
define('FS_METHOD', 'direct');
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');
// ** WordPress Core Updates ** // if ( ! defined( 'ABSPATH' ) ) {
define( 'WP_AUTO_UPDATE_CORE', false );
// ** WordPress Debug Mode ** //
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false);
// ** File System Method ** //
define('FS_METHOD', 'direct');
// ** Memory Limits ** //
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');
/* That's all, stop editing! Happy publishing. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' ); define( 'ABSPATH', __DIR__ . '/' );
} }
require_once ABSPATH . 'wp-settings.php'; require_once ABSPATH . 'wp-settings.php';
WPCONFIG EOF
echo "wp-config.php generated" echo "wp-config.php generated"
echo "wp-config.php generated"
# WordPress CLI のセットアップ
echo "Setting up WP-CLI..." echo "Setting up WP-CLI..."
curl -o /tmp/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar 2>/dev/null || true curl -o /tmp/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar 2>/dev/null || true
chmod +x /tmp/wp-cli.phar chmod +x /tmp/wp-cli.phar
# データベース接続待機
echo "Waiting for database connection..." echo "Waiting for database connection..."
max_attempts=30 max_attempts=30
attempt=0 attempt=0
@@ -140,24 +109,20 @@ WPCONFIG
sleep 2 sleep 2
done done
# データベーステーブルの確認
echo "Checking if WordPress is already installed..." echo "Checking if WordPress is already installed..."
TABLES=$(/tmp/wp-cli.phar --path=/var/www/html db query "SHOW TABLES LIKE '${WORDPRESS_TABLE_PREFIX}options';" 2>/dev/null || echo "") TABLES=$(/tmp/wp-cli.phar --path=/var/www/html db query "SHOW TABLES LIKE '${WORDPRESS_TABLE_PREFIX}options';" 2>/dev/null || echo "")
if [ -z "$TABLES" ]; then if [ -z "$TABLES" ]; then
echo "=== Starting WordPress Installation ===" echo "=== Starting WordPress Installation ==="
# 管理者パスワードの処理
if [ -z "$WP_ADMIN_PASSWORD" ]; then if [ -z "$WP_ADMIN_PASSWORD" ]; then
WP_ADMIN_PASSWORD=$(tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 16) WP_ADMIN_PASSWORD=$(tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 16)
echo "Generated admin password: $WP_ADMIN_PASSWORD" echo "Generated admin password: $WP_ADMIN_PASSWORD"
echo "$WP_ADMIN_PASSWORD" > /var/www/html-persistent/.initial-admin-password echo "$WP_ADMIN_PASSWORD" > /var/www/html-persistent/.initial-admin-password
fi fi
# 初回インストール用の一時的なURL後で動的に変更される
INSTALL_URL="http://127.0.0.1" INSTALL_URL="http://127.0.0.1"
# WordPressのインストール
echo "Installing WordPress..." echo "Installing WordPress..."
/tmp/wp-cli.phar --path=/var/www/html core install \ /tmp/wp-cli.phar --path=/var/www/html core install \
--url="$INSTALL_URL" \ --url="$INSTALL_URL" \
@@ -176,7 +141,6 @@ WPCONFIG
else else
echo "WordPress is already installed, skipping installation" echo "WordPress is already installed, skipping installation"
# コアアップデートの確認
if /tmp/wp-cli.phar --path=/var/www/html core version 2>/dev/null; then if /tmp/wp-cli.phar --path=/var/www/html core version 2>/dev/null; then
CURRENT_VERSION=$(cat /usr/src/wordpress/wp-includes/version.php | grep "wp_version = " | cut -d "'" -f 2) 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" echo "Checking for WordPress updates... Target version: $CURRENT_VERSION"
@@ -187,7 +151,7 @@ WPCONFIG
{{- if .Values.wordpress.adsTxt.enabled }} {{- if .Values.wordpress.adsTxt.enabled }}
echo "Deploying ads.txt..." echo "Deploying ads.txt..."
cat > /var/www/html/ads.txt <<'ADSTXT' cat > /var/www/html/ads.txt << 'ADSTXT'
{{ .Values.wordpress.adsTxt.content }} {{ .Values.wordpress.adsTxt.content }}
ADSTXT ADSTXT
echo "ads.txt deployed" echo "ads.txt deployed"