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.

65 lines
2.1 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.keys 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_CONFIGSTORE${local_path_key}"
  22. key_nb=0
  23. ## ident are unique by construction (they are struct keys)
  24. ## but keys need to be also unique
  25. declare -A keys
  26. while read-0 ident key; do
  27. if [ "${keys[$key]}" ]; then
  28. err "Duplicate key: key for ident '$ident' is same as ident '${keys["$key"]}'."
  29. exit 1
  30. fi
  31. if ! [[ "$ident" =~ ^[a-zA-Z0-9._-]+$ ]]; then
  32. err "Invalid identifier '$ident'," \
  33. "please use only alphanumerical char, dots, dash or underscores."
  34. exit 1
  35. fi
  36. debug "Creating access key for ${ident}" || true
  37. echo "$key" | file_put "$host_path_key/${ident}.pub"
  38. keys["$key"]="$ident"
  39. done < <(echo "$keys" | shyaml key-values-0)
  40. debug "Adding config hash to enable recreating upon config change."
  41. config_hash=$({
  42. ## XXXvlab: ``env -i`` sole purpose is to protect find
  43. ## against big shell environments, and prevent it to fail.
  44. env -i find "${host_path_key}" \
  45. -name \*.pub -exec md5sum {} \;
  46. } | md5_compat) || exit 1
  47. init-config-add "\
  48. $SERVICE_NAME:
  49. volumes:
  50. - $host_path_key:$local_path_key:ro
  51. labels:
  52. - compose.config_hash=$config_hash
  53. "