From 439aed680fa8ee3c6eb5e243b896a4e1f55840c2 Mon Sep 17 00:00:00 2001 From: Stephane de Labrusse Date: Wed, 24 Apr 2024 15:20:40 +0200 Subject: [PATCH] Add script to import emails from IMAP server to piler --- imageroot/bin/import-emails | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 imageroot/bin/import-emails diff --git a/imageroot/bin/import-emails b/imageroot/bin/import-emails new file mode 100755 index 0000000..5668aad --- /dev/null +++ b/imageroot/bin/import-emails @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# +# Copyright (C) 2024 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +import subprocess +import sys +import os +import agent + + +rdb = agent.redis_connect() + +providers = agent.list_service_providers(rdb, 'imap', 'tcp', { + 'module_uuid': os.environ['MAIL_SERVER'], +}) + +if providers: + mail_id = providers[0]["module_id"] + ip_address = providers[0]["host"] + + # retrieve the list of users + response = agent.tasks.run(f"module/{mail_id}", action='list-user-mailboxes') + agent.assert_exp(response['exit_code'] == 0) + + # Extract user names into a list + user_list = [user_data["user"] for user_data in response['output'].get("user_mailboxes")] + + # retrieve the vmail password + response = agent.tasks.run(f"module/{mail_id}", action='reveal-master-credentials') + agent.assert_exp(response['exit_code'] == 0) + password = response['output']['password'] + + for user in user_list: + try: + print(f"### Importing {user} to {os.environ['MODULE_ID']}", file=sys.stderr) + + subprocess.run([ + '/usr/bin/podman', 'exec', 'piler-app', '/bin/bash', '-c', + f"su - piler;cd /var/piler/imap;/usr/bin/pilerimport -i {ip_address} -u {user}*vmail -p {password} -P 993" + ]) + except Exception as e: + print(f"### Error to import {user} to {os.environ['MODULE_ID']}: {e}", file=sys.stderr) + continue