🔖 Bump version: 0.1.0 → 1.0.0
This commit is contained in:
parent
cbf68294ca
commit
b7311d088d
21 changed files with 1993 additions and 223 deletions
128
src/adgroupsync/conf.py
Normal file
128
src/adgroupsync/conf.py
Normal file
|
@ -0,0 +1,128 @@
|
|||
import argparse
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytz
|
||||
import yaml
|
||||
|
||||
logger = logging.getLogger(__package__)
|
||||
|
||||
|
||||
def create_config_file(dest: Path):
|
||||
if dest.is_dir():
|
||||
dest = dest / f"{__package__}_config.yml"
|
||||
example_conf = Path(__file__).parent / 'resources' / 'example_config.yml'
|
||||
with open(example_conf, "r") as source:
|
||||
with open(dest, "w") as d:
|
||||
d.writelines(source)
|
||||
|
||||
|
||||
# Assign environment variables or configuration file values
|
||||
AD_DOMAIN = os.getenv('AD_DOMAIN')
|
||||
AD_USER_NAME = os.getenv('AD_USER')
|
||||
AD_PASSWORD = os.getenv('AD_PASSWORD')
|
||||
AD_LDAP_SERVER = [s.strip() for s in os.getenv('AD_LDAP_SERVER').split(',')] \
|
||||
if os.getenv('AD_LDAP_SERVER') is not None else None
|
||||
AD_TIMEZONE = pytz.timezone(os.getenv('AD_TIMEZONE')) \
|
||||
if os.getenv('AD_TIMEZONE') else None
|
||||
AD_PARENT_GROUP = os.getenv('AD_PARENT_GROUP')
|
||||
STDOUT_LOG_LEVEL = os.getenv('STDOUT_LOG_LEVEL')
|
||||
FILE_LOG_LEVEL = os.getenv('FILE_LOG_LEVEL')
|
||||
LOG_DIR = os.getenv('LOG_DIR')
|
||||
CIVICRM_BASE_URL = os.getenv('CIVICRM_BASE_URL')
|
||||
CIVICRM_API_KEY = os.getenv('CIVICRM_API_KEY')
|
||||
CIVICRM_BATCH_SIZE = int(os.getenv('CIVICRM_BATCH_SIZE')) \
|
||||
if os.getenv('CIVICRM_BATCH_SIZE') is not None else None
|
||||
CIVICRM_RETRIES = int(os.getenv('CIVICRM_RETRIES')) \
|
||||
if os.getenv('CIVICRM_RETRIES') is not None else None
|
||||
CIVICRM_IGNORE_SSL = bool(os.getenv('CIVICRM_IGNORE_SSL')) \
|
||||
if os.getenv('CIVICRM_IGNORE_SSL') is not None else None
|
||||
NTFY_URL = os.getenv('NTFY_URL')
|
||||
NTFY_TOPIC = os.getenv('NTFY_TOPIC')
|
||||
NTFY_ACCESS_TOKEN = os.getenv('NTFY_ACCESS_TOKEN')
|
||||
|
||||
try:
|
||||
argparser = argparse.ArgumentParser(
|
||||
description='This program synchronizes Active Directory groups with '
|
||||
'CiviCRM groups.')
|
||||
|
||||
argparser.add_argument(
|
||||
"--conf",
|
||||
action="store",
|
||||
type=Path,
|
||||
help="Path the configuration file",
|
||||
)
|
||||
|
||||
argparser.add_argument(
|
||||
"--create-conf",
|
||||
action="store_true",
|
||||
help="Create a configuration file",
|
||||
)
|
||||
|
||||
args = argparser.parse_args()
|
||||
|
||||
# If a path to a config file was provided
|
||||
if args.conf:
|
||||
|
||||
# Check if configuration file exists
|
||||
config_file = Path(args.conf)
|
||||
if not config_file.is_file() and not args.create_conf:
|
||||
raise FileNotFoundError(
|
||||
f"Configuration file '{config_file}' does not exist.")
|
||||
|
||||
# Create configuration file if requested and exit
|
||||
if args.create_conf:
|
||||
create_config_file(config_file)
|
||||
exit(0)
|
||||
|
||||
# Load configuration file
|
||||
with open(config_file, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
|
||||
# Get values from configuration file
|
||||
AD_DOMAIN = AD_DOMAIN or config['AD']['DOMAIN']
|
||||
AD_USER_NAME = AD_USER_NAME or config['AD']['USER']
|
||||
AD_PASSWORD = AD_PASSWORD or config['AD']['PASSWORD']
|
||||
AD_LDAP_SERVER = AD_LDAP_SERVER or config['AD'].get('LDAP_SERVER')
|
||||
AD_TIMEZONE = AD_TIMEZONE \
|
||||
or pytz.timezone(config['AD'].get('TIMEZONE', 'UTC'))
|
||||
AD_PARENT_GROUP = AD_PARENT_GROUP or config['AD']['PARENT_GROUP']
|
||||
STDOUT_LOG_LEVEL = STDOUT_LOG_LEVEL \
|
||||
or config['LOGGING'].get('STDOUT_LOG_LEVEL', 'INFO')
|
||||
FILE_LOG_LEVEL = FILE_LOG_LEVEL \
|
||||
or config['LOGGING'].get('FILE_LOG_LEVEL', 'WARNING')
|
||||
LOG_DIR = LOG_DIR or config['LOGGING'].get('LOG_DIR')
|
||||
CIVICRM_BASE_URL = CIVICRM_BASE_URL or config['CIVICRM']['BASE_URL']
|
||||
CIVICRM_API_KEY = CIVICRM_API_KEY or config['CIVICRM']['API_KEY']
|
||||
CIVICRM_BATCH_SIZE = CIVICRM_BATCH_SIZE \
|
||||
or config['CIVICRM']['BATCH_SIZE']
|
||||
CIVICRM_RETRIES = CIVICRM_RETRIES \
|
||||
or config['CIVICRM'].get('RETRIES', 3)
|
||||
CIVICRM_IGNORE_SSL = CIVICRM_IGNORE_SSL \
|
||||
or bool(config['CIVICRM'].get('IGNORE_SSL', False))
|
||||
NTFY_URL = NTFY_URL or config['NTFY'].get('URL') if 'NTFY' in config else None
|
||||
NTFY_TOPIC = NTFY_TOPIC or config['NTFY'].get('TOPIC') if 'NTFY' in config else None
|
||||
NTFY_ACCESS_TOKEN = NTFY_ACCESS_TOKEN \
|
||||
or config['NTFY'].get('ACCESS_TOKEN') if 'NTFY' in config else None
|
||||
|
||||
# Check if some required values are missing
|
||||
required = {
|
||||
"AD_DOMAIN": AD_DOMAIN,
|
||||
"AD_USER_NAME": AD_USER_NAME,
|
||||
"AD_PASSWORD": AD_PASSWORD,
|
||||
"AD_LDAP_SERVER": AD_LDAP_SERVER,
|
||||
"AD_PARENT_GROUP": AD_PARENT_GROUP,
|
||||
"CIVICRM_BASE_URL": CIVICRM_BASE_URL,
|
||||
"CIVICRM_API_KEY": CIVICRM_API_KEY,
|
||||
}
|
||||
if len(missing := [k for k, v in required.items() if v is None]) > 0:
|
||||
raise ValueError('Some required values are missing. '
|
||||
'Please use a configuration file '
|
||||
'or provide all required environment variables. '
|
||||
'Missing: %s'
|
||||
% ','.join(missing))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(e, exc_info=True)
|
||||
exit(1)
|
Loading…
Add table
Add a link
Reference in a new issue