#!/opt/imh-python/bin/python3
"""Removes domlogs for terminated accounts"""
# Vanessa V 12/21/12 (aka "the end of the world that didn't happen" day)
LOG_DIR = '/var/log/apache2/domlogs/'
# Create a list of deleted domains
with open('/var/cpanel/deleteddomains.yaml', encoding='utf-8') as f:
del_map = yaml.load(f, Loader=yaml.SafeLoader)
with open('/etc/userdomains', encoding='utf-8') as f:
userdoms.add(line.split(':')[0])
# For each domain, see if a domlogs entry exists
for domain in del_map.keys():
if domain is None or domain in userdoms: # it was added back
# remove leftover domlogs for domains which no longer exist
dom_path = os.path.join(LOG_DIR, domain)
ext_path = f'{dom_path}{ext}'
user = del_map[domain]['user']
# Remove the user's folder if it exists and the user does not
user_logdir = os.path.join(LOG_DIR, user)
users_file = os.path.join('/var/cpanel/users', user)
if not os.path.exists(users_file) and os.path.isdir(user_logdir):
shutil.rmtree(user_logdir)
print('Deleted logs in', user_logdir)
except (OSError, shutil.Error) as exc:
print('Could not delete', user_logdir)
print(exc, file=sys.stderr)
if __name__ == '__main__':