devilbox/.tests/.lib.sh

518 lines
12 KiB
Bash
Raw Normal View History

2016-12-22 19:17:35 +00:00
#!/bin/sh -eu
2016-12-26 21:17:31 +00:00
DEVILBOX_PATH="$( echo "${1}"| sed 's/\/*$//' )" # remove last slash(es): /
2016-12-22 19:17:35 +00:00
2016-12-25 14:30:33 +00:00
################################################################################
#
# H E L P E R
#
################################################################################
run() {
_cmd="${1}"
2016-12-26 21:03:21 +00:00
_debug="0"
2016-12-25 14:30:33 +00:00
_red="\033[0;31m"
_green="\033[0;32m"
_reset="\033[0m"
_user="$(whoami)"
# If 2nd argument is set and enabled, allow debug command
if [ "${#}" = "2" ]; then
if [ "${2}" = "1" ]; then
_debug="1"
fi
fi
if [ "${_debug}" = "1" ]; then
printf "${_red}%s \$ ${_green}${_cmd}${_reset}\n" "${_user}"
fi
sh -c "LANG=C LC_ALL=C ${_cmd}"
}
runsu() {
_cmd="${1}"
_debug="0"
_red="\033[0;31m"
_green="\033[0;32m"
_reset="\033[0m"
_user="$(whoami)"
# If 2nd argument is set and enabled, allow debug command
if [ "${#}" = "2" ]; then
if [ "${2}" = "1" ]; then
_debug="1"
fi
fi
if [ "${_debug}" = "1" ]; then
printf "${_red}%s \$ ${_green}sudo ${_cmd}${_reset}\n" "${_user}"
fi
sudo "LANG=C LC_ALL=C ${_cmd}"
}
2016-12-28 15:39:19 +00:00
print_h1() {
_headline="${1}"
_blue="\033[0;34m"
_reset="\033[0m"
printf "${_blue}%s${_reset}\n" "################################################################################"
printf "${_blue}%s${_reset}\n" "#"
printf "${_blue}%s %s${_reset}\n" "#" "${_headline}"
printf "${_blue}%s${_reset}\n" "#"
printf "${_blue}%s${_reset}\n" "################################################################################"
}
print_h2() {
_headline="${1}"
_blue="\033[0;34m"
_reset="\033[0m"
printf "${_blue}%s${_reset}\n" "############################################################"
printf "${_blue}%s %s${_reset}\n" "#" "${_headline}"
printf "${_blue}%s${_reset}\n" "############################################################"
}
2016-12-27 16:13:27 +00:00
wait_for() {
_time="${1}"
_debug="0"
# Sleep with debug output
if [ "${#}" = "2" ]; then
if [ "${2}" = "1" ]; then
printf "wait "
2017-04-20 09:19:47 +00:00
# shellcheck disable=SC2034
2016-12-27 16:13:27 +00:00
for i in $(seq 1 "${_time}"); do
sleep 1
printf "."
done
printf "\n"
return 0
fi
fi
# Sleep silently
sleep "${_time}"
}
2016-12-22 19:17:35 +00:00
################################################################################
#
# G E T D E F A U L T S
#
################################################################################
###
### Default enabled Docker Versions
###
get_default_version_httpd() {
_default="$( grep -E '^HTTPD_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_default_version_mysql() {
_default="$( grep -E '^MYSQL_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_default_version_postgres() {
_default="$( grep -E '^POSTGRES_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_default_version_php() {
_default="$( grep -E '^PHP_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
echo "${_default}"
}
###
### Default enabled Host Ports
###
get_default_port_httpd() {
_default="$( grep -E '^HOST_PORT_HTTPD=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_default_port_mysql() {
_default="$( grep -E '^HOST_PORT_MYSQL=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_default_port_postgres() {
_default="$( grep -E '^HOST_PORT_POSTGRES=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
echo "${_default}"
}
###
### Default enabled Host Mounts
###
get_default_mount_httpd() {
_default="$( grep -E '^HOST_PATH_TO_WWW_DOCROOTS=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
_prefix="$( echo "${_default}" | cut -c-1 )"
# Relative path?
if [ "${_prefix}" = "." ]; then
_default="$( echo "${_default}" | sed 's/^\.//g' )" # Remove leading dot: .
_default="$( echo "${_default}" | sed 's/^\///' )" # Remove leading slash: /
echo "${DEVILBOX_PATH}/${_default}"
else
echo "${_default}"
fi
}
get_default_mount_mysql() {
_default="$( grep -E '^HOST_PATH_TO_MYSQL_DATADIR=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
_prefix="$( echo "${_default}" | cut -c-1 )"
# Relative path?
if [ "${_prefix}" = "." ]; then
_default="$( echo "${_default}" | sed 's/^\.//g' )" # Remove leading dot: .
_default="$( echo "${_default}" | sed 's/^\///' )" # Remove leading slash: /
echo "${DEVILBOX_PATH}/${_default}"
else
echo "${_default}"
fi
}
get_default_mount_postgres() {
_default="$( grep -E '^HOST_PATH_TO_POSTGRES_DATADIR=' "${DEVILBOX_PATH}/env-example" | sed 's/^.*=//g' )"
_prefix="$( echo "${_default}" | cut -c-1 )"
# Relative path?
if [ "${_prefix}" = "." ]; then
_default="$( echo "${_default}" | sed 's/^\.//g' )" # Remove leading dot: .
_default="$( echo "${_default}" | sed 's/^\///' )" # Remove leading slash: /
echo "${DEVILBOX_PATH}/${_default}"
else
echo "${_default}"
fi
}
2016-12-25 14:16:11 +00:00
################################################################################
#
# G E T E N A B L E D
#
################################################################################
###
### Default enabled Docker Versions
###
get_enabled_version_httpd() {
_default="$( grep -E '^HTTPD_SERVER=' "${DEVILBOX_PATH}/.env" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_enabled_version_mysql() {
_default="$( grep -E '^MYSQL_SERVER=' "${DEVILBOX_PATH}/.env" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_enabled_version_postgres() {
_default="$( grep -E '^POSTGRES_SERVER=' "${DEVILBOX_PATH}/.env" | sed 's/^.*=//g' )"
echo "${_default}"
}
get_enabled_version_php() {
_default="$( grep -E '^PHP_SERVER=' "${DEVILBOX_PATH}/.env" | sed 's/^.*=//g' )"
echo "${_default}"
}
2016-12-22 19:17:35 +00:00
################################################################################
#
# G E T A L L D O C K E R V E R S I O N S
#
################################################################################
###
### All Docker Versions
###
get_all_docker_httpd() {
_all="$( grep -E '^#?HTTPD_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/.*=//g' )"
echo "${_all}"
}
get_all_docker_mysql() {
_all="$( grep -E '^#?MYSQL_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/.*=//g' )"
echo "${_all}"
}
get_all_docker_postgres() {
_all="$( grep -E '^#?POSTGRES_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/.*=//g' )"
echo "${_all}"
}
get_all_docker_php() {
_all="$( grep -E '^#?PHP_SERVER=' "${DEVILBOX_PATH}/env-example" | sed 's/.*=//g' )"
echo "${_all}"
}
################################################################################
#
# S E T / R E S E T F U N C T I O N S
#
################################################################################
###
### Recreate .env file from env-example
###
reset_env_file() {
# Re-create .env file
if [ -f "${DEVILBOX_PATH}/.env" ]; then
rm -f "${DEVILBOX_PATH}/.env"
fi
cp "${DEVILBOX_PATH}/env-example" "${DEVILBOX_PATH}/.env"
}
###
### Comment out all docker versions
###
comment_all_dockers() {
# Comment out all enabled docker versions
2016-12-26 02:54:59 +00:00
run "sed -i'' \"s/^HTTPD_SERVER=/#HTTPD_SERVER=/g\" \"${DEVILBOX_PATH}/.env\""
run "sed -i'' \"s/^MYSQL_SERVER=/#MYSQL_SERVER=/g\" \"${DEVILBOX_PATH}/.env\""
run "sed -i'' \"s/^POSTGRES_SERVER=/#POSTGRES_SERVER=/g\" \"${DEVILBOX_PATH}/.env\""
run "sed -i'' \"s/^PHP_SERVER=/#PHP_SERVER=/g\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
###
### Eenable desired docker version
###
enable_docker_httpd() {
_docker_version="${1}"
2016-12-25 14:30:33 +00:00
run "sed -i'' \"s/#HTTPD_SERVER=${_docker_version}/HTTPD_SERVER=${_docker_version}/g\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
enable_docker_mysql() {
_docker_version="${1}"
2016-12-25 14:30:33 +00:00
run "sed -i'' \"s/#MYSQL_SERVER=${_docker_version}/MYSQL_SERVER=${_docker_version}/g\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
enable_docker_postgres() {
_docker_version="${1}"
2016-12-25 14:30:33 +00:00
run "sed -i'' \"s/#POSTGRES_SERVER=${_docker_version}/POSTGRES_SERVER=${_docker_version}/g\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
enable_docker_php() {
_docker_version="${1}"
2016-12-25 14:30:33 +00:00
run "sed -i'' \"s/#PHP_SERVER=${_docker_version}/PHP_SERVER=${_docker_version}/g\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
set_host_port_httpd() {
_port="${1}"
2016-12-25 14:30:33 +00:00
run "sed -i'' \"s/^HOST_PORT_HTTPD=.*/HOST_PORT_HTTPD=${_port}/\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
set_host_port_mysql() {
_port="${1}"
2016-12-25 14:30:33 +00:00
run "sed -i'' \"s/^HOST_PORT_MYSQL=.*/HOST_PORT_MYSQL=${_port}/\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
set_host_port_pgsql() {
_port="${1}"
2016-12-25 14:30:33 +00:00
run "sed -i'' \"s/^HOST_PORT_POSTGRES=.*/HOST_PORT_POSTGRES=${_port}/\" \"${DEVILBOX_PATH}/.env\""
2016-12-22 19:17:35 +00:00
}
2016-12-26 20:44:04 +00:00
set_debug_enable() {
run "sed -i'' \"s/^DEBUG_COMPOSE_ENTRYPOINT=.*/DEBUG_COMPOSE_ENTRYPOINT=1/\" \"${DEVILBOX_PATH}/.env\""
}
2016-12-22 19:17:35 +00:00
################################################################################
#
# S T A R T / S T O P T H E D E V I L B O X
#
################################################################################
devilbox_start() {
2016-12-25 14:35:30 +00:00
_new_httpd="$1"
_new_mysql="$2"
_new_pysql="$3"
_new_php="$4"
_new_head="$5"
2016-12-22 19:17:35 +00:00
2016-12-25 14:46:02 +00:00
# Print Headline
2016-12-28 15:39:19 +00:00
print_h1 "${_new_head}"
2016-12-22 19:17:35 +00:00
# Adjust .env
comment_all_dockers
2016-12-25 14:35:30 +00:00
enable_docker_httpd "${_new_httpd}"
enable_docker_mysql "${_new_mysql}"
enable_docker_postgres "${_new_pysql}"
enable_docker_php "${_new_php}"
2016-12-22 19:17:35 +00:00
# Run
2016-12-26 20:50:50 +00:00
docker-compose up -d
2016-12-22 19:17:35 +00:00
# Wait for it to come up
wait_for 90 1
2016-12-26 20:50:50 +00:00
# Show log/info
docker-compose logs
2016-12-28 15:39:19 +00:00
#docker-compose ps
2016-12-22 19:17:35 +00:00
}
2016-12-25 15:30:18 +00:00
devilbox_stop() {
# Stop existing dockers
cd "${DEVILBOX_PATH}" || exit 1
2016-12-26 20:44:04 +00:00
docker-compose down > /dev/null 2>&1 || true
docker-compose stop > /dev/null 2>&1 || true
docker-compose kill > /dev/null 2>&1 || true
2016-12-25 15:30:18 +00:00
docker-compose rm -f || true
# Delete existing data dirs
sudo rm -rf "$( get_default_mount_httpd )"
sudo rm -rf "$( get_default_mount_mysql )"
sudo rm -rf "$( get_default_mount_postgres )"
}
2016-12-22 19:17:35 +00:00
2016-12-25 15:30:18 +00:00
################################################################################
#
# T E S T T H E D E V I L B O X
#
################################################################################
2016-12-26 20:44:04 +00:00
2016-12-22 19:17:35 +00:00
debilbox_test() {
2016-12-28 15:50:22 +00:00
###
### Variables
###
_ret=0 # Final exit code
2017-04-20 09:19:47 +00:00
_oks=4 # Require this many [OK]'s on the page
2016-12-28 15:39:19 +00:00
###
### 1. Show Info
###
print_h2 "1. Info"
# Show wanted versions
2016-12-25 14:16:11 +00:00
echo ".env settings"
echo "------------------------------------------------------------"
echo "HTTPD: $(get_enabled_version_httpd)"
echo "PHP: $(get_enabled_version_php)"
echo "MySQL: $(get_enabled_version_mysql)"
echo "PgSQL: $(get_enabled_version_postgres)"
echo
2016-12-28 15:39:19 +00:00
# Get actual versions
2016-12-25 14:16:11 +00:00
echo "http://localhost settings"
echo "------------------------------------------------------------"
curl -q http://localhost/index.php 2>/dev/null | grep -E '<h3>.*</h3>' | sed 's/.*<h3>//g' | sed 's/<\/h3>//g'
2016-12-25 14:16:11 +00:00
echo
2016-12-28 15:39:19 +00:00
###
### 2. Test docker-compose
###
print_h2 "2. docker-compose"
echo "docker-compose ps"
echo "------------------------------------------------------------"
2016-12-28 16:00:21 +00:00
if _test_docker_compose >/dev/null 2>&1; then
echo "[OK]: All running"
else
echo "[ERR]: Broken"
2016-12-28 15:39:19 +00:00
_ret="$(( _ret + 1 ))"
fi
###
### 3. Show Curl output
###
print_h2 "3. Test status via curl"
echo "Count [OK]'s on curl-ed url"
echo "------------------------------------------------------------"
2016-12-28 15:50:22 +00:00
if _cnt="$( _test_curled_oks "${_oks}" )"; then
echo "[OK]: ${_cnt} of ${_oks}"
else
echo "[ERR]: ${_cnt} of ${_oks}"
2016-12-28 15:39:19 +00:00
_ret="$(( _ret + 1 ))"
2016-12-22 19:17:35 +00:00
fi
2016-12-28 15:50:22 +00:00
echo
2016-12-28 15:39:19 +00:00
2016-12-28 15:50:22 +00:00
###
### Final return
###
2016-12-28 16:00:21 +00:00
if [ "${_ret}" != "0" ]; then
print_h2 "4. Error output"
echo "Curl"
echo "------------------------------------------------------------"
curl -vv http://localhost/index.php || true
2016-12-28 16:00:21 +00:00
echo
echo "docker-compose ps"
echo "------------------------------------------------------------"
docker-compose ps
echo
2017-04-21 00:07:45 +00:00
echo "docker-compose logs"
echo "------------------------------------------------------------"
docker-compose logs
echo
echo "log files"
echo "------------------------------------------------------------"
2017-04-21 09:40:19 +00:00
ls -lap log/
sudo find log -type f -exec sh -c 'echo "{}:\n-----------------"; cat "{}"; echo "\n\n"' \;
2017-04-21 00:07:45 +00:00
2016-12-28 16:00:21 +00:00
return 1
2017-04-21 00:07:45 +00:00
2016-12-28 16:00:21 +00:00
fi
return 0
2016-12-22 19:17:35 +00:00
}
2016-12-26 20:44:04 +00:00
2016-12-28 15:39:19 +00:00
###
### Test against stopped containers
###
_test_docker_compose() {
2016-12-28 16:00:21 +00:00
_broken="$( docker-compose ps | grep -c 'Exit' || true )"
_running="$( docker-compose ps | grep -c 'Up' || true )"
_total="$( docker-compose ps -q | grep -c '' || true )"
2016-12-28 15:39:19 +00:00
if [ "${_broken}" != "0" ]; then
return 1
fi
2016-12-28 16:00:21 +00:00
if [ "${_running}" != "${_total}" ]; then
return 1
fi
return 0
2016-12-28 15:50:22 +00:00
}
###
### Test [OK]'s found on website
###
_test_curled_oks() {
_oks="${1}"
max="20"
i=0
while [ $i -lt $max ]; do
if [ "$( curl -s -o /dev/null -w '%{http_code}' http://localhost/index.php )" != "404" ]; then
break;
fi
sleep 1s
i=$(( i + 1 ))
done
# sleep (in case hhvm segfaulted and needs to be restarted)
sleep 10
_count="$( curl -q http://localhost/index.php 2>/dev/null | grep -c 'OK' || true )"
2016-12-28 15:50:22 +00:00
echo "${_count}"
if [ "${_count}" != "${_oks}" ]; then
return 1
else
return 0
fi
2017-04-20 09:19:47 +00:00
}