Skip to content

Commit

Permalink
Add sh/ script files
Browse files Browse the repository at this point in the history
  • Loading branch information
JonJagger committed Mar 18, 2020
1 parent ca843b0 commit b9892f8
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 0 deletions.
119 changes: 119 additions & 0 deletions sh/api_demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash -Eeu

readonly SH_DIR="$(cd "$(dirname "${0}")" && pwd)"
source "${SH_DIR}/versioner_env_vars.sh" # for build-image
export $(versioner_env_vars)
source "${SH_DIR}/ip_address.sh" # slow
readonly IP_ADDRESS="$(ip_address)"

#- - - - - - - - - - - - - - - - - - - - - - - - - - -
main()
{
"${SH_DIR}/build_images.sh"
"${SH_DIR}/containers_up.sh" api-demo
echo
demo
echo
if [ "${1:-}" == '--no-browser' ]; then
"${SH_DIR}/containers_down.sh"
else
open "http://${IP_ADDRESS}:80/languages-chooser/group_choose"
fi
}

#- - - - - - - - - - - - - - - - - - - - - - - - - - -
demo()
{
echo API
curl_json_body_200 GET alive
curl_json_body_200 GET ready
curl_json_body_200 GET sha
echo
curl_200 GET assets/app.css 'Content-Type: text/css'
echo
curl_200 GET language_choose exercise
#curl_params_302 GET group_create "$(params_display_names)"
#curl_json_body_200 POST group_create "$(json_display_names)"
echo
curl_200 GET language_choose exercise
#curl_params_302 GET kata_create "$(params_display_name)"
#curl_json_body_200 POST kata_create "$(json_display_name)"
}

#- - - - - - - - - - - - - - - - - - - - - - - - - - -
curl_json_body_200()
{
local -r log=/tmp/creator.log
local -r type="${1}" # eg GET|POST
local -r route="${2}" # eg create_group
local -r json="${3:-}" # eg '{"display_name":"Java Countdown, Round 1"}'
curl \
--data "${json}" \
--fail \
--header 'Content-type: application/json' \
--header 'Accept: application/json' \
--request "${type}" \
--silent \
--verbose \
"http://${IP_ADDRESS}:$(port)/${route}" \
> "${log}" 2>&1

grep --quiet 200 "${log}" # eg HTTP/1.1 200 OK
local -r result=$(tail -n 1 "${log}") # eg {"sha":"78c19640aa43ea214da17d0bcb16abbd420d7642"}
echo "$(tab)${type} ${route} => 200 ${result}"
}

#- - - - - - - - - - - - - - - - - - - - - - - - - - -
curl_params_302()
{
local -r log=/tmp/creator.log
local -r type="${1}" # eg GET|POST
local -r route="${2}" # eg create_kata
local -r params="${3:-}" # eg "display_name=Java Countdown, Round 1"
curl \
--data-urlencode "${params}" \
--fail \
--request "${type}" \
--silent \
--verbose \
"http://${IP_ADDRESS}:$(port)/${route}" \
> "${log}" 2>&1

grep --quiet 302 "${log}" # eg HTTP/1.1 302 Moved Temporarily
local -r result=$(grep Location "${log}") # Location: http://192.168.99.100:4536/kata/edit/5B65RC
echo "$(tab)${type} ${route} => 302 ${result}"
}

#- - - - - - - - - - - - - - - - - - - - - - - - - - -
curl_200()
{
local -r log=/tmp/creator.log
local -r type="${1}" # eg GET|POST
local -r route="${2}" # eg index_kata
local -r pattern="${3}" # eg session
curl \
--fail \
--request "${type}" \
--silent \
--verbose \
"http://${IP_ADDRESS}:$(port)/${route}" \
> "${log}" 2>&1

grep --quiet 200 "${log}" # eg HTTP/1.1 200 OK
local -r result=$(grep "${pattern}" "${log}")
echo "$(tab)${type} ${route} => 200 ${result}"
}

