abra-debian-scripts/setup_abra.sh
2024-09-28 10:39:26 +02:00

160 lines
4.6 KiB
Bash

#!/bin/bash
# Load configuration from config.sh
if [ -f "config.sh" ]; then
source config.sh
else
echo "Error: config.sh not found! Please ensure the config file is in the same directory."
exit 1
fi
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to pause and get confirmation from the user
pause() {
read -p "$*"
}
# Arguments: Email for SSH key and Abra user
email=$1
abra_user=$2
# Check if both arguments are provided
if [ -z "$email" ] || [ -z "$abra_user" ]; then
echo "Usage: ./setup_abra.sh <your_email@example.com> <abra_user>"
exit 1
fi
# Extract directory name from GIT_REPO_URL
repo_dir=$(basename -s .git "$GIT_REPO_URL")
# Install dependencies
sudo apt update && sudo apt install curl git make docker.io -y
# Check if the SSH agent is running; if not, start it
if [ -z "$SSH_AGENT_PID" ]; then
eval $(ssh-agent -s)
echo "Started SSH agent."
else
echo "SSH agent is already running."
fi
# Generate SSH keys if they don't exist
if [ ! -f $SSH_KEY_FILE ]; then
ssh-keygen -t ed25519 -C "$email"
fi
# Add SSH key to the agent to avoid repeated passphrase prompts
echo "Adding the SSH key to the SSH agent..."
ssh-add $SSH_KEY_FILE
# Print SSH public key and instructions to add it to Git and server
echo "Your SSH public key:"
pub_key=$(cat $SSH_KEY_FILE.pub)
echo "$pub_key"
echo ""
echo "1. Copy the above key and add it to your Git (for example Gitea, Github, Bitbucket, etc.) instance at: https://$GIT_SSH_HOST under 'User Settings -> SSH Keys'."
echo " You can display the key again anytime by running: cat $SSH_KEY_FILE.pub"
echo "2. Add this key to the Abra server ($ABRA_SERVER_HOST) under the '~/.ssh/authorized_keys' file for the appropriate user."
echo " If you do not have access, ask an administrator to do this for you."
echo ""
echo "Server-side (admin) instructions for adding the key:"
echo "To add the SSH key to the authorized_keys file on the server, run the following command on the Abra server:"
echo ""
echo "echo \"$pub_key\" >> ~/.ssh/authorized_keys"
echo ""
echo "This will append your public key to the 'authorized_keys' file, allowing access to the server."
# Pause to allow the user to add the key to Git
pause "Press Enter once you've added the SSH key to Git ($GIT_SSH_HOST) to continue..."
# Add SSH config for Git and Abra server
if [ ! -f ~/.ssh/config ]; then
touch ~/.ssh/config
fi
if ! grep -q "$GIT_SSH_HOST" ~/.ssh/config; then
echo "Adding $GIT_SSH_HOST to SSH config..."
echo "
# Git SSH Config
Host $GIT_SSH_HOST
Port $GIT_SSH_PORT
User $GIT_USER
IdentityFile $SSH_KEY_FILE
" >> ~/.ssh/config
fi
if ! grep -q "$ABRA_SERVER_HOST" ~/.ssh/config; then
echo "Adding $ABRA_SERVER_HOST to SSH config..."
echo "
# Abra Server Config
Host $ABRA_SERVER_HOST
Port $ABRA_SERVER_PORT
User $abra_user
IdentityFile $SSH_KEY_FILE
" >> ~/.ssh/config
fi
# Clone the repo if it doesn't exist
if [ ! -d "$repo_dir" ]; then
git clone $GIT_REPO_URL
# Check if git clone was successful
if [ $? -ne 0 ]; then
echo "Error: Failed to clone the repository. Please check your SSH configuration and try again."
exit 1
fi
else
cd "$repo_dir"
fi
# Verify if the repository directory exists
if [ ! -d "$repo_dir" ]; then
echo "Error: Repository directory not found. Please start over and ensure the SSH key is added to $GIT_SSH_HOST."
exit 1
fi
# Fix symlink
rm -rf ~/.abra/servers/$ABRA_SERVER_HOST
ln -sfF "$(pwd)/$ABRA_SERVER_HOST" ~/.abra/servers/
# Check if Abra is already installed, if not, install it
if ! command_exists abra; then
curl https://install.abra.coopcloud.tech | bash
fi
# Check if Abra was added to the PATH
if ! command_exists abra; then
echo "export PATH=\$PATH:$ABRA_PATH" >> ~/.bashrc
source ~/.bashrc
fi
# Verify if Abra was successfully added to the PATH
if command_exists abra; then
echo "Abra was successfully added to your PATH."
else
echo "Abra was NOT added to your PATH. Please manually add it by running:"
echo "export PATH=\$PATH:$ABRA_PATH"
exit 1
fi
# Enable autocomplete for Abra
abra autocomplete
# Pause to ensure SSH key has been added to the server
pause "Please confirm that the SSH key has been added to the server ($ABRA_SERVER_HOST). Press Enter to continue."
# Ask if the user wants to continue and set up Abra
read -p "Do you want to continue by adding the Abra server and listing the app status? (y/n): " choice
if [[ "$choice" == "y" || "$choice" == "Y" ]]; then
# Add the Abra server
abra server add $ABRA_SERVER_HOST $abra_user $ABRA_SERVER_PORT
# List the status of the apps
abra app list --status
fi
echo "Setup complete!"