Skip to content

Commit

Permalink
Merge pull request #27 from NethServer/fixEjabberd
Browse files Browse the repository at this point in the history
Retrieve ejabberd servers host by discovery and event NethServer/dev#6780
  • Loading branch information
stephdl authored Nov 29, 2023
2 parents d8ea45d + 12747bf commit 1796182
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 1 deletion.
33 changes: 33 additions & 0 deletions imageroot/actions/configure-module/20config
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ if "timezone" in data and data["timezone"] != os.environ["WEBTOP_TIMEZONE"]:
agent.set_env("RESTART_WEBDAV", "1")
agent.set_env("RESTART_Z_PUSH", "1")

# adjust the ejabberd module, when we disabled globally the chat module we use ''
if "ejabberd_module" in data and "ejabberd_domain" in data:
if data["ejabberd_module"] != os.getenv("EJABBERD_MODULE","") and data["ejabberd_domain"] != os.getenv("EJABBERD_DOMAIN",""):
ejabberd_module = data["ejabberd_module"]
ejabberd_domain = data["ejabberd_domain"]
# Save the ejabberd module UUID to automatically re-configure the module if it is
# already available:
if ejabberd_module != "":
ejabberd_uuid = rdb.hget(f"module/{ejabberd_module}/srv/tcp/xmpp", "module_uuid") or ""
# retrieve the ejabberd host and port
ejabberd_host = rdb.hget(f"module/{ejabberd_module}/srv/tcp/xmpp", "host") or ""
ejabberd_port = rdb.hget(f"module/{ejabberd_module}/srv/tcp/xmpp", "port") or ""
else:
# if ejabberd module is empty, we set the default values
# we do not want to use the chat module
ejabberd_uuid = ""
ejabberd_host = "localhost"
ejabberd_port = "5222"

with subprocess.Popen(['podman', 'exec', '-i', 'postgres', 'psql', '-U', 'postgres', 'webtop5'], stdout=sys.stderr, stdin=subprocess.PIPE, text=True) as psql:
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'xmpp.host';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'xmpp.port';\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'xmpp.host', '" + ejabberd_host + "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'xmpp.port', '" + ejabberd_port + "');\n", file=psql.stdin)

agent.assert_exp(psql.returncode == 0) # check the command is succesfull

# we set the env variable to configure the module
agent.set_env("EJABBERD_MODULE", ejabberd_module)
agent.set_env("EJABBERD_DOMAIN", ejabberd_domain)
agent.set_env("EJABBERD_MODULE_UUID", ejabberd_uuid)
agent.set_env("RESTART_WEBAPP", "1")

if "mail_module" in data and "mail_domain" in data:
if data["mail_module"] != os.getenv("MAIL_MODULE") or data["mail_domain"] != os.getenv("MAIL_DOMAIN"):
mail_module = data["mail_module"]
Expand Down
16 changes: 16 additions & 0 deletions imageroot/actions/configure-module/validate-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@
"Etc/UTC"
]
},
"ejabberd_module": {
"type": "string",
"title": "Ejabberd module ID",
"description": "Ejabberd module used by WebTop for xmpp",
"examples": [
"ejabberd1"
]
},
"ejabberd_domain": {
"type": "string",
"title": "Ejabberd domain of users",
"description": "Ejabberd domain used by WebTop users for xmpp",
"examples": [
"domain.com"
]
},
"mail_module": {
"type": "string",
"title": "Mail module ID",
Expand Down
2 changes: 2 additions & 0 deletions imageroot/actions/get-configuration/20readconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ config={
"timezone": os.environ["WEBTOP_TIMEZONE"],
"mail_module": os.getenv("MAIL_MODULE", ""),
"mail_domain": os.getenv("MAIL_DOMAIN", ""),
"ejabberd_module": os.getenv("EJABBERD_MODULE", ""),
"ejabberd_domain": os.getenv("EJABBERD_DOMAIN", ""),
"webapp": {
"debug": os.environ["WEBAPP_JS_DEBUG"].lower() in ('true', '1', 't'),
"min_memory": int(os.environ["WEBAPP_MIN_MEMORY"]),
Expand Down
16 changes: 16 additions & 0 deletions imageroot/actions/get-configuration/validate-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@
"domain.com"
]
},
"ejabberd_domain": {
"type": "string",
"title": "Ejabberd domain of users",
"description": "Ejabberd domain used by WebTop users for xmpp",
"examples": [
"domain.com"
]
},
"ejabberd_module": {
"type": "string",
"title": "Ejabberd module ID",
"description": "Ejabberd module used by WebTop for xmpp",
"examples": [
"ejabberd1"
]
},
"webapp": {
"type":"object",
"title": "WebTop's Java webapp configuration",
Expand Down
11 changes: 11 additions & 0 deletions imageroot/actions/get-defaults/20readconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rdb = agent.redis_connect() # full read-only access on every key
modules=[]
accepted_timezone_list = []
list_of_timezone_widget = []
ejabberd = []

# we query about all mail server to use it inside the user interface
for key in agent.list_service_providers(rdb,'imap','tcp'):
Expand All @@ -38,8 +39,18 @@ for value in accepted_timezone_list:
"value": value,
})