#- - - - - - - - - - - - - - - - - - - - - - - - - - -
port() { echo -n "${CYBER_DOJO_LANGUAGES_CHOOSER_PORT}"; }
json_display_names() { echo -n "{\"display_names\":[\"$(display_name)\"]}"; }
params_display_names() { params display_names[] "$(display_name)"; }
json_display_name() { json display_name "$(display_name)"; }
params_display_name() { params display_name "$(display_name)"; }
json() { echo -n "{\"${1}\":\"${2}\"}"; }
params() { echo -n "${1}=${2}"; }
display_name() { echo -n 'C (gcc), assert'; }
tab() { printf '\t'; }

#- - - - - - - - - - - - - - - - - - - - - - - - - - -
main "${1:-}"
41 changes: 41 additions & 0 deletions sh/build_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash -Eeu

readonly ROOT_DIR="$(cd "$(dirname "${0}")/.." && pwd)"

#- - - - - - - - - - - - - - - - - - - - - - - -
build_images()
{
docker-compose \
--file "${ROOT_DIR}/docker-compose.yml" \
build \
--build-arg COMMIT_SHA="$(git_commit_sha)"
}

# - - - - - - - - - - - - - - - - - - - - - - - -
git_commit_sha()
{
echo $(cd "${ROOT_DIR}" && git rev-parse HEAD)
}

# - - - - - - - - - - - - - - - - - - - - - - - -
assert_equal()
{
local -r name="${1}"
local -r expected="${2}"
local -r actual="${3}"
if [ "${expected}" != "${actual}" ]; then
echo "ERROR: unexpected ${name} inside image"
echo "expected: ${name}='${expected}'"
echo " actual: ${name}='${actual}'"
exit 42
fi
}

#- - - - - - - - - - - - - - - - - - - - - - - -
readonly SH_DIR="${ROOT_DIR}/sh"
source ${SH_DIR}/versioner_env_vars.sh
export $(versioner_env_vars)
source ${SH_DIR}/image_sha.sh

build_images
assert_equal SHA "$(git_commit_sha)" "$(image_sha)"
9 changes: 9 additions & 0 deletions sh/containers_down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash -Eeu

readonly ROOT_DIR="$(cd "$(dirname "${0}")/.." && pwd)"

docker-compose \
--file "${ROOT_DIR}/docker-compose.yml" \
down \
--remove-orphans \
--volumes
151 changes: 151 additions & 0 deletions sh/containers_up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/bin/bash -Eeu

readonly ROOT_DIR="$(cd "$(dirname "${0}")/.." && pwd)"
source "${ROOT_DIR}/sh/ip_address.sh"
readonly IP_ADDRESS=$(ip_address) # slow

# - - - - - - - - - - - - - - - - - - - - - -
wait_briefly_until_ready()
{
local -r port="${1}"
local -r name="${2}"
local -r max_tries=40
printf "Waiting until ${name} is ready"
for _ in $(seq ${max_tries}); do
if curl_ready ${port}; then
printf '.OK\n'
return
else
printf .
sleep 0.1
fi
done
printf 'FAIL\n'
printf "${name} not ready after ${max_tries} tries\n"
if [ -f "$(ready_filename)" ]; then
printf "$(ready_response)\n"
else
printf "$(ready_filename) does not exist?!\n"
fi
docker logs ${name}
exit 42
}

# - - - - - - - - - - - - - - - - - - -
curl_ready()
{
local -r port="${1}"
local -r path=ready?
local -r url="http://${IP_ADDRESS}:${port}/${path}"
rm -f $(ready_filename)
curl \
--fail \
--output $(ready_filename) \
--request GET \
--silent \
"${url}"

[ "$?" == '0' ] && [ "$(ready_response)" == '{"ready?":true}' ]
}

# - - - - - - - - - - - - - - - - - - -
ready_response()
{
cat "$(ready_filename)"
}

