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.

55 lines
1.7 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. init-config-add "\
  41. $SERVICE_NAME:
  42. volumes:
  43. - $host_path_key:$local_path_key:ro
  44. "