mirror of
https://github.com/wickedyoda/public-setupfiles.git
synced 2026-11-16 13:31:56 -06:00
Fix update-from_repo scripts to pull if already cloned
Fixes Copilot review comments: - Added error checking on package-manager calls - Verified git availability post-install - Scripts now fail fast on installer errors
This commit is contained in:
1 parent
bcbe8b1d40
commit
cddd0be078
3 files changed
+130
-46
No files matched your search
File renamed without changes.
Regular → Executable
+91
-31
@@ -1,37 +1,97 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Update or clone the public-setupfiles repository.
|
||||||
|
If already inside the repo, pulls latest changes.
|
||||||
|
Otherwise clones from GitHub to ./public-setupfiles.
|
||||||
|
"""
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
subprocess.run(["apt", "install", "python", "python3", "-y"])
|
|
||||||
|
|
||||||
# run apt update to make sure the system has the latest repo files
|
|
||||||
subprocess.run(["apt", "update", "-y"])
|
|
||||||
|
|
||||||
# Check if git is installed
|
|
||||||
try:
|
|
||||||
subprocess.run(["git", "--version"], check=True)
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
# Install git
|
|
||||||
subprocess.run(["apt-get", "install", "git", "-y"])
|
|
||||||
|
|
||||||
# Specify the remote repository URL
|
|
||||||
remote_repo = "https://github.com/wickedyoda/public-setupfiles.git"
|
|
||||||
|
|
||||||
# Specify the local file path
|
|
||||||
local_file = "./pubic-setupfiles"
|
|
||||||
|
|
||||||
|
|
||||||
# Check if the local file path exists and is not empty
|
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
if os.path.isdir(local_file) and os.listdir(local_file):
|
remote_repo = "https://github.com/wickedyoda/public-setupfiles.git"
|
||||||
# Delete the existing directory
|
local_dir = "public-setupfiles"
|
||||||
subprocess.run(["sudo", "rm", "-rf", local_file])
|
|
||||||
|
|
||||||
# Clone the remote repository to the local file pathh
|
|
||||||
subprocess.run(["git", "clone", remote_repo, local_file])
|
|
||||||
|
|
||||||
|
|
||||||
# Make all files executable and public ownership
|
def run(cmd, **kwargs):
|
||||||
subprocess.run(["sudo", "chmod", "777", "-R", local_file])
|
"""Run a command and return the result."""
|
||||||
|
print(f" Running: {' '.join(cmd)}")
|
||||||
|
return subprocess.run(cmd, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def install_git():
|
||||||
|
"""Install git if not present."""
|
||||||
|
# Already installed?
|
||||||
|
if shutil.which("git"):
|
||||||
|
return True
|
||||||
|
|
||||||
|
if shutil.which("apt-get"):
|
||||||
|
subprocess.run(["apt-get", "update", "-y"], check=True)
|
||||||
|
subprocess.run(["apt-get", "install", "-y", "git"], check=True)
|
||||||
|
elif shutil.which("opkg"):
|
||||||
|
subprocess.run(["opkg", "update"], check=True)
|
||||||
|
subprocess.run(["opkg", "install", "git", "git-http"], check=True)
|
||||||
|
else:
|
||||||
|
print("Error: Neither apt-get nor opkg found to install git.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Verify git is actually available after install
|
||||||
|
return shutil.which("git") is not None
|
||||||
|
|
||||||
|
|
||||||
|
def is_in_repo():
|
||||||
|
"""Check if we're inside the public-setupfiles git repo."""
|
||||||
|
if not os.path.isdir(".git"):
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
["git", "remote", "get-url", "origin"],
|
||||||
|
capture_output=True, text=True
|
||||||
|
)
|
||||||
|
return "wickedyoda/public-setupfiles" in result.stdout
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if os.geteuid() != 0:
|
||||||
|
print("Warning: Not running as root. Some operations may fail.")
|
||||||
|
|
||||||
|
if not install_git():
|
||||||
|
print("Failed to install or find git. Exiting.")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# If we're already inside the cloned repo, pull latest
|
||||||
|
if is_in_repo():
|
||||||
|
print("Already in public-setupfiles repo. Pulling latest changes...")
|
||||||
|
result = run(["git", "pull", "origin", "main"])
|
||||||
|
if result.returncode == 0:
|
||||||
|
print("Update complete!")
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
print(f"Update failed with exit code {result.returncode}")
|
||||||
|
return result.returncode
|
||||||
|
|
||||||
|
# Check if directory already exists
|
||||||
|
if os.path.isdir(local_dir) and os.listdir(local_dir):
|
||||||
|
print(f"Directory '{local_dir}' exists. Pulling latest changes...")
|
||||||
|
os.chdir(local_dir)
|
||||||
|
result = run(["git", "pull", "origin", "main"])
|
||||||
|
if result.returncode != 0:
|
||||||
|
print("Git pull failed, re-cloning...")
|
||||||
|
os.chdir("..")
|
||||||
|
shutil.rmtree(local_dir)
|
||||||
|
run(["git", "clone", remote_repo, local_dir])
|
||||||
|
else:
|
||||||
|
os.chdir(local_dir)
|
||||||
|
else:
|
||||||
|
print("Cloning repository...")
|
||||||
|
run(["git", "clone", remote_repo, local_dir])
|
||||||
|
os.chdir(local_dir)
|
||||||
|
|
||||||
|
subprocess.run(["chmod", "-R", "755", "."], check=True)
|
||||||
|
print("Done!")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
exit(main())
|
||||||
Regular → Executable
+39
-15
@@ -1,4 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
# Ensure script is run as root
|
# Ensure script is run as root
|
||||||
if [ "$(id -u)" -ne 0 ]; then
|
if [ "$(id -u)" -ne 0 ]; then
|
||||||
@@ -6,8 +7,8 @@ if [ "$(id -u)" -ne 0 ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Function to auto-install git dynamically based on platform
|
# Auto-install Git if missing instead of failing silently
|
||||||
install_git() {
|
if ! command -v git >/dev/null 2>&1; then
|
||||||
if command -v apt-get >/dev/null 2>&1; then
|
if command -v apt-get >/dev/null 2>&1; then
|
||||||
echo "Debian detected. Installing git..."
|
echo "Debian detected. Installing git..."
|
||||||
apt-get update && apt-get install -y git
|
apt-get update && apt-get install -y git
|
||||||
@@ -15,25 +16,48 @@ install_git() {
|
|||||||
echo "OpenWrt detected. Installing git..."
|
echo "OpenWrt detected. Installing git..."
|
||||||
opkg update && opkg install git git-http
|
opkg update && opkg install git git-http
|
||||||
else
|
else
|
||||||
echo "Error: Neither apt nor opkg found."
|
echo "Error: Neither apt-get nor opkg found to install git."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
# Verify git is actually available after install
|
||||||
|
if ! command -v git >/dev/null 2>&1; then
|
||||||
|
echo "Error: Git installation failed. git is not available."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
# Auto-install Git if missing instead of failing silently
|
|
||||||
if ! command -v git >/dev/null 2>&1; then
|
|
||||||
install_git
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
REMOTE_REPO="https://github.com/wickedyoda/public-setupfiles.git"
|
REMOTE_REPO="https://github.com/wickedyoda/public-setupfiles.git"
|
||||||
LOCAL_DIR="./public-setupfiles"
|
LOCAL_DIR="public-setupfiles"
|
||||||
|
|
||||||
if [ -d "$LOCAL_DIR" ] && [ "$(ls -A "$LOCAL_DIR" 2>/dev/null)" ]; then
|
# If we're already inside the cloned repo, pull latest
|
||||||
echo "Cleaning old directory..."
|
if [ -d ".git" ] && git remote get-url origin 2>/dev/null | grep -q "wickedyoda/public-setupfiles"; then
|
||||||
rm -rf "$LOCAL_DIR"
|
echo "Already in public-setupfiles repo. Pulling latest changes..."
|
||||||
|
git pull origin main
|
||||||
|
exit_code=$?
|
||||||
|
if [ $exit_code -eq 0 ]; then
|
||||||
|
echo "Update complete!"
|
||||||
|
else
|
||||||
|
echo "Update failed with exit code $exit_code"
|
||||||
|
exit $exit_code
|
||||||
|
fi
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Cloning repository..."
|
# Check if directory already exists
|
||||||
git clone "$REMOTE_REPO" "$LOCAL_DIR"
|
if [ -d "$LOCAL_DIR" ] && [ "$(ls -A "$LOCAL_DIR" 2>/dev/null)" ]; then
|
||||||
chmod -R 755 "$LOCAL_DIR"
|
echo "Existing directory found. Pulling latest changes..."
|
||||||
|
cd "$LOCAL_DIR" || exit 1
|
||||||
|
if ! git pull origin main; then
|
||||||
|
echo "Git pull failed, re-cloning..."
|
||||||
|
cd ..
|
||||||
|
rm -rf "$LOCAL_DIR"
|
||||||
|
git clone "$REMOTE_REPO" "$LOCAL_DIR"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "Cloning repository..."
|
||||||
|
git clone "$REMOTE_REPO" "$LOCAL_DIR"
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd "$LOCAL_DIR" || exit 1
|
||||||
|
chmod -R 755 .
|
||||||
echo "Done!"
|
echo "Done!"
|
||||||
Reference in new issue
Block a user