Skip to content

Commit

Permalink
actions: use new admin webapp settings REST API
Browse files Browse the repository at this point in the history
Use the new REST API to set up the admin setting instead of directly
accessing the database.
  • Loading branch information
Amygos committed Jun 13, 2023
1 parent 262c80a commit 202becd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 65 deletions.
21 changes: 10 additions & 11 deletions imageroot/actions/configure-module/15start_service
Original file line number Diff line number Diff line change
Expand Up @@ -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
91 changes: 37 additions & 54 deletions imageroot/actions/configure-module/20config
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -152,40 +151,31 @@ 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"])

agent.set_env("RESTART_WEBAPP", "1")
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 ""
Expand All @@ -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 {}
Expand Down
1 change: 1 addition & 0 deletions imageroot/scripts/expandconfig-webapp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 202becd

Please sign in to comment.