install.sh 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. timestamp() {
  3. date +"%Y-%m-%d %T"
  4. }
  5. print() {
  6. flag=$(timestamp)
  7. echo -e "\033[1;32m\033[1m INFO [$flag] >> $* \033[0m"
  8. }
  9. warn() {
  10. flag=$(timestamp)
  11. echo -e "\033[33m WARN [$flag] >> $* \033[0m"
  12. }
  13. info() {
  14. flag=$(timestamp)
  15. echo -e "\033[36m INFO [$flag] >> $* \033[0m"
  16. }
  17. wait_for_secret() {
  18. local secret_name=$1
  19. local namespace=${2:-hubble-service}
  20. info "Checking if secret $secret_name exists..."
  21. while ! kubectl get secret "$secret_name" -n "$namespace" > /dev/null 2>&1; do
  22. warn "Secret $secret_name does not exist, retrying in 5 seconds..."
  23. sleep 5
  24. done
  25. info "Secret $secret_name exists, proceeding with the next steps."
  26. }
  27. #===========================================================================
  28. HELM_OPTS=${HELM_OPTS:-""}
  29. NODE_COUNT=$(kubectl get nodes --no-headers | wc -l)
  30. REPLICA_OPTIONS=""
  31. if [ "$NODE_COUNT" -eq 1 ]; then
  32. REPLICA_OPTIONS="--set pgsql.replicas=1 --set pgsqlLog.replicas=1 --set redis.replicas=1 --set redis.sentinelReplicas=0 --set replicas=1 "
  33. HELM_OPTS="${HELM_OPTS} ${REPLICA_OPTIONS}"
  34. fi
  35. helm upgrade -i aiproxy-database -n aiproxy-system --create-namespace charts/aiproxy-database ${HELM_OPTS} --wait
  36. wait_for_secret "aiproxy-conn-credential" "aiproxy-system"
  37. wait_for_secret "aiproxy-log-conn-credential" "aiproxy-system"
  38. wait_for_secret "aiproxy-redis-conn-credential" "aiproxy-system"
  39. AIPROXY_USER=$(kubectl get secret -n aiproxy-system aiproxy-conn-credential -ojsonpath="{.data.username}" | base64 -d)
  40. AIPROXY_PASSWORD=$(kubectl get secret -n aiproxy-system aiproxy-conn-credential -ojsonpath="{.data.password}" | base64 -d)
  41. AIPROXY_PORT=$(kubectl get secret -n aiproxy-system aiproxy-conn-credential -ojsonpath="{.data.port}" | base64 -d)
  42. AIPROXY_HOST=$(kubectl get secret -n aiproxy-system aiproxy-conn-credential -ojsonpath="{.data.host}" | base64 -d).aiproxy-system.svc
  43. AIPROXY_URI="postgres://${AIPROXY_USER}:${AIPROXY_PASSWORD}@${AIPROXY_HOST}:${AIPROXY_PORT}/postgres?sslmode=disable"
  44. LOG_USER=$(kubectl get secret -n aiproxy-system aiproxy-log-conn-credential -ojsonpath="{.data.username}" | base64 -d)
  45. LOG_PASSWORD=$(kubectl get secret -n aiproxy-system aiproxy-log-conn-credential -ojsonpath="{.data.password}" | base64 -d)
  46. LOG_PORT=$(kubectl get secret -n aiproxy-system aiproxy-log-conn-credential -ojsonpath="{.data.port}" | base64 -d)
  47. LOG_HOST=$(kubectl get secret -n aiproxy-system aiproxy-log-conn-credential -ojsonpath="{.data.host}" | base64 -d).aiproxy-system.svc
  48. LOG_URI="postgres://${LOG_USER}:${LOG_PASSWORD}@${LOG_HOST}:${LOG_PORT}/postgres?sslmode=disable"
  49. REDIS_USER=$(kubectl get secret -n aiproxy-system aiproxy-redis-conn-credential -ojsonpath="{.data.username}" | base64 -d)
  50. REDIS_PASSWORD=$(kubectl get secret -n aiproxy-system aiproxy-redis-conn-credential -ojsonpath="{.data.password}" | base64 -d)
  51. REDIS_PORT=$(kubectl get secret -n aiproxy-system aiproxy-redis-conn-credential -ojsonpath="{.data.port}" | base64 -d)
  52. REDIS_HOST=$(kubectl get secret -n aiproxy-system aiproxy-redis-conn-credential -ojsonpath="{.data.host}" | base64 -d).aiproxy-system.svc
  53. REDIS_URI="redis://${REDIS_USER}:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}"
  54. varJwtInternal=$(kubectl get configmap sealos-config -n sealos-system -o jsonpath='{.data.jwtInternal}')
  55. adminKey=$(kubectl get configmap aiproxy-env -n aiproxy-system -o jsonpath='{.data.ADMIN_KEY}' )
  56. if [ -z "$adminKey" ]; then
  57. print "adminKey is empty, generating new credentials."
  58. adminKey=$(openssl rand -hex 64 | head -c 32)
  59. fi
  60. kubectl delete configmap aiproxy-env -n aiproxy-system --ignore-not-found
  61. kubectl delete ingress -n aiproxy-system aiproxy --ignore-not-found
  62. kubectl delete deployment -n aiproxy-system aiproxy --ignore-not-found
  63. kubectl delete service -n aiproxy-system aiproxy --ignore-not-found
  64. SEALOS_CLOUD_DOMAIN=$(kubectl get configmap sealos-config -n sealos-system -o jsonpath='{.data.cloudDomain}')
  65. SEALOS_CLOUD_PORT=$(kubectl get configmap sealos-config -n sealos-system -o jsonpath='{.data.cloudPort}')
  66. helm upgrade -i aiproxy -n aiproxy-system --create-namespace charts/aiproxy ${HELM_OPTS} --set aiproxy.SQL_DSN=${AIPROXY_URI} --set aiproxy.LOG_SQL_DSN=${LOG_URI} --set aiproxy.REDIS=${REDIS_URI} \
  67. --set aiproxy.SEALOS_JWT_KEY=${varJwtInternal} --set aiproxy.ADMIN_KEY=${adminKey} --set cloudDomain=${SEALOS_CLOUD_DOMAIN} --set cloudPort=${SEALOS_CLOUD_PORT}