kc-setup.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # ===============================
  2. # Keycloak VM provisioning script
  3. # ===============================
  4. # Get Keycloak input data
  5. source kc-data.sh
  6. # Change Timezone
  7. timedatectl set-timezone Europe/Moscow
  8. # Install Packages
  9. apt-get update > /dev/null
  10. apt-get install -y unzip openjdk-18-jre jq > /dev/null
  11. # Install Yandex Cloud CLI (yc CLI)
  12. YC_PATH="/opt/yc"
  13. mkdir -p ${YC_PATH}
  14. curl -s -O https://storage.yandexcloud.net/yandexcloud-yc/install.sh
  15. chmod u+x install.sh
  16. ./install.sh -a -i ${YC_PATH}/ 2>/dev/null
  17. ln -s ${YC_PATH}/bin/yc /usr/bin/yc
  18. rm -f install.sh
  19. sed -i "\$ a source ${YC_PATH}/completion.bash.inc" /etc/profile
  20. # Configuring yc CLI
  21. VM_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
  22. FOLDER_ID=$(yc compute instance get $VM_ID --format=json | jq -r .folder_id )
  23. CLOUD_ID=$(yc resource folder get $FOLDER_ID --format=json | jq -r .cloud_id)
  24. yc config profile create default
  25. yc config set cloud-id $CLOUD_ID
  26. yc config set folder-id $FOLDER_ID
  27. unset CLOUD_ID FOLDER_ID VM_ID
  28. # Get Keycloak distro and put files to the right place
  29. curl -sLO https://github.com/keycloak/keycloak/releases/download/$KC_VER/keycloak-$KC_VER.zip
  30. unzip -q keycloak-$KC_VER.zip
  31. rm -f keycloak-$KC_VER/bin/*.bat
  32. mkdir -p /opt/keycloak
  33. cp -R keycloak-$KC_VER/* /opt/keycloak
  34. rm -rf keycloak-$KC_VER/ keycloak-$KC_VER.zip
  35. export PATH=$PATH:/opt/keycloak/bin
  36. kc.sh build
  37. # Get Let's Encrypt certificate from the YC Certificate Manager
  38. # Let's Encrypt should validate certificate request within 30 minutes
  39. mkdir -p $KC_CERT_PATH
  40. status=None
  41. while [ $status != 'ISSUED' ]
  42. do
  43. status=$(yc cm certificate get --full --name=$KC_CERT_NAME --format=json | jq -r .status)
  44. echo $(date +'%H:%M:%S') $status
  45. sleep 60
  46. done
  47. yc cm certificate download --name=$KC_CERT_NAME --chain=$KC_CERT_PATH/$KC_CERT_PUB --key=$KC_CERT_PATH/$KC_CERT_PRIV > /dev/null
  48. # Prepare systemd things
  49. groupadd keycloak
  50. useradd -r -g keycloak -d /opt/keycloak -s /sbin/nologin keycloak
  51. chown -R keycloak:keycloak /opt/keycloak
  52. chmod o+x /opt/keycloak/bin/
  53. cat <<EOF > /lib/systemd/system/keycloak.service
  54. [Unit]
  55. Description=Keycloak Service
  56. After=network.target
  57. [Service]
  58. User=keycloak
  59. Group=keycloak
  60. PIDFile=/var/run/keycloak/keycloak.pid
  61. WorkingDirectory=/opt/keycloak
  62. Environment="KEYCLOAK_ADMIN=$KC_ADM_USER"
  63. Environment="KEYCLOAK_ADMIN_PASSWORD=$KC_ADM_PASS"
  64. ExecStart=/opt/keycloak/bin/kc.sh start \\
  65. --db-url-database=$PG_DB_NAME \\
  66. --db-url-host=$PG_DB_HOST \\
  67. --db-username=$PG_DB_USER \\
  68. --db-password=$PG_DB_PASS \\
  69. --hostname=$KC_FQDN \\
  70. --hostname-strict=true \\
  71. --http-enabled=false \\
  72. --https-protocols=TLSv1.3,TLSv1.2 \\
  73. --https-port=$KC_PORT \\
  74. --https-certificate-file=$KC_CERT_PATH/$KC_CERT_PUB \\
  75. --https-certificate-key-file=$KC_CERT_PATH/$KC_CERT_PRIV \\
  76. --log-level=INFO
  77. [Install]
  78. WantedBy=multi-user.target
  79. EOF
  80. # Start Keycloak via systemd
  81. systemctl daemon-reload
  82. sleep 3
  83. systemctl start keycloak
  84. systemctl enable keycloak
  85. # Remove KC admin credentials from the systemd unit after the first start
  86. sed -i '/KEYCLOAK_ADMIN/d' /lib/systemd/system/keycloak.service
  87. systemctl daemon-reload