Skip to content

Commit 336e36b

Browse files
authored
Merge pull request #993 from metacpan/oalders/wait-for-es
wait-for-es.sh should work inside and outside of Docker containers
2 parents 9b69ff2 + 43dd8ac commit 336e36b

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

.circleci/config.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference
22
version: 2.1
3-
# Use a package of configuration called an orb.
43
# Orchestrate or schedule a set of jobs
54
workflows:
65
docker-compose:
7-
# Run the welcome/run job in its own container
86
jobs:
97
- build-and-test
108
jobs:
@@ -16,8 +14,10 @@ jobs:
1614
name: Install Docker Compose
1715
command: |
1816
set -x
19-
curl -L https://github.com/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
20-
sudo chmod +x ~/docker-compose
17+
curl -L https://github.com/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /home/circleci/bin/docker-compose
18+
sudo chmod +x /home/circleci/bin/docker-compose
19+
which docker-compose
20+
docker-compose --version
2121
- run:
2222
command: |
2323
git clone https://github.com/metacpan/metacpan-docker.git
@@ -36,27 +36,27 @@ jobs:
3636
- run:
3737
command: |
3838
pushd metacpan-docker
39-
~/docker-compose build --build-arg CPM_ARGS='--with-test' api_test
39+
docker-compose build --build-arg CPM_ARGS='--with-test' api_test
4040
name: compose build
4141
- run:
4242
command: |
4343
pushd metacpan-docker
4444
./bin/metacpan-docker init
45-
~/docker-compose --verbose up -d api_test
45+
docker-compose --verbose up -d api_test
4646
name: compose up
4747
- run:
4848
command: |
49-
pushd metacpan-docker/src/metacpan-api
50-
./wait-for-es.sh http://localhost:9200
49+
pushd metacpan-docker
50+
./src/metacpan-api/wait-for-es.sh http://localhost:9200 elasticsearch_test --
5151
name: wait for ES
5252
- run:
5353
command: |
5454
pushd metacpan-docker
55-
~/docker-compose exec -T api_test prove -lr --jobs 2 t
55+
docker-compose exec -T api_test prove -lr --jobs 2 t
5656
- run:
5757
command: |
5858
pushd metacpan-docker
59-
~/docker-compose logs
59+
docker-compose logs
6060
docker stats --no-stream
6161
docker ps -a | head
6262
name: docker-compose logs

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ WORKDIR /metacpan-api
99
# size of the images.
1010
RUN mkdir /CPAN \
1111
&& apt-get update \
12-
&& apt-get install -y --no-install-recommends rsync=3.1.3-6 \
12+
&& apt-get install -y --no-install-recommends rsync=3.1.3-6 jq \
1313
&& apt-get clean \
1414
&& rm -rf /var/lib/apt/lists/* \
1515
&& cpm install --global \

wait-for-es.sh

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55

66
set -ux
77

8-
host="$1"
8+
9+
HOST="$1"
10+
CONTAINER=${2:-""}
11+
PREAMBLE=""
12+
13+
echo "container |$CONTAINER|"
14+
if [[ $CONTAINER != "" ]]; then
15+
PREAMBLE="docker-compose exec $CONTAINER"
16+
fi
917

1018
while true; do
11-
response=$(curl --write-out '%{http_code}' --silent --fail --output /dev/null "$host")
19+
response=$($PREAMBLE curl --write-out '%{http_code}' --silent --fail --output /dev/null "$HOST")
1220
if [[ "$response" -eq "200" ]]; then
1321
break
1422
fi
@@ -21,18 +29,27 @@ done
2129
# if the server was not available
2230
set -e
2331

32+
COUNTER=0
33+
MAX_LOOPS=60
2434
while true; do
2535
## Wait for ES status to turn to yellow.
2636
## TODO: Ideally we'd be waiting for green, but we need multiple nodes for that.
27-
health="$(curl -fsSL "$host/_cat/health?h=status")"
28-
health="$(echo "$health" | xargs)" # trim whitespace (otherwise we'll have "green ")
29-
if [[ $health == 'yellow' || $health == 'green' ]]; then
37+
health=$($PREAMBLE curl -fsSL "$HOST/_cat/health?format=JSON" | jq '.[0].status == "yellow" or .[0].status == "green"')
38+
if [[ $health == 'true' ]]; then
39+
echo "Elasticsearch is up" >&2
3040
break
3141
fi
3242
echo "Elastic Search is unavailable ($health) - sleeping" >&2
43+
COUNTER=$((COUNTER + 1))
44+
if [[ $COUNTER -gt $MAX_LOOPS ]]; then
45+
echo "Giving up after $COUNTER attempts"
46+
exit 1
47+
break
48+
fi
3349
sleep 1
3450
done
3551

36-
echo "Elastic Search is up" >&2
52+
# Allow commands to be chained
53+
shift
3754
shift
3855
exec "$@"

0 commit comments

Comments
 (0)