# - - - - - - - - - - - - - - - - - - -
ready_filename()
{
printf /tmp/curl-languages-chooser-ready-output
}

# - - - - - - - - - - - - - - - - - - -
strip_known_warning()
{
local -r log="${1}"
local -r known_warning="${2}"
local stripped=$(printf "${log}" | grep --invert-match -E "${known_warning}")
if [ "${log}" != "${stripped}" ]; then
>&2 echo "SERVICE START-UP WARNING: ${known_warning}"
else
>&2 echo "DID _NOT_ FIND WARNING!!: ${known_warning}"
fi
echo "${stripped}"
}

# - - - - - - - - - - - - - - - - - - -
exit_if_unclean()
{
local -r container_name="${1}"
local log=$(docker logs "${container_name}" 2>&1)

local -r mismatched_indent_warning="application(.*): warning: mismatched indentations at 'rescue' with 'begin'"
log=$(strip_known_warning "${log}" "${mismatched_indent_warning}")

printf "Checking ${container_name} started cleanly..."
local -r line_count=$(echo -n "${log}" | grep -c '^')
# 3 lines on Thin (Unicorn=6, Puma=6)
#Thin web server (v1.7.2 codename Bachmanity)
#Maximum connections set to 1024
#Listening on 0.0.0.0:4536, CTRL+C to stop
if [ "${line_count}" == '3' ]; then
printf 'OK\n'
else
printf 'FAIL\n'
print_docker_log "${container_name}" "${log}"
exit 42
fi
}

# - - - - - - - - - - - - - - - - - - -
print_docker_log()
{
local -r container_name="${1}"
local -r log="${2}"
printf "[docker logs ${container_name}]\n"
printf '<docker_log>\n'
printf "${log}\n"
printf '</docker_log>\n'
}

# - - - - - - - - - - - - - - - - - - -
container_up_ready_and_clean()
{
local -r port="${1}"
local -r service_name="${2}"
local -r container_name="test-${service_name}"
container_up "${port}" "${service_name}"
wait_briefly_until_ready "${port}" "${container_name}"
exit_if_unclean "${container_name}"
}

# - - - - - - - - - - - - - - - - - - -
container_up()
{
local -r port="${1}"
local -r service_name="${2}"
local -r container_name="test-${service_name}"
printf '\n'
export NO_PROMETHEUS=true
docker-compose \
--file "${ROOT_DIR}/docker-compose.yml" \
up \
--detach \
--force-recreate \
"${service_name}"
}

# - - - - - - - - - - - - - - - - - - -

if [ "${1:-}" == 'api-demo' ]; then
container_up 80 nginx
wait_briefly_until_ready ${CYBER_DOJO_LANGUAGES_CHOOSER_PORT} languages-chooser-server
fi

if [ "${1:-}" == 'server' ]; then
container_up_ready_and_clean ${CYBER_DOJO_LANGUAGES_CHOOSER_PORT} languages-chooser-server
else
container_up 80 nginx
container_up_ready_and_clean ${CYBER_DOJO_LANGUAGES_CHOOSER_CLIENT_PORT} languages-chooser-client
fi
6 changes: 6 additions & 0 deletions sh/image_name.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -Eeu

image_name()
{
echo "${CYBER_DOJO_LANGUAGES_CHOOSER_IMAGE}"
}
6 changes: 6 additions & 0 deletions sh/image_sha.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -Eeu

image_sha()
{
docker run --rm ${CYBER_DOJO_LANGUAGES_CHOOSER_IMAGE}:latest sh -c 'echo ${SHA}'
}
10 changes: 10 additions & 0 deletions sh/ip_address.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash -Eeu

ip_address()
{
if [ ! -z "${DOCKER_MACHINE_NAME:-}" ]; then
docker-machine ip "${DOCKER_MACHINE_NAME}"
else
echo localhost
fi
}
Loading

0 comments on commit b9892f8

Please sign in to comment.