From 6667c774c32ecc867479a7b6f896a7144d6a52e3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 12 Feb 2025 11:06:22 -0500 Subject: [PATCH] automated terminal push --- RestAPIClient.py | 7 +- SullyGnomeRobot.py | 6 +- consumer.bash | 20 ++++++ consumer.py | 4 +- crontabX | 6 ++ smart-cron-builder.bash | 5 ++ smart-cron-builder.py | 149 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 crontabX create mode 100755 smart-cron-builder.bash create mode 100755 smart-cron-builder.py diff --git a/RestAPIClient.py b/RestAPIClient.py index 6a2db6f..414bd4c 100644 --- a/RestAPIClient.py +++ b/RestAPIClient.py @@ -22,7 +22,6 @@ def ip(): return None - def hostname(): try: @@ -56,13 +55,15 @@ def addDownloadSuccess(url): print(response.text) -def addDownloadFailure(url): +def addDownloadFailure(url, error): print("enter -> addDownloadFailure()") print("param / url / ",url) + + print("param / error / ",error) - data = {"url": url, "hostIP": ip(),"ip": ip(),"hostName":hostname()} + data = {"url": url, "hostIP": ip(),"ip": ip(),"hostName":hostname(), "reason":error} json_data = json.dumps(data) diff --git a/SullyGnomeRobot.py b/SullyGnomeRobot.py index b56f8cb..8067077 100644 --- a/SullyGnomeRobot.py +++ b/SullyGnomeRobot.py @@ -16,8 +16,8 @@ def download(url): print(f"launch -> SullyGnomeRobot -> download() -> {url}") - opts = FirefoxOptions() + opts.add_argument("--headless") driver=webdriver.Firefox(options=opts) @@ -49,7 +49,7 @@ def download(url): print(f"Element with class name 'tblControl_length' not found: {e}") - RestAPIClient.addDownloadFailure(url) + RestAPIClient.addDownloadFailure(url, "Element with class name 'tblControl_length' not found") driver.quit() @@ -75,7 +75,7 @@ def download(url): print(f"Element with class name 'TableExportLinkButton' not found: {e}") - RestAPIClient.addDownloadFailure(url) + RestAPIClient.addDownloadFailure(url,"Element with class name 'TableExportLinkButton' not found") driver.quit() diff --git a/consumer.bash b/consumer.bash index e8d1c7c..d4ce21d 100755 --- a/consumer.bash +++ b/consumer.bash @@ -3,10 +3,29 @@ set -e set -x + ## echo "what up????" +#### + +bash smart-cron-builder.bash + +#### + +cp crontabX /etc/cron.d/cronfile + +# Give execution rights on the cron job +chmod 0644 /etc/cron.d/cronfile + +# Apply the cron job +crontab -u root /etc/cron.d/cronfile + +crontab -e root + +#### + cd /yankee-gnome-fire-consumer pwd @@ -14,4 +33,5 @@ pwd ls whoami + python3 consumer.py diff --git a/consumer.py b/consumer.py index d21bf04..3841495 100644 --- a/consumer.py +++ b/consumer.py @@ -51,13 +51,13 @@ class MyListener(stomp.ConnectionListener): print(f"exception / something went wrong when the sully gnome robot was downloading: {e}") - RestAPIClient.addDownloadFailure(message.body) + RestAPIClient.addDownloadFailure(message.body, e) def on_error(headers, message): print('received an error "%s"' % message) - RestAPIClient.addDownloadFailure(message.body) + RestAPIClient.addDownloadFailure(message.body, "error from onmessage (consumer.py)") print("## ") print("## starting > JMS consumer (yankee-sully-channels-monthly)") diff --git a/crontabX b/crontabX new file mode 100644 index 0000000..a88bb9b --- /dev/null +++ b/crontabX @@ -0,0 +1,6 @@ +SHELL=/bin/bash + +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +6 11 * * * python3 /yankee-gnome-fire-consumer/consumer.py >> /tmp/root/Downloads/.logs/yankee-node-96.255.165.82-veneno.log 2>&1 + diff --git a/smart-cron-builder.bash b/smart-cron-builder.bash new file mode 100755 index 0000000..2c7761f --- /dev/null +++ b/smart-cron-builder.bash @@ -0,0 +1,5 @@ +reset + +clear + +python3 smart-cron-builder.py \ No newline at end of file diff --git a/smart-cron-builder.py b/smart-cron-builder.py new file mode 100755 index 0000000..407db64 --- /dev/null +++ b/smart-cron-builder.py @@ -0,0 +1,149 @@ +import datetime +import subprocess +import socket +import os +import requests + +def buildLogDirectory(): + + return "/root/Downloads/.logs/" + +def buildLogFileName(): + + logFileDir = buildLogDirectory() + + try: + + response = requests.get("https://api.ipify.org?format=json") # Or another similar service + + response.raise_for_status() # Check for HTTP errors (4xx or 5xx) + + ip_data = response.json() + + server_ip = ip_data['ip'] + + print("server_ip / ",ip_data['ip']) + + server_hostname = socket.gethostname() + + print("server_hostname / ",server_hostname) + + node_id= logFileDir + "yankee-node-" + server_ip + "-" + server_hostname + ".log" + + return node_id + + except socket.gaierror: + + return "!!error!!" + +def buildCronExecuteTime(): + + current_time = datetime.datetime.now() + + new_minute = (current_time.minute + 2) % 60 # The % operator performs the wrap-around + + print("new_minute / ",new_minute) + + new_hour = current_time.hour + (current_time.minute + 5) // 60 # Integer division for hour increment + + print("new_hour / ",new_hour) + + cron = str(new_minute) + " " + str(new_hour) + " " + "* * *" + + return cron + +def buildCronSituation(): + + return buildCronExecuteTime() +" python3 /yankee-gnome-fire-consumer/consumer.py >> " + buildLogFileName() + " 2>&1" + +#### + +cronTiming = buildCronExecuteTime() + +print("cronTiming / ",cronTiming) + +## + +logger = buildLogFileName(); + +print("logger / ",logger) + +## + +cronFile = buildCronSituation(); + +print() +print(cronFile) +print() + + + +def createLogDir(): + + log_dir = buildLogDirectory() + + print("creating log dir",log_dir) + + try: + if not os.path.exists(log_dir): # Check if the directory exists + os.makedirs(log_dir) # Create the directory (and any necessary parent directories) + print(f"Directory '{log_dir}' created successfully.") + else: + print(f"Directory '{log_dir}' already exists.") + return True # Directory exists or was created successfully + + except OSError as e: + print(f"Error creating directory '{log_dir}': {e}") + return False + +## + + +def writeCrontabSituation(): + + file_path ="crontabX" + + crontab_text = """ +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +* * * * * python3 /yankee-gnome-fire-consumer/consumer.py >> /var/log/yankee-downloader.log 2>&1 + +* * * * * /tmp/ping2.bash >> /var/log/cron.log 2>&1""" + + try: + # Check if the file exists and delete if it does + if os.path.exists(file_path): + os.remove(file_path) + + with open(file_path, "w") as f: + + sep="\n" + + f.write("SHELL=/bin/bash") + f.write(sep) + f.write(sep) + + f.write("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") + f.write(sep) + f.write(sep) + + f.write(buildCronSituation()) + f.write(sep) + f.write(sep) + + #f.write("* * * * * /tmp/ping2.bash >> /var/log/cron.log 2>&1") + #f.write(sep) + #f.write(sep) + + print(f"Crontab written to '{file_path}' successfully.") + + return True + + except OSError as e: + print(f"Error writing to file '{file_path}': {e}") + return False + +#################### +createLogDir() + +writeCrontabSituation() \ No newline at end of file