Full blog engine source: build_blog.py, content, deploy scripts
This commit is contained in:
70
add_robots_location.py
Normal file
70
add_robots_location.py
Normal file
@ -0,0 +1,70 @@
|
||||
"""Safely add robots.txt location block to gfil-lab.com Nginx config"""
|
||||
import paramiko
|
||||
|
||||
JD_HOST = "111.228.37.165"
|
||||
JD_USER = "root"
|
||||
JD_PASS = "Liudecai110"
|
||||
|
||||
LAB_HOST = "216.144.233.14"
|
||||
LAB_USER = "root"
|
||||
LAB_PASS = "Kt9V72Tx2c48ChikKU"
|
||||
|
||||
jd = paramiko.SSHClient()
|
||||
jd.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
jd.connect(JD_HOST, port=22, username=JD_USER, password=JD_PASS,
|
||||
timeout=20, banner_timeout=60, allow_agent=False, look_for_keys=False)
|
||||
|
||||
# Use python on remote to do precise insertion instead of sed
|
||||
cmd = f"""sshpass -p '{LAB_PASS}' ssh -o StrictHostKeyChecking=no {LAB_USER}@{LAB_HOST} '
|
||||
# Backup first
|
||||
cp /etc/nginx/sites-enabled/gfil /etc/nginx/sites-enabled/gfil.bak.before-robots
|
||||
|
||||
# Use python to insert robots.txt block precisely before "location / {"
|
||||
python3 -c "
|
||||
import re
|
||||
with open(\"/etc/nginx/sites-enabled/gfil\", \"r\") as f:
|
||||
content = f.read()
|
||||
|
||||
robots_block = \"\"\" location = /robots.txt {
|
||||
alias /var/www/gfil-lab/robots.txt;
|
||||
default_type text/plain;
|
||||
}
|
||||
|
||||
\"\"\"
|
||||
|
||||
# Insert before the first \"location / {\"
|
||||
if \"robots.txt\" not in content:
|
||||
content = content.replace(\" location / {\", robots_block + \" location / {\")
|
||||
with open(\"/etc/nginx/sites-enabled/gfil\", \"w\") as f:
|
||||
f.write(content)
|
||||
print(\"INSERTED robots.txt block\")
|
||||
else:
|
||||
print(\"robots.txt block already exists, skipping\")
|
||||
"
|
||||
|
||||
# Verify
|
||||
echo "=== Updated config ==="
|
||||
cat /etc/nginx/sites-enabled/gfil
|
||||
|
||||
# Test
|
||||
nginx -t 2>&1
|
||||
if [ $? -eq 0 ]; then
|
||||
systemctl reload nginx
|
||||
echo "SUCCESS: Nginx reloaded"
|
||||
else
|
||||
echo "FAILED: rolling back"
|
||||
cp /etc/nginx/sites-enabled/gfil.bak.before-robots /etc/nginx/sites-enabled/gfil
|
||||
nginx -t 2>&1
|
||||
systemctl reload nginx
|
||||
echo "Rolled back to original"
|
||||
fi
|
||||
|
||||
# Verify site still works
|
||||
curl -s -o /dev/null -w "Site: HTTP %{{http_code}}" http://localhost/ 2>/dev/null
|
||||
|
||||
# Verify robots.txt
|
||||
curl -s http://localhost/robots.txt 2>/dev/null | head -5
|
||||
'"""
|
||||
stdin, stdout, stderr = jd.exec_command(cmd, timeout=30)
|
||||
print(stdout.read().decode())
|
||||
jd.close()
|
||||
Reference in New Issue
Block a user