diff --git a/README.md b/README.md index e75c945..ce606ea 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,157 @@ # abra-debian-scripts -Helper scripts for installing Abra on Debian. Geared towards beginners and WSL2 friendly for people on windows machines. \ No newline at end of file +Helper scripts for installing Abra on Debian. While it probably works on any Debian system, it is geared towards beginners and is friendly for anyone on Windows machines as it has been tested on the Windows Sub-system for Linux 2 (WSL2). + +## 🛠️ Abra Setup Scripts + +This repository contains a set of scripts designed to help you configure and deploy applications using the [Abra](https://coopcloud.tech/abra) command line (CLI) tool on your computer to manage remote servers. + +### 📖 Table of Contents +[Introduction](#introduction) +[Repository Structure](#repository-structure) +[Key concepts](#key-concepts) +[Prerequisites](#prerequisites) +[Getting Started](#getting-started) +[Script Overview](#script-overview) +[Troubleshooting](#troubleshooting) +[Additional Resources](#additional-resources) + +### 📝 Introduction + +This repository includes a set of Bash scripts that handle the creation and configuration of SSH keys, server connections, and Abra settings, making it easier for new users to get up and running quickly. + +The scripts are primarily designed to work with **Git, Docker**, and **Abra**, and they have been tested on **Debian-based systems**. They can also be used on **WSL2** setups for Windows users, providing a smooth integration between Windows and Linux environments. + +### 📁 Repository Structure + +Here's a quick overview of the main files in this repository: + +- **`config.sh.example`**: + Contains the server, Git, and path configurations used by the setup script. This is where you can customize the shared settings use by yourself and potentially your team so that the configuration only needs to be set once. + +- **`setup_abra.sh`**: + The main setup script that will configure SSH keys, connect to your Git and Abra servers, and set up Abra CLI. + +### 💡 Key concepts + +In case you are new to the Abra world, here are some key concepts: + +**Abra**: The tool used to manage remote servers to host apps online - such as wordpress or other web tools and systems. +**Debian**: A linux operating system distribution that Abra can use +**SSH**: Secure shell for securely connecting to remote servers. This protocol is used widely for communication on the internet and Abra uses it too. +**SSH keys**: The equivalent of a password or secret to be able to uniquely identify yourself and gain access to a server. These scripts will generate SSH keys for you. Protect these. +**Public key**: The SSH key that is publicly shared and used by others, especially remote computers, to know that you are the one communicating with them +**Private key**: The SSH key that you must be keep to yourself and never shared with anyone, not even an administrator. This is used to identify yourself uniquely. The fact that you have this key can be verified with the public key on the server and is how the server knows who you are and can communicate securely. +**Git**: A tool used keep track of files (a version control system). Abra works out of a git "repository", like this one, to store settings for server apps and to access recipes for how to run apps on the server. +**Docker**: A tool to create an app-specific environment that allows apps to be isolated from one another and from the server. These container-based environments add a layer of additional security and often simplify the management and development of apps. + +### 📋 Prerequisites + +Before you get started, ensure you have the following: + +1. **A remote server** where you can connect via SSH with a **a friendly server admin** who can add your public SSH key. Never share a private key. +2. **Access to a Git server** (e.g., [Gitea](https://gitea.io)) where the Abra repository is stored and where you will add your own public **SSH key**. +4. **A Linux-based OS** (Debian or Ubuntu) or **WSL2** on Windows with an Debian or Ubuntu container. + +### 🚀 Getting Started + +#### Step 1: Configuring the scripts +You may skip this step and go to step 2 if the scripts have been configured for you. + +Follow these steps to set up your environment and start using the Abra setup scripts: + +1. **Clone this repository** to your local machine: + +On Windows, make sure you install Git for Windows first. On Linux or WSL2, you will need to ensure git is already installed. + +```shell +git clone https://git.washnote.org/nickdickinson/abra-debian-scripts +cd abra-debian-scripts +``` + +Copy the file `config.sh.example` to `config.sh` and then open it in your favorite text editor. + +If on Windows, make sure you use one like VSCode or Notepad++ that will not add windows line-endings. + +2. **Edit the configuration file** (config.sh): + +Open the `config.sh` file in a text editor and update the values to match your settings: + +```shell +# Git Configuration +GIT_REPO_URL="git@git.example.org:WASHNote/example-apps.git" +GIT_SSH_HOST="git.example.org" +GIT_SSH_PORT="22" +GIT_USER="git" + +# Abra Server Configuration (shared) +ABRA_SERVER_HOST="example.org" +ABRA_SERVER_PORT="22" + +# Path configuration - this is the default - do not change unless required +ABRA_PATH="~/.local/bin" + +# SSH Key Configuration +SSH_KEY_FILE="$HOME/.ssh/id_ed25519" +``` + +Save the file after editing. + +#### Step 2: Run the setup script + +1. **Copy scripts to your home and make them executable** + +On Windows, from a normal windows shell: +```shell +wsl cp config.sh ~/config.sh +wsl cp setup_bash.sh ~/setup_bash.sh +wsl chmod +x ~/setup_bash.sh +``` + +On Linux or from a WSL2 shell: +```bash +cp config.sh ~/config.sh +cp setup_bash.sh ~/setup_bash.sh +cd ~ +chmod +x ./setup_bash.sh +``` + +2. **Run your scripts** + +On Windows, you will now need to open your Ubuntu or Debian shell. Abra will be installed and live in Linux. + +Use the following command to run the setup script from your home director. Make sure to replace and with your own email address and username from the server you are managing with Abra: + +```bash +cd ~ # go to your home directory +./setup_abra.sh +``` + +For example: +```bash +./setup_abra.sh myemail@example.com myusername +``` + +**Note:** At the moment, the script may need to be re-run after the installation of Abra. If it stops because Abra was not added to the path, simply follow the instructions and after that run it again. + +3. **Follow the on-screen instructions:** + +- The script will guide you through adding your SSH keys to both the Git and Abra app servers. +- You will need to confirm when you have added the keys before continuing. + +When the script completes, if it succeeds, it will list the status of all applications manage by Abra on your server. + +### 📚 Additional Resources + +For more information on how to use the Abra CLI, refer to the official documentation: + +- [Abra documentation](https://docs.coopcloud.tech/abra/) + +If you are new to Docker or SSH, start with + +- [SSH Debian documentation](https://wiki.debian.org/SSH) +- [Docker intro](https://docs.docker.com/get-started/docker-overview/) + +## Authorship and AI disclosure + +These scripts and the readme were created by Nicolas Dickinson based on existing documentation, experimentation in WSL2 Debian and partly generated and debugged during testing by Nicolas Dickinson using OpenAI ChatGPT in September 2024. \ No newline at end of file diff --git a/config.sh.example b/config.sh.example index 81d0177..fcbad7a 100644 --- a/config.sh.example +++ b/config.sh.example @@ -1,5 +1,5 @@ # Git Configuration -GIT_REPO_URL="git@git.example.org:WASHNote/example-apps.git" +GIT_REPO_URL="git@git.example.org:ExampleOrg/example-apps.git" GIT_SSH_HOST="git.example.org" GIT_SSH_PORT="22" GIT_USER="git" @@ -9,7 +9,7 @@ ABRA_SERVER_HOST="example.org" ABRA_SERVER_PORT="22" # Path configuration -ABRA_PATH="/root/.local/bin" +ABRA_PATH="~/.local/bin" # SSH Key Configuration SSH_KEY_FILE="$HOME/.ssh/id_ed25519" \ No newline at end of file diff --git a/setup_abra.sh b/setup_abra.sh index a9dcbcb..225775b 100644 --- a/setup_abra.sh +++ b/setup_abra.sh @@ -1,7 +1,15 @@ #!/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." @@ -18,13 +26,13 @@ pause() { read -p "$*" } -# Arguments: Email for SSH key and Abra user +# Arguments: Email for SSH key and the server user name used by Abra email=$1 -abra_user=$2 +abra_server_user=$2 # Check if both arguments are provided -if [ -z "$email" ] || [ -z "$abra_user" ]; then - echo "Usage: ./setup_abra.sh " +if [ -z "$email" ] || [ -z "$abra_server_user" ]; then + echo "Usage: ./setup_abra.sh " exit 1 fi @@ -32,7 +40,7 @@ fi repo_dir=$(basename -s .git "$GIT_REPO_URL") # Install dependencies -sudo apt update && sudo apt install curl git make docker.io -y +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 @@ -48,8 +56,12 @@ if [ ! -f $SSH_KEY_FILE ]; then 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 +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:" @@ -76,7 +88,7 @@ if [ ! -f ~/.ssh/config ]; then touch ~/.ssh/config fi -if ! grep -q "$GIT_SSH_HOST" ~/.ssh/config; then +if ! grep -Fxq "Host $GIT_SSH_HOST" ~/.ssh/config; then echo "Adding $GIT_SSH_HOST to SSH config..." echo " # Git SSH Config @@ -87,13 +99,13 @@ Host $GIT_SSH_HOST " >> ~/.ssh/config fi -if ! grep -q "$ABRA_SERVER_HOST" ~/.ssh/config; then +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_user + User $abra_server_user IdentityFile $SSH_KEY_FILE " >> ~/.ssh/config fi @@ -106,54 +118,85 @@ if [ ! -d "$repo_dir" ]; 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 -# 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 +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" + 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 - -# 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." +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_user $ABRA_SERVER_PORT + 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 + abra app list --status || { echo "Error: Failed to list Abra apps. Please check the server configuration."; exit 1; } fi +set -x + echo "Setup complete!"