generated from NethServer/ns8-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration from Esmith smarthosts to sqlite relayrules (#102)
Add import-module/50import_smarthosts action for importing smarthosts into relayrules table
- Loading branch information
Showing
1 changed file
with
37 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,37 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2024 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import sys | ||
import json | ||
import agent | ||
import mail | ||
|
||
# Global stdout redirection: | ||
sys.stdout = sys.stderr | ||
|
||
sdb = mail.pcdb_connect() | ||
sdb.set_trace_callback(lambda m: print(agent.SD_DEBUG + m)) | ||
|
||
# import the Esmith smarthosts database | ||
with sdb: | ||
cur = sdb.cursor() | ||
|
||
print(agent.SD_INFO + "Importing smarthosts...", file=sys.stderr) | ||
for smart in json.load(open('smarthosts.json')): | ||
|
||
vals = { | ||
'rule_type':smart['type'], | ||
'rule_subject': smart['name'].removeprefix('@'), | ||
'host': smart['props'].get('Host'), | ||
'port': smart['props'].get('Port'), | ||
'mandatory_tls': 'encrypt' if smart['props'].get('TlsStatus') == 'enabled' else 'may', | ||
'username': smart['props'].get('Username'), | ||
'password': smart['props'].get('Password'), | ||
'enabled': True if smart['props'].get('status') == 'enabled' else False, | ||
} | ||
cur.execute("""INSERT INTO relayrules (rule_type, rule_subject , host, port, tls, username, password, enabled) | ||
VALUES (:rule_type, :rule_subject, :host, :port, :mandatory_tls, :username, :password, :enabled)""", vals) |