-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching to modular design from monolithic
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
import os | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
|
||
class Config: | ||
WEBMIN_SERVERS = os.getenv('WEBMIN_SERVERS').split(',') | ||
WEBMIN_API_KEYS = os.getenv('WEBMIN_API_KEYS').split(',') | ||
EMAIL_HOST = os.getenv('EMAIL_HOST') | ||
EMAIL_PORT = int(os.getenv('EMAIL_PORT')) | ||
EMAIL_USER = os.getenv('EMAIL_USER') | ||
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD') | ||
EMAIL_RECIPIENTS = os.getenv('EMAIL_RECIPIENTS').split(',') | ||
SSL_ALERT_DAYS = int(os.getenv('SSL_ALERT_DAYS', 15)) | ||
DOMAIN_EXPIRATION_ALERT_DAYS = int(os.getenv('DOMAIN_EXPIRATION_ALERT_DAYS', 45)) | ||
DOMAIN_FILE = os.getenv('DOMAIN_FILE', 'domains.txt') | ||
|
||
# New variable for additional domains | ||
ADDITIONAL_DOMAINS = os.getenv('ADDITIONAL_DOMAINS', '').split(',') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
from domain_operations import gather_all_domains | ||
from notifications import send_email, render_email_template | ||
from logger import setup_logger | ||
from config import Config | ||
import time | ||
|
||
logger = setup_logger() | ||
|
||
def main(): | ||
all_domains = gather_all_domains() | ||
|
||
for domain in all_domains: | ||
# Placeholder for checking expiration or any other processing | ||
pass | ||
|
||
def continuous_loop(): | ||
while True: | ||
main() | ||
logger.info(f"Sleeping for {Config.CHECK_INTERVAL} seconds before the next run.") | ||
time.sleep(Config.CHECK_INTERVAL) | ||
|
||
if __name__ == "__main__": | ||
# Uncomment the next line to enable continuous loop mode | ||
# continuous_loop() | ||
|
||
# Comment the following line if enabling continuous loop mode | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
import requests | ||
from config import Config | ||
|
||
def get_domains(webmin_url, api_key): | ||
headers = {'Authorization': f"Bearer {api_key}", 'Accept': 'application/json'} | ||
try: | ||
response = requests.get( | ||
f"{webmin_url}/virtual-server/remote.cgi?program=list-domains&name-only", | ||
headers=headers, timeout=10, verify=False | ||
) | ||
response.raise_for_status() | ||
return response.text.splitlines() | ||
except requests.RequestException as e: | ||
return [] | ||
|
||
def gather_all_domains(): | ||
all_domains = [] | ||
|
||
# Fetch domains from Webmin servers | ||
for i, webmin_url in enumerate(Config.WEBMIN_SERVERS): | ||
domains = get_domains(webmin_url, Config.WEBMIN_API_KEYS[i]) | ||
all_domains.extend(domains) | ||
|
||
# Include additional domains from the environment variable | ||
if Config.ADDITIONAL_DOMAINS: | ||
all_domains.extend([domain.strip() for domain in Config.ADDITIONAL_DOMAINS if domain.strip()]) | ||
|
||
# Ensure the domain list is unique | ||
all_domains = list(set(all_domains)) | ||
return all_domains |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
import logging | ||
|
||
def setup_logger(log_file='app.log'): | ||
logger = logging.getLogger(__name__) | ||
handler = logging.FileHandler(log_file) | ||
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
return logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from jinja2 import Environment, FileSystemLoader | ||
|
||
from config import Config | ||
|
||
def send_email(subject, html_content, plain_content): | ||
msg = MIMEMultipart("alternative") | ||
msg['Subject'] = subject | ||
msg['From'] = Config.EMAIL_USER | ||
msg['To'] = ", ".join(Config.EMAIL_RECIPIENTS) | ||
|
||
msg.attach(MIMEText(plain_content, "plain")) | ||
msg.attach(MIMEText(html_content, "html")) | ||
|
||
with smtplib.SMTP(Config.EMAIL_HOST, Config.EMAIL_PORT) as smtp: | ||
smtp.starttls() | ||
smtp.login(Config.EMAIL_USER, Config.EMAIL_PASSWORD) | ||
smtp.sendmail(Config.EMAIL_USER, Config.EMAIL_RECIPIENTS, msg.as_string()) | ||
|
||
def render_email_template(template_name, context): | ||
env = Environment(loader=FileSystemLoader('./templates')) | ||
template = env.get_template(template_name) | ||
return template.render(context) |