kc-vm-init.tpl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #cloud-config
  2. datasource:
  3. Ec2:
  4. strict_id: false
  5. ssh_pwauth: yes
  6. users:
  7. - name: "${ADMIN_NAME}"
  8. sudo: ALL=(ALL) NOPASSWD:ALL
  9. shell: /bin/bash
  10. ssh-authorized-keys:
  11. - "${ADMIN_SSH_KEY}"
  12. write_files:
  13. - path: "/root/${KC_CERT_PUB}"
  14. permissions: "0644"
  15. content: "${KC_CERT_PUB_DATA}"
  16. - path: "/root/${KC_CERT_PRIV}"
  17. permissions: "0644"
  18. content: "${KC_CERT_PRIV_DATA}"
  19. - path: "/root/kc-init.sh"
  20. permissions: "0740"
  21. content: |
  22. #!/bin/bash
  23. # SSL certificates normalization for use
  24. base64 -d /root/${KC_CERT_PUB} > /root/pub.txt
  25. mv -f /root/pub.txt /root/${KC_CERT_PUB}
  26. base64 -d /root/${KC_CERT_PRIV} > /root/priv.txt
  27. mv -f /root/priv.txt /root/${KC_CERT_PRIV}
  28. echo "export KC_CERT_NAME=${KC_CERT_NAME}" > /root/kc_cert_name.sh
  29. usermod -a -G docker ${ADMIN_NAME}
  30. # Get Keycloak container image version (tag)
  31. KC_VER=$(docker image ls keycloak --format "{{.Tag}}")
  32. # Create Keycloak container
  33. docker create --name=keycloak --hostname=keycloak --network=host \
  34. --volume /etc/localtime:/etc/localtime:ro \
  35. --env KEYCLOAK_ADMIN="${KC_ADM_USER}" \
  36. --env KEYCLOAK_ADMIN_PASSWORD="${KC_ADM_PASS}" \
  37. --env KC_FQDN="${KC_FQDN}" \
  38. --env KC_PORT="${KC_PORT}" \
  39. --env PG_DB_HOST="${PG_DB_HOST}" \
  40. --env PG_DB_NAME="${PG_DB_NAME}" \
  41. --env PG_DB_USER="${PG_DB_USER}" \
  42. --env PG_DB_PASS="${PG_DB_PASS}" \
  43. --env KC_CERT_PUB="${KC_CERT_PUB}" \
  44. --env KC_CERT_PRIV="${KC_CERT_PRIV}" \
  45. keycloak:$KC_VER
  46. # Put SSL certificates to Keycloak container
  47. docker cp /root/${KC_CERT_PUB} keycloak:/opt/keycloak/conf/${KC_CERT_PUB}
  48. docker cp /root/${KC_CERT_PRIV} keycloak:/opt/keycloak/conf/${KC_CERT_PRIV}
  49. # Start Keycloak container
  50. docker start keycloak
  51. runcmd:
  52. - sleep 1
  53. - sudo -i
  54. - /root/kc-init.sh