new: [opensem] add charm

This commit is contained in:
Valentin Lab
2025-09-25 16:31:23 +02:00
commit 32b0ed5645
11 changed files with 642 additions and 0 deletions

21
opensem/hooks/init Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
## Init is run on host
## For now it is run every time the script is launched, but
## it should be launched only once after build.
## Accessible variables are:
## - SERVICE_NAME Name of current service
## - DOCKER_BASE_IMAGE Base image from which this service might be built if any
## - SERVICE_DATASTORE Location on host of the DATASTORE of this service
## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service
. lib/common
set -e
opensem:init || exit 1
opensem:config || exit 1

View File

@@ -0,0 +1,86 @@
#!/bin/bash
## Should be executable N time in a row with same result.
. lib/common
set -e
LOGS=/var/log/opensem
php_fpm_service=$(service:traverse "$SERVICE_NAME":php-fpm) || {
err "Could not find php-fpm service for $SERVICE_NAME"
exit 1
}
uid_gid=$(get_service_base_image_dir_uid_gid "$php_fpm_service" /var/www/html) || {
err "Could not determine uid:gid for $php_fpm_service of dir /var/www/html"
exit 1
}
gid="${uid_gid#* }"
dirs=(
"$LOGS"
)
to_create=()
volumes=""
for d in "${dirs[@]}"; do
fdir="${SERVICE_DATASTORE}$d"
if [ -d "$fdir" ]; then
find "$fdir" \! -gid "$gid" -print0 | while read-0 f; do
chgrp -v "$gid" "$f" || return 1
done
find "$fdir" \! -perm -g+rwx -print0 | while read-0 f; do
chmod -v g+rwx "$f" || return 1
done
else
to_create+=("$fdir")
fi
done
if [ "${#to_create[@]}" -gt 0 ]; then
mkdir -p "${to_create[@]}" || return 1
chgrp -v "${gid}" "${to_create[@]}" || return 1
chmod g+rwx "${to_create[@]}" || return 1
fi
rotated_count=$(relation-get rotated-count 2>/dev/null) || true
rotated_count=${rotated_count:-52}
## XXXvlab: a lot of this intelligence should be moved away into ``logrotate`` charm
DST="$CONFIGSTORE/$TARGET_SERVICE_NAME/etc/logrotate.d/$SERVICE_NAME"
file_put "$DST" <<EOF
/var/log/docker/$SERVICE_NAME/laravel.log {
weekly
missingok
dateext
dateyesterday
dateformat _%Y-%m-%d
extension .log
rotate $rotated_count
compress
delaycompress
notifempty
create 640 root root
sharedscripts
}
EOF
opensem:config-set "LOG_CHANNEL" "single" || {
err "Could not set LOG_CHANNEL=single in opensem config"
exit 1
}
config-add "\
services:
$MASTER_TARGET_SERVICE_NAME:
volumes:
- $DST:/etc/logrotate.d/docker-${SERVICE_NAME}:ro
- $SERVICE_DATASTORE$LOGS:/var/log/docker/$SERVICE_NAME:rw
$php_fpm_service:
volumes:
- $SERVICE_DATASTORE$LOGS:/opt/apps/$SERVICE_NAME/storage/logs:rw
"

View File

@@ -0,0 +1,17 @@
#!/bin/bash
. lib/common
set -e
master_key=$(service:password:get "${TARGET_SERVICE_NAME}" "master_key" internal) || exit 1
cat <<EOF >> "${OPENSEM_CONFIG_FILE}"
## meilisearch settings
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://${TARGET_SERVICE_NAME}:7700
MEILISEARCH_KEY=${master_key}
EOF

View File

@@ -0,0 +1,28 @@
#!/bin/bash
. lib/common
PASSWORD="$(relation-get password)"
USER="$(relation-get user)"
DBNAME="$(relation-get dbname)"
# control=$(H "$USER" "$DBNAME" "$PASSWORD")
# [ "$control" == "$(relation-get control || true)" ] && exit 0
set -e
cat <<EOF >> "${OPENSEM_CONFIG_FILE}"
DB_CONNECTION=mysql
DB_HOST=${TARGET_SERVICE_NAME}
DB_PORT=3306
DB_DATABASE=$DBNAME
DB_USERNAME=$USER
DB_PASSWORD=$PASSWORD
EOF
# relation-set control "$control"
info "Configured opensem code for mysql access."

7
opensem/hooks/pre_deploy Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/bash
. lib/common
set -e
mv -v "${OPENSEM_CONFIG_FILE}" "${OPENSEM_CONFIG_FILE%.prepare}"

View File

