wip
This commit is contained in:
parent
51dd337f8c
commit
14afc57f44
1 changed files with 141 additions and 148 deletions
|
@ -1,159 +1,152 @@
|
||||||
{
|
#!/usr/bin/env bash
|
||||||
#!/usr/bin/env bash
|
# ─< ANSI color codes >───────────────────────────────────────────────────────────────────
|
||||||
# ─< ANSI color codes >───────────────────────────────────────────────────────────────────
|
RED='\033[0;31m'
|
||||||
RED='\033[0;31m'
|
CYAN='\033[0;36m'
|
||||||
CYAN='\033[0;36m'
|
YELLOW='\033[0;33m'
|
||||||
YELLOW='\033[0;33m'
|
LIGHT_GREEN='\033[0;92m'
|
||||||
LIGHT_GREEN='\033[0;92m'
|
BOLD='\033[1m'
|
||||||
BOLD='\033[1m'
|
NC='\033[0m' # No Color
|
||||||
NC='\033[0m' # No Color
|
|
||||||
|
|
||||||
echo_error() {
|
echo_error() {
|
||||||
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
|
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
echo_info() {
|
echo_info() {
|
||||||
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
|
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
echo_warning() {
|
echo_warning() {
|
||||||
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
|
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
echo_note() {
|
echo_note() {
|
||||||
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
|
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ─< Check if the given command exists silently >─────────────────────────────────────────
|
# ─< Check if the given command exists silently >─────────────────────────────────────────
|
||||||
command_exists() {
|
command_exists() {
|
||||||
command -v "$@" >/dev/null 2>&1
|
command -v "$@" >/dev/null 2>&1
|
||||||
}
|
}
|
||||||
|
|
||||||
# ─< Check if the given command exists silently >─────────────────────────────────────────
|
# ─< Check if the user is root and set sudo variable if necessary >───────────────────────
|
||||||
command_exists() {
|
check_root() {
|
||||||
command -v "$@" >/dev/null 2>&1
|
if [[ "$(id -u)" -ne 0 ]]; then
|
||||||
}
|
if command_exists sudo; then
|
||||||
|
echo_info "User is not root. Using sudo for privileged operations."
|
||||||
# ─< Check if the user is root and set sudo variable if necessary >───────────────────────
|
_sudo="sudo"
|
||||||
check_root() {
|
|
||||||
if [[ "$(id -u)" -ne 0 ]]; then
|
|
||||||
if command_exists sudo; then
|
|
||||||
echo_info "User is not root. Using sudo for privileged operations."
|
|
||||||
_sudo="sudo"
|
|
||||||
else
|
|
||||||
echo_error "No sudo found and you're not root! Can't install packages."
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
echo_info "Root access confirmed."
|
echo_error "No sudo found and you're not root! Can't install packages."
|
||||||
_sudo=""
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
check_root
|
|
||||||
|
|
||||||
gitUserSetup() {
|
|
||||||
local g_username="server-agent"
|
|
||||||
local g_mail="info@team-pieck.de"
|
|
||||||
local g_branch="main"
|
|
||||||
local g_domain="git.k4li.de"
|
|
||||||
|
|
||||||
local CREDENTIALS_FILE="$HOME/.git-credentials"
|
|
||||||
|
|
||||||
git config --global user.name "$g_username"
|
|
||||||
git config --global user.email "$g_mail"
|
|
||||||
git config --global init.defaultBranch "$g_branch"
|
|
||||||
|
|
||||||
if [[ ! -e "$CREDENTIALS_FILE" ]]; then
|
|
||||||
echo_note "Enter your authentication token:"
|
|
||||||
read -r -s g_token </dev/tty # The '-s' flag hides input for privacy
|
|
||||||
# Append the new credentials to the file
|
|
||||||
echo "https://$g_username:$g_token@$g_domain" >>"$CREDENTIALS_FILE"
|
|
||||||
|
|
||||||
echo "Credentials added for $g_domain in $CREDENTIALS_FILE"
|
|
||||||
|
|
||||||
git config --global credential.helper store
|
|
||||||
|
|
||||||
chmod 600 "$HOME/.git-credentials"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
gitDirSetup() {
|
|
||||||
cd /opt/docker || echo_error "could not cd /opt/docker"
|
|
||||||
|
|
||||||
if $_sudo ping -w2 10.255.255.1; then
|
|
||||||
local location="hl"
|
|
||||||
elif $_sudo ping -w2 10.69.69.2; then
|
|
||||||
location="vps"
|
|
||||||
fi
|
|
||||||
|
|
||||||
git init .
|
|
||||||
|
|
||||||
git branch -m main
|
|
||||||
|
|
||||||
if git remote add origin "https://git.k4li.de/homelab/${location}-$(hostname)"; then
|
|
||||||
|
|
||||||
git add .
|
|
||||||
|
|
||||||
git commit -m "Initial script commit"
|
|
||||||
|
|
||||||
git push -u origin main
|
|
||||||
else
|
|
||||||
echo_error 'Failed setting the git repo up with git remote add "$(hostname)" "https://git.k4li.de/homelab/${location}-$(hostname).git"'
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
set_cronjob() {
|
|
||||||
|
|
||||||
# Configuration
|
|
||||||
CRON_COMMAND="/opt/scripts/hlpush.sh"
|
|
||||||
CRON_SCHEDULE="0 3,15 * * *"
|
|
||||||
CRON_LOG="./cronjobs.log"
|
|
||||||
CRON_ENTRY="$CRON_SCHEDULE $CRON_COMMAND >> $CRON_LOG 2>&1"
|
|
||||||
|
|
||||||
# Check for existing entry
|
|
||||||
EXISTING_ENTRIES=$(crontab -l 2>/dev/null)
|
|
||||||
|
|
||||||
# Add entry if not exists
|
|
||||||
if ! echo "$EXISTING_ENTRIES" | grep -qF "$CRON_ENTRY"; then
|
|
||||||
# Create temporary cron file
|
|
||||||
TMPFILE=$(mktemp)
|
|
||||||
|
|
||||||
# Preserve existing entries
|
|
||||||
[ -n "$EXISTING_ENTRIES" ] && echo "$EXISTING_ENTRIES" >"$TMPFILE"
|
|
||||||
|
|
||||||
# Add header and new entry
|
|
||||||
echo -e "\n# Daily 3AM/3PM job added $(date)" >>"$TMPFILE"
|
|
||||||
echo "$CRON_ENTRY" >>"$TMPFILE"
|
|
||||||
|
|
||||||
# Install new cron file
|
|
||||||
crontab "$TMPFILE"
|
|
||||||
rm -f "$TMPFILE"
|
|
||||||
|
|
||||||
echo_info "Success: Cronjob installed"
|
|
||||||
echo_note "Verify with: crontab -l"
|
|
||||||
else
|
|
||||||
echo_warning "Notice: Cronjob already exists"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
if ! command_exists git; then
|
|
||||||
return 1
|
|
||||||
echo_error "You don't have git installed!"
|
|
||||||
fi
|
|
||||||
|
|
||||||
gitUserSetup
|
|
||||||
|
|
||||||
if [[ -d /opt/docker/ ]]; then
|
|
||||||
gitDirSetup
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -d /opt/scripts/bash ]]; then
|
|
||||||
set_cronjob
|
|
||||||
else
|
else
|
||||||
if command_exists curl; then
|
echo_info "Root access confirmed."
|
||||||
curl -o /opt/scripts/hlpush.sh https://git.k4li.de/scripts/bash/raw/branch/main/setup/hlpush.sh
|
_sudo=""
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
check_root
|
||||||
|
|
||||||
|
gitUserSetup() {
|
||||||
|
local g_username="server-agent"
|
||||||
|
local g_mail="info@team-pieck.de"
|
||||||
|
local g_branch="main"
|
||||||
|
local g_domain="git.k4li.de"
|
||||||
|
|
||||||
|
local CREDENTIALS_FILE="$HOME/.git-credentials"
|
||||||
|
|
||||||
|
git config --global user.name "$g_username"
|
||||||
|
git config --global user.email "$g_mail"
|
||||||
|
git config --global init.defaultBranch "$g_branch"
|
||||||
|
|
||||||
|
if [[ ! -e "$CREDENTIALS_FILE" ]]; then
|
||||||
|
echo_note "Enter your authentication token:"
|
||||||
|
read -r -s g_token </dev/tty # The '-s' flag hides input for privacy
|
||||||
|
# Append the new credentials to the file
|
||||||
|
echo "https://$g_username:$g_token@$g_domain" >>"$CREDENTIALS_FILE"
|
||||||
|
|
||||||
|
echo "Credentials added for $g_domain in $CREDENTIALS_FILE"
|
||||||
|
|
||||||
|
git config --global credential.helper store
|
||||||
|
|
||||||
|
chmod 600 "$HOME/.git-credentials"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
gitDirSetup() {
|
||||||
|
cd /opt/docker || echo_error "could not cd /opt/docker"
|
||||||
|
|
||||||
|
if $_sudo ping -w2 10.255.255.1; then
|
||||||
|
local location="hl"
|
||||||
|
elif $_sudo ping -w2 10.69.69.2; then
|
||||||
|
location="vps"
|
||||||
|
fi
|
||||||
|
|
||||||
|
git init .
|
||||||
|
|
||||||
|
git branch -m main
|
||||||
|
|
||||||
|
if git remote add origin "https://git.k4li.de/homelab/${location}-$(hostname)"; then
|
||||||
|
|
||||||
|
git add .
|
||||||
|
|
||||||
|
git commit -m "Initial script commit"
|
||||||
|
|
||||||
|
git push -u origin main
|
||||||
|
else
|
||||||
|
echo_error 'Failed setting the git repo up with git remote add "$(hostname)" "https://git.k4li.de/homelab/${location}-$(hostname).git"'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
set_cronjob() {
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
CRON_COMMAND="/opt/scripts/hlpush.sh"
|
||||||
|
CRON_SCHEDULE="0 3,15 * * *"
|
||||||
|
CRON_LOG="./cronjobs.log"
|
||||||
|
CRON_ENTRY="$CRON_SCHEDULE $CRON_COMMAND >> $CRON_LOG 2>&1"
|
||||||
|
|
||||||
|
# Check for existing entry
|
||||||
|
EXISTING_ENTRIES=$(crontab -l 2>/dev/null)
|
||||||
|
|
||||||
|
# Add entry if not exists
|
||||||
|
if ! echo "$EXISTING_ENTRIES" | grep -qF "$CRON_ENTRY"; then
|
||||||
|
# Create temporary cron file
|
||||||
|
TMPFILE=$(mktemp)
|
||||||
|
|
||||||
|
# Preserve existing entries
|
||||||
|
[ -n "$EXISTING_ENTRIES" ] && echo "$EXISTING_ENTRIES" >"$TMPFILE"
|
||||||
|
|
||||||
|
# Add header and new entry
|
||||||
|
echo -e "\n# Daily 3AM/3PM job added $(date)" >>"$TMPFILE"
|
||||||
|
echo "$CRON_ENTRY" >>"$TMPFILE"
|
||||||
|
|
||||||
|
# Install new cron file
|
||||||
|
crontab "$TMPFILE"
|
||||||
|
rm -f "$TMPFILE"
|
||||||
|
|
||||||
|
echo_info "Success: Cronjob installed"
|
||||||
|
echo_note "Verify with: crontab -l"
|
||||||
|
else
|
||||||
|
echo_warning "Notice: Cronjob already exists"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
if ! command_exists git; then
|
||||||
|
return 1
|
||||||
|
echo_error "You don't have git installed!"
|
||||||
|
fi
|
||||||
|
|
||||||
|
gitUserSetup
|
||||||
|
|
||||||
|
if [[ -d /opt/docker/ ]]; then
|
||||||
|
gitDirSetup
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -d /opt/scripts/bash ]]; then
|
||||||
|
set_cronjob
|
||||||
|
else
|
||||||
|
if command_exists curl; then
|
||||||
|
curl -o /opt/scripts/hlpush.sh https://git.k4li.de/scripts/bash/raw/branch/main/setup/hlpush.sh
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue