feat: add sysyadm initialization script
This commit is contained in:
parent
f6672dcfc5
commit
76edde097c
|
@ -0,0 +1,113 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Exit immediately if a command exits with a non-zero status
|
||||
set -e
|
||||
|
||||
# Google Bash Script Guidelines: All static variables are defined at the top and marked readonly.
|
||||
readonly REPO_URL="ssh://git@git.jilits.se:2222/JILITS/sysfiles.git"
|
||||
readonly YADM_DIR="/etc/yadm"
|
||||
readonly YADM_DATA_DIR="$YADM_DIR/data"
|
||||
readonly YADM_REPO_PATH="$YADM_DATA_DIR/repo.git"
|
||||
|
||||
readonly ROOT_SSH_KEY="/root/.ssh/id_rsa"
|
||||
readonly ALIASES_FILE="$HOME/.bash_aliases"
|
||||
readonly -A ALIASES=(
|
||||
["y"]="yadm"
|
||||
["sysyadm"]="sudo yadm --yadm-dir '$YADM_DIR' --yadm-data '$YADM_DATA_DIR'"
|
||||
["s"]="sysyadm"
|
||||
)
|
||||
|
||||
# Function to print messages
|
||||
function iprint() {
|
||||
echo -e "\e[32m[INFO]\e[0m $1"
|
||||
}
|
||||
function eprint() {
|
||||
echo -e "\e[31m[ERROR]\e[0m $1" >&2
|
||||
}
|
||||
|
||||
# Function to run sysyadm commands
|
||||
function sysyadm() {
|
||||
sudo yadm --yadm-dir "$YADM_DIR" --yadm-data "$YADM_DATA_DIR" "$@"
|
||||
}
|
||||
|
||||
# 1. Ensure script is run as a non-root user
|
||||
if [ "$EUID" -eq 0 ]; then
|
||||
eprint "This script must be run as a non-root user. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 2. Ensure the system is Debian-compatible
|
||||
if ! command -v apt &> /dev/null; then
|
||||
eprint "This script is intended for Debian-based systems. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
iprint "System is Debian-compatible. Proceeding..."
|
||||
|
||||
# 3. Install yadm if not already installed
|
||||
if ! command -v yadm &> /dev/null; then
|
||||
iprint "Installing yadm..."
|
||||
sudo apt update && sudo apt install -y yadm
|
||||
else
|
||||
iprint "yadm is already installed."
|
||||
fi
|
||||
|
||||
# 4. Ensure aliases are added to ~/.bash_aliases
|
||||
iprint "Ensuring aliases are in $ALIASES_FILE..."
|
||||
|
||||
for alias_name in "${!ALIASES[@]}"; do
|
||||
alias_cmd="alias $alias_name=\"${ALIASES[$alias_name]}\""
|
||||
grep -qxF "$alias_cmd" "$ALIASES_FILE" || echo "$alias_cmd" >> "$ALIASES_FILE"
|
||||
done
|
||||
|
||||
iprint "Aliases added. Reloading bash aliases."
|
||||
# shellcheck disable=SC1090
|
||||
source "$ALIASES_FILE"
|
||||
|
||||
# 5. Generate SSH key for root if not exists
|
||||
if sudo test -f "$ROOT_SSH_KEY"; then
|
||||
iprint "SSH key for root already exists. Skipping key generation."
|
||||
else
|
||||
iprint "Generating SSH key for root..."
|
||||
sudo ssh-keygen -t rsa -N "" -f "$ROOT_SSH_KEY"
|
||||
fi
|
||||
|
||||
iprint "Printing the public SSH key for root:"
|
||||
sudo cat "$ROOT_SSH_KEY.pub"
|
||||
|
||||
# 6. Wait for user confirmation to add SSH key to the git server
|
||||
while true; do
|
||||
read -p "Has the SSH key been added to the Git server? (y/n): " CONFIRM
|
||||
case $CONFIRM in
|
||||
[Yy])
|
||||
break
|
||||
;;
|
||||
[Nn])
|
||||
iprint "Please add the key to the Git server and confirm when done."
|
||||
;;
|
||||
*)
|
||||
iprint "Please answer y or n."
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# 7. Run sysyadm commands
|
||||
iprint "Using repository URL: $REPO_URL"
|
||||
|
||||
if [ -d "$YADM_REPO_PATH" ]; then
|
||||
iprint "Yadm repository already exists at $YADM_REPO_PATH. Skipping initialization and clone."
|
||||
else
|
||||
iprint "Initializing sysyadm with root privileges..."
|
||||
sysyadm init -w /
|
||||
|
||||
iprint "Cloning sysyadm repository..."
|
||||
sysyadm clone -w / "$REPO_URL"
|
||||
fi
|
||||
|
||||
iprint "Running sysyadm alts..."
|
||||
sysyadm alts
|
||||
|
||||
iprint "Running sysyadm bootstrap..."
|
||||
sysyadm bootstrap
|
||||
|
||||
iprint "Script completed successfully."
|
Loading…
Reference in New Issue