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.

62 lines
2.0 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. keys=$(echo "$service_def" | shyaml -y get-value options.admin 2>/dev/null) || {
  13. err "You must specify a ${WHITE}keys${NORMAL} struct to use this service"
  14. exit 1
  15. }
  16. [ "$(echo "$keys" | shyaml -y get-type 2>/dev/null)" == "struct" ] || {
  17. err "Invalid value type for ${WHITE}keys${NORMAL}, please provide a struct"
  18. exit 1
  19. }
  20. local_path_key=/etc/rsync/keys
  21. host_path_key="$SERVICE_DATASTORE${local_path_key}"
  22. ## ident are unique by construction (they are struct keys)
  23. ## but keys need to be also unique
  24. declare -A keys
  25. while read-0 ident key; do
  26. if [ "${keys[$key]}" ]; then
  27. err "Duplicate key: key for ident '$ident' is same as ident '${keys["$key"]}'."
  28. exit 1
  29. fi
  30. if ! [[ "$ident" =~ ^[a-zA-Z0-9._-]+$ ]]; then
  31. err "Invalid identifier '$ident'," \
  32. "please use only alphanumerical char, dots, dash or underscores."
  33. exit 1
  34. fi
  35. debug "Creating access key for ${ident}" || true
  36. echo "$key" | file_put "$host_path_key/admin/${ident}.pub"
  37. keys["$key"]="$ident"
  38. done < <(echo "$keys" | shyaml key-values-0)
  39. debug "Adding config hash to enable recreating upon config change."
  40. config_hash=$({
  41. ## XXXvlab: ``env -i`` sole purpose is to protect find
  42. ## against big shell environments, and prevent it to fail.
  43. env -i find "${host_path_key}/admin" \
  44. -name \*.pub -exec md5sum {} \;
  45. } | md5_compat) || exit 1
  46. init-config-add "\
  47. $SERVICE_NAME:
  48. labels:
  49. - compose.config_hash=$config_hash
  50. "