2024-09-28 10:39:26 +02:00
#!/bin/bash
2024-09-28 22:08:18 +02:00
# 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
2024-09-28 10:39:26 +02:00
# Load configuration from config.sh
if [ -f "config.sh" ] ; then
2024-09-28 22:08:18 +02:00
sed -i 's/\r$//' config.sh
sudo chmod +x config.sh
2024-09-28 10:39:26 +02:00
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 " $* "
}
2024-09-28 22:08:18 +02:00
# Arguments: Email for SSH key and the server user name used by Abra
2024-09-28 10:39:26 +02:00
email = $1
2024-09-28 22:08:18 +02:00
abra_server_user = $2
2024-09-28 10:39:26 +02:00
# Check if both arguments are provided
2024-09-28 22:08:18 +02:00
if [ -z " $email " ] || [ -z " $abra_server_user " ] ; then
echo "Usage: ./setup_abra.sh <your_email@example.com> <abra_server_user>"
2024-09-28 10:39:26 +02:00
exit 1
fi
# Extract directory name from GIT_REPO_URL
repo_dir = $( basename -s .git " $GIT_REPO_URL " )
# Install dependencies
2024-09-28 22:08:18 +02:00
sudo apt update && sudo apt install curl wget git make docker.io -y
2024-09-28 10:39:26 +02:00
# 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
2024-09-28 22:08:18 +02:00
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
2024-09-28 10:39:26 +02:00
# 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
2024-09-28 22:08:18 +02:00
if ! grep -Fxq " Host $GIT_SSH_HOST " ~/.ssh/config; then
2024-09-28 10:39:26 +02:00
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
2024-09-28 22:08:18 +02:00
if ! grep -Fxq " Host $ABRA_SERVER_HOST " ~/.ssh/config; then
2024-09-28 10:39:26 +02:00
echo " Adding $ABRA_SERVER_HOST to SSH config... "
echo "
# Abra Server Config
Host $ABRA_SERVER_HOST
Port $ABRA_SERVER_PORT
2024-09-28 22:08:18 +02:00
User $abra_server_user
2024-09-28 10:39:26 +02:00
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
2024-09-28 22:08:18 +02:00
# 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
2024-09-28 10:39:26 +02:00
cd " $repo_dir "
2024-09-28 22:08:18 +02:00
else
cd " $repo_dir "
2024-09-28 10:39:26 +02:00
fi
# Check if Abra is already installed, if not, install it
if ! command_exists abra; then
curl https://install.abra.coopcloud.tech | bash
fi
2024-09-28 22:08:18 +02:00
2024-09-28 10:39:26 +02:00
# Check if Abra was added to the PATH
if ! command_exists abra; then
echo " export PATH=\$PATH: $ABRA_PATH " >> ~/.bashrc
fi
2024-09-28 22:08:18 +02:00
export PATH = \$ PATH:$ABRA_PATH
source ~/.bashrc
2024-09-28 10:39:26 +02:00
# 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:"
2024-09-28 22:08:18 +02:00
echo " export PATH=\$PATH: $ABRA_PATH && source ~/.bashrc "
echo "After that please re-run this script to finish"
2024-09-28 10:39:26 +02:00
exit 1
fi
# Enable autocomplete for Abra
2024-09-28 22:08:18 +02:00
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
2024-09-28 10:39:26 +02:00
# 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
2024-09-28 22:08:18 +02:00
# 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
2024-09-28 10:39:26 +02:00
# Add the Abra server
2024-09-28 22:08:18 +02:00
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; }
2024-09-28 10:39:26 +02:00
# List the status of the apps
2024-09-28 22:08:18 +02:00
abra app list --status || { echo "Error: Failed to list Abra apps. Please check the server configuration." ; exit 1; }
2024-09-28 10:39:26 +02:00
fi
2024-09-28 22:08:18 +02:00
set -x
2024-09-28 10:39:26 +02:00
echo "Setup complete!"