Files
Rules/scripts/invoke-machine-bootstrap.sh

129 lines
3.9 KiB
Bash

#!/usr/bin/env bash
# Настройка git на машине: user, SSH, Gitea keys, clone/update Rules.
set -euo pipefail
ROOT="${ROOT:?ROOT is required}"
RULES_DEST="${RULES_DEST:-$HOME/CursorRules}"
CLONE_URL="${CLONE_URL:-ssh://git@git.papatramp.ru:2222/PapaTramp/Rules.git}"
HTTPS_CLONE_URL="${HTTPS_CLONE_URL:-https://git.papatramp.ru/PapaTramp/Rules.git}"
SKIP_CLONE="${SKIP_CLONE:-0}"
SKIP_PROFILE="${SKIP_PROFILE:-0}"
SKIP_GIT_SETUP="${SKIP_GIT_SETUP:-0}"
WHAT_IF="${WHAT_IF:-0}"
log_step() { printf '[*] %s\n' "$1"; }
log_ok() { printf '[+] %s\n' "$1"; }
log_warn() { printf '[!] %s\n' "$1"; }
CONFIG_DIR="$ROOT/local"
[[ -d "$CONFIG_DIR" ]] || { echo "Bootstrap config not found: $CONFIG_DIR" >&2; exit 1; }
test_machine_git_ready() {
local hosts_config="$CONFIG_DIR/git-ssh-hosts.conf"
local ssh_config="$HOME/.ssh/config"
[[ -f "$hosts_config" && -f "$ssh_config" ]] || return 1
grep -q '^Host git.kalinamall.ru$' "$ssh_config" || return 1
local line current_profile='' key_name
while IFS= read -r line || [[ -n "$line" ]]; do
line="${line%%#*}"
line="${line%"${line##*[![:space:]]}"}"
[[ -z "$line" ]] && continue
if [[ "$line" =~ ^\[profile:(.+)\]$ ]]; then
current_profile="${BASH_REMATCH[1]}"
continue
fi
if [[ "$line" =~ ^\[host: ]]; then
current_profile=''
continue
fi
if [[ "$line" == KeyName=* && -n "$current_profile" ]]; then
key_name="${line#KeyName=}"
[[ -f "$HOME/.ssh/$key_name" ]] || return 1
fi
done < "$hosts_config"
return 0
}
ensure_rules_repository() {
if [[ -d "$RULES_DEST/.git" ]]; then
return 0
fi
if [[ -e "$RULES_DEST" ]]; then
local backup="${RULES_DEST}.bak-$(date +%Y%m%d-%H%M%S)"
log_warn "Replacing non-git folder: $RULES_DEST -> $backup"
if [[ "$WHAT_IF" != '1' ]]; then
mv "$RULES_DEST" "$backup"
fi
fi
log_step "Cloning Rules via SSH: $CLONE_URL"
if [[ "$WHAT_IF" == '1' ]]; then
log_step "WhatIf: clone -> $RULES_DEST"
return 0
fi
if git clone --depth 1 "$CLONE_URL" "$RULES_DEST" 2>/dev/null; then
log_ok "Cloned to $RULES_DEST"
return 0
fi
log_warn 'SSH clone failed - trying HTTPS (enter Gitea login once)'
git clone --depth 1 "$HTTPS_CLONE_URL" "$RULES_DEST"
log_ok "Cloned via HTTPS to $RULES_DEST"
}
ensure_init_cursor_profile() {
local hook="source \"$RULES_DEST/init-cursor.sh\""
local profile
for profile in "$HOME/.bashrc" "$HOME/.zshrc"; do
[[ -f "$profile" ]] || continue
if [[ "$WHAT_IF" == '1' ]]; then
log_step "WhatIf: profile hook -> $profile"
continue
fi
if ! grep -Fq "$hook" "$profile" 2>/dev/null; then
printf '\n%s\n' "$hook" >> "$profile"
log_ok "Profile updated: $profile"
fi
done
}
git_ready=0
repo_ready=0
test_machine_git_ready && git_ready=1
[[ -d "$RULES_DEST/.git" ]] && repo_ready=1
if [[ "$git_ready" -eq 0 && "$SKIP_GIT_SETUP" != '1' ]]; then
log_step 'Machine bootstrap: git + SSH'
if [[ -f "$CONFIG_DIR/git-user.conf" ]]; then
while IFS= read -r kv; do
key="${kv%%=*}"
value="${kv#*=}"
case "$key" in
UserName) git config --global user.name "$value"; log_ok "git user.name = $value" ;;
UserEmail) git config --global user.email "$value"; log_ok "git user.email = $value" ;;
esac
done < <(grep -v '^#' "$CONFIG_DIR/git-user.conf" | grep '=' || true)
fi
export CONFIG_DIR WHAT_IF
bash "$ROOT/scripts/setup-git-ssh.sh"
bash "$ROOT/scripts/register-gitea-ssh-keys.sh"
log_step 'Testing git@git.kalinamall.ru'
ssh -T -o BatchMode=yes -o ConnectTimeout=10 git@git.kalinamall.ru || true
elif [[ "$git_ready" -eq 1 ]]; then
log_ok 'Git SSH already configured'
fi
if [[ "$SKIP_CLONE" != '1' && "$repo_ready" -eq 0 ]]; then
ensure_rules_repository
elif [[ "$repo_ready" -eq 1 ]]; then
log_ok "CursorRules repo OK: $RULES_DEST"
fi
if [[ "$SKIP_PROFILE" != '1' ]]; then
ensure_init_cursor_profile
fi
log_ok 'Machine bootstrap complete'