Skip to content

Commit

Permalink
Merge pull request #4 from NethServer/fixMigration
Browse files Browse the repository at this point in the history
Refactor imapsync and cron handling
  • Loading branch information
stephdl authored Nov 21, 2023
2 parents a8d2989 + d93c8e6 commit 5666031
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
16 changes: 2 additions & 14 deletions imageroot/actions/create-task/20configure
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,10 @@ if data.get("exclude",'') == "":
else:
exclude = ","+ data.get("exclude",'')

# cron
# Cron is expanded before the service start
cron = data.get("cron","")
if cron != "":
if 'h' in cron:
cron_env = "1 */"+cron.replace('h','')+" * * * root /usr/local/bin/syncctl start "+localuser+'_'+task_id
elif 'm' in cron:
cron_env = "*/"+cron.replace('m','')+" * * * * root /usr/local/bin/syncctl start "+localuser+'_'+task_id
f = open("./cron/"+localuser+'_'+task_id+".cron", "w", encoding="utf-8")
# MAIL_HOST is an env variable used by perl/imapsync
f.write('MAIL_HOST='+os.environ['MAIL_HOST']+"\n")
f.write(cron_env+"\n")
f.close()
# cron does not exist we remove or ignore
elif cron == "" and os.path.exists("./cron/"+localuser+'_'+task_id+".cron"):
os.remove("./cron/"+localuser+'_'+task_id+".cron")

# folder synchronization
foldersynchronization = data.get("foldersynchronization","all")
if foldersynchronization == "inbox":
folder_inbox = "--folder\ 'INBOX'"
Expand Down
15 changes: 6 additions & 9 deletions imageroot/actions/import-module/40migration
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import uuid
import os
import sys

# create directories
os.makedirs('imapsync', exist_ok=True)
os.makedirs('cron', exist_ok=True)

file_path = './getmail.json'

with open(file_path, 'r') as file:
Expand Down Expand Up @@ -96,19 +100,12 @@ for properties in getmail_properties:
# not available in getmail, we disable for validation
exclude = ""

# cron
# Cron is expanded before the service start
if cron != "":
if cron == '60':
cron_env = "1 */1 * * * root /usr/local/bin/syncctl start "+localuser+'_'+task_id
cron = '1h'
else:
cron_env = "*/"+cron+" * * * * root /usr/local/bin/syncctl start "+localuser+'_'+task_id
cron = cron + 'm'
f = open("./cron/"+localuser+'_'+task_id+".cron", "w", encoding="utf-8")
# MAIL_HOST is an env variable used by perl/imapsync
f.write('MAIL_HOST='+os.environ['MAIL_HOST']+"\n")
f.write(cron_env+"\n")
f.close()

foldersynchronization = "inbox"
folder_inbox = "--folder\ 'INBOX'"
Expand All @@ -128,7 +125,7 @@ EXPUNGE_REMOTE={expunge_remote}
CRON={cron}
FOLDER_INBOX={folder_inbox}
FOLDERSYNCHRONIZATION={foldersynchronization}
"""
"""
os.umask(0o77)

f = open("./imapsync/"+localuser+'_'+task_id+".env", "w", encoding="utf-8")
Expand Down
36 changes: 36 additions & 0 deletions imageroot/bin/expand-cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3

#
# Copyright (C) 2023 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#


# This script expands cron tasks based on environment files.
# It reads environment files in the 'imapsync' directory, extracts relevant information,
# and generates corresponding cron files in the 'cron' directory.



import agent
import glob
import os

for file in glob.iglob("cron/*.cron"):
os.remove(file)

env_files = list(glob.iglob("imapsync/*.env"))
for task_id in env_files:
task_id = task_id.removesuffix('.env').removeprefix('imapsync/')
data = agent.read_envfile('imapsync/'+task_id+'.env')
if data['CRON'] != '':
if '1h' in data['CRON']:
cron_env = "1 */"+data['CRON'].replace('h','')+" * * * root /usr/local/bin/syncctl start "+data['LOCALUSER']+'_'+data['TASK_ID']
elif 'm' in data['CRON']:
cron_env = "*/"+data['CRON'].replace('m','')+" * * * * root /usr/local/bin/syncctl start "+data['LOCALUSER']+'_'+data['TASK_ID']
f = open("./cron/"+data['LOCALUSER']+'_'+data['TASK_ID']+".cron", "w", encoding="utf-8")
# MAIL_HOST is an env variable used by perl/imapsync
# env of the cron !== env of the service
f.write('MAIL_HOST='+os.environ['MAIL_HOST']+"\n")
f.write(cron_env+"\n")
f.close()
2 changes: 1 addition & 1 deletion imageroot/events/mail-settings-changed/80start_services
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ if event['module_uuid'] == os.getenv('MAIL_SERVER', ''):
providers = agent.list_service_providers(rdb, 'imap', 'tcp', {
'module_uuid': mail_server,
})
mail_host = providers[0]["mail_host"]
mail_host = providers[0]["host"]
agent.set_env("MAIL_HOST",mail_host)
agent.run_helper("systemctl", "--user", "try-restart", "imapsync.service").check_returncode()
1 change: 1 addition & 0 deletions imageroot/systemd/user/imapsync.service
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ExecStartPre=/bin/rm -f %t/imapsync.pid %t/imapsync.ctr-id
ExecStartPre=mkdir -vp %S/state/imapsync
ExecStartPre=mkdir -vp %S/state/cron
ExecStartPre=runagent reveal-master-secret
ExecStartPre=runagent expand-cron
ExecStart=/usr/bin/podman run \
--detach \
--conmon-pidfile=%t/imapsync.pid \
Expand Down

0 comments on commit 5666031

Please sign in to comment.