Get Ip for koji.koozali.org initially and use it from then
This commit is contained in:
parent
8fea558792
commit
d3905a0e89
@ -6,12 +6,45 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
|
||||||
def join_lines_with_newline(lines):
|
def join_lines_with_newline(lines):
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
def extract_changelog_top(koji_link):
|
def get_ip_address(domain, retries=10, delay=1):
|
||||||
response = requests.get(koji_link)
|
"""
|
||||||
|
Resolves the IP address of a domain, retrying up to `retries` times if it fails.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
domain (str): The domain name to resolve.
|
||||||
|
retries (int): Number of retry attempts (default is 10).
|
||||||
|
delay (int): Delay between retries in seconds (default is 1 second).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The IP address if resolved successfully.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
RuntimeError: If unable to resolve the domain after all attempts.
|
||||||
|
"""
|
||||||
|
for attempt in range(1, retries + 1):
|
||||||
|
try:
|
||||||
|
ip_address = socket.gethostbyname(domain)
|
||||||
|
logging.info(f"Successfully resolved {domain} to {ip_address}")
|
||||||
|
return ip_address
|
||||||
|
except socket.gaierror:
|
||||||
|
logging.warning(f"Attempt {attempt} failed. Retrying...")
|
||||||
|
time.sleep(delay)
|
||||||
|
|
||||||
|
raise RuntimeError(f"Unable to resolve domain '{domain}' after {retries} attempts.")
|
||||||
|
|
||||||
|
def extract_changelog_top(koji_link, host_header):
|
||||||
|
try:
|
||||||
|
response = requests.get(koji_link, headers={'Host': host_header})
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Koji link failed: {str(e)}")
|
||||||
|
return []
|
||||||
|
|
||||||
soup = BeautifulSoup(response.text, 'html.parser')
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
changelog_td = soup.find('td', class_='changelog')
|
changelog_td = soup.find('td', class_='changelog')
|
||||||
|
|
||||||
@ -24,7 +57,7 @@ def extract_changelog_top(koji_link):
|
|||||||
for line in lines:
|
for line in lines:
|
||||||
stripped_line = line.strip()
|
stripped_line = line.strip()
|
||||||
if not stripped_line:
|
if not stripped_line:
|
||||||
if result: # Stop at the first blank line after content
|
if result:
|
||||||
break
|
break
|
||||||
continue
|
continue
|
||||||
if stripped_line not in seen:
|
if stripped_line not in seen:
|
||||||
@ -36,16 +69,12 @@ def extract_changelog_top(koji_link):
|
|||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# Koji RSS feed URL
|
|
||||||
KOJI_RSS_URL = "http://koji.koozali.org/koji/recentbuilds?feed=rss"
|
|
||||||
# Updated Rocket.Chat webhook URL
|
# Updated Rocket.Chat webhook URL
|
||||||
ROCKET_CHAT_URL = "https://chat.koozali.org/hooks/66d24441effca216c2ca309f/KJLaNwc5vyHwqz5MhRDpBkKWnQuAfsCX3xZMHxPhpuqmFgBn"
|
ROCKET_CHAT_URL = "https://chat.koozali.org/hooks/66d24441effca216c2ca309f/KJLaNwc5vyHwqz5MhRDpBkKWnQuAfsCX3xZMHxPhpuqmFgBn"
|
||||||
|
|
||||||
# Set up logging
|
# Set up logging
|
||||||
log_file = "/var/log/Koji2Rocket.log"
|
log_file = "Koji2Rocket.log"
|
||||||
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
# Ensure the log directory exists
|
|
||||||
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
||||||
|
|
||||||
# Database setup
|
# Database setup
|
||||||
@ -60,7 +89,6 @@ def setup_database():
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
# Function to check if a build ID has been sent
|
|
||||||
def has_build_been_sent(build_id):
|
def has_build_been_sent(build_id):
|
||||||
conn = sqlite3.connect('sent_builds.db')
|
conn = sqlite3.connect('sent_builds.db')
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
@ -69,7 +97,6 @@ def has_build_been_sent(build_id):
|
|||||||
conn.close()
|
conn.close()
|
||||||
return exists
|
return exists
|
||||||
|
|
||||||
# Function to mark a build ID as sent
|
|
||||||
def mark_build_as_sent(build_id):
|
def mark_build_as_sent(build_id):
|
||||||
conn = sqlite3.connect('sent_builds.db')
|
conn = sqlite3.connect('sent_builds.db')
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
@ -77,7 +104,6 @@ def mark_build_as_sent(build_id):
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
# Function to send message to Rocket.Chat
|
|
||||||
def send_to_rocket_chat(build_title, build_link, build_id, changelog):
|
def send_to_rocket_chat(build_title, build_link, build_id, changelog):
|
||||||
payload = {
|
payload = {
|
||||||
"alias": "Koji",
|
"alias": "Koji",
|
||||||
@ -96,7 +122,6 @@ def send_to_rocket_chat(build_title, build_link, build_id,changelog):
|
|||||||
else:
|
else:
|
||||||
logging.error(f"Failed to send build notification: {response.status_code} - {response.text}")
|
logging.error(f"Failed to send build notification: {response.status_code} - {response.text}")
|
||||||
|
|
||||||
# Function to send startup message to Rocket.Chat
|
|
||||||
def send_startup_message():
|
def send_startup_message():
|
||||||
payload = {
|
payload = {
|
||||||
"alias": "Koji",
|
"alias": "Koji",
|
||||||
@ -108,39 +133,44 @@ def send_startup_message():
|
|||||||
else:
|
else:
|
||||||
logging.error(f"Failed to send startup message: {response.status_code} - {response.text}")
|
logging.error(f"Failed to send startup message: {response.status_code} - {response.text}")
|
||||||
|
|
||||||
# Function to fetch and parse the Koji RSS feed
|
def fetch_koji_feed(koji_rss_url, host_header):
|
||||||
def fetch_koji_feed():
|
|
||||||
try:
|
try:
|
||||||
feed = feedparser.parse(KOJI_RSS_URL)
|
response = requests.get(koji_rss_url, headers={'Host': host_header})
|
||||||
|
feed = feedparser.parse(response.content)
|
||||||
return feed.entries
|
return feed.entries
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exc_type, exc_value, exc_traceback = sys.exc_info()
|
logging.error(f"RSS request to koji failed: {str(e)}")
|
||||||
logging.error(f"RSS request to koji failed: {exc_type.__name__}: {str(e)}")
|
|
||||||
logging.debug(f"Exception traceback: {''.join(traceback.format_tb(exc_traceback))}")
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# Main polling loop
|
|
||||||
def main():
|
def main():
|
||||||
setup_database() # Initialize the database
|
setup_database()
|
||||||
send_startup_message() # Send startup message
|
send_startup_message()
|
||||||
|
|
||||||
|
original_domain = "koji.koozali.org"
|
||||||
|
try:
|
||||||
|
koji_ip = get_ip_address(original_domain)
|
||||||
|
except RuntimeError as e:
|
||||||
|
logging.error(str(e))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Create IP-based URL and preserve original domain for headers
|
||||||
|
koji_rss_url = f"http://{koji_ip}/koji/recentbuilds?feed=rss"
|
||||||
|
logging.info(f"Using Koji RSS URL: {koji_rss_url}")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
entries = fetch_koji_feed()
|
entries = fetch_koji_feed(koji_rss_url, original_domain)
|
||||||
|
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
build_title = entry.title.strip()
|
build_title = entry.title.strip()
|
||||||
build_link = entry.link
|
original_link = entry.link
|
||||||
# Extracting the build ID from the title
|
build_link = original_link.replace(original_domain, koji_ip)
|
||||||
build_id = build_title.split(':')[1].split(',')[0].strip() # Extract the relevant part from the title
|
build_id = build_title.split(':')[1].split(',')[0].strip()
|
||||||
changelog = extract_changelog_top(entry.link)
|
changelog = extract_changelog_top(build_link, original_domain)
|
||||||
# print(f"{entry.link}\n{changelog}")
|
|
||||||
# quit(1)
|
|
||||||
|
|
||||||
if not has_build_been_sent(build_id): # Check if the build has been sent
|
if not has_build_been_sent(build_id):
|
||||||
send_to_rocket_chat(build_title, build_link, build_id, changelog)
|
send_to_rocket_chat(build_title, build_link, build_id, changelog)
|
||||||
mark_build_as_sent(build_id) # Mark the build ID as sent
|
mark_build_as_sent(build_id)
|
||||||
|
|
||||||
# Wait for 1 minute before polling again
|
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user