Skip to content

Commit

Permalink
Launcher: stop che-server cleanly (fix eclipse-che#1949) (eclipse-che…
Browse files Browse the repository at this point in the history
…#1956)

Signed-off-by: Mario Loriedo <[email protected]>
  • Loading branch information
l0rd authored and ShimonBenYair committed Aug 4, 2016
1 parent bc17594 commit a84f71f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 201 deletions.
195 changes: 0 additions & 195 deletions dockerfiles/che-launcher/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,201 +87,6 @@ Docs: http://eclipse.org/che/getting-started.
"
}

usage () {
printf "%s" "${USAGE}"
}

info() {
printf "${GREEN}INFO:${NC} %s\n" "${1}"
}

debug() {
printf "${BLUE}DEBUG:${NC} %s\n" "${1}"
}

error() {
printf "${RED}ERROR:${NC} %s\n" "${1}"
}

error_exit() {
echo "---------------------------------------"
error "!!!"
error "!!! ${1}"
error "!!!"
echo "---------------------------------------"
exit 1
}

print_debug_info() {
debug "---------------------------------------"
debug "--------- CHE DEBUG INFO -----------"
debug "---------------------------------------"
debug ""
debug "DOCKER_INSTALL_TYPE = ${DOCKER_INSTALL_TYPE}"
debug ""
debug "CHE_SERVER_CONTAINER_NAME = ${CHE_SERVER_CONTAINER_NAME}"
debug "CHE_SERVER_IMAGE_NAME = ${CHE_SERVER_IMAGE_NAME}"
debug ""
VAL=$(if che_container_exist;then echo "YES"; else echo "NO"; fi)
debug "CHE CONTAINER EXISTS? ${VAL}"
VAL=$(if che_container_is_running;then echo "YES"; else echo "NO"; fi)
debug "CHE CONTAINER IS RUNNING? ${VAL}"
VAL=$(if che_container_is_stopped;then echo "YES"; else echo "NO"; fi)
debug "CHE CONTAINER IS STOPPED? ${VAL}"
VAL=$(if server_is_booted;then echo "YES"; else echo "NO"; fi)
debug "CHE SERVER IS BOOTED? ${VAL}"
debug ""
debug "CHE_PORT = ${CHE_PORT}"
debug "CHE_VERSION = ${CHE_VERSION}"
debug "CHE_RESTART_POLICY = ${CHE_RESTART_POLICY}"
debug "CHE_USER = ${CHE_USER}"
debug "CHE_HOST_IP = ${CHE_HOST_IP}"
debug "CHE_LOG_LEVEL = ${CHE_LOG_LEVEL}"
debug "CHE_HOSTNAME = ${CHE_HOSTNAME}"
debug "CHE_DATA_FOLDER = ${CHE_DATA_FOLDER}"
debug "CHE_CONF_FOLDER = ${CHE_CONF_FOLDER:-not set}"
debug "CHE_LOCAL_BINARY = ${CHE_LOCAL_BINARY:-not set}"
debug ""
debug "---------------------------------------"
debug "---------------------------------------"
debug "---------------------------------------"
}

get_che_launcher_container_id() {
hostname
}

get_che_launcher_version() {
LAUNCHER_CONTAINER_ID=$(get_che_launcher_container_id)
LAUNCHER_IMAGE_NAME=$(docker inspect --format='{{.Config.Image}}' "${LAUNCHER_CONTAINER_ID}")
echo "${LAUNCHER_IMAGE_NAME}" | cut -d : -f2
}

is_boot2docker() {
if uname -r | grep -q 'boot2docker'; then
return 0
else
return 1
fi
}

has_docker_for_windows_ip() {
DOCKER_HOST_IP=$(get_docker_host_ip)
if [ "${DOCKER_HOST_IP}" = "10.0.75.2" ]; then
return 0
else
return 1
fi
}

is_docker_for_mac() {
if uname -r | grep -q 'moby' && ! has_docker_for_windows_ip; then
return 0
else
return 1
fi
}

is_docker_for_windows() {
if uname -r | grep -q 'moby' && has_docker_for_windows_ip; then
return 0
else
return 1
fi
}

get_docker_install_type() {
if is_boot2docker; then
echo "boot2docker"
elif is_docker_for_windows; then
echo "docker4windows"
elif is_docker_for_mac; then
echo "docker4mac"
else
echo "native"
fi
}

get_docker_host_ip() {
NETWORK_IF="eth0"
if is_boot2docker; then
NETWORK_IF="eth1"
fi

docker run --rm --net host \
alpine sh -c \
"ip a show ${NETWORK_IF}" | \
grep 'inet ' | \
cut -d/ -f1 | \
awk '{ print $2}'
}

get_che_hostname() {
INSTALL_TYPE=$(get_docker_install_type)
if [ "${INSTALL_TYPE}" = "boot2docker" ] ||
[ "${INSTALL_TYPE}" = "docker4windows" ]; then
get_docker_host_ip
else
echo "localhost"
fi
}

