run.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. [ "$DEBUG" ] && set -x
  3. set -eo pipefail
  4. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  5. image="$1"
  6. clientImage='buildpack-deps:bookworm-curl'
  7. # ensure the clientImage is ready and available
  8. if ! docker image inspect "$clientImage" &> /dev/null; then
  9. docker pull "$clientImage" > /dev/null
  10. fi
  11. # The keygen below is a bit ugly, because spiped-generate-key.sh expects /spiped/key to be a directory (you can't bind mount a non-existing file),
  12. # but the entrypoint expects /spiped/key to be the actual keyfile.
  13. # So we first symlink /spiped/key to some directory, then generate the keyfile and then replace the symlink by the generated keyfile.
  14. cid_keygen="$(docker run -d "$image" sh -c 'ln -s /tmp /spiped/key && spiped-generate-key.sh && rm /spiped/key && mv /tmp/spiped-keyfile /spiped/key')"
  15. trap "docker rm -vf $cid_keygen > /dev/null" EXIT
  16. cid_d="$(docker run --volumes-from="$cid_keygen" -d "$image" -d -s '[0.0.0.0]:8080' -t 'example.com:80')"
  17. trap "docker rm -vf $cid_keygen $cid_d > /dev/null" EXIT
  18. cid_e="$(docker run --volumes-from="$cid_keygen" --link "$cid_d":spiped_d -d "$image" -e -s '[0.0.0.0]:80' -t 'spiped_d:8080')"
  19. trap "docker rm -vf $cid_keygen $cid_d $cid_e > /dev/null" EXIT
  20. _request() {
  21. local method="$1"
  22. shift
  23. local proto="$1"
  24. shift
  25. local url="${1#/}"
  26. shift
  27. if [ "$(docker inspect -f '{{.State.Running}}' "$cid_d" 2>/dev/null)" != 'true' ]; then
  28. echo >&2 "$image stopped unexpectedly!"
  29. ( set -x && docker logs "$cid_d" ) >&2 || true
  30. false
  31. fi
  32. if [ "$(docker inspect -f '{{.State.Running}}' "$cid_e" 2>/dev/null)" != 'true' ]; then
  33. echo >&2 "$image stopped unexpectedly!"
  34. ( set -x && docker logs "$cid_e" ) >&2 || true
  35. false
  36. fi
  37. docker run --rm \
  38. --link "$cid_e":spiped \
  39. "$clientImage" \
  40. curl -fsSL -X"$method" --connect-to '::spiped:' "$@" "$proto://example.com/$url"
  41. }
  42. . "$dir/../../retry.sh" '[ "$(_request GET / --output /dev/null || echo $?)" != 7 ]'
  43. # Check that we can request / (which is proxying example.com)
  44. _request GET http '/' | grep '<h1>Example Domain</h1>' > /dev/null