78 lines
2.5 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 to use this service"
  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}, please provide a struct"
  18. exit 1
  19. }
  20. rebuild-config() {
  21. rm -rf "$SERVICE_CONFIGSTORE/etc/rsync/keys/admin"
  22. mkdir -p "$host_path_key"
  23. while read-0 ident keys; do
  24. ident=$(e "$ident" | shyaml get-value)
  25. if ! [[ "$ident" =~ ^[a-zA-Z0-9._-]+$ ]]; then
  26. err "Invalid identifier '$ident'," \
  27. "please use only alphanumerical char, dots, dash or underscores."
  28. exit 1
  29. fi
  30. debug "Setting access keys for ${ident}"
  31. [ "$(echo "$keys" | shyaml -y get-type 2>/dev/null)" == "sequence" ] || {
  32. err "Invalid value type for ${WHITE}admin.$ident${NORMAL}, please provide a sequence"
  33. echo " Received: '$keys'" >&2
  34. exit 1
  35. }
  36. while read-0 key; do
  37. echo "command=\"/usr/local/sbin/ssh-admin-cmd-validate \\\"$ident\\\"\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty $key"
  38. done < <(echo "$keys" | shyaml get-values-0) | file_put "$host_path_key/$ident/.ssh/authorized_keys"
  39. done < <(echo "$admin_keys" | shyaml -y key-values-0)
  40. e "$control_users" > "$CONTROL_USERS_FILE"
  41. }
  42. local_path_key=/etc/rsync/keys/admin
  43. host_path_key="$SERVICE_CONFIGSTORE${local_path_key}"
  44. init-config-add "\
  45. $SERVICE_NAME:
  46. volumes:
  47. - $host_path_key:$local_path_key
  48. labels:
  49. - compose.config_hash=$control_users
  50. "
  51. CONTROL_USERS_FILE="$SERVICE_DATASTORE/.control-pass"
  52. ## Was it already properly propagated to database ?
  53. control_users=$(H "${admin_keys}" "$(declare -f "rebuild-config")")
  54. if [ -e "$CONTROL_USERS_FILE" ] && [ "$control_users" == "$(cat "$CONTROL_USERS_FILE")" ]; then
  55. exit 0
  56. fi
  57. rebuild-config