major refactor of setup procedure

This commit is contained in:
Matthew Tran
2025-02-17 23:15:07 -08:00
parent fff87a07ad
commit be0530cafe
16 changed files with 349 additions and 232 deletions
-1
View File
@@ -18,5 +18,4 @@ COPY html /var/www/matthewtran.com/html
# start script
WORKDIR /root
COPY sendgrid.ke[y] ip_update.py ./
COPY cert_update.py ./
COPY entry.sh ./
-10
View File
@@ -1,10 +0,0 @@
#!/usr/bin/env python3
import subprocess
import time
if __name__ == '__main__':
while True:
# try renew once a day
subprocess.run(['certbot', 'renew', '--quiet'])
time.sleep(86400)
+11 -5
View File
@@ -1,26 +1,32 @@
#!/bin/sh
# server needs to be up to grab certificates
# server needs to be up to get certs
nginx
while [ ! -f /var/run/nginx.pid ]
do
sleep 1
done
# get certs if needed
certbot --nginx \
--webroot-path /var/www/matthewtran.com \
--non-interactive --agree-tos -m matthewlamtran@berkeley.edu \
-d matthewtran.com \
-d www.matthewtran.com \
-d git.matthewtran.com
nginx -s reload
python3 ip_update.py &
python3 cert_update.py &
# background process to renew certs and check ip changes
update() {
certbot renew --quiet
sleep 86400
}
update &
./ip_update.py &
# wait for termination
cleanup() {
echo "stopping..."
}
trap 'cleanup' TERM
wait $! # wait SIGTERM, other processes can just be killed
Regular → Executable
+15 -11
View File
@@ -2,19 +2,20 @@
import time
import urllib.request
from ipaddress import ip_network
from pathlib import Path
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
if __name__ == '__main__':
sg = SendGridAPIClient(Path('sendgrid.key').read_text())
if __name__ == "__main__":
sg = SendGridAPIClient(Path("sendgrid.key").read_text())
old_ipv4, old_ipv6 = None, None
while True:
try:
ipv4 = urllib.request.urlopen('https://v4.ident.me').read().decode('utf8')
# ipv6 = urllib.request.urlopen('https://v6.ident.me').read().decode('utf8')
ipv6 = None
ipv4 = urllib.request.urlopen("https://v4.ident.me").read().decode("utf8")
ipv6 = urllib.request.urlopen("https://v6.ident.me").read().decode("utf8")
ipv6 = str(ip_network(ipv6 + "/64", strict=False).network_address) # xfinity gives /64
except Exception as e:
print(e)
time.sleep(60)
@@ -22,17 +23,20 @@ if __name__ == '__main__':
if ipv4 != old_ipv4 or ipv6 != old_ipv6:
msg = Mail(
from_email='mtran319@gmail.com',
to_emails='mtran319@gmail.com',
subject='pls update ip',
html_content=f'<p>ipv4: {ipv4}</p><p>ipv6: {ipv6}</p>'
from_email="mtran319@gmail.com",
to_emails="mtran319@gmail.com",
subject="pls update ip",
html_content=f"<p>old ipv4: {old_ipv4}</p>"
f"<p>old ipv6: {old_ipv6}</p>"
f"<p>new ipv4: {ipv4}</p>"
f"<p>new ipv6: {ipv6}</p>"
)
try:
print(f'IP changed to {ipv4} and {ipv6}')
print(f"IP changed to {ipv4} and {ipv6}")
resp = sg.send(msg)
except Exception as e:
print(e)
sg = SendGridAPIClient(Path('sendgrid.key').read_text())
sg = SendGridAPIClient(Path("sendgrid.key").read_text())
time.sleep(60)
continue