generated from NethServer/ns8-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to import emails from IMAP server to piler
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |