From fc63fa4e40c5df508a8dbec9af60a5df97863f03 Mon Sep 17 00:00:00 2001 From: Matteo Valentini Date: Mon, 23 Jan 2023 14:37:53 +0100 Subject: [PATCH] actions: use new admin webapp settings REST API Use the new REST API to set up the admin setting instead of directly accessing the database. --- .../actions/configure-module/15start_service | 21 ++--- imageroot/actions/configure-module/20config | 91 ++++++++----------- imageroot/actions/create-module/10env | 1 + imageroot/scripts/expandconfig-webapp | 1 + 4 files changed, 49 insertions(+), 65 deletions(-) diff --git a/imageroot/actions/configure-module/15start_service b/imageroot/actions/configure-module/15start_service index b983f575..07e86933 100755 --- a/imageroot/actions/configure-module/15start_service +++ b/imageroot/actions/configure-module/15start_service @@ -6,21 +6,20 @@ # # Redirect any output to the journal (stderr) -set -e - exec 1>&2 -systemctl --user start postgres +systemctl --user start webapp -podman exec -i postgres sh -s <<'EOF' -query="SELECT EXISTS ( SELECT * FROM pg_tables WHERE schemaname = 'core' AND tablename = 'settings');" -psql -q -U postgres webtop5 -tA -c "$query" 2> /dev/null | grep -q t -db_check=$? +curl -s -f "http://localhost:$TCP_PORT/webtop/api/com.sonicle.webtop.core/v1/admin/settings" \ + -H "Authorization: Bearer $WEBAPP_API_TOKEN" \ + -H 'accept: application/json'> /dev/null +webapp_check=$? c=10 -while [ "$db_check" -ne 0 -o $c -eq 0 ]; do +while [ "$webapp_check" -ne 0 -o $c -eq 0 ]; do sleep 1s - psql -q -U postgres webtop5 -tA -c "$query" 2> /dev/null | grep -q t - db_check=$? + curl -s -f "http://localhost:$TCP_PORT/webtop/api/com.sonicle.webtop.core/v1/admin/settings" \ + -H "Authorization: Bearer $WEBAPP_API_TOKEN" \ + -H 'accept: application/json' > /dev/null + webapp_check=$? c=$(expr $c - 1 ) done -EOF diff --git a/imageroot/actions/configure-module/20config b/imageroot/actions/configure-module/20config index 5e2f9855..9ff215f5 100755 --- a/imageroot/actions/configure-module/20config +++ b/imageroot/actions/configure-module/20config @@ -11,6 +11,10 @@ import os import agent import agent.ldapproxy import subprocess +import urllib.request + +api_endpoint = "http://localhost:" + os.environ["TCP_PORT"] + "/webtop/api/com.sonicle.webtop.core/v1" +api_headers={"Content-Type": "application/json", "Authorization": "Bearer " + os.environ["WEBAPP_API_TOKEN"]} def domain_setup(user_domain_name, user_domain): user_domain_uri = "ldapneth://accountprovider" + ":" + user_domain["port"] @@ -74,24 +78,19 @@ response = agent.tasks.run( # Check if traefik configuration has been successfull agent.assert_exp(response['exit_code'] == 0) -if data["hostname"] != os.getenv("WEBTOP_HOSTNAME"): - - public_url = 'https://' + data["hostname"] + '/webtop' - dav_url = 'https://' + data["hostname"] + '/webtop-dav/server.php' - - 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 = 'public.url';\n", file=psql.stdin) - print("INSERT INTO \"core\".\"settings\" (\"service_id\", \"key\", \"value\") VALUES ('com.sonicle.webtop.core', 'public.url', \'" + public_url + "\');\n", file=psql.stdin) +# Configure the webapp +public_url = 'https://' + data["hostname"] + '/webtop' +dav_url = 'https://' + data["hostname"] + '/webtop-dav/server.php' - print("DELETE FROM \"core\".\"settings\" WHERE service_id = 'com.sonicle.webtop.core' and key = 'davserver.url';\n", file=psql.stdin) - print("INSERT INTO \"core\".\"settings\" (\"service_id\", \"key\", \"value\") VALUES ('com.sonicle.webtop.core', 'davserver.url',\'" + dav_url + "\');\n", file=psql.stdin) +try: + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/public.url", data=public_url.encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/davserver.url", data=dav_url.encode(), headers=api_headers, method='PUT')) +except urllib.error.URLError as e: + raise Exception(f'Error reaching webapp daemon: {e.reason}') - agent.assert_exp(psql.returncode == 0) # check the command is succesfull +agent.set_env("WEBTOP_HOSTNAME", data["hostname"]) - agent.set_env("WEBTOP_HOSTNAME", data["hostname"]) - agent.set_env("RESTART_WEBAPP", "1") - -if "locale" in data and data["locale"] != os.environ["WEBTOP_LOCALE"]: +if "locale" in data: locales = { 'it_IT': { @@ -152,32 +151,23 @@ if "locale" in data and data["locale"] != os.environ["WEBTOP_LOCALE"]: } } - 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 = 'default.i18n.languageTag';\n", file=psql.stdin) - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.startDay';\n", file=psql.stdin) - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.date.short';\n", file=psql.stdin) - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.date.long';\n", file=psql.stdin) - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.time.short';\n", file=psql.stdin) - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.time.long';\n", file=psql.stdin) - - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.languageTag', '" + locales[data['locale']]['LANGUAGE'] + "');\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.startDay', '" + locales[data['locale']]['START_DAY']+ "');\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.date.short', '" + locales[data['locale']]['DATE_FORMAT_SHORT'] + "');\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.date.long', '" + locales[data['locale']]['DATE_FORMAT_LONG'] + "');\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.time.short', '" + locales[data['locale']]['TIME_FORMAT_SHORT'] + "');\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.time.long', '" + locales[data['locale']]['TIME_FORMAT_LONG'] + "');\n", file=psql.stdin) - - agent.assert_exp(psql.returncode == 0) # check the command is succesfull + try: + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.languageTag", data=locales[data['locale']]['LANGUAGE'].encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.startDay", data=locales[data['locale']]['START_DAY'].encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.date.short", data=locales[data['locale']]['DATE_FORMAT_SHORT'].encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.date.long", data=locales[data['locale']]['DATE_FORMAT_LONG'].encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.time.short", data=locales[data['locale']]['TIME_FORMAT_SHORT'].encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.time.long", locales[data['locale']]['TIME_FORMAT_LONG'].encode(), headers=api_headers, method='PUT')) + except urllib.error.URLError as e: + raise Exception(f'Error reaching webapp daemon: {e.reason}') agent.set_env("WEBTOP_LOCALE", data["locale"]) - agent.set_env("RESTART_WEBAPP", "1") - -if "timezone" in data and data["timezone"] != os.environ["WEBTOP_TIMEZONE"]: - 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 = 'default.i18n.timezone';\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.timezone','" + data["timezone"] + "');\n", file=psql.stdin) - agent.assert_exp(psql.returncode == 0) # check the command is succesfull +if "timezone" in data: + try: + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.timezone", data=data["timezone"].encode(), headers=api_headers, method='PUT')) + except urllib.error.URLError as e: + raise Exception(f'Error reaching webapp daemon: {e.reason}') agent.set_env("WEBTOP_TIMEZONE", data["timezone"]) @@ -185,7 +175,7 @@ if "timezone" in data and data["timezone"] != os.environ["WEBTOP_TIMEZONE"]: agent.set_env("RESTART_WEBDAV", "1") agent.set_env("RESTART_Z_PUSH", "1") -if "mail_module" in data and data["mail_module"] != os.getenv("MAIL_MODULE"): +if "mail_module" in data: mail_module = data["mail_module"] smtp_host = rdb.hget(f"module/{mail_module}/srv/tcp/submission", "host") or "" @@ -207,21 +197,14 @@ if "mail_module" in data and data["mail_module"] != os.getenv("MAIL_MODULE"): vmail_password = response['output']['password'] - 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 = 'smtp.host';\n", file=psql.stdin) - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'smtp.port';\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'smtp.host', '" + smtp_host + "');\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'smtp.port', '" + smtp_port + "');\n", file=psql.stdin) - - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.mail' and key = 'default.host';\n", file=psql.stdin) - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.mail' and key = 'default.port';\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.mail', 'default.host', '" + imap_host + "');\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.mail', 'default.port', '" + imap_port + "');\n", file=psql.stdin) - - print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.mail' and key = 'nethtop.vmail.secret';\n", file=psql.stdin) - print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.mail', 'nethtop.vmail.secret', '" + vmail_password + "');\n", file=psql.stdin) - - agent.assert_exp(psql.returncode == 0) # check the command is succesfull + try: + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/smtp.host", smtp_host.encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/smtp.port", smtp_port.encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.mail/default.host", imap_host.encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.mail/default.port", imap_port.encode(), headers=api_headers, method='PUT')) + urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.mail/nethtop.vmail.secret", vmail_password.encode(), headers=api_headers, method='PUT')) + except urllib.error.URLError as e: + raise Exception(f'Error reaching webapp daemon: {e.reason}') user_domain_name = rdb.hget(f"module/{mail_module}/srv/tcp/imap", "user_domain") or "" user_domain = agent.ldapproxy.Ldapproxy().get_domain(user_domain_name) or {} diff --git a/imageroot/actions/create-module/10env b/imageroot/actions/create-module/10env index fc4aa52a..77524cd8 100755 --- a/imageroot/actions/create-module/10env +++ b/imageroot/actions/create-module/10env @@ -21,4 +21,5 @@ set-env Z_PUSH_USE_LEGACY_FOLDER_IDS false set-env WEBAPP_JS_DEBUG False set-env WEBAPP_MIN_MEMORY 512 set-env WEBAPP_MAX_MEMORY 1024 +set-env WEBAPP_API_TOKEN $(uuidgen) EOF diff --git a/imageroot/scripts/expandconfig-webapp b/imageroot/scripts/expandconfig-webapp index 96a3d4c0..4fc3eae3 100755 --- a/imageroot/scripts/expandconfig-webapp +++ b/imageroot/scripts/expandconfig-webapp @@ -12,4 +12,5 @@ webtop.log.target=console webtop.session.forcesecurecookie=true webtop.js.debug=$WEBAPP_JS_DEBUG webtop.home=/var/lib/nethserver/webtop +webtop.provisioning.api.token=$WEBAPP_API_TOKEN EOF