Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api.get-configuration read env files
Browse files Browse the repository at this point in the history
stephdl committed Oct 2, 2023
1 parent 7b86302 commit 782018b
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions imageroot/actions/get-configuration/20read
Original file line number Diff line number Diff line change
@@ -27,28 +27,54 @@ modules=[]
tmp = []
# we query about all mail server to use it inside the user interface
for key in agent.list_service_providers(rdb,'imap','tcp'):
obj = {
server = {
"name": key['module_id'],
"label": f"{key['module_id']} ({key['mail_hostname']})",
"value": key['module_uuid']+','+key['mail_hostname'],
}
modules.append(obj)
modules.append(server)

# use it inside a dropdown
config['mail_server_URL'] = modules

# we want to find all enabled mailbox, first retrieve the mail server we are bound
# second retrieve the mail box by action list-user-mailboxes and make a list of enabled mailbox
# read env files, test if mailbox is enabled to push to config[user_properties]

# retrieve mail module_id from module_uuid
mail_server = os.environ["MAIL_SERVER"]
providers = agent.list_service_providers(rdb, 'imap', 'tcp', {
'module_uuid': mail_server,
})
mail_id = providers[0]["module_id"]
response = agent.tasks.run(f"module/{mail_id}", action='list-user-mailboxes')
agent.assert_exp(response['exit_code'] == 0)
user_mailboxes = response['output']['user_mailboxes']

# Create a dictionary to store enabled user mailboxes
enabled_mailboxes = []

# Iterate through user_mailboxes and filter by 'enabled' (if the field exists)
for mailbox in user_mailboxes:
if 'enabled' in mailbox and not mailbox['enabled']:
continue # Skip disabled mailboxes
enabled_mailboxes.append(mailbox['user'])


# we load env file from imapsync/*.env and remote password
# Define the path to the file containing environment variables
json_object = []
user_properties = []
for file in glob.iglob("imapsync/*.env"):
local_user = file.removeprefix('imapsync/').removesuffix('.env')
if local_user not in enabled_mailboxes:
continue
# Read the environment variables from the file
with open(file, 'r') as file:
env_lines = file.readlines()

# Initialize an empty list to store the environment variable key-value pairs
# Initialize an empty list and empty read_file to store the environment variable key-value pairs
env_vars = []
obj = json.loads('{}')
read_file = {}
# Parse the environment variables from the file
for line in env_lines:
stripped_line = line.strip()
@@ -70,24 +96,24 @@ for file in glob.iglob("imapsync/*.env"):
value = 'disabled'
if key == "deletefolder":
continue
obj.update({key:value})
read_file.update({key:value})
# remote password
with open('imapsync/'+local_user+'.pwd', 'r') as file:
env_lines = file.readlines()
for line in env_lines:
obj.update({'password':line})
read_file.update({'password':line})

#test if service is running
service = subprocess.call(["systemctl", "is-active","--user", "--quiet", "imapsync@"+local_user+".service"])
service_status = True if service == 0 else False

# load the obj to list
env_vars.append(obj)
# Create a JSON object containing the list of environment variables
json_object.append({"props": env_vars, "local_user":local_user, "service_running":service_status})
# load the read_file to list
env_vars.append(read_file)
# Create a JSON read_fileect containing the list of environment variables
user_properties.append({"props": env_vars, "local_user":local_user, "service_running":service_status})

# Serialize the JSON object to a JSON-formatted string
config['user_properties'] = json_object
# Serialize the JSON read_fileect to a JSON-formatted string
config['user_properties'] = user_properties

# Dump the configuration to stdout
json.dump(config, fp=sys.stdout)

0 comments on commit 782018b

Please sign in to comment.