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

cleanup

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