Files
debian-first-boot-setup/setup.sh

251 lines
8.0 KiB
Bash
Executable File

#!/bin/bash
set -e
### === SETTINGS === ###
GIT_SERVER="git.del-c.net"
USERNAME=""
SELECTED_REPO=""
SELECTED_DIR=""
### === GET USERNAME === ###
get_username() {
echo "[?] Enter your git username:"
read -p "Username: " USERNAME
if [ -z "$USERNAME" ]; then
echo "[!] Username cannot be empty. Please try again."
get_username
fi
echo "[*] Using username: $USERNAME"
echo
}
### === CHECK FOR SSH KEY === ###
generate_ssh_key() {
if [ ! -f "$HOME/.ssh/id_ed25519.pub" ]; then
echo "[+] No SSH key found. Need to generate a new SSH key..."
echo "[+] Are you using the correct system user?"
echo "[+] If not, please switch to the correct user and run this script again."
echo "[+] If you are using the correct user, please enter your email for the SSH key."
read -p "Enter your email for the SSH key: " user_email
ssh-keygen -t ed25519 -C "$user_email" -f "$HOME/.ssh/id_ed25519" -N ""
else
echo "[~] SSH key already exists."
fi
}
show_ssh_key() {
echo "[*] Add the following public key to your GitHub account:"
echo "----------------------------------------"
cat "$HOME/.ssh/id_ed25519.pub"
echo "----------------------------------------"
echo "[*] Go to your git site and add the key to your account."
echo
read -p "Press Enter after you've added the key..."
}
### === LIST AVAILABLE GIT PROJECTS === ###
list_git_projects() {
echo "[+] Fetching all accessible git projects..."
local repo_count=1
local found_repos=()
# Try to use git server API if available (GitLab/Gitea style)
if command -v curl &> /dev/null; then
echo "[*] Attempting to fetch repositories via API..."
# Try GitLab API format
local gitlab_repos=$(curl -s -H "Authorization: Bearer $(ssh-add -L 2>/dev/null || echo '')" \
"https://$GIT_SERVER/api/v4/projects?membership=true&per_page=100" 2>/dev/null | \
grep -o '"path_with_namespace":"[^"]*' | cut -d'"' -f4 2>/dev/null || echo "")
# Try Gitea API format
if [ -z "$gitlab_repos" ]; then
local gitea_repos=$(curl -s "https://$GIT_SERVER/api/v1/user/repos" 2>/dev/null | \
grep -o '"full_name":"[^"]*' | cut -d'"' -f4 2>/dev/null || echo "")
gitlab_repos="$gitea_repos"
fi
if [ -n "$gitlab_repos" ]; then
echo "[*] Found repositories via API:"
while IFS= read -r repo; do
if [ -n "$repo" ]; then
echo "$repo_count. $repo"
found_repos+=("$repo")
((repo_count++))
fi
done <<< "$gitlab_repos"
fi
fi
# Fallback: Check common patterns for multiple users and the current user
if [ ${#found_repos[@]} -eq 0 ]; then
echo "[*] API not available, scanning common repository patterns..."
# Check user's own repositories first
for repo in "dotfiles" "scripts" "configs" "tools" "projects" "notes" "backup" "workspace"; do
local full_repo="$USERNAME/$repo"
if git ls-remote "git@$GIT_SERVER:$full_repo.git" &>/dev/null; then
echo "$repo_count. $full_repo (owner)"
found_repos+=("$full_repo")
((repo_count++))
fi
done
# Check common shared repositories and other users' public repos
local common_users=("admin" "shared" "public" "team" "common" "devops" "infrastructure")
local common_repos=("dotfiles" "scripts" "configs" "tools" "projects" "notes" "backup" "workspace" "templates" "shared-configs" "common-tools")
for user in "${common_users[@]}"; do
for repo in "${common_repos[@]}"; do
local full_repo="$user/$repo"
if [ "$user" != "$USERNAME" ] && git ls-remote "git@$GIT_SERVER:$full_repo.git" &>/dev/null; then
echo "$repo_count. $full_repo (shared)"
found_repos+=("$full_repo")
((repo_count++))
fi
done
done
# Try to discover repositories by checking SSH access to common paths
echo "[*] Checking for additional accessible repositories..."
# This is a more aggressive approach - try common project names
local project_names=("website" "api" "frontend" "backend" "database" "monitoring" "deployment" "ci-cd" "documentation")
for user in "${common_users[@]}" "$USERNAME"; do
for project in "${project_names[@]}"; do
local full_repo="$user/$project"
if git ls-remote "git@$GIT_SERVER:$full_repo.git" &>/dev/null 2>&1; then
# Check if we haven't already added this repo
local already_added=false
for existing_repo in "${found_repos[@]}"; do
if [ "$existing_repo" = "$full_repo" ]; then
already_added=true
break
fi
done
if [ "$already_added" = false ]; then
local access_type="shared"
if [ "$user" = "$USERNAME" ]; then
access_type="owner"
fi
echo "$repo_count. $full_repo ($access_type)"
found_repos+=("$full_repo")
((repo_count++))
fi
fi
done
done
fi
if [ ${#found_repos[@]} -eq 0 ]; then
echo "[!] No accessible repositories found"
echo "[*] You can still enter a custom repository path."
else
echo
echo "[*] Found ${#found_repos[@]} accessible repositories"
fi
echo
}
### === SELECT PROJECT TO DOWNLOAD === ###
select_project() {
list_git_projects
echo "[?] Which project would you like to download?"
echo "Enter the number of the repository from the list above, or:"
echo "- Press Enter to use $USERNAME/dotfiles (if available)"
echo "- Type 'custom' to enter a custom repository path"
echo
read -p "Enter your choice: " choice
if [ -z "$choice" ]; then
# Default to dotfiles if it exists
if git ls-remote "git@$GIT_SERVER:$USERNAME/dotfiles.git" &>/dev/null; then
SELECTED_REPO="git@$GIT_SERVER:$USERNAME/dotfiles.git"
SELECTED_DIR="$HOME/.dotfiles"
else
echo "[!] $USERNAME/dotfiles not found. Please select a repository."
select_project
return
fi
elif [ "$choice" = "custom" ]; then
read -p "Enter repository path (e.g., $USERNAME/myproject): " custom_repo
SELECTED_REPO="git@$GIT_SERVER:$custom_repo.git"
SELECTED_DIR="$HOME/$(basename $custom_repo)"
elif [[ "$choice" =~ ^[0-9]+$ ]]; then
# User selected a number from the list
local repo_count=1
local selected_repo=""
for repo in "dotfiles" "scripts" "configs" "tools" "projects" "notes" "backup" "workspace"; do
local full_repo="$USERNAME/$repo"
if git ls-remote "git@$GIT_SERVER:$full_repo.git" &>/dev/null; then
if [ "$repo_count" -eq "$choice" ]; then
selected_repo="$full_repo"
break
fi
((repo_count++))
fi
done
if [ -n "$selected_repo" ]; then
SELECTED_REPO="git@$GIT_SERVER:$selected_repo.git"
if [ "$(basename $selected_repo)" = "dotfiles" ]; then
SELECTED_DIR="$HOME/.dotfiles"
else
SELECTED_DIR="$HOME/$(basename $selected_repo)"
fi
else
echo "[!] Invalid choice. Please try again."
select_project
return
fi
else
echo "[!] Invalid choice. Please try again."
select_project
return
fi
echo "[*] Selected repository: $SELECTED_REPO"
echo "[*] Download directory: $SELECTED_DIR"
echo
}
### === CLONE SELECTED REPO === ###
clone_selected_repo() {
if [ ! -d "$SELECTED_DIR" ]; then
echo "[+] Cloning selected repository..."
git clone "$SELECTED_REPO" "$SELECTED_DIR"
else
echo "[~] Repository already cloned. Force pulling from remote (overwriting local changes)..."
cd "$SELECTED_DIR"
git fetch origin
git reset --hard origin/$(git symbolic-ref --short HEAD)
git clean -fd
fi
# Only run setup if it's the dotfiles repository
if [[ "$SELECTED_REPO" == *"dotfiles.git" && -f "$SELECTED_DIR/setup.sh" ]]; then
echo "[+] Launching dotfiles setup using Bash..."
bash "$SELECTED_DIR/setup.sh"
else
echo "[*] Repository cloned successfully to: $SELECTED_DIR"
fi
}
### === MAIN === ###
main() {
get_username
generate_ssh_key
show_ssh_key
select_project
clone_selected_repo
}
main