Skip to content

Commit

Permalink
Merge pull request #8 from jpbochi/curl-instead-of-netcat
Browse files Browse the repository at this point in the history
Tries curl or netcat to ping the docker daemon via its unix socket
  • Loading branch information
jpbochi authored Feb 3, 2017
2 parents 85561ee + 7d15474 commit 320069e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
17 changes: 12 additions & 5 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
machine:
pre:
- curl -sSL https://s3.amazonaws.com/circle-downloads/install-circleci-docker.sh | bash -s -- 1.10.0
- sudo apt-get install devscripts
services:
- docker
environment:
RM_OPT: "" # tells drun not to --rm since CircleCI doesn't like it

test:
override:
- ./drun -x docker:1.10.0 docker version
- ./drun ubuntu:16.04 cat /etc/issue | grep 'Ubuntu 16.04'
- ./drun mhart/alpine-node:5.12.0 node --version | grep 'v5.12.0'
- echo '{"test":"passing"}' | ./drun -x stedolan/jq '.test' | grep '"passing"'
- echo '{"test":"passing"}' > test.json && ./drun -x stedolan/jq '.test' test.json | grep '"passing"'
- checkbashisms -f ./drun
- ./drun -x ubuntu:16.04 cat /etc/issue | grep 'Ubuntu 16.04' # should execute command inside container
- ./drun -x docker:1.10.0 docker version # should bind docker.sock
- ./drun -x docker:1.10.0 sh -c './drun -x docker:1.10.0 true' # should do inception in 2 levels
- ./drun -x docker:1.10.0 sh -c './drun -x docker:1.10.0 sh -c "./drun -x docker:1.10.0 true"' # should do inception in 3 levels
- ./drun -x mhart/alpine-node:5.12.0 node --version | grep 'v5.12.0' # should get version of node
- ./drun -xNA node --version | grep 'v5.12.0' # should read version from package.json
- echo '{"test":"passing"}' | ./drun -x stedolan/jq '.test' | grep '"passing"' # should pipe into command inside container
- echo '{"test":"passing"}' > test.json && ./drun -x stedolan/jq '.test' test.json | grep '"passing"' # should mount current directory
20 changes: 16 additions & 4 deletions drun
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@ usage() {
exit 1
}

node_version_jq() {
jq -r -e '.engines.node // "latest"' package.json
}
node_version_python() {
python -c "import json;print json.load(open('package.json')).get('engines',{}).get('node','latestpython')"
}
node_version_ruby() {
cat package.json | ruby -e 'require "json"; puts JSON[STDIN.read].fetch("engine",{}).fetch("node","lastestruby")'
}
node_version() {
if [ ! -f package.json ]; then
echo 'latest'
elif hash jq 2> /dev/null; then
echo $(jq -r -e '.engines.node // "latest"' package.json)
else
echo $(python -c "import json;print json.load(open('package.json'))['engines']['node']" 2> /dev/null || echo latest)
node_version_jq 2> /dev/null || \
node_version_python 2> /dev/null || \
node_version_ruby 2> /dev/null || \
(echo >&2 'drun ERR! Failed to resolve node version. Defaulting to "latest"' && echo 'latest')
fi
}

Expand Down Expand Up @@ -108,7 +118,9 @@ fi

DSOCK=/var/run/docker.sock
if [ "${MOUNTDOCKERSOCK:-}" != 'n' -a -r "$DSOCK" -a -w "$DSOCK" -a -S "$DSOCK" ]; then
if printf 'GET /_ping HTTP/1.1\nHost: daemon\n\n' | nc -U /var/run/docker.sock | head -n 1 | grep -q '200 OK'; then
if curl --help | grep -q unix-socket && curl -sS --unix-socket "$DSOCK" http://docker/_ping | grep -q OK; then
EXTRA_OPTS="${EXTRA_OPTS:-} -v ${DSOCK}:${DSOCK}"
elif nc -h 2>&1 | grep -qe '-X\b' && printf 'GET /_ping HTTP/1.1\nHost: daemon\n\n' | nc -U "$DSOCK" | head -n 1 | grep -q '200 OK'; then
EXTRA_OPTS="${EXTRA_OPTS:-} -v ${DSOCK}:${DSOCK}"
fi
fi
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"engines": {
"node": "5.12.0"
}
}

0 comments on commit 320069e

Please sign in to comment.