Commit f6173497 authored by Ivan Mazhukin's avatar Ivan Mazhukin

cleanup; add color output

parent d259f1f6
...@@ -3,9 +3,7 @@ ...@@ -3,9 +3,7 @@
set -euo pipefail set -euo pipefail
SCRIPT_REF="${BASH_SOURCE[0]-$0}" SCRIPT_REF="${BASH_SOURCE[0]-$0}"
SCRIPT_NAME="$(basename "$SCRIPT_REF")"
SCRIPT_PATH="$(realpath "$SCRIPT_REF" 2>/dev/null || printf '%s\n' "$SCRIPT_REF")" SCRIPT_PATH="$(realpath "$SCRIPT_REF" 2>/dev/null || printf '%s\n' "$SCRIPT_REF")"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
DEFAULT_REMOTE_HOST="${EPM_DOCKER_TEST_REMOTE_HOST:-builder64}" DEFAULT_REMOTE_HOST="${EPM_DOCKER_TEST_REMOTE_HOST:-builder64}"
DEFAULT_REMOTE_USER="${EPM_DOCKER_TEST_REMOTE_USER:-builder-robot}" DEFAULT_REMOTE_USER="${EPM_DOCKER_TEST_REMOTE_USER:-builder-robot}"
DEFAULT_LOG_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}/epm-docker-test" DEFAULT_LOG_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}/epm-docker-test"
...@@ -28,6 +26,20 @@ REMOTE_ARGS=() ...@@ -28,6 +26,20 @@ REMOTE_ARGS=()
REMOTE_SYNC_DIR="" REMOTE_SYNC_DIR=""
RUN_TOKEN="$(date +%Y%m%d-%H%M%S)-$$" RUN_TOKEN="$(date +%Y%m%d-%H%M%S)-$$"
COLOR_RESET=""
COLOR_WARN=""
COLOR_ERROR=""
COLOR_LOG_FATAL=""
COLOR_LOG_ERROR=""
if [[ -t 2 && -z "${NO_COLOR:-}" ]]; then
COLOR_RESET=$'\033[0m'
COLOR_WARN=$'\033[33m'
COLOR_ERROR=$'\033[31m'
COLOR_LOG_FATAL=$'\033[31m'
COLOR_LOG_ERROR=$'\033[33m'
fi
usage() { usage() {
cat <<'EOF' cat <<'EOF'
Usage: Usage:
...@@ -60,11 +72,11 @@ info() { ...@@ -60,11 +72,11 @@ info() {
} }
warn() { warn() {
printf '[warn] %s\n' "$*" >&2 printf '%s[warn]%s %s\n' "$COLOR_WARN" "$COLOR_RESET" "$*" >&2
} }
fatal() { fatal() {
printf '[error] %s\n' "$*" >&2 printf '%s[error]%s %s\n' "$COLOR_ERROR" "$COLOR_RESET" "$*" >&2
exit 1 exit 1
} }
...@@ -73,8 +85,9 @@ require_command() { ...@@ -73,8 +85,9 @@ require_command() {
command -v "$cmd" >/dev/null 2>&1 || fatal "Required command not found: $cmd" command -v "$cmd" >/dev/null 2>&1 || fatal "Required command not found: $cmd"
} }
slugify() { sanitize_name() {
local value="${1,,}" local value="${1,,}"
#TODO сделать в одну строку и убрать башизм.
value="${value//[^a-z0-9]/-}" value="${value//[^a-z0-9]/-}"
value="${value##-}" value="${value##-}"
value="${value%%-}" value="${value%%-}"
...@@ -84,7 +97,7 @@ slugify() { ...@@ -84,7 +97,7 @@ slugify() {
normalize_system_name() { normalize_system_name() {
local raw="$1" local raw="$1"
local normalized local normalized
#TODO сделать в одну строку и убрать башизм.
normalized="${raw// /}" normalized="${raw// /}"
normalized="${normalized,,}" normalized="${normalized,,}"
normalized="${normalized//\//:}" normalized="${normalized//\//:}"
...@@ -108,34 +121,6 @@ can_use_local_docker() { ...@@ -108,34 +121,6 @@ can_use_local_docker() {
docker info >/dev/null 2>&1 docker info >/dev/null 2>&1
} }
user_home_dir() {
local user="$1"
local passwd_line
if command -v getent >/dev/null 2>&1; then
passwd_line="$(getent passwd "$user" || true)"
if [[ -n "$passwd_line" ]]; then
printf '%s\n' "${passwd_line##*:}"
return 0
fi
fi
printf '/home/%s\n' "$user"
}
project_suffix_from_pwd() {
local cwd marker
cwd="$(pwd -P)"
marker="/Projects/"
if [[ "$cwd" == *"$marker"* ]]; then
printf '%s\n' "${cwd#*"$marker"}"
return 0
fi
basename "$cwd"
}
verify_eepm_tree() { verify_eepm_tree() {
local tree="$1" local tree="$1"
local epm_path local epm_path
...@@ -161,7 +146,9 @@ resolve_local_source_path() { ...@@ -161,7 +146,9 @@ resolve_local_source_path() {
candidate="${SOURCE_PATH:-$(pwd -P)}" candidate="${SOURCE_PATH:-$(pwd -P)}"
candidate="$(realpath "$candidate")" candidate="$(realpath "$candidate")"
verify_eepm_tree "$candidate" verify_eepm_tree "$candidate"
printf '%s\n' "$candidate" printf '%s\n' "$candidate"
} }
...@@ -170,6 +157,7 @@ resolve_explicit_source_path() { ...@@ -170,6 +157,7 @@ resolve_explicit_source_path() {
[[ -n "$candidate" ]] || fatal "Explicit source path is empty" [[ -n "$candidate" ]] || fatal "Explicit source path is empty"
candidate="$(realpath "$candidate" 2>/dev/null || printf '%s\n' "$candidate")" candidate="$(realpath "$candidate" 2>/dev/null || printf '%s\n' "$candidate")"
verify_eepm_tree "$candidate" verify_eepm_tree "$candidate"
printf '%s\n' "$candidate" printf '%s\n' "$candidate"
} }
...@@ -179,6 +167,7 @@ default_builder_source_user() { ...@@ -179,6 +167,7 @@ default_builder_source_user() {
current_user="$(id -un)" current_user="$(id -un)"
source_user="${BUILDER_USER:-$current_user}" source_user="${BUILDER_USER:-$current_user}"
printf '%s\n' "$source_user" printf '%s\n' "$source_user"
} }
...@@ -186,6 +175,7 @@ builder_source_candidates() { ...@@ -186,6 +175,7 @@ builder_source_candidates() {
local source_user local source_user
source_user="$(default_builder_source_user)" source_user="$(default_builder_source_user)"
printf '/srv/%s/Projects/eepm\n' "$source_user" printf '/srv/%s/Projects/eepm\n' "$source_user"
} }
...@@ -279,9 +269,9 @@ remote_sync_dir_for_source() { ...@@ -279,9 +269,9 @@ remote_sync_dir_for_source() {
local_user="$(id -un)" local_user="$(id -un)"
base_name="$(basename "$source_dir")" base_name="$(basename "$source_dir")"
printf '/tmp/epm-docker-test-sync/%s-%s-%s\n' \ printf '/tmp/epm-docker-test-sync/%s-%s-%s\n' \
"$(slugify "$local_user")" \ "$(sanitize_name "$local_user")" \
"$(slugify "$base_name")" \ "$(sanitize_name "$base_name")" \
"$(slugify "$RUN_TOKEN")" "$(sanitize_name "$RUN_TOKEN")"
} }
sync_local_source_to_remote() { sync_local_source_to_remote() {
...@@ -332,8 +322,8 @@ create_log_file() { ...@@ -332,8 +322,8 @@ create_log_file() {
mkdir -p "$LOG_ROOT" || fatal "Could not create log root: $LOG_ROOT" mkdir -p "$LOG_ROOT" || fatal "Could not create log root: $LOG_ROOT"
fi fi
safe_app="$(slugify "$APP_NAME")" safe_app="$(sanitize_name "$APP_NAME")"
safe_system="$(slugify "$SYSTEM_IMAGE")" safe_system="$(sanitize_name "$SYSTEM_IMAGE")"
timestamp="$(date +%Y%m%d-%H%M%S)" timestamp="$(date +%Y%m%d-%H%M%S)"
log_name="${safe_app}-${safe_system}-${timestamp}.log" log_name="${safe_app}-${safe_system}-${timestamp}.log"
printf '%s\n' "$LOG_ROOT/$log_name" printf '%s\n' "$LOG_ROOT/$log_name"
...@@ -341,6 +331,7 @@ create_log_file() { ...@@ -341,6 +331,7 @@ create_log_file() {
print_failure_excerpt() { print_failure_excerpt() {
local matches local matches
local line
[[ -f "$LOG_FILE" ]] || { [[ -f "$LOG_FILE" ]] || {
printf '\nNo log file was captured.\n' >&2 printf '\nNo log file was captured.\n' >&2
return 0 return 0
...@@ -349,7 +340,19 @@ print_failure_excerpt() { ...@@ -349,7 +340,19 @@ print_failure_excerpt() {
printf '\nCritical log lines:\n' >&2 printf '\nCritical log lines:\n' >&2
matches="$(grep -nEi 'critical|fatal|error|failed|traceback|no such|permission denied' "$LOG_FILE" | tail -n 20 || true)" matches="$(grep -nEi 'critical|fatal|error|failed|traceback|no such|permission denied' "$LOG_FILE" | tail -n 20 || true)"
if [[ -n "$matches" ]]; then if [[ -n "$matches" ]]; then
printf '%s\n' "$matches" >&2 while IFS= read -r line; do
case "$line" in
*FATAL:*|*fatal:*)
printf '%s%s%s\n' "$COLOR_LOG_FATAL" "$line" "$COLOR_RESET" >&2
;;
*ERROR:*|*error:*)
printf '%s%s%s\n' "$COLOR_LOG_ERROR" "$line" "$COLOR_RESET" >&2
;;
*)
printf '%s\n' "$line" >&2
;;
esac
done <<<"$matches"
return 0 return 0
fi fi
...@@ -431,7 +434,7 @@ run_container_locally() { ...@@ -431,7 +434,7 @@ run_container_locally() {
inner_script="$(mktemp "${TMPDIR:-/tmp}/epm-docker-test-inner.XXXXXX.sh")" inner_script="$(mktemp "${TMPDIR:-/tmp}/epm-docker-test-inner.XXXXXX.sh")"
build_container_script "$inner_script" build_container_script "$inner_script"
container_name="epm-test-$(slugify "$APP_NAME")-$(slugify "$SYSTEM_IMAGE")-$$" container_name="epm-test-$(sanitize_name "$APP_NAME")-$(sanitize_name "$SYSTEM_IMAGE")-$$"
info "Using eepm tree: $resolved_source" info "Using eepm tree: $resolved_source"
info "Target image: $SYSTEM_IMAGE" info "Target image: $SYSTEM_IMAGE"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment