67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
|
#!/usr/bin/env python3
|
||
|
import argparse
|
||
|
import configparser
|
||
|
import shutil
|
||
|
import os
|
||
|
from dotenv import set_key
|
||
|
from pathlib import Path
|
||
|
import socket
|
||
|
import secrets
|
||
|
import string
|
||
|
import color_log
|
||
|
def find_available_port(start_port=80):
|
||
|
"""Finds an available port starting from the given port."""
|
||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||
|
while True:
|
||
|
try:
|
||
|
sock.bind(('0.0.0.0', start_port))
|
||
|
color_log.Show(3,f" {start_port} is Open")
|
||
|
return start_port
|
||
|
except OSError as e:
|
||
|
if e.errno == 98: # Address already in use
|
||
|
print(f"{start_port} already in use , Try other port ...")
|
||
|
start_port += 1
|
||
|
else:
|
||
|
raise
|
||
|
def main():
|
||
|
"""
|
||
|
Generates a random password and finds an available port.
|
||
|
Updates the Odoo configuration file and .env file with these values.
|
||
|
"""
|
||
|
parser = argparse.ArgumentParser(description="Generate Odoo configuration")
|
||
|
parser.add_argument('--db_user', type=str, help='')
|
||
|
parser.add_argument('--db_pass', type=str, help='')
|
||
|
parser.add_argument('--deploy_path', type=str, help='')
|
||
|
parser.add_argument('--addons_path', type=str, help='')
|
||
|
# parser.add_argument('--db_filter', type=str, help='')
|
||
|
parser.add_argument('--db_port', type=int, help='')
|
||
|
parser.add_argument('--db_server', type=str, help='')
|
||
|
args = parser.parse_args()
|
||
|
db_port = args.db_port
|
||
|
db_user = args.db_user
|
||
|
db_pass = args.db_pass
|
||
|
db_server = args.db_server
|
||
|
app_port = find_available_port(8069)
|
||
|
addons_path = args.addons_path
|
||
|
base_dir= args.deploy_path
|
||
|
# db_filter= args.db_filter
|
||
|
# Copy template files
|
||
|
os.makedirs(f"{base_dir}/etc", exist_ok=True)
|
||
|
color_log.Show(3,f"Copy {base_dir}/odoo.conf.template to {base_dir}/etc/odoo.conf")
|
||
|
shutil.copyfile(f'{base_dir}/odoo.conf.template', f'{base_dir}/odoo.conf')
|
||
|
|
||
|
# Update Odoo configuration file
|
||
|
config = configparser.ConfigParser()
|
||
|
config.read(f'{base_dir}/odoo.conf')
|
||
|
config['options']['db_host'] = str(db_server)
|
||
|
config['options']['db_user'] = db_user
|
||
|
config['options']['db_password'] = db_pass
|
||
|
config['options']['db_port'] = str(db_port)
|
||
|
config['options']['addons_path'] = addons_path
|
||
|
config['options']['xmlrpc_port'] = str(app_port)
|
||
|
config['options']['dbfilter'] = ".*"
|
||
|
config['options']['proxy_mode'] = "True"
|
||
|
with open(f'{base_dir}/odoo.conf', 'w') as configfile:
|
||
|
config.write(configfile)
|
||
|
if __name__ == "__main__":
|
||
|
main()
|