This commit is contained in:
Alfredo Oliviero 2024-07-12 17:25:31 +02:00
parent c9760aaab8
commit f22deb4426
2 changed files with 19 additions and 9 deletions

1
update_robots/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
update_robots.log

View File

@ -2,12 +2,14 @@ import psycopg2
import re import re
import argparse import argparse
import os import os
import logging
# Default values # Default values
DEFAULT_CONFIG_FILE = "/home/life/Portal-Bundle/portal-setup-wizard.properties" DEFAULT_CONFIG_FILE = "/home/life/Portal-Bundle/portal-setup-wizard.properties"
DEFAULT_TEMPLATE_FILE = "template-robots.txt" DEFAULT_TEMPLATE_FILE = "template-robots.txt"
DEFAULT_PLACEHOLDER = "{{HOSTNAME}}" DEFAULT_PLACEHOLDER = "{{HOSTNAME}}"
DEFAULT_OUTPUT_DIR = "currents_robots" DEFAULT_OUTPUT_DIR = "currents_robots"
LOG_FILE = "update_robots.log"
# Function to read database connection parameters from the configuration file # Function to read database connection parameters from the configuration file
def read_db_config(config_file_path): def read_db_config(config_file_path):
@ -25,7 +27,7 @@ def read_template_file(file_path):
return file.read().strip().replace('\n', '_SAFE_NEWLINE_CHARACTER_').replace('\r\n', '_SAFE_NEWLINE_CHARACTER_') return file.read().strip().replace('\n', '_SAFE_NEWLINE_CHARACTER_').replace('\r\n', '_SAFE_NEWLINE_CHARACTER_')
# Function to update the typesettings field for all groups containing false-robots.txt # Function to update the typesettings field for all groups containing false-robots.txt
def update_typesettings_for_all_groups(cursor, template_content, placeholder_pattern, execute_updates, specific_hostname=None): def update_typesettings(cursor, template_content, placeholder_pattern, execute_updates, specific_hostname=None):
try: try:
# Query to extract the necessary data with a join # Query to extract the necessary data with a join
query = """ query = """
@ -58,14 +60,20 @@ def update_typesettings_for_all_groups(cursor, template_content, placeholder_pat
) )
# Update query # Update query
update_query = f"UPDATE public.group_ SET typesettings = %s WHERE groupid = %s;" update_query = "UPDATE public.group_ SET typesettings = %s WHERE groupid = %s;"
if execute_updates: if execute_updates:
cursor.execute(update_query, (updated_typesettings, groupid)) cursor.execute(update_query, (updated_typesettings, groupid))
executed_query = cursor.mogrify(update_query, (updated_typesettings, groupid)).decode('utf-8')
restore_query = cursor.mogrify(update_query, (current_typesettings, groupid)).decode('utf-8')
logging.info(f"Executed: \n{executed_query}\n")
logging.info(f"### Restore command: \n{restore_query}\n")
else: else:
print(cursor.mogrify(update_query, (updated_typesettings, groupid))) print(cursor.mogrify(update_query, (updated_typesettings, groupid)).decode('utf-8'))
except Exception as e: except Exception as e:
print(f"Error during update: {e}") print(f"Error during update: {e}")
logging.error(f"Error during update: {e}")
# Function to print the list of current vhosts # Function to print the list of current vhosts
def print_current_vhosts(cursor): def print_current_vhosts(cursor):
@ -121,7 +129,6 @@ def save_current_vhosts(cursor, output_dir):
file.write(match.group(1)) file.write(match.group(1))
print(f"Saved {hostname} robots.txt to {file_path}") print(f"Saved {hostname} robots.txt to {file_path}")
file_path = os.path.join(output_dir, f"typesettings_{hostname}.txt") file_path = os.path.join(output_dir, f"typesettings_{hostname}.txt")
with open(file_path, 'w') as file: with open(file_path, 'w') as file:
file.write(typesettings) file.write(typesettings)
@ -249,6 +256,10 @@ Examples of usage:
if args.db_password: if args.db_password:
db_password = args.db_password db_password = args.db_password
# Configure logging
logging.basicConfig(filename=LOG_FILE, level=logging.INFO,
format='%(asctime)s %(message)s')
# Connect to the database # Connect to the database
conn = psycopg2.connect( conn = psycopg2.connect(
host=db_host, host=db_host,
@ -268,11 +279,10 @@ Examples of usage:
elif args.print_differences: elif args.print_differences:
print_differences(cursor, template_content, args.placeholder, args.hostname) print_differences(cursor, template_content, args.placeholder, args.hostname)
elif args.execute: elif args.execute:
update_typesettings_for_all_groups(cursor, template_content, args.placeholder, True, args.hostname) update_typesettings(cursor, template_content, args.placeholder, True, args.hostname)
print ("robots.txt updated. clean the liferay database cache on all the instances (Configuration => Server Administration => Clear the database cache)") print("robots.txt updated. Clean the Liferay database cache on all the instances (Configuration => Server Administration => Clear the database cache)")
elif args.print_updates: elif args.print_updates:
update_typesettings_for_all_groups(cursor, template_content, args.placeholder, False, args.hostname) update_typesettings(cursor, template_content, args.placeholder, False, args.hostname)
else: else:
parser.print_help() parser.print_help()
return return
@ -283,4 +293,3 @@ Examples of usage:
if __name__ == "__main__": if __name__ == "__main__":
main() main()