Skip to content

Commit

Permalink
fix start-up order through wait-for-it.sh script.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredTan95 committed Oct 18, 2018
1 parent 1be196f commit f9f4638
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 46 deletions.
7 changes: 4 additions & 3 deletions 5.x/quick-start/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ services:
environment:
- ES_CLUSTER_NAME=elasticsearch
- ES_ADDRESSES=elasticsearch-service:9300
- BIND_HOST=0.0.0.0
- AGENT_JETTY_BIND_HOST=0.0.0.0
- NAMING_BIND_HOST=0.0.0.0
- BIND_HOST=127.0.0.1
- AGENT_JETTY_BIND_HOST=127.0.0.1
- NAMING_BIND_HOST=127.0.0.1
- UI_JETTY_BIND_HOST=0.0.0.0
depends_on:
- elasticsearch-service
command: ["/wait-for-it.sh", "elasticsearch-service:9300", "-t", "20"]
links:
- elasticsearch-service
ports:
Expand Down
7 changes: 4 additions & 3 deletions 5.x/quick-start/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ services:
environment:
- ES_CLUSTER_NAME=elasticsearch
- ES_ADDRESSES=elasticsearch-service:9300
- BIND_HOST=0.0.0.0
- AGENT_JETTY_BIND_HOST=0.0.0.0
- NAMING_BIND_HOST=0.0.0.0
- BIND_HOST=127.0.0.1
- AGENT_JETTY_BIND_HOST=127.0.0.1
- NAMING_BIND_HOST=127.0.0.1
- UI_JETTY_BIND_HOST=0.0.0.0
depends_on:
- elasticsearch-service
command: ["/wait-for-it.sh", "elasticsearch-service:9300", "-t", "20"]
links:
- elasticsearch-service
ports:
Expand Down
2 changes: 2 additions & 0 deletions 5.x/standalone/all-in-one/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ COPY webapp.yml /apache-skywalking-apm-incubating/webapp/webapp.yml

COPY docker-entrypoint.sh /docker-entrypoint.sh

COPY wait-for-it.sh /wait-for-it.sh

# logs locations in /sky/logs folder.

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
Expand Down
177 changes: 177 additions & 0 deletions 5.x/standalone/all-in-one/wait-for-it.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available

cmdname=$(basename $0)

echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

usage()
{
cat << USAGE >&2
Usage:
$cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}

wait_for()
{
if [[ $TIMEOUT -gt 0 ]]; then
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
else
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
fi
start_ts=$(date +%s)
while :
do
if [[ $ISBUSY -eq 1 ]]; then
nc -z $HOST $PORT
result=$?
else
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
result=$?
fi
if [[ $result -eq 0 ]]; then
end_ts=$(date +%s)
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
break
fi
sleep 1
done
return $result
}

wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $QUIET -eq 1 ]]; then
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
else
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
fi
PID=$!
trap "kill -INT -$PID" INT
wait $PID
RESULT=$?
if [[ $RESULT -ne 0 ]]; then
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
fi
return $RESULT
}

# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
hostport=(${1//:/ })
HOST=${hostport[0]}
PORT=${hostport[1]}
shift 1
;;
--child)
CHILD=1
shift 1
;;
-q | --quiet)
QUIET=1
shift 1
;;
-s | --strict)
STRICT=1
shift 1
;;
-h)
HOST="$2"
if [[ $HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
HOST="${1#*=}"
shift 1
;;
-p)
PORT="$2"
if [[ $PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
PORT="${1#*=}"
shift 1
;;
-t)
TIMEOUT="$2"
if [[ $TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
CLI=("$@")
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done

if [[ "$HOST" == "" || "$PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi

TIMEOUT=${TIMEOUT:-15}
STRICT=${STRICT:-0}
CHILD=${CHILD:-0}
QUIET=${QUIET:-0}

# check to see if timeout is from busybox?
# check to see if timeout is from busybox?
TIMEOUT_PATH=$(realpath $(which timeout))
if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
ISBUSY=1
BUSYTIMEFLAG="-t"
else
ISBUSY=0
BUSYTIMEFLAG=""
fi

if [[ $CHILD -gt 0 ]]; then
wait_for
RESULT=$?
exit $RESULT
else
if [[ $TIMEOUT -gt 0 ]]; then
wait_for_wrapper
RESULT=$?
else
wait_for
RESULT=$?
fi
fi

if [[ $CLI != "" ]]; then
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
echoerr "$cmdname: strict mode, refusing to execute subprocess"
exit $RESULT
fi
exec "${CLI[@]}"
else
exit $RESULT
fi
40 changes: 0 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,3 @@
### 通过Docker Compose 一键启动(Start With Docker Compose)
参考:[Skywalking-Dcoker Quick Start](https://github.com/JaredTan95/skywalking-docker/blob/master/5.x/quick-start/README.md)

```
version: '2'
services:
elasticsearch-service:
image: wutang/elasticsearch-shanghai-zone
container_name: elasticsearch
environment:
- cluster.name=elasticsearch
- bootstrap.memory_lock=true
- xpack.security.enabled=false
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"
- node.name=elasticsearch_node_1
ulimits:
memlock:
soft: -1
hard: -1
ports:
- 9200:9200
- 9300:9300
skywalking:
image: wutang/skywalking-docker:5.x
container_name: skywalking
environment:
- ES_CLUSTER_NAME=elasticsearch
- ES_ADDRESSES=elasticsearch-service:9300
- BIND_HOST=0.0.0.0
- AGENT_JETTY_BIND_HOST=0.0.0.0
- NAMING_BIND_HOST=0.0.0.0
- UI_JETTY_BIND_HOST=0.0.0.0
depends_on:
- elasticsearch-service
links:
- elasticsearch-service
ports:
- 8080:8080
- 10800:10800
- 11800:11800
- 12800:12800
```

0 comments on commit f9f4638

Please sign in to comment.