Skip to content

Commit

Permalink
Merge pull request #1 from NethServer/firsRelease
Browse files Browse the repository at this point in the history
First push to wordpress module
  • Loading branch information
stephdl authored Feb 25, 2024
2 parents d6b4cdb + 1084e09 commit 29a06c1
Show file tree
Hide file tree
Showing 26 changed files with 827 additions and 830 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clean-registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ jobs:
steps:
- uses: NethServer/ns8-github-actions/.github/actions/delete-image@v1
with:
images: "kickstart"
images: "wordpress"
delete_image_token: ${{ secrets.IMAGES_CLEANUP_TOKEN }}
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# ns8-kickstart
# ns8-wordpress

This is a template module for [NethServer 8](https://github.com/NethServer/ns8-core).
To start a new module from it:

1. Click on [Use this template](https://github.com/NethServer/ns8-kickstart/generate).
1. Click on [Use this template](https://github.com/NethServer/ns8-wordpress/generate).
Name your repo with `ns8-` prefix (e.g. `ns8-mymodule`).
Do not end your module name with a number, like ~~`ns8-baaad2`~~!

Expand All @@ -13,10 +13,10 @@ To start a new module from it:
1. Rename some references inside the repo:
```
modulename=$(basename $(pwd) | sed 's/^ns8-//') &&
git mv imageroot/systemd/user/kickstart.service imageroot/systemd/user/${modulename}.service &&
git mv imageroot/systemd/user/kickstart-app.service imageroot/systemd/user/${modulename}-app.service &&
git mv tests/kickstart.robot tests/${modulename}.robot &&
sed -i "s/kickstart/${modulename}/g" $(find .github/ * -type f) &&
git mv imageroot/systemd/user/wordpress.service imageroot/systemd/user/${modulename}.service &&
git mv imageroot/systemd/user/wordpress-app.service imageroot/systemd/user/${modulename}-app.service &&
git mv tests/wordpress.robot tests/${modulename}.robot &&
sed -i "s/wordpress/${modulename}/g" $(find .github/ * -type f) &&
git commit -a -m "Repository initialization"
```

Expand All @@ -33,16 +33,16 @@ To start a new module from it:

Instantiate the module with:

add-module ghcr.io/nethserver/kickstart:latest 1
add-module ghcr.io/nethserver/wordpress:latest 1

The output of the command will return the instance name.
Output example:

{"module_id": "kickstart1", "image_name": "kickstart", "image_url": "ghcr.io/nethserver/kickstart:latest"}
{"module_id": "wordpress1", "image_name": "wordpress", "image_url": "ghcr.io/nethserver/wordpress:latest"}

## Configure

Let's assume that the mattermost instance is named `kickstart1`.
Let's assume that the mattermost instance is named `wordpress1`.

Launch `configure-module`, by setting the following parameters:
- `host`: a fully qualified domain name for the application
Expand All @@ -53,31 +53,31 @@ Launch `configure-module`, by setting the following parameters:
Example:

```
api-cli run configure-module --agent module/kickstart1 --data - <<EOF
api-cli run configure-module --agent module/wordpress1 --data - <<EOF
{
"host": "kickstart.domain.com",
"host": "wordpress.domain.com",
"http2https": true,
"lets_encrypt": false
}
EOF
```

The above command will:
- start and configure the kickstart instance
- start and configure the wordpress instance
- configure a virtual host for trafik to access the instance

## Get the configuration
You can retrieve the configuration with

```
api-cli run get-configuration --agent module/kickstart1
api-cli run get-configuration --agent module/wordpress1
```

## Uninstall

To uninstall the instance:

remove-module --no-preserve kickstart1
remove-module --no-preserve wordpress1

## Smarthost setting discovery

Expand All @@ -86,14 +86,14 @@ Some configuration settings, like the smarthost setup, are not part of the
Redis keys. To ensure the module is always up-to-date with the
centralized [smarthost
setup](https://nethserver.github.io/ns8-core/core/smarthost/) every time
kickstart starts, the command `bin/discover-smarthost` runs and refreshes
wordpress starts, the command `bin/discover-smarthost` runs and refreshes
the `state/smarthost.env` file with fresh values from Redis.

Furthermore if smarthost setup is changed when kickstart is already
Furthermore if smarthost setup is changed when wordpress is already
running, the event handler `events/smarthost-changed/10reload_services`
restarts the main module service.

See also the `systemd/user/kickstart.service` file.
See also the `systemd/user/wordpress.service` file.

This setting discovery is just an example to understand how the module is
expected to work: it can be rewritten or discarded completely.
Expand All @@ -102,62 +102,62 @@ expected to work: it can be rewritten or discarded completely.

some CLI are needed to debug

- The module runs under an agent that initiate a lot of environment variables (in /home/kickstart1/.config/state), it could be nice to verify them
- The module runs under an agent that initiate a lot of environment variables (in /home/wordpress1/.config/state), it could be nice to verify them
on the root terminal

`runagent -m kickstart1 env`
`runagent -m wordpress1 env`

- you can become runagent for testing scripts and initiate all environment variables

`runagent -m kickstart1`
`runagent -m wordpress1`

the path become :
```
echo $PATH
/home/kickstart1/.config/bin:/usr/local/agent/pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/
/home/wordpress1/.config/bin:/usr/local/agent/pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/
```

- if you want to debug a container or see environment inside
`runagent -m kickstart1`
`runagent -m wordpress1`
```
podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d292c6ff28e9 localhost/podman-pause:4.6.1-1702418000 9 minutes ago Up 9 minutes 127.0.0.1:20015->80/tcp 80b8de25945f-infra
d8df02bf6f4a docker.io/library/mariadb:10.11.5 --character-set-s... 9 minutes ago Up 9 minutes 127.0.0.1:20015->80/tcp mariadb-app
9e58e5bd676f docker.io/library/nginx:stable-alpine3.17 nginx -g daemon o... 9 minutes ago Up 9 minutes 127.0.0.1:20015->80/tcp kickstart-app
9e58e5bd676f docker.io/library/nginx:stable-alpine3.17 nginx -g daemon o... 9 minutes ago Up 9 minutes 127.0.0.1:20015->80/tcp wordpress-app
```

you can see what environment variable is inside the container
```
podman exec kickstart-app env
podman exec wordpress-app env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TERM=xterm
PKG_RELEASE=1
MARIADB_DB_HOST=127.0.0.1
MARIADB_DB_NAME=kickstart
MARIADB_DB_NAME=wordpress
MARIADB_IMAGE=docker.io/mariadb:10.11.5
MARIADB_DB_TYPE=mysql
container=podman
NGINX_VERSION=1.24.0
NJS_VERSION=0.7.12
MARIADB_DB_USER=kickstart
MARIADB_DB_PASSWORD=kickstart
MARIADB_DB_USER=wordpress
MARIADB_DB_PASSWORD=wordpress
MARIADB_DB_PORT=3306
HOME=/root
```

you can run a shell inside the container

```
podman exec -ti kickstart-app sh
podman exec -ti wordpress-app sh
/ #
```
## Testing

Test the module using the `test-module.sh` script:


./test-module.sh <NODE_ADDR> ghcr.io/nethserver/kickstart:latest
./test-module.sh <NODE_ADDR> ghcr.io/nethserver/wordpress:latest

The tests are made using [Robot Framework](https://robotframework.org/)

Expand Down
12 changes: 6 additions & 6 deletions build-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ images=()
# The image will be pushed to GitHub container registry
repobase="${REPOBASE:-ghcr.io/nethserver}"
# Configure the image name
reponame="kickstart"
reponame="wordpress"

# Create a new empty container image
container=$(buildah from scratch)

# Reuse existing nodebuilder-kickstart container, to speed up builds
if ! buildah containers --format "{{.ContainerName}}" | grep -q nodebuilder-kickstart; then
# Reuse existing nodebuilder-wordpress container, to speed up builds
if ! buildah containers --format "{{.ContainerName}}" | grep -q nodebuilder-wordpress; then
echo "Pulling NodeJS runtime..."
buildah from --name nodebuilder-kickstart -v "${PWD}:/usr/src:Z" docker.io/library/node:lts
buildah from --name nodebuilder-wordpress -v "${PWD}:/usr/src:Z" docker.io/library/node:lts
fi

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

# Add imageroot directory to the container image
Expand All @@ -45,7 +45,7 @@ buildah config --entrypoint=/ \
--label="org.nethserver.authorizations=traefik@node:routeadm" \
--label="org.nethserver.tcp-ports-demand=1" \
--label="org.nethserver.rootfull=0" \
--label="org.nethserver.images=docker.io/mariadb:10.11.5 docker.io/nginx:stable-alpine3.17" \
--label="org.nethserver.images=docker.io/mariadb:10.11.5 docker.io/wordpress:6.4.3-php8.3-apache" \
"${container}"
# Commit the image
buildah commit "${container}" "${repobase}/${reponame}"
Expand Down
33 changes: 0 additions & 33 deletions imageroot/actions/configure-module/10configure_environment_vars

This file was deleted.

22 changes: 22 additions & 0 deletions imageroot/actions/configure-module/10secrets
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

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

import os
import agent
import secrets
import binascii

# Generate a random hex string of length 32
def generate_random_hex(length):
random_bytes = secrets.token_bytes(length)
random_hex = binascii.hexlify(random_bytes).decode('utf-8')
return random_hex

# Set the SECRET environment variable
if not os.environ.get("SECRET"):
secret = generate_random_hex(32)
agent.set_env("SECRET", secret)
2 changes: 1 addition & 1 deletion imageroot/actions/configure-module/80start_services
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ exec 1>&2

touch smarthost.env

systemctl --user enable --now kickstart.service
systemctl --user enable --now wordpress.service
10 changes: 5 additions & 5 deletions imageroot/actions/configure-module/validate-input.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Configure kickstart",
"$id": "http://nethserver.org/json-schema/task/input/kickstart/configure-module",
"description": "Configure kickstart",
"title": "Configure wordpress",
"$id": "http://nethserver.org/json-schema/task/input/wordpress/configure-module",
"description": "Configure wordpress",
"examples": [
{
"host": "kickstart.domain.org",
"host": "wordpress.domain.org",
"http2https": true,
"lets_encrypt": true
}
Expand All @@ -19,7 +19,7 @@
"properties": {
"host": {
"type": "string",
"description": "Host name for the application, like 'kickstart.domain.org'",
"description": "Host name for the application, like 'wordpress.domain.org'",
"format": "hostname",
"pattern": "\\."
},
Expand Down
10 changes: 5 additions & 5 deletions imageroot/actions/get-configuration/validate-output.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Get kickstart settings",
"$id": "http://nethserver.org/json-schema/task/input/kickstart/get-configuration",
"description": "Get kickstart settings",
"title": "Get wordpress settings",
"$id": "http://nethserver.org/json-schema/task/input/wordpress/get-configuration",
"description": "Get wordpress settings",
"examples": [
{
"host": "kickstart.domain.org",
"host": "wordpress.domain.org",
"http2https": true,
"lets_encrypt": true
}
Expand All @@ -19,7 +19,7 @@
"properties": {
"host": {
"type": "string",
"description": "Host name for the application, like 'kickstart.domain.org'",
"description": "Host name for the application, like 'wordpress.domain.org'",
"format": "idn-hostname"
},
"lets_encrypt": {
Expand Down
12 changes: 6 additions & 6 deletions imageroot/actions/restore-module/40restore_database
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ exec 1>&2 # Redirect any output to the journal (stderr)

# Prepare an initialization script that restores the dump file
mkdir -vp initdb.d
mv -v kickstart.sql initdb.d
mv -v wordpress.sql initdb.d
#do the bash file to restore and exit once done
cat - >initdb.d/zz_kickstart_restore.sh <<'EOS'
cat - >initdb.d/zz_wordpress_restore.sh <<'EOS'
# Print additional information:
mysql --version
# The script is sourced, override entrypoint args and exit:
Expand All @@ -25,7 +25,7 @@ EOS
trap 'rm -rfv initdb.d/' EXIT

# we start a container to initiate a database and load the dump
# at the end of kickstart_restore.sh the dump is loaded and
# at the end of wordpress_restore.sh the dump is loaded and
# we exit the container
podman run \
--rm \
Expand All @@ -34,8 +34,8 @@ podman run \
--volume=./initdb.d:/docker-entrypoint-initdb.d:z \
--volume mysql-data:/var/lib/mysql/:Z \
--env MARIADB_ROOT_PASSWORD=Nethesis,1234 \
--env MARIADB_DATABASE=kickstart \
--env MARIADB_USER=kickstart \
--env MARIADB_PASSWORD=kickstart \
--env MARIADB_DATABASE=wordpress \
--env MARIADB_USER=wordpress \
--env MARIADB_PASSWORD=wordpress \
--replace --name=restore_db \
${MARIADB_IMAGE}
2 changes: 1 addition & 1 deletion imageroot/bin/discover-smarthost
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ envfile = "smarthost.env"
# Using .tmp suffix: do not overwrite the target file until the new one is
# saved to disk:
with open(envfile + ".tmp", "w") as efp:
# HINT for kickstart: adjust variable names as needed
# HINT for wordpress: adjust variable names as needed
print(f"SMTP_ENABLED={'1' if smtp_settings['enabled'] else ''}", file=efp)
print(f"SMTP_HOST={smtp_settings['host']}", file=efp)
print(f"SMTP_PORT={smtp_settings['port']}", file=efp)
Expand Down
2 changes: 1 addition & 1 deletion imageroot/bin/module-cleanup-state
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#

rm -vf kickstart.sql
rm -vf wordpress.sql
Loading

0 comments on commit 29a06c1

Please sign in to comment.