Commit 2edfc216 authored by Ivan Mazhukin's avatar Ivan Mazhukin

cleanup

parent 29f51e57
......@@ -41,6 +41,10 @@ COLOR_WARN=""
COLOR_ERROR=""
COLOR_LOG_FATAL=""
COLOR_LOG_ERROR=""
SSH_BASE_ARGS=(
-o BatchMode=yes
-o StrictHostKeyChecking=accept-new
)
if [[ -t 2 && -z "${NO_COLOR:-}" ]]; then
COLOR_RESET=$'\033[0m'
......@@ -96,11 +100,8 @@ require_command() {
}
sanitize_name() {
local value="${1,,}"
#TODO сделать в одну строку и убрать башизм.
value="${value//[^a-z0-9]/-}"
value="${value##-}"
value="${value%%-}"
local value
value="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed -e 's/[^a-z0-9]/-/g' -e 's/^-*//' -e 's/-*$//')"
printf '%s\n' "${value:-run}"
}
......@@ -110,38 +111,47 @@ normalize_system_name() {
local image_name
local image_tag
normalized="${raw// /}"
normalized="${normalized,,}"
normalized="$(printf '%s' "$raw" | tr -d ' ' | tr '[:upper:]' '[:lower:]')"
[[ -n "$normalized" ]] || fatal "Target system is empty"
if [[ "$normalized" == *:* ]]; then
if [[ "$normalized" =~ ^[a-z0-9._/-]+:[a-z0-9._-]+$ ]]; then
printf '%s\n' "$normalized"
return 0
fi
fatal "Unsupported system format: $raw"
case "$normalized" in
[a-z0-9._/-]*:[a-z0-9._-]*)
printf '%s\n' "$normalized"
return 0
;;
*)
fatal "Unsupported system format: $raw"
;;
esac
fi
if [[ "$normalized" == */* ]]; then
image_name="${normalized%/*}"
image_tag="${normalized##*/}"
if [[ "$image_tag" =~ ^v?[0-9][a-z0-9._-]*$ ]]; then
normalized="${image_name}:${image_tag}"
else
normalized="${normalized}:latest"
fi
case "$image_tag" in
[0-9]*|v[0-9]*)
normalized="${image_name}:${image_tag}"
;;
*)
normalized="${normalized}:latest"
;;
esac
else
normalized="${normalized}:latest"
fi
if [[ "$normalized" =~ ^[a-z0-9._/-]+:[a-z0-9._-]+$ ]]; then
printf '%s\n' "$normalized"
return 0
fi
fatal "Unsupported system format: $raw"
case "$normalized" in
[a-z0-9._/-]*:[a-z0-9._-]*)
printf '%s\n' "$normalized"
return 0
;;
*)
fatal "Unsupported system format: $raw"
;;
esac
}
can_use_local_docker() {
......@@ -169,41 +179,20 @@ verify_eepm_tree() {
fi
}
resolve_local_source_path() {
local candidate
candidate="${SOURCE_PATH:-$(pwd -P)}"
candidate="$(realpath "$candidate")"
verify_eepm_tree "$candidate"
printf '%s\n' "$candidate"
}
resolve_explicit_source_path() {
resolve_checked_source_path() {
local candidate="$1"
[[ -n "$candidate" ]] || fatal "Explicit source path is empty"
[[ -n "$candidate" ]] || fatal "Source path is empty"
candidate="$(realpath "$candidate" 2>/dev/null || printf '%s\n' "$candidate")"
verify_eepm_tree "$candidate"
printf '%s\n' "$candidate"
}
default_builder_source_user() {
local current_user source_user
current_user="$(id -un)"
source_user="${BUILDER_USER:-$current_user}"
printf '%s\n' "$source_user"
}
builder_source_candidates() {
default_builder_source_path() {
local source_user
source_user="$(default_builder_source_user)"
source_user="${BUILDER_USER:-$(id -un)}"
printf '/srv/%s/Projects/eepm\n' "$source_user"
}
......@@ -217,27 +206,26 @@ resolve_builder_source_path() {
return 0
fi
while IFS= read -r candidate; do
candidate="$(realpath "$candidate" 2>/dev/null || printf '%s\n' "$candidate")"
if [[ -d "$candidate" && -f "$candidate/bin/eepm" && -r "$candidate/bin/eepm" ]]; then
printf '%s\n' "$candidate"
return 0
fi
done < <(builder_source_candidates)
candidate="$(default_builder_source_path)"
candidate="$(realpath "$candidate" 2>/dev/null || printf '%s\n' "$candidate")"
if [[ -d "$candidate" && -f "$candidate/bin/eepm" && -r "$candidate/bin/eepm" ]]; then
printf '%s\n' "$candidate"
return 0
fi
fatal "Could not find builder64 eepm tree under /srv/$(default_builder_source_user)/Projects/eepm"
fatal "Could not find builder64 eepm tree under $(default_builder_source_path)"
}
resolve_source_path() {
case "$SOURCE_KIND" in
local)
resolve_local_source_path
resolve_checked_source_path "${SOURCE_PATH:-$(pwd -P)}"
;;
builder64)
resolve_builder_source_path
;;
explicit)
resolve_explicit_source_path "$SOURCE_PATH"
resolve_checked_source_path "$SOURCE_PATH"
;;
*)
fatal "Unsupported eepm source kind: $SOURCE_KIND"
......@@ -245,13 +233,6 @@ resolve_source_path() {
esac
}
build_ssh_base_args() {
SSH_BASE_ARGS=(
-o BatchMode=yes
-o StrictHostKeyChecking=accept-new
)
}
remote_target() {
[[ -n "$REMOTE_USER" ]] || fatal "Remote user is empty"
printf '%s@%s\n' "$REMOTE_USER" "$REMOTE_HOST"
......@@ -261,7 +242,6 @@ remote_eepm_tree_exists() {
local target="$1"
local tree="$2"
build_ssh_base_args
ssh "${SSH_BASE_ARGS[@]}" "$target" bash -s -- "$tree" >/dev/null 2>&1 <<'EOF'
tree="$1"
test -d "$tree" && test -f "$tree/bin/eepm" && test -r "$tree/bin/eepm"
......@@ -280,12 +260,11 @@ find_remote_builder_source_path() {
return 1
fi
while IFS= read -r candidate; do
if remote_eepm_tree_exists "$target" "$candidate"; then
printf '%s\n' "$candidate"
return 0
fi
done < <(builder_source_candidates)
candidate="$(default_builder_source_path)"
if remote_eepm_tree_exists "$target" "$candidate"; then
printf '%s\n' "$candidate"
return 0
fi
return 1
}
......@@ -310,7 +289,6 @@ sync_local_source_to_remote() {
require_command rsync
remote_dir="$(remote_sync_dir_for_source "$source_dir")"
build_ssh_base_args
info "Remote eepm tree not found in /srv; syncing local tree to $target:$remote_dir"
......@@ -333,7 +311,6 @@ cleanup_remote_sync_dir() {
return 0
}
build_ssh_base_args
info "Cleaning up remote sync dir: $target:$remote_dir"
ssh "${SSH_BASE_ARGS[@]}" "$target" bash -s -- "$remote_dir" <<'EOF'
remote_dir="$1"
......@@ -503,7 +480,7 @@ run_container_locally() {
}
build_remote_args() {
local explicit_source
local local_source
local remote_source
local target
......@@ -511,25 +488,16 @@ build_remote_args() {
REMOTE_SYNC_DIR=""
target="$(remote_target)"
if [[ "$SOURCE_KIND" == "local" ]]; then
if [[ "$SOURCE_KIND" == "local" || "$SOURCE_KIND" == "explicit" ]]; then
if remote_source="$(find_remote_builder_source_path "$target")"; then
info "Remote eepm tree found at: $remote_source"
else
explicit_source="${SOURCE_PATH:-$(pwd -P)}"
explicit_source="$(realpath "$explicit_source")"
verify_eepm_tree "$explicit_source"
remote_source="$(sync_local_source_to_remote "$explicit_source" "$target")"
REMOTE_SYNC_DIR="$remote_source"
fi
REMOTE_ARGS+=(--eepm-source explicit --eepm-dir "$remote_source")
elif [[ "$SOURCE_KIND" == "explicit" ]]; then
if remote_source="$(find_remote_builder_source_path "$target")"; then
info "Remote eepm tree found at: $remote_source"
else
[[ -n "$SOURCE_PATH" ]] || fatal "Explicit source path is empty"
explicit_source="$(realpath "$SOURCE_PATH" 2>/dev/null || printf '%s\n' "$SOURCE_PATH")"
verify_eepm_tree "$explicit_source"
remote_source="$(sync_local_source_to_remote "$explicit_source" "$target")"
if [[ "$SOURCE_KIND" == "local" ]]; then
local_source="$(resolve_checked_source_path "${SOURCE_PATH:-$(pwd -P)}")"
else
local_source="$(resolve_checked_source_path "$SOURCE_PATH")"
fi
remote_source="$(sync_local_source_to_remote "$local_source" "$target")"
REMOTE_SYNC_DIR="$remote_source"
fi
REMOTE_ARGS+=(--eepm-source explicit --eepm-dir "$remote_source")
......
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