Skip to content

Commit

Permalink
add sharness test, fix bugs in init script
Browse files Browse the repository at this point in the history
Fixed in init script:

- Added some missing quotes around expansions
- Fixed INIT_ARGS to not pass any args if IPFS_PROFILE isn't specified
- Use printf instead of "echo -e"
- Only run scripts in top-level of init dir
- Handle filenames correctly when finding init scripts (by using find + xargs)
  • Loading branch information
guseggert committed Mar 30, 2022
1 parent 8e87456 commit b31c311
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ LABEL maintainer="Steven Allen <steven@stebalien.com>"
ENV SRC_DIR /go-ipfs
COPY --from=0 $SRC_DIR/cmd/ipfs/ipfs /usr/local/bin/ipfs
COPY --from=0 $SRC_DIR/bin/container_daemon /usr/local/bin/start_ipfs
COPY --from=0 $SRC_DIR/bin/container_init_run /usr/local/bin/container_init_run
COPY --from=0 /tmp/su-exec/su-exec-static /sbin/su-exec
COPY --from=0 /tmp/tini /sbin/tini
COPY --from=0 /bin/fusermount /usr/local/bin/fusermount
Expand Down
39 changes: 9 additions & 30 deletions bin/container_daemon
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/bin/sh
set -e

user=ipfs
repo="$IPFS_PATH"

if [ `id -u` -eq 0 ]; then
if [ "$(id -u)" -eq 0 ]; then
echo "Changing user to $user"
# ensure folder is writable
su-exec "$user" test -w "$repo" || chown -R -- "$user" "$repo"
Expand All @@ -14,14 +15,11 @@ fi
# 2nd invocation with regular user
ipfs version


if [ -e "$repo/config" ]; then
echo "Found IPFS fs-repo at $repo"
else
case "$IPFS_PROFILE" in
"") INIT_ARGS="" ;;
*) INIT_ARGS="--profile=$IPFS_PROFILE" ;;
esac
ipfs init $INIT_ARGS
ipfs init ${IPFS_PROFILE:+"--profile=$IPFS_PROFILE"}
ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080

Expand All @@ -31,9 +29,9 @@ else
SWARM_KEY_PERM=0400

# Create a swarm key from a given environment variable
if [ ! -z "$IPFS_SWARM_KEY" ] ; then
if [ -n "$IPFS_SWARM_KEY" ] ; then
echo "Copying swarm key from variable..."
echo -e "$IPFS_SWARM_KEY" >"$SWARM_KEY_FILE" || exit 1
printf "%s\n" "$IPFS_SWARM_KEY" >"$SWARM_KEY_FILE" || exit 1
chmod $SWARM_KEY_PERM "$SWARM_KEY_FILE"
fi

Expand All @@ -43,34 +41,15 @@ else
# Check during initialization if a swarm key was provided and
# copy it to the ipfs directory with the right permissions
# WARNING: This will replace the swarm key if it exists
if [ ! -z "$IPFS_SWARM_KEY_FILE" ] ; then
if [ -n "$IPFS_SWARM_KEY_FILE" ] ; then
echo "Copying swarm key from file..."
install -m $SWARM_KEY_PERM "$IPFS_SWARM_KEY_FILE" "$SWARM_KEY_FILE" || exit 1
fi

# Unset the swarm key file variable
unset IPFS_SWARM_KEY_FILE

# Initialization directory
IPFS_INIT_SCRIPTS_DIR="/container-init.d"

# Check if initialization directory exists
if [ -d "$IPFS_INIT_SCRIPTS_DIR" ] ; then
echo "Configuring IPFS using init scripts..."
# Check for any *.sh scripts
for script in $(find $IPFS_INIT_SCRIPTS_DIR -type f \( -iname \*.sh \)) ; do
# Execute the scripts with +x permission and
# source the others
if [ -x "$script" ] ; then
echo "-Executing '$script'..."
$script || exit 1
else
echo "-Sourcing '$script'..."
source $script
fi
done
fi

fi

find /container-init.d -maxdepth 1 -type f -iname '*.sh' -print0 | sort -z | xargs -n 1 -0 -r container_init_run

exec ipfs "$@"
14 changes: 14 additions & 0 deletions bin/container_init_run
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

set -e

# used by the container startup script for running initialization scripts

script="$1"
if [ -x "$script" ] ; then
printf "Executing '%s'...\n" "$script"
"$script"
else
printf "Sourcing '%s'...\n" "$script"
. "$script"
fi
29 changes: 27 additions & 2 deletions test/sharness/t0300-docker-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST_TESTS_DIR=$(dirname "$TEST_SCRIPTS_DIR")
APP_ROOT_DIR=$(dirname "$TEST_TESTS_DIR")

test_expect_success "docker image build succeeds" '
docker_build "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" >build-actual ||
docker_build "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" | tee build-actual ||
test_fsh echo "TEST_TESTS_DIR: $TEST_TESTS_DIR" ||
test_fsh echo "APP_ROOT_DIR : $APP_ROOT_DIR" ||
test_fsh cat build-actual
Expand All @@ -41,8 +41,18 @@ test_expect_success "docker image build output looks good" '
test_fsh cat build-actual
'

test_expect_success "write init scripts" '
echo "ipfs config Foo Bar" > 001.sh &&
echo "ipfs config Baz Qux" > 002.sh &&
chmod +x 002.sh
'

test_expect_success "docker image runs" '
DOC_ID=$(docker run -d -p 127.0.0.1:5001:5001 -p 127.0.0.1:8080:8080 "$IMAGE_ID")
DOC_ID=$(docker run -d \
-p 127.0.0.1:5001:5001 -p 127.0.0.1:8080:8080 \
-v "$PWD/001.sh":/container-init.d/001.sh \
-v "$PWD/002.sh":/container-init.d/002.sh \
"$IMAGE_ID")
'

test_expect_success "docker container gateway is up" '
Expand All @@ -53,6 +63,21 @@ test_expect_success "docker container API is up" '
pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -v -tries 30 -tout 1s
'

test_expect_success "check that init scripts were run correctly and in the correct order" "
echo -e \"Sourcing '/container-init.d/001.sh'...\nExecuting '/container-init.d/002.sh'...\" > expected &&
docker logs $DOC_ID 2>/dev/null | grep -e 001.sh -e 002.sh > actual &&
test_cmp actual expected
"

test_expect_success "check that init script configs were applied" '
echo Bar > expected &&
docker exec "$DOC_ID" ipfs config Foo > actual &&
test_cmp actual expected &&
echo Qux > expected &&
docker exec "$DOC_ID" ipfs config Baz > actual &&
test_cmp actual expected
'

test_expect_success "simple ipfs add/cat can be run in docker container" '
expected="Hello Worlds" &&
HASH=$(docker_exec "$DOC_ID" "echo $(cat expected) | ipfs add | cut -d' ' -f2") &&
Expand Down

0 comments on commit b31c311

Please sign in to comment.