@@ -0,0 +1,88 @@
#!/bin/bash
. lib/common
set -e
domain=$(relation-get domain) || exit 1
url=$(relation-get url) || exit 1
location=$CONFIGSTORE/$BASE_SERVICE_NAME/var/www/$domain
php_fpm_service=$(service:traverse "$SERVICE_NAME":php-fpm) || {
err "Could not find php-fpm service for $SERVICE_NAME"
exit 1
}
uid_gid=$(get_service_base_image_dir_uid_gid "$php_fpm_service" /var/www/html) || {
err "Could not determine uid:gid for $php_fpm_service of dir /var/www/html"
exit 1
}
gid="${uid_gid#* }"
dirs=(
/var/cache/opensem/bootstrap
/var/cache/opensem/framework
/var/lib/opensem/views
/var/lib/opensem/sessions
/var/lib/opensem/app
/var/lib/opensem/app/public
/var/log/opensem
)
to_create=()
volumes=""
for d in "${dirs[@]}"; do
fdir="${SERVICE_DATASTORE}$d"
if [ -d "$fdir" ]; then
find "$fdir" \! -gid "$gid" -print0 | while read-0 f; do
chgrp -v "$gid" "$f" || return 1
done
find "$fdir" \! -perm -g+rwx -print0 | while read-0 f; do
chmod -v g+rwx "$f" || return 1
done
else
to_create+=("$fdir")
fi
done
if [ "${#to_create[@]}" -gt 0 ]; then
mkdir -p "${to_create[@]}" || return 1
chgrp -v "${gid}" "${to_create[@]}" || return 1
chmod g+rwx "${to_create[@]}" || return 1
fi
opensem:config-set APP_URL "$url"
dev=$(options-get dev 2>/dev/null) || true
if [ -n "$dev" ]; then
# if ! [ -d "$dev" ]; then
# err "The 'dev' option is set to '$dev' but this is not a directory."
# exit 1
# fi
OPENSEM_CODE="$dev"
fi
service:docker-compose:directive-merge "$MASTER_TARGET_SERVICE_NAME" <<EOF
volumes:
- $SERVICE_DATASTORE/var/lib/opensem/app/public:/var/www/$domain/storage:ro
EOF
config-add "
services:
$php_fpm_service:
volumes:
# - $OPENSEM_CODE/public:/var/www/$domain:ro
- $OPENSEM_CODE:/opt/apps/$SERVICE_NAME:ro
- $SERVICE_DATASTORE/var/cache/opensem/bootstrap:/opt/apps/$SERVICE_NAME/bootstrap/cache:rw
- $SERVICE_DATASTORE/var/cache/opensem/framework:/opt/apps/$SERVICE_NAME/storage/framework/cache:rw
- $SERVICE_DATASTORE/var/lib/opensem/views:/opt/apps/$SERVICE_NAME/storage/framework/views:rw
- $SERVICE_DATASTORE/var/lib/opensem/sessions:/opt/apps/$SERVICE_NAME/storage/framework/sessions:rw
## Required to give PHP write access to this dir
- $SERVICE_DATASTORE/var/lib/opensem/app:/opt/apps/$SERVICE_NAME/storage/app:rw
- $SERVICE_DATASTORE/var/lib/opensem/app/public:/opt/apps/$SERVICE_NAME/storage/app/public:rw
"

View File

@@ -0,0 +1,21 @@
#!/bin/bash
. lib/common
set -e
password=$(relation-get password) || {
err "Can't get password for '$SERVICE_NAME' from '$TARGET_SERVICE_NAME'."
exit 1
}
cat <<EOF >> "${OPENSEM_CONFIG_FILE}"
BROADCAST_DRIVER=redis
REDIS_HOST=${TARGET_SERVICE_NAME}
REDIS_PASSWORD=$password
REDIS_PORT=6379
EOF
info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."

View File

@@ -0,0 +1,66 @@
#!/bin/bash
. lib/common
set -e
host=$(relation-get host)
port=$(relation-get port)
connection_security=$(relation-get connection-security)
auth_method=$(relation-get auth-method)
declare -A ENV
ENV[DRIVER]=smtp
ENV[HOST]="$host"
ENV[PORT]="$port"
case "$connection_security" in
"none")
ENV[ENCRYPTION]=null
;;
"ssl/tls")
ENV[ENCRYPTION]="tls"
;;
"ssl")
ENV[ENCRYPTION]="ssl"
;;
*)
error "Unsupported connection security: $connection_security"
exit 1
;;
esac
case "$auth_method" in
"none")
ENV[USERNAME]=null
;;
"password")
login=$(relation-get login) || true
ENV[USERNAME]="$login"
password=$(relation-get password) || true
ENV[PASSWORD]="$password"
;;
*)
error "Unsupported auth method: $auth_method"
exit 1
;;
esac
mail_from=$(relation-get mail-from) || true
if [ -n "$mail_from" ]; then
ENV[FROM_ADDRESS]="$mail_from"
fi
from_name=$(relation-get from-name) || true
if [ -n "$from_name" ]; then
ENV[FROM_NAME]="$from_name"
fi
for key in "${!ENV[@]}"; do
value=${ENV[$key]}
opensem:config-set "MAIL_$key" "$value"
done