Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configure-module action #2

Merged
merged 10 commits into from
Nov 29, 2022
4 changes: 2 additions & 2 deletions build-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ container=$(buildah from scratch)
# Reuse existing nodebuilder-webtop container, to speed up builds
if ! buildah containers --format "{{.ContainerName}}" | grep -q nodebuilder-webtop; then
echo "Pulling NodeJS runtime..."
buildah from --name nodebuilder-webtop -v "${PWD}:/usr/src:Z" docker.io/library/node:lts
buildah from --name nodebuilder-webtop -v "${PWD}:/usr/src:Z" docker.io/library/node:18-slim
fi

echo "Build static UI files with node..."
buildah run nodebuilder-webtop sh -c "cd /usr/src/ui && yarn install && yarn build"
buildah run --env="NODE_OPTIONS=--openssl-legacy-provider" nodebuilder-webtop sh -c "cd /usr/src/ui && yarn install && yarn build"

# Add imageroot directory to the container image
buildah add "${container}" imageroot /imageroot
Expand Down
41 changes: 41 additions & 0 deletions imageroot/actions/configure-module/10start_service
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

#
# Copyright (C) 2022 Nethesis S.r.l.
# http://www.nethesis.it - [email protected]
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer. If not, see COPYING.
#
Comment on lines +3 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to use SPDX

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix the license header in all files in other corrective commits.

# Redirect any output to the journal (stderr)

set -e

exec 1>&2

systemctl --user start postgres

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=$?
c=10
while [ "$db_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=$?
c=$(expr $c - 1 )
done
EOF
87 changes: 87 additions & 0 deletions imageroot/actions/configure-module/20config
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3

#
# Copyright (C) 2022 Nethesis S.r.l.
# http://www.nethesis.it - [email protected]
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer. If not, see COPYING.
#

#
# Create a virtual host configuration
# Input example:
# {"instance": "module1", "url": "http://127.0.0.0:2000", "path": "/foo", "http2https": true}
#

import json
import sys
import os
import agent
import subprocess

# Try to parse the stdin as JSON.
# If parsing fails, output everything to stderr
data = json.load(sys.stdin)

agent_id = os.getenv("AGENT_ID", "")
if not agent_id:
raise Exception("AGENT_ID not found inside the environemnt")

# Connect to redis
r = agent.redis_connect(privileged=True).pipeline()

restart_webapp = False

webtop_request_https_certificate = os.environ["WEBTOP_REQUEST_HTTPS_CERTIFICATE"].lower() in ('true', '1', 't')
if data.get("request_https_certificate") is not None:
if data.get("request_https_certificate") != webtop_request_https_certificate:
webtop_request_https_certificate = data["request_https_certificate"]
agent.set_env("WEBTOP_REQUEST_HTTPS_CERTIFICATE", data["request_https_certificate"])

# Configure Traefik to route WebTop's host requests to the webtop module
response = agent.tasks.run(
agent_id=agent.resolve_agent_id('traefik@node'),
action='set-route',
data={
'instance': os.environ['MODULE_ID'],
'url': 'http://127.0.0.1:' + os.environ["TCP_PORT"],
'http2https': True,
'lets_encrypt': webtop_request_https_certificate,
'host': data["hostname"],
},
)
# 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'], 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)

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)

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

agent.set_env("WEBTOP_HOSTNAME", data["hostname"])
restart_webapp = True

if restart_webapp:
agent.run_helper("systemctl", "--user", "restart", "webapp").check_returncode()
31 changes: 31 additions & 0 deletions imageroot/actions/configure-module/validate-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "configure-module input",
"$id": "http://schema.nethserver.org/webtop/configure-route-input.json",
"description": "Configure webtop",
"examples": [
{
"hostname": "example.com"
},
{
"hostname": "example.com",
"request_https_certificate": true
}
],
"type": "object",
"required": ["hostname"],
"properties": {
"hostname": {
"type": "string",
"format": "hostname",
"title": "Hostname of the WebTop instance",
"examples": [
"example.com"
]
},
"request_https_certificate": {
"type": "boolean",
"title": "Request a valid HTTPS certificate"
}
}
}
1 change: 1 addition & 0 deletions imageroot/actions/create-module/10env
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exec 1>&2 # Send any output to stderr, to not alter the action response protocol

cat >&${AGENT_COMFD} <<EOF
set-env WEBTOP_TIMEZONE Etc/UTC
set-env WEBTOP_REQUEST_HTTPS_CERTIFICATE False
set-env WEBDAV_DEBUG True
set-env WEBDAV_LOG_LEVEL DEBUG
set-env Z_PUSH_LOG_LEVEL DEBUG
Expand Down
99 changes: 0 additions & 99 deletions imageroot/actions/create-module/20configure

This file was deleted.

35 changes: 0 additions & 35 deletions imageroot/actions/destroy-module/20destroy
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,3 @@ response = agent.tasks.run(

# Check if traefik configuration has been successfull
agent.assert_exp(response['exit_code'] == 0)

response = agent.tasks.run(
agent_id=agent.resolve_agent_id('traefik@node'),
action='delete-route',
data={
'instance': f"{os.environ['MODULE_ID']}-webdav",
},
)

# Check if traefik configuration has been successfull
agent.assert_exp(response['exit_code'] == 0)

response = agent.tasks.runp_brief([{
"agent_id": agent.resolve_agent_id('traefik@node'),
"action": "delete-route",
"data": {
'instance': f"{os.environ['MODULE_ID']}-well-known-caldav",
}},{
"agent_id": agent.resolve_agent_id('traefik@node'),
"action": "delete-route",
"data": {
'instance': f"{os.environ['MODULE_ID']}-well-known-carddav",
}}]
)

response = agent.tasks.run(
agent_id=agent.resolve_agent_id('traefik@node'),
action='delete-route',
data={
'instance': f"{os.environ['MODULE_ID']}-z-push",
},
)

# Check if traefik configuration has been successfull
agent.assert_exp(response['exit_code'] == 0)
2 changes: 2 additions & 0 deletions imageroot/systemd/user/postgres.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[Unit]
Description=postgres server
PartOf=webtop.service
Requires=webtop.service
After=webtop.service

[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Expand Down
2 changes: 1 addition & 1 deletion webtop5-build/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wt-5.18.2
wt-5.18.3