You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB

  1. #!/bin/bash
  2. ## Init is run on host
  3. ## For now it is run every time the script is launched, but
  4. ## it should be launched only once after build.
  5. ## Accessible variables are:
  6. ## - SERVICE_NAME Name of current service
  7. ## - DOCKER_BASE_IMAGE Base image from which this service might be built if any
  8. ## - SERVICE_DATASTORE Location on host of the DATASTORE of this service
  9. ## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service
  10. set -e
  11. service_def=$(get_compose_service_def "$SERVICE_NAME")
  12. admin_keys=$(echo "$service_def" | shyaml -y get-value options.admin 2>/dev/null) || {
  13. err "You must specify a ${WHITE}admin${NORMAL} struct in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s options"
  14. exit 1
  15. }
  16. [ "$(echo "$admin_keys" | shyaml -y get-type 2>/dev/null)" == "struct" ] || {
  17. err "Invalid value type for ${WHITE}admin${NORMAL} in" \
  18. "${DARKYELLOW}$SERVICE_NAME${NORMAL}'s options, please provide a struct"
  19. exit 1
  20. }
  21. rebuild-config() {
  22. rm -rf "$SERVICE_CONFIGSTORE/etc/rsync/keys/admin"
  23. mkdir -p "$host_path_key"
  24. while read-0 ident keys; do
  25. ident=$(e "$ident" | shyaml get-value)
  26. if ! [[ "$ident" =~ ^[a-zA-Z0-9._-]+$ ]]; then
  27. err "Invalid identifier '$ident'," \
  28. "please use only alphanumerical char, dots, dash or underscores."
  29. exit 1
  30. fi
  31. debug "Setting access keys for ${ident}"
  32. [ "$(echo "$keys" | shyaml -y get-type 2>/dev/null)" == "sequence" ] || {
  33. err "Invalid value type for ${WHITE}admin.$ident${NORMAL}, please provide a sequence"
  34. echo " Received: '$keys'" >&2
  35. exit 1
  36. }
  37. while read-0 key; do
  38. echo "command=\"/usr/local/sbin/ssh-admin-cmd-validate \\\"$ident\\\"\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $key"
  39. done < <(echo "$keys" | shyaml get-values-0) | file_put "$host_path_key/$ident/.ssh/authorized_keys"
  40. done < <(echo "$admin_keys" | shyaml -y key-values-0)
  41. mkdir -p "${CONTROL_USERS_FILE%/*}" || return 1
  42. e "$control_users" > "$CONTROL_USERS_FILE"
  43. }
  44. local_path_key=/etc/rsync/keys/admin
  45. host_path_key="$SERVICE_CONFIGSTORE${local_path_key}"
  46. CONTROL_USERS_FILE="$SERVICE_DATASTORE/.control-pass"
  47. ## Was it already properly propagated to database ?
  48. control_users=$(H "${admin_keys}" "$(declare -f "rebuild-config")")
  49. init-config-add "\
  50. $SERVICE_NAME:
  51. volumes:
  52. - $host_path_key:$local_path_key
  53. labels:
  54. - compose.config_hash=$control_users
  55. "
  56. if [ -e "$CONTROL_USERS_FILE" ] && [ "$control_users" == "$(cat "$CONTROL_USERS_FILE")" ]; then
  57. exit 0
  58. fi
  59. rebuild-config