init.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash
  2. set -e
  3. # Create namespace
  4. kubectl create ns aiproxy-system || true
  5. # Function to wait for secret
  6. wait_for_secret() {
  7. local secret_name=$1
  8. local retries=0
  9. while ! kubectl get secret -n aiproxy-system ${secret_name} >/dev/null 2>&1; do
  10. sleep 3
  11. retries=$((retries + 1))
  12. if [ $retries -ge 30 ]; then
  13. echo "Timeout waiting for secret ${secret_name}"
  14. exit 1
  15. fi
  16. done
  17. }
  18. # Function to get secret value
  19. get_secret_value() {
  20. local secret_name=$1
  21. local key=$2
  22. base64_value=$(kubectl get secret -n aiproxy-system ${secret_name} -o jsonpath="{.data.${key}}") || return $?
  23. echo "$base64_value" | base64 -d
  24. }
  25. # Function to build postgres connection string
  26. build_postgres_dsn() {
  27. local secret_name=$1
  28. username=$(get_secret_value ${secret_name} "username") || return $?
  29. password=$(get_secret_value ${secret_name} "password") || return $?
  30. host=$(get_secret_value ${secret_name} "host") || return $?
  31. port=$(get_secret_value ${secret_name} "port") || return $?
  32. echo "postgres://${username}:${password}@${host}:${port}/postgres?sslmode=disable"
  33. }
  34. build_redis_conn() {
  35. local secret_name=$1
  36. username=$(get_secret_value ${secret_name} "username") || return $?
  37. password=$(get_secret_value ${secret_name} "password") || return $?
  38. host=$(get_secret_value ${secret_name} "host") || return $?
  39. port=$(get_secret_value ${secret_name} "port") || return $?
  40. echo "redis://${username}:${password}@${host}:${port}"
  41. }
  42. # Handle JWT configuration
  43. if grep "<sealos-jwt-key-placeholder>" manifests/aiproxy-config.yaml >/dev/null 2>&1; then
  44. JWT_SECRET=$(kubectl get cm -n account-system account-manager-env -o jsonpath="{.data.ACCOUNT_API_JWT_SECRET}") || exit $?
  45. sed -i "s|<sealos-jwt-key-placeholder>|${JWT_SECRET}|g" manifests/aiproxy-config.yaml
  46. fi
  47. # Handle PostgreSQL configuration
  48. if grep "<sql-placeholder>" manifests/aiproxy-config.yaml >/dev/null 2>&1; then
  49. if grep "<sql-log-placeholder>" manifests/aiproxy-config.yaml >/dev/null 2>&1; then
  50. # Deploy PostgreSQL resources
  51. kubectl apply -f manifests/pgsql.yaml -n aiproxy-system
  52. kubectl apply -f manifests/pgsql-log.yaml -n aiproxy-system
  53. # Wait for secrets
  54. wait_for_secret "aiproxy-conn-credential"
  55. wait_for_secret "aiproxy-log-conn-credential"
  56. # Build connection strings
  57. SQL_DSN=$(build_postgres_dsn "aiproxy-conn-credential") || exit $?
  58. LOG_SQL_DSN=$(build_postgres_dsn "aiproxy-log-conn-credential") || exit $?
  59. # Update config
  60. sed -i "s|<sql-placeholder>|${SQL_DSN}|g" manifests/aiproxy-config.yaml
  61. sed -i "s|<sql-log-placeholder>|${LOG_SQL_DSN}|g" manifests/aiproxy-config.yaml
  62. elif grep "LOG_SQL_DSN: \"\"" manifests/aiproxy-config.yaml >/dev/null 2>&1; then
  63. # Deploy PostgreSQL resources
  64. kubectl apply -f manifests/pgsql.yaml -n aiproxy-system
  65. # Wait for secrets
  66. wait_for_secret "aiproxy-conn-credential"
  67. # Build connection strings
  68. SQL_DSN=$(build_postgres_dsn "aiproxy-conn-credential") || exit $?
  69. # Update config
  70. sed -i "s|<sql-placeholder>|${SQL_DSN}|g" manifests/aiproxy-config.yaml
  71. else
  72. echo "Error: LOG_SQL_DSN is not allowed to be passed alone, please provide both SQL_DSN and LOG_SQL_DSN or provide SQL_DSN only or neither."
  73. exit 1
  74. fi
  75. elif grep "<sql-log-placeholder>" manifests/aiproxy-config.yaml >/dev/null 2>&1; then
  76. sed -i 's/<sql-log-placeholder>//g' manifests/aiproxy-config.yaml
  77. fi
  78. # Handle Redis configuration
  79. if grep "<redis-placeholder>" manifests/aiproxy-config.yaml >/dev/null 2>&1; then
  80. kubectl apply -f manifests/redis.yaml -n aiproxy-system
  81. wait_for_secret "aiproxy-redis-conn-credential"
  82. # Build redis connection string
  83. REDIS_CONN=$(build_redis_conn "aiproxy-redis-conn-credential") || exit $?
  84. sed -i "s|<redis-placeholder>|${REDIS_CONN}|g" manifests/aiproxy-config.yaml
  85. fi
  86. # Deploy application
  87. kubectl apply -f manifests/aiproxy-config.yaml -n aiproxy-system
  88. kubectl apply -f manifests/deploy.yaml -n aiproxy-system
  89. # Create ingress if domain is specified
  90. if [[ -n "$cloudDomain" ]]; then
  91. kubectl create -f manifests/ingress.yaml -n aiproxy-system || true
  92. fi