59 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. users_def=$(echo "$service_def" | shyaml get-value options.users 2>/dev/null) || true
  13. users_file="$SERVICE_CONFIGSTORE/etc/sftp-users.conf"
  14. echo | file_put "$users_file"
  15. [ "$users_def" ] || exit 0
  16. rm -f "$users_file"
  17. volume_keys=()
  18. while read-0 login user_def; do
  19. key_nb=0
  20. local_path_key="/home/$login/.ssh/keys"
  21. host_path_key="$SERVICE_CONFIGSTORE${local_path_key}"
  22. while read-0 key; do
  23. debug "Creating login key ${key_nb} for '$login'" || true
  24. echo "$key" | file_put "$host_path_key/key_${key_nb}.pub"
  25. ((key_nb++)) || true
  26. done < <(echo "$user_def" | shyaml get-values-0 keys)
  27. volume_keys+=("$host_path_key:$local_path_key:ro")
  28. gids=()
  29. while read-0 group; do
  30. if ! group_ent=$(getent group "$group"); then
  31. debug groupadd -K GID_MIN=3000 -K GID_MAX=4000 "$group"
  32. groupadd -K GID_MIN=3000 -K GID_MAX=4000 "$group"
  33. group_ent=$(getent group "$group")
  34. fi
  35. gids+=("$(echo "$group_ent" | cut -f3 -d:)")
  36. done < <(echo "$user_def" | shyaml get-values-0 groups 2>/dev/null)
  37. password=$(echo "$user_def" | shyaml get-value password 2>/dev/null) ||
  38. password=$(gen_password 14)
  39. line="$login:$password::$(echo "${gids[@]}" | tr " " ",")"
  40. debug "Adding line: $line"
  41. echo "$line" >> "$users_file"
  42. done < <(echo "$users_def" | shyaml key-values-0)
  43. init-config-add "\
  44. $SERVICE_NAME:
  45. volumes:
  46. $(for volume in "${volume_keys[@]}"; do
  47. echo " - $volume"
  48. done)
  49. "