Automated update

This commit is contained in:
klein panic
2024-09-29 15:41:04 -04:00
parent 89d9326bc1
commit 1ee7d36d15
8 changed files with 423 additions and 42 deletions

View File

@@ -12,7 +12,6 @@ fi
# If the security variable is 2, continue without any conditions
if [ "$security_variable" -eq 2 ]; then
echo "security_variable is set to 2. Running the script without further checks."
# Continue with the rest of your script
else
# If the security variable is 1, check if today is Friday
DAY_OF_WEEK=$(date +%A)
@@ -34,15 +33,32 @@ done
# Configuration
TXT_FILE="/home/klein/.config/setup/autogitupdate.txt"
PHONE_NUMBER="2673479614@vtext.com" # Verizon SMS gateway
EMAIL="kleinpanic@gmail.com"
SUBJECT="G-U-R"
CREDENTIALS_FILE="$HOME/.config/setup/credentials.config"
REPORT="/tmp/git_update_report.txt" # Full detailed report
SMS_REPORT="/tmp/git_update_sms.txt" # Minimal SMS summary
SSH_HOST="eulerpi5" # Alias for the SSH connection
SSH_DIR="/home/klein/reports" # Remote directory to store the report
SMS_CHAR_LIMIT=160 # Character limit for SMS messages
# Check for the existence of the configuration file
if [ ! -f "$TXT_FILE" ]; then
echo "Error: Configuration file '$TXT_FILE' not found. Exiting the script."
exit 1
fi
# Check if the credentials file exists
if [ ! -f "$CREDENTIALS_FILE" ]; then
echo "Error: Credentials file '$CREDENTIALS_FILE' not found. Exiting the script."
exit 1
fi
# Source the credentials from the config file
source "$CREDENTIALS_FILE"
# Validate that required variables are set
if [ -z "$EMAIL" ] || [ -z "$SSH_HOST" ] || [ -z "$PHONE_NUMBER" ]; then
echo "Error: Missing required credentials (EMAIL, SSH_HOST, or PHONE_NUMBER) in '$CREDENTIALS_FILE'. Exiting the script."
exit 1
fi
# Ensure the report files exist and have the correct permissions
ensure_file() {
local file="$1"
@@ -147,6 +163,44 @@ check_github_remote() {
return 0
}
# Function to check if the repository is up-to-date
is_repo_up_to_date() {
git remote update &>/dev/null
if git status -uno | grep -q "Your branch is up to date"; then
echo "Repository is up to date. No fetch needed."
return 0
else
echo "Repository is not up to date. Fetching changes..."
return 1
fi
}
# Function to check if the .git folder is valid
is_valid_git_repo() {
local repo_dir="$1"
if [ ! -d "$repo_dir/.git" ]; then
echo "Error: '.git' directory is missing in '$repo_dir'. Skipping this repository." >> "$REPORT"
return 1
fi
# Check for essential .git files
for file in HEAD config; do
if [ ! -f "$repo_dir/.git/$file" ]; then
echo "Error: Missing '$file' in '.git' directory of '$repo_dir'. Skipping this repository." >> "$REPORT"
return 1
fi
done
# Ensure the repository has at least one commit
if ! git -C "$repo_dir" rev-parse HEAD &>/dev/null; then
echo "Error: The repository in '$repo_dir' appears to be corrupt or does not have any commits." >> "$REPORT"
return 1
fi
return 0
}
# Function to update, commit, and push the changes
update_repo() {
local repo_dir="$1"
@@ -155,6 +209,12 @@ update_repo() {
echo "Processing repository #$repo_number located at $repo_dir" >> "$REPORT"
# Validate .git folder before proceeding
if ! is_valid_git_repo "$repo_dir"; then
echo "Repo #$repo_number: Invalid .git folder, skipping..." >> "$SMS_REPORT"
return 1
fi
local github_exists=true
if ! check_github_remote; then
echo "No valid GitHub repository found for repo #$repo_number." >> "$REPORT"
@@ -168,7 +228,12 @@ update_repo() {
fi
if [ "$github_exists" = true ]; then
git fetch origin &>/dev/null
if is_repo_up_to_date; then
return 0 # Exit the update function early since there's nothing to fetch
else
git fetch origin &>/dev/null # Proceed with the fetch if not up-to-date
fi
if ! git merge origin/main --no-commit --no-ff; then
echo "Merge conflict detected in repo #$repo_number" >> "$REPORT"
echo "Repo #$repo_number: MC" >> "$SMS_REPORT"