Skip to content

Commit

Permalink
Merge pull request #4 from docksal/develop
Browse files Browse the repository at this point in the history
Release 1.2.1
  • Loading branch information
lmakarov committed Nov 14, 2018
2 parents 0108e1e + b3a4541 commit 81460ff
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 26 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ sudo: required
language: generic

env:
REPO: docksal/ssh-agent
IMAGE_DNS: ${REPO}:dev
DOCKSAL_VERSION: feature/ssh-key
DOCKSAL_VERSION: develop

services:
- docker

install:
# Install Docksal to have a matching versions of Docker on the build host
- curl -fsSL https://get.docksal.io | DOCKSAL_VERSION=${DOCKSAL_VERSION} bash
- curl -fsSL https://get.docksal.io | bash
- fin version
- fin sysinfo

Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apk add --no-cache \
&& rm -rf /var/cache/apk/*

COPY bin /usr/local/bin
COPY healthcheck.sh /opt/healthcheck.sh

ENV SSH_DIR /.ssh
ENV SOCKET_DIR /.ssh-agent
Expand All @@ -18,3 +19,6 @@ VOLUME ${SOCKET_DIR}
ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["ssh-agent"]

# Health check script
HEALTHCHECK --interval=5s --timeout=1s --retries=3 CMD ["/opt/healthcheck.sh"]
31 changes: 13 additions & 18 deletions bin/ssh-key
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,22 @@ ssh_key_loaded ()

ssh_key_add ()
{
ssh_key_name="${1}"
ssh_key_path="${SSH_DIR}/${ssh_key_name}"

# Make sure the key file was provided and exists
[[ "${ssh_key_name}" == "" ]] && echo 'Missing SSH key file name' >&2 && return 1
[[ ! -f "${ssh_key_path}" ]] && echo "SSH key file does not exist in '${ssh_key_path}'" >&2 && return 1

# Fix permissions on keys before trying to add them to the agent
chmod 700 ${SSH_DIR}
chmod 600 ${SSH_DIR}/* >/dev/null 2>&1 || true
chmod 644 ${SSH_DIR}/*.pub >/dev/null 2>&1 || true

# Make sure the key exists if provided.
# Otherwise we may be getting an argumet, which we'll handle late.
# When $ssh_key_path is empty, ssh-agent will be looking for both id_rsa and id_dsa in the home directory.
if [[ "${1}" != "" ]] && [[ -f "${SSH_DIR}/${1}" ]]; then
ssh_key_name="${1}"
ssh_key_path="${SSH_DIR}/${ssh_key_name}"

# Check whether the key is already loaded in the agent and skip adding if so.
if ssh_key_loaded ${ssh_key_name}; then
echo "Key '${ssh_key_name}' already loaded in the agent. Skipping."
return 0
fi
# Check whether the key is already loaded in the agent and skip adding if so.
if ssh_key_loaded ${ssh_key_name}; then
echo "Key '${ssh_key_name}' already loaded in the agent. Skipping."
return 0
fi

# Calling ssh-add. This should handle all arguments cases.
Expand All @@ -54,7 +53,7 @@ ssh_key_add ()
ret=${PIPESTATUS[0]}

# Remove the key immediately
rm -f /.ssh/${ssh_key_name}
rm -f ${ssh_key_path}

# Return the exit code from ssh-add above
return ${ret}
Expand Down Expand Up @@ -94,14 +93,10 @@ case "$1" in
shift
ssh_key_list "$@"
;;
new)
shift
ssh_key_list "$@"
;;
debug)
shift
eval "$@"
;;
*)
echo "Usage: $0 add|rm|ls|new"
echo "Usage: ssh-key add|rm|ls"
esac
6 changes: 6 additions & 0 deletions healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

netstat -nlp | grep -E "LISTENING.*${SSH_AUTH_PROXY_SOCK}" >/dev/null || exit 1
netstat -nlp | grep -E "LISTENING.*${SSH_AUTH_SOCK}" >/dev/null || exit 1

exit 0
2 changes: 1 addition & 1 deletion scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [[ "${TRAVIS_PULL_REQUEST}" == "false" ]]; then

if [[ "$TAG" != "" ]]; then
docker login -u "${DOCKER_USER}" -p "${DOCKER_PASS}"
docker tag ${IMAGE_DNS} ${REPO}:${TAG}
docker tag ${REPO}:dev ${REPO}:${TAG}
docker push ${REPO}:${TAG}
fi;
fi;
54 changes: 51 additions & 3 deletions tests/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,62 @@ teardown() {
echo "================================================================"
}

# Checks container health status (if available)
# @param $1 container id/name
_healthcheck ()
{
local health_status
health_status=$(docker inspect --format='{{json .State.Health.Status}}' "$1" 2>/dev/null)

# Wait for 5s then exit with 0 if a container does not have a health status property
# Necessary for backward compatibility with images that do not support health checks
if [[ $? != 0 ]]; then
echo "Waiting 10s for container to start..."
sleep 10
return 0
fi

# If it does, check the status
echo $health_status | grep '"healthy"' >/dev/null 2>&1
}

# Waits for containers to become healthy
# For reasoning why we are not using `depends_on` `condition` see here:
# https://github.com/docksal/docksal/issues/225#issuecomment-306604063
_healthcheck_wait ()
{
# Wait for cli to become ready by watching its health status
local container_name="${NAME}"
local delay=5
local timeout=30
local elapsed=0

until _healthcheck "$container_name"; do
echo "Waiting for $container_name to become ready..."
sleep "$delay";

# Give the container 30s to become ready
elapsed=$((elapsed + delay))
if ((elapsed > timeout)); then
echo-error "$container_name heathcheck failed" \
"Container did not enter a healthy state within the expected amount of time." \
"Try ${yellow}fin restart${NC}"
exit 1
fi
done

return 0
}

# To work on a specific test:
# run `export SKIP=1` locally, then comment skip in the test you want to debug

@test "ssh-agent container is up and using the \"${IMAGE}\" image" {
@test "${NAME} container is up and using the \"${IMAGE}\" image" {
[[ ${SKIP} == 1 ]] && skip
_healthcheck_wait

run docker ps --filter "name=docksal-ssh-agent" --format "{{ .Image }}"
[[ "$output" =~ "$IMAGE" ]]
run docker ps --filter "name=${NAME}" --format "{{ .Image }}"
[[ "$output" =~ "${IMAGE}" ]]
unset output
}

Expand Down

0 comments on commit 81460ff

Please sign in to comment.