gitfield-netmon
1 #!/bin/bash 2 set -eo pipefail 3 IFS=$'\n\t' 4 5 # Configuration 6 GIT_REMOTE_NAME="gogs" 7 GOGS_DOMAIN="netmon.thefoldwithin.earth" 8 GOGS_API="https://$GOGS_DOMAIN/api/v1" 9 USERNAME="mrhavens" 10 REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || { echo "[ERROR] Not inside a git repository." >&2; exit 1; } 11 REPO_NAME=$(basename "$REPO_ROOT") || { echo "[ERROR] Failed to get repository name" >&2; exit 1; } 12 MARKDOWN_FILE="$REPO_ROOT/.gitfield/gogs.sigil.md" 13 DEFAULT_NAME="Mark Randall Havens" 14 DEFAULT_EMAIL="mark.r.havens@gmail.com" 15 TOKEN_FILE="$HOME/.gitfield_token_gogs" 16 SCRIPT_VERSION="2.3" 17 18 # Logging functions 19 info() { echo -e "\e[1;34m[INFO]\e[0m ${*:-}"; } 20 warn() { echo -e "\e[1;33m[WARN]\e[0m ${*:-}"; } 21 error() { echo -e "\e[1;31m[ERROR]\e[0m ${*:-}" >&2; exit 1; } 22 23 # Check for required tools 24 info "Checking for required tools..." 25 for cmd in git curl jq; do 26 command -v "$cmd" >/dev/null || { 27 sudo apt update -qq || warn "Failed to update package lists, continuing..." 28 sudo apt install -y git curl jq || error "Failed to install $cmd" 29 } 30 done 31 32 # Function to prompt for GOGS token or password 33 prompt_for_credentials() { 34 info "Credentials required." 35 echo "š Generate a token at https://$GOGS_DOMAIN/user/settings/applications (Recommended)" 36 echo " - REQUIRED: Select the 'write:repository' scope" 37 echo "š Alternatively, use your GOGS password" 38 echo "š Paste your GOGS Personal Access Token or Password (will not be echoed):" 39 read -rsp "Token/Password: " CRED 40 echo 41 [[ -z "$CRED" ]] && error "Credentials cannot be empty" 42 echo "$CRED" > "$TOKEN_FILE" || error "Failed to write credentials to $TOKEN_FILE" 43 chmod 600 "$TOKEN_FILE" || error "Failed to set permissions on $TOKEN_FILE" 44 info "Credentials saved at $TOKEN_FILE" 45 } 46 47 # Handle credentials 48 RESET_AUTH=false 49 if [[ "${1:-}" == "--reset-auth" ]]; then 50 RESET_AUTH=true 51 rm -f "$TOKEN_FILE" "$HOME/.git-credentials" 2>/dev/null || warn "Failed to remove credential files" 52 info "Authentication reset requested." 53 fi 54 55 if [[ -f "$TOKEN_FILE" && "$RESET_AUTH" == false ]]; then 56 CRED=$(cat "$TOKEN_FILE" 2>/dev/null) || error "Failed to read credentials from $TOKEN_FILE" 57 info "Using cached credentials from $TOKEN_FILE" 58 else 59 prompt_for_credentials 60 fi 61 62 # Verify GOGS token 63 info "Verifying GOGS credentials (read access)..." 64 TOKEN_TEST=$(curl -k -s -H "Authorization: token $CRED" "$GOGS_API/user" | jq -r .login 2>/dev/null || echo "") 65 if [[ "$TOKEN_TEST" != "$USERNAME" ]]; then 66 warn "Token verification failed. Credentials may be a password or invalid token." 67 # Retry with credentials as password if token fails 68 PASSWORD_TEST=$(curl -k -s -u "$USERNAME:$CRED" "$GOGS_API/user" | jq -r .login 2>/dev/null || echo "") 69 if [[ "$PASSWORD_TEST" != "$USERNAME" ]]; then 70 warn "Password verification also failed. Please provide valid credentials." 71 rm -f "$TOKEN_FILE" 72 prompt_for_credentials 73 TOKEN_TEST=$(curl -k -s -H "Authorization: token $CRED" "$GOGS_API/user" | jq -r .login 2>/dev/null || echo "") 74 PASSWORD_TEST=$(curl -k -s -u "$USERNAME:$CRED" "$GOGS_API/user" | jq -r .login 2>/dev/null || echo "") 75 [[ "$TOKEN_TEST" != "$USERNAME" && "$PASSWORD_TEST" != "$USERNAME" ]] && error "New credentials verification failed. Ensure they are valid." 76 fi 77 info "Credentials verified as password: $PASSWORD_TEST" 78 else 79 info "Credentials verified as token: $TOKEN_TEST" 80 fi 81 82 # Test write access via API 83 info "Testing write access via API..." 84 TEST_REPO="test-repo-$(date +%s)" 85 WRITE_TEST=$(curl -k -v -H "Authorization: token $CRED" -X POST "$GOGS_API/user/repos" -H "Content-Type: application/json" -d "{\"name\": \"$TEST_REPO\", \"description\": \"Test\", \"private\": false, \"auto_init\": false}" 2>&1) 86 if [[ $? -ne 0 || $(echo "$WRITE_TEST" | grep -i "401" 2>/dev/null) ]]; then 87 warn "Write access test failed with token: $WRITE_TEST" 88 WRITE_TEST=$(curl -k -v -u "$USERNAME:$CRED" -X POST "$GOGS_API/user/repos" -H "Content-Type: application/json" -d "{\"name\": \"$TEST_REPO\", \"description\": \"Test\", \"private\": false, \"auto_init\": false}" 2>&1) 89 if [[ $? -ne 0 || $(echo "$WRITE_TEST" | grep -i "401" 2>/dev/null) ]]; then 90 error "Write access failed with both token and password. Check GOGS configuration." 91 fi 92 info "Write access test passed with password: $WRITE_TEST" 93 else 94 info "Write access test passed with token: $WRITE_TEST" 95 fi 96 97 # Test Git push with credentials 98 info "Testing Git push with credentials..." 99 GIT_TEST=$(git ls-remote --heads "https://$USERNAME:$CRED@$GOGS_DOMAIN/$USERNAME/$REPO_NAME.git" 2>&1) 100 if [[ $? -ne 0 || $(echo "$GIT_TEST" | grep -i "401" 2>/dev/null) ]]; then 101 warn "Git push test failed with token: $GIT_TEST" 102 GIT_TEST=$(git ls-remote --heads "https://$USERNAME:$CRED@$GOGS_DOMAIN/$USERNAME/$REPO_NAME.git" 2>&1) 103 if [[ $? -ne 0 || $(echo "$GIT_TEST" | grep -i "401" 2>/dev/null) ]]; then 104 warn "Git push test also failed with password. This suggests a GOGS Git-over-HTTP issue." 105 warn "1. Edit /home/git/gogs/custom/conf/app.ini and ensure:" 106 warn " [auth] ENABLE_ACCESS_TOKEN = true" 107 warn " [git] DISABLE_HTTP_GIT = false" 108 warn "2. Restart GOGS: sudo systemctl restart gogs" 109 warn "3. Try manual push with token: git push https://$USERNAME:$CRED@$GOGS_DOMAIN/$USERNAME/$REPO_NAME.git $DEFAULT_BRANCH" 110 warn "4. Try manual push with password: git push https://$USERNAME:$CRED@$GOGS_DOMAIN/$USERNAME/$REPO_NAME.git $DEFAULT_BRANCH" 111 warn "5. Check GOGS logs: sudo tail -f /home/git/gogs/log/gogs.log" 112 error "Git push test failed. Adjust GOGS configuration or use manual workaround." 113 fi 114 info "Git push test passed with password: $GIT_TEST" 115 else 116 info "Git push test passed with token: $GIT_TEST" 117 fi 118 119 # Set git user info 120 git config --global user.name "$DEFAULT_NAME" || warn "Failed to set git user name" 121 git config --global user.email "$DEFAULT_EMAIL" || warn "Failed to set git user email" 122 info "Git identity set to: $DEFAULT_NAME <$DEFAULT_EMAIL>" 123 124 # Ensure at least one commit exists 125 if ! git rev-parse HEAD &>/dev/null; then 126 error "No commits found. Please add and commit files first." 127 fi 128 129 # Configure git credentials for HTTPS 130 info "Configuring git credentials for HTTPS..." 131 if [[ "$TOKEN_TEST" == "$USERNAME" ]]; then 132 echo "https://$USERNAME:$CRED@$GOGS_DOMAIN" > "$HOME/.git-credentials" || error "Failed to write git credentials" 133 else 134 echo "https://$USERNAME:$CRED@$GOGS_DOMAIN" > "$HOME/.git-credentials" || error "Failed to write git credentials" 135 fi 136 chmod 600 "$HOME/.git-credentials" || error "Failed to set permissions on git credentials" 137 138 # Check and create GOGS repository 139 info "Checking if repository exists..." 140 EXISTS=$(curl -k -s -H "Authorization: token $CRED" "$GOGS_API/repos/$USERNAME/$REPO_NAME" | jq -r .name 2>/dev/null || echo "") 141 if [[ "$EXISTS" != "$REPO_NAME" ]]; then 142 info "Creating repository $REPO_NAME on GOGS..." 143 CURL_OUTPUT=$(curl -k -s --fail -X POST "$GOGS_API/user/repos" \ 144 -H "Authorization: token $CRED" \ 145 -H "Content-Type: application/json" \ 146 -d "{\"name\": \"$REPO_NAME\", \"description\": \"Created via gitfield-gogs\", \"private\": false, \"auto_init\": false}" 2>&1) || { 147 warn "Failed to create repository with token: $CURL_OUTPUT" 148 CURL_OUTPUT=$(curl -k -s --fail -u "$USERNAME:$CRED" -X POST "$GOGS_API/user/repos" \ 149 -H "Content-Type: application/json" \ 150 -d "{\"name\": \"$REPO_NAME\", \"description\": \"Created via gitfield-gogs\", \"private\": false, \"auto_init\": false}" 2>&1) 151 if [[ $? -ne 0 ]]; then 152 error "Repository creation failed with both token and password. Check GOGS configuration." 153 fi 154 info "Repository created successfully with password." 155 } 156 info "Repository created successfully with token." 157 fi 158 159 # Set up git remote 160 REMOTE_URL="https://$GOGS_DOMAIN/$USERNAME/$REPO_NAME.git" 161 if ! git remote get-url "$GIT_REMOTE_NAME" &>/dev/null; then 162 info "Adding remote $GIT_REMOTE_NAME..." 163 git remote add "$GIT_REMOTE_NAME" "$REMOTE_URL" || error "Failed to add remote $GIT_REMOTE_NAME" 164 else 165 info "Updating remote $GIT_REMOTE_NAME..." 166 git remote set-url "$GIT_REMOTE_NAME" "$REMOTE_URL" || error "Failed to set remote URL for $GIT_REMOTE_NAME" 167 fi 168 169 # Generate metadata file 170 mkdir -p "$(dirname "$MARKDOWN_FILE")" || error "Failed to create directory for $MARKDOWN_FILE" 171 TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') || error "Failed to get timestamp" 172 DEFAULT_BRANCH=$(git symbolic-ref --short HEAD) || error "Failed to get default branch" 173 REPO_PATH="$REPO_ROOT" 174 LATEST_SHA=$(git rev-parse HEAD) || error "Failed to get latest commit SHA" 175 LAST_COMMIT_MSG=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Unknown") 176 LAST_COMMIT_DATE=$(git log -1 --pretty=format:"%ad" 2>/dev/null || echo "Unknown") 177 LAST_COMMIT_AUTHOR=$(git log -1 --pretty=format:"%an <%ae>" 2>/dev/null || echo "Unknown") 178 TOTAL_COMMITS=$(git rev-list --count HEAD 2>/dev/null || echo "Unknown") 179 TRACKED_FILES=$(git ls-files 2>/dev/null | wc -l 2>/dev/null || echo "Unknown") 180 UNCOMMITTED=$(if ! git diff --quiet 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then echo "Yes"; else echo "No"; fi) 181 LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "None") 182 HOSTNAME=$(hostname 2>/dev/null || echo "Unknown") 183 CURRENT_USER=$(whoami 2>/dev/null || echo "Unknown") 184 TIMEZONE=$(date +%Z 2>/dev/null || echo "Unknown") 185 OS_NAME=$(uname -s 2>/dev/null || echo "Unknown") 186 KERNEL_VERSION=$(uname -r 2>/dev/null || echo "Unknown") 187 ARCHITECTURE=$(uname -m 2>/dev/null || echo "Unknown") 188 OS_PRETTY_NAME=$(grep PRETTY_NAME /etc/os-release 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "Unknown") 189 WEB_LINK="https://$GOGS_DOMAIN/$USERNAME/$REPO_NAME" 190 191 cat > "$MARKDOWN_FILE" <<EOF 192 # š GOGS Repository Link 193 194 - **Repo Name**: \`$REPO_NAME\` 195 - **GOGS User**: \`$USERNAME\` 196 - **Remote URL**: [$WEB_LINK]($WEB_LINK) 197 - **Local Repo Path**: \`$REPO_PATH\` 198 - **Remote Label**: \`$GIT_REMOTE_NAME\` 199 - **Default Branch**: \`$DEFAULT_BRANCH\` 200 - **Repo Created**: \`$TIMESTAMP\` 201 202 --- 203 204 ## š¦ Commit Info 205 206 - **This Commit Timestamp**: \`$TIMESTAMP\` 207 - **Last Commit SHA**: \`$LATEST_SHA\` 208 - **Last Commit Message**: \`$LAST_COMMIT_MSG\` 209 - **Last Commit Author**: \`$LAST_COMMIT_AUTHOR\` 210 - **Last Commit Date**: \`$LAST_COMMIT_DATE\` 211 - **This Commit URL**: [$WEB_LINK/commit/$LATEST_SHA]($WEB_LINK/commit/$LATEST_SHA) 212 213 --- 214 215 ## š Repo Status 216 217 - **Total Commits**: \`$TOTAL_COMMITS\` 218 - **Tracked Files**: \`$TRACKED_FILES\` 219 - **Uncommitted Changes**: \`$UNCOMMITTED\` 220 - **Latest Tag**: \`$LATEST_TAG\` 221 222 --- 223 224 ## š§ Environment 225 226 - **Host Machine**: \`$HOSTNAME\` 227 - **Current User**: \`$CURRENT_USER\` 228 - **Time Zone**: \`$TIMEZONE\` 229 - **Script Version**: \`$SCRIPT_VERSION\` 230 231 --- 232 233 ## 𧬠Hardware & OS Fingerprint 234 235 - **OS Name**: \`$OS_NAME\` 236 - **OS Version**: \`$OS_PRETTY_NAME\` 237 - **Kernel Version**: \`$KERNEL_VERSION\` 238 - **Architecture**: \`$ARCHITECTURE\` 239 - **System Uptime**: \$(uptime -p 2>/dev/null || echo "Unknown")\` 240 - **Local IP**: \$(hostname -I 2>/dev/null | awk '{print $1}' 2>/dev/null || echo "Unknown")\` 241 - **CPU Model**: \$(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2 | sed 's/^ //' 2>/dev/null || echo "Unknown")\` 242 - **Total RAM (GB)**: \$(awk '/MemTotal/ {printf "%.2f", $2/1024/1024}' /proc/meminfo 2>/dev/null || echo "Unknown")\` 243 244 --- 245 246 _Auto-generated by \`gitfield-gogs\` push script._ 247 EOF 248 [[ $? -eq 0 ]] || error "Failed to write metadata to $MARKDOWN_FILE" 249 250 # Commit and push 251 set +e 252 info "Committing markdown file..." 253 git add "$MARKDOWN_FILE" || warn "Failed to add markdown file" 254 git commit -m "GOGS metadata link commit at $TIMESTAMP ā $WEB_LINK/commit/$LATEST_SHA" || warn "No changes to commit" 255 256 info "Pushing to GOGS..." 257 if ! git config --get branch."$DEFAULT_BRANCH".remote &>/dev/null; then 258 if ! git push -u "$GIT_REMOTE_NAME" "$DEFAULT_BRANCH" 2>&1 | tee /tmp/git-push.log; then 259 warn "Push to GOGS failed. Check /tmp/git-push.log for details." 260 if grep -q "401" /tmp/git-push.log; then 261 warn "HTTP 401 error detected. Token or password failed for Git push." 262 warn "This suggests a GOGS Git-over-HTTP configuration issue." 263 warn "1. Edit /home/git/gogs/custom/conf/app.ini and ensure:" 264 warn " [auth] ENABLE_ACCESS_TOKEN = true" 265 warn " [git] DISABLE_HTTP_GIT = false" 266 warn "2. Restart GOGS: sudo systemctl restart gogs" 267 warn "3. Try manual push with current credentials: git push https://$USERNAME:$CRED@$GOGS_DOMAIN/$USERNAME/$REPO_NAME.git $DEFAULT_BRANCH" 268 warn "4. Check GOGS logs: sudo tail -f /home/git/gogs/log/gogs.log" 269 error "Push failed. Adjust GOGS configuration or verify credentials." 270 else 271 error "Failed to push to $REMOTE_URL. Check network or GOGS server." 272 fi 273 fi 274 else 275 if ! git push "$GIT_REMOTE_NAME" "$DEFAULT_BRANCH" 2>&1 | tee /tmp/git-push.log; then 276 warn "Push to GOGS failed. Check /tmp/git-push.log for details." 277 if grep -q "401" /tmp/git-push.log; then 278 warn "HTTP 401 error detected. Token or password failed for Git push." 279 warn "This suggests a GOGS Git-over-HTTP configuration issue." 280 warn "1. Edit /home/git/gogs/custom/conf/app.ini and ensure:" 281 warn " [auth] ENABLE_ACCESS_TOKEN = true" 282 warn " [git] DISABLE_HTTP_GIT = false" 283 warn "2. Restart GOGS: sudo systemctl restart gogs" 284 warn "3. Try manual push with current credentials: git push https://$USERNAME:$CRED@$GOGS_DOMAIN/$USERNAME/$REPO_NAME.git $DEFAULT_BRANCH" 285 warn "4. Check GOGS logs: sudo tail -f /home/git/gogs/log/gogs.log" 286 error "Push failed. Adjust GOGS configuration or verify credentials." 287 else 288 error "Failed to push to $REMOTE_URL. Check network or GOGS server." 289 fi 290 fi 291 fi 292 set -e 293 294 info "ā GOGS push complete." 295 echo -e "\nš View in browser: $WEB_LINK\n"