check_docker() {
if [ ! -S /var/run/docker.sock ]; then
error_exit "Docker socket (/var/run/docker.sock) hasn't be mounted \
inside the container. Verify the syntax of the \"docker run\" command."
fi

if ! docker ps > /dev/null 2>&1; then
output=$(docker ps)
error_exit "Error when running \"docker ps\": ${output}"
fi
}

che_container_exist() {
if [ "$(docker ps -aq -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" = "0" ]; then
return 1
else
return 0
fi
}

che_container_is_running() {
if [ "$(docker ps -qa -f "status=running" -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" = "0" ]; then
return 1
else
return 0
fi
}

che_container_is_stopped() {
if [ "$(docker ps -qa -f "status=exited" -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" = "0" ]; then
return 1
else
return 0
fi
}

wait_until_container_is_running() {
CONTAINER_START_TIMEOUT=${1}

ELAPSED=0
until che_container_is_running || [ ${ELAPSED} -eq "${CONTAINER_START_TIMEOUT}" ]; do
sleep 1
ELAPSED=$((ELAPSED+1))
done
}

server_is_booted() {
HTTP_STATUS_CODE=$(curl -I http://"${CHE_HOST_IP}":"${CHE_PORT}"/api/ \
-s -o /dev/null --write-out "%{http_code}")
if [ "${HTTP_STATUS_CODE}" = "200" ]; then
return 0
else
return 1
fi
}

parse_command_line () {
if [ $# -eq 0 ]; then
usage
Expand Down
10 changes: 7 additions & 3 deletions dockerfiles/che-launcher/launcher_cmds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ stop_che_server() {
info "-------------------------------------------------------"
else
info "ECLIPSE CHE: STOPPING SERVER..."
docker exec ${CHE_SERVER_CONTAINER_NAME} /home/user/che/bin/che.sh -c stop > /dev/null 2>&1
sleep 5
docker exec ${CHE_SERVER_CONTAINER_NAME} /home/user/che/bin/che.sh -c -s:uid stop > /dev/null
wait_until_container_is_stopped 60
if che_container_is_running; then
error_exit "ECLIPSE CHE: Timeout waiting Che container to stop."
fi

info "ECLIPSE CHE: REMOVING CONTAINER"
docker rm -f ${CHE_SERVER_CONTAINER_NAME} > /dev/null 2>&1
docker rm ${CHE_SERVER_CONTAINER_NAME} > /dev/null
info "ECLIPSE CHE: STOPPED"
fi
}
Expand Down
16 changes: 13 additions & 3 deletions dockerfiles/che-launcher/launcher_funcs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,23 @@ inside the container. Verify the syntax of the \"docker run\" command."
}

che_container_exist() {
if [ "$(docker ps -aq -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" = "0" ]; then
if [ "$(docker ps -aq -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" -eq 0 ]; then
return 1
else
return 0
fi
}

che_container_is_running() {
if [ "$(docker ps -qa -f "status=running" -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" = "0" ]; then
if [ "$(docker ps -qa -f "status=running" -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" -eq 0 ]; then
return 1
else
return 0
fi
}

che_container_is_stopped() {
if [ "$(docker ps -qa -f "status=exited" -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" = "0" ]; then
if [ "$(docker ps -qa -f "status=exited" -f "name=${CHE_SERVER_CONTAINER_NAME}" | wc -l)" -eq 0 ]; then
return 1
else
return 0
Expand All @@ -173,6 +173,16 @@ wait_until_container_is_running() {
done
}

wait_until_container_is_stopped() {
CONTAINER_STOP_TIMEOUT=${1}

ELAPSED=0
until che_container_is_stopped || [ ${ELAPSED} -eq "${CONTAINER_STOP_TIMEOUT}" ]; do
sleep 1
ELAPSED=$((ELAPSED+1))
done
}

server_is_booted() {
HTTP_STATUS_CODE=$(curl -I http://"${CHE_HOST_IP}":"${CHE_PORT}"/api/ \
-s -o /dev/null --write-out "%{http_code}")
Expand Down
18 changes: 18 additions & 0 deletions dockerfiles/che-launcher/launcher_test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,21 @@ source ./launcher_funcs.sh
result="$(get_clean_path \"/some\"/path\")"
[ "$result" = "/some/path" ]
}

@test "wait for che container that never stops" {
export CHE_SERVER_CONTAINER_NAME="wait-for-che-test1"
docker run -d --name ${CHE_SERVER_CONTAINER_NAME} alpine:3.4 ping localhost
wait_until_container_is_stopped 2
run che_container_is_stopped
docker rm -f ${CHE_SERVER_CONTAINER_NAME}
[ "$status" -eq 1 ]
}

@test "wait for che container that stops" {
export CHE_SERVER_CONTAINER_NAME="wait-for-che-test2"
docker run -d --name ${CHE_SERVER_CONTAINER_NAME} alpine:3.4 ping -c 2 localhost
wait_until_container_is_stopped 3
run che_container_is_stopped
docker rm -f ${CHE_SERVER_CONTAINER_NAME}
[ "$status" -eq 0 ]
}

0 comments on commit a84f71f

Please sign in to comment.