abra-debian-scripts/setup_abra.sh

203 lines
6.2 KiB
Bash

#!/bin/bash
# Request sudo upfront
if ! sudo -v; then
echo "This script requires sudo privileges. Please run it with a user that has sudo access."
exit 1
fi
# Load configuration from config.sh
if [ -f "config.sh" ]; then
sed -i 's/\r$//' config.sh
sudo chmod +x config.sh
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 the server user name used by Abra
email=$1
abra_server_user=$2
# Check if both arguments are provided
if [ -z "$email" ] || [ -z "$abra_server_user" ]; then
echo "Usage: ./setup_abra.sh <your_email@example.com> <abra_server_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 wget 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
if ssh-add -l | grep -q "$(cat $SSH_KEY_FILE.pub | awk '{print $2}')"; then
echo "SSH key is already added to the SSH agent."
else
echo "Adding SSH key to the SSH agent..."
ssh-add $SSH_KEY_FILE
fi
# 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 -Fxq "Host $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 -Fxq "Host $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_server_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
# Verify if the repository directory exists
if [ ! -d "$repo_dir" ]; then
echo "Error: Repository directory '$repo_dir' not found. Please start over and ensure the SSH key is added to $GIT_SSH_HOST."
exit 1
fi
cd "$repo_dir"
else
cd "$repo_dir"
fi
# 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
fi
export PATH=\$PATH:$ABRA_PATH
source ~/.bashrc
# 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 && source ~/.bashrc"
echo "After that please re-run this script to finish"
exit 1
fi
# Enable autocomplete for Abra
abra autocomplete bash
sudo mkdir /etc/bash_completion.d/
echo "source /etc/bash_completion.d/abra" >> ~/.bashrc
sudo cp /home/nick/.abra/autocompletion/bash /etc/bash_completion.d/abra
source ~/.bashrc
# 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
# 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."
set -x
# Enable error handling
set -e
# Check if the server's host key is already in known_hosts
if ! ssh-keygen -F "$ABRA_SERVER_HOST" | grep -q "$ABRA_SERVER_HOST"; then
echo "SSH host key for $ABRA_SERVER_HOST not found in known_hosts. Attempting to connect to accept the host key..."
# Connect to the server to accept the host key
ssh -o StrictHostKeyChecking=accept-new -p $ABRA_SERVER_PORT $abra_server_user@$ABRA_SERVER_HOST exit
fi
# Fix symlink
rm -rf ~/.abra/servers/$ABRA_SERVER_HOST
ln -sfF "$(pwd)/$ABRA_SERVER_HOST" ~/.abra/servers/
# Use trap to control the final message based on success or failure
trap 'error_exit' ERR
trap 'echo "Setup complete!"' EXIT
# Add the Abra server
abra server add $ABRA_SERVER_HOST $abra_server_user $ABRA_SERVER_PORT || { echo "Error: Unable to add Abra server. SSH key or configuration might be missing."; exit 1; }
# List the status of the apps
abra app list --status || { echo "Error: Failed to list Abra apps. Please check the server configuration."; exit 1; }
fi
set -x
echo "Setup complete!"