# Retrieve all ejabberd servers among the cluster
for key in agent.list_service_providers(rdb,'xmpp','tcp'):
obj = {
"name": key['module_id'],
"label": f"{key['ejabberd_hostname']} (node: {key['node']})",
"value": key['module_id']+','+key['ejabberd_hostname']
}
ejabberd.append(obj)

config={
"mail_modules_id": modules,
"ejabberd_modules_id": ejabberd,
"accepted_timezone_list": list_of_timezone_widget
}

Expand Down
19 changes: 19 additions & 0 deletions imageroot/events/ejabberd-settings-changed/10configure_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3

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

import sys
import json
import os
import webtop
import agent

event_input = json.load(sys.stdin)

if event_input['module_uuid'] == os.getenv('EJABBERD_MODULE_UUID'):
# We need to set the NEW EJABBERD_MODULE env variable to configure the module
agent.set_env('EJABBERD_MODULE', event_input['module_id'])
webtop.configure_module(os.getenv('MAIL_MODULE'), os.environ) # Ignore exit code
1 change: 1 addition & 0 deletions imageroot/pypkg/webtop.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def configure_module(mail_module, penv):
"locale": penv['WEBTOP_LOCALE'],
"timezone": penv['WEBTOP_TIMEZONE'],
"mail_module": mail_module,
"ejabberd_module": penv['EJABBERD_MODULE'],
"webapp": {
"debug": penv['WEBAPP_JS_DEBUG'] == 'True',
"min_memory": int(penv['WEBAPP_MIN_MEMORY']),
Expand Down
6 changes: 5 additions & 1 deletion ui/public/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
"LOCALE_hu_HU": "hu_HU",
"LOCALE_fr_FR": "fr_FR",
"configuring": "Configuring",
"domain_already_used_in_traefik": "This domain is already used in Traefik"
"domain_already_used_in_traefik": "This domain is already used in Traefik",
"ejabberd_server": "Ejabberd/Chat server",
"choose_ejabberd_server": "Choose an Ejabberd/Chat server in the list",
"choose_the_ejabberd_server_to_use": "Refer to WebTop documentation to enable the Chat integration, the WebTop mail domain must match the Ejabberd domain",
"no_ejabberd_server": "No Ejabberd/Chat server"
},
"about": {
"title": "About"
Expand Down
56 changes: 56 additions & 0 deletions ui/src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@
{{ $t("settings.choose_the_mail_server_to_use") }}
</template>
</NsComboBox>
<NsComboBox
v-if="ejabberd_modules_id.length > 1"
v-model.trim="ejabberd_module"
:autoFilter="true"
:autoHighlight="true"
:title="$t('settings.ejabberd_server')"
:label="$t('settings.choose_ejabberd_server')"
:options="ejabberd_modules_id"
:acceptUserInput="false"
:showItemType="true"
:invalid-message="$t(error.ejabberd_module)"
:disabled="
loading.getConfiguration ||
loading.configureModule ||
loading.getDefaults ||
! mail_module
"
tooltipAlignment="start"
tooltipDirection="top"
ref="ejabberd_module"
>
<template slot="tooltip">
{{ $t("settings.choose_the_ejabberd_server_to_use") }}
</template>
</NsComboBox>
<cv-dropdown
:value="locale"
v-model="locale"
Expand Down Expand Up @@ -374,6 +399,9 @@ export default {
mail_module: "",
mail_domain: "",
mail_modules_id: [],
ejabberd_module: "",
ejabberd_domain: "",
ejabberd_modules_id: [],
accepted_timezone_list: [],
locale: "",
timezone: "",
Expand All @@ -400,6 +428,7 @@ export default {
hostname: "",
request_https_certificate: "",
mail_module: "",
ejabberd_module: "",
locale: "",
timezone: "",
limit_min: "",
Expand Down Expand Up @@ -483,6 +512,20 @@ export default {
listWidgetOptionsCompleted(taskContext, taskResult) {
const config = taskResult.output;
this.mail_modules_id = config.mail_modules_id;
// Extract hostnames from mail_module_id values
const mailHostnames = config.mail_modules_id.map(item => item.value.split(',')[1]);

// Filter ejabberd_module_id based on matching hostnames
this.ejabberd_modules_id = config.ejabberd_modules_id.filter(item => {
const ejabberdHostname = item.value.split(',')[1];
return mailHostnames.includes(ejabberdHostname);
});

this.ejabberd_modules_id.unshift({
name: "-",
label: this.$t("settings.no_ejabberd_server"),
value: "-",
});
this.accepted_timezone_list = config.accepted_timezone_list;
this.getConfiguration();
this.loading.getDefaults = false;
Expand Down Expand Up @@ -549,6 +592,13 @@ export default {
} else {
this.mail_module = "";
}
const ejabberd_module_tmp = config.ejabberd_module;
const ejabberd_domain_tmp = config.ejabberd_domain;
if (ejabberd_module_tmp && ejabberd_domain_tmp) {
this.ejabberd_module = ejabberd_module_tmp + ',' + ejabberd_domain_tmp;
} else {
this.ejabberd_module = "-";
}
this.timezone = config.timezone;
});
this.loading.getConfiguration = false;
Expand All @@ -574,6 +624,7 @@ export default {
}
isValidationOk = false;
}

if (parseInt(this.webapp.min_memory) > parseInt(this.webapp.max_memory)) {
this.error.limit_min = "error.choose_min_webapp_memory_MB";
this.webapp.min_memory = this.webapp.max_memory;
Expand Down Expand Up @@ -629,6 +680,9 @@ export default {
const tmparray = this.mail_module.split(',');
const mail_module_tmp = tmparray[0];
const mail_domain_tmp = tmparray[1];
const tmp_ejabberd = this.ejabberd_module.split(',');
const ejabberd_module_tmp = tmp_ejabberd[0] !== '-' ? tmp_ejabberd[0] || "" : "";
const ejabberd_domain_tmp = tmp_ejabberd[1] || "";
const res = await to(
this.createModuleTaskForApp(this.instanceName, {
action: taskAction,
Expand All @@ -637,6 +691,8 @@ export default {
request_https_certificate: this.isLetsEncryptEnabled,
mail_module: mail_module_tmp,
mail_domain: mail_domain_tmp,
ejabberd_module: ejabberd_module_tmp,
ejabberd_domain: ejabberd_domain_tmp,
locale: this.locale,
timezone: this.timezone,
webapp: {
Expand Down

0 comments on commit 1796182

Please sign in to comment.