Skip to content

Commit

Permalink
created preping.py
Browse files Browse the repository at this point in the history
  • Loading branch information
masloffvs committed May 23, 2024
1 parent 8eb4841 commit 3154fae
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,5 @@ modules.xml
# End of https://www.toptal.com/developers/gitignore/api/jetbrains,jetbrains+iml,jetbrains+all

routes.trp
.idea
.idea
errors.log
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ RUN chmod +x /usr/local/bin/watch_routes.sh
COPY url_validate_and_trim.py /usr/local/bin/url_validate_and_trim.py
RUN chmod +x /usr/local/bin/url_validate_and_trim.py

# Copy the script for preping URLs
COPY preping.py /usr/local/bin/preping.py
RUN chmod +x /usr/local/bin/preping.py

# Expose ports
EXPOSE 80
EXPOSE 443
Expand Down
43 changes: 43 additions & 0 deletions preping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import time

param = '-c'
packages = 1
file_name = "/etc/nginx/routes.trp"

def ping(hostname):
"""Ping host and return True if reachable, False otherwise
This is much faster than using os.system to ping, as it avoids
starting up a new shell and avoiding the overhead of running
the ping command.
"""
from subprocess import Popen, PIPE
host_only = hostname.replace('https://', '').replace('http://', '')

if '/' in host_only:
print("Not a valid hostname")
exit(1)

ping_proc = Popen(
['ping', '-c', str(packages), host_only],

stdout=PIPE,
stderr=PIPE
)

ping_proc.communicate()
return ping_proc.returncode == 0


def ping2(hostname):
if not ping(hostname):
with open('/etc/nginx/host-errors.logs', 'a+') as f:
f.write(f"[{time.time()}] {hostname} is not reachable\n")

with open(file_name, 'r+') as f:
for line in f.readlines():
if " => " in line:
host = line.split(" => ")[1]
print("Ping " + host)
ping2(host)
4 changes: 4 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

echo "Starting the Treepath application configurator..."

echo "Creating errors.log if it doesn't exist..."
touch errors.logs

# Name of the Docker container
CONTAINER_NAME="treepath"

Expand Down Expand Up @@ -30,6 +33,7 @@ docker run \
-d \
-p 80:80 \
-v "$(pwd)/routes.trp:/etc/nginx/routes.trp" \
-v "$(pwd)/errors.logs:/etc/nginx/host-errors.logs" \
--network datahub \
--restart always \
--hostname $CONTAINER_NAME \
Expand Down
2 changes: 2 additions & 0 deletions watch_routes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ generate_config() {

echo "Dynamic routing script started..."
python3 /usr/local/bin/url_validate_and_trim.py
python3 /usr/local/bin/preping.py

# Generate initial configuration
generate_config
Expand All @@ -67,5 +68,6 @@ echo "Monitoring routes.trp for changes..."
while inotifywait -e modify /etc/nginx/routes.trp; do
echo "routes.trp has been modified. Regenerating Nginx configuration..."
python3 /usr/local/bin/url_validate_and_trim.py
python3 /usr/local/bin/preping.py
generate_config
done

0 comments on commit 3154fae

Please sign in to comment.