add fix_collation_mismatch function for db container

This commit is contained in:
2025-09-23 15:57:48 +02:00
parent 9d1d89ff81
commit fde3efb0d0

172
abra.sh
View File

@ -21,43 +21,40 @@ export PG_BACKUP_VERSION=v2
export ENTRYPOINT_CSS_VERSION=v1 export ENTRYPOINT_CSS_VERSION=v1
customize() { customize() {
if [ -z "$1" ] if [ -z "$1" ]; then
then echo "Usage: ... customize <assets_path>"
echo "Usage: ... customize <assets_path>" exit 1
exit 1 fi
fi asset_dir=$1
asset_dir=$1 for asset in $COPY_ASSETS; do
for asset in $COPY_ASSETS; do source=$(echo $asset | cut -d "|" -f1)
source=$(echo $asset | cut -d "|" -f1) target=$(echo $asset | cut -d "|" -f2)
target=$(echo $asset | cut -d "|" -f2) echo copy $source to $target
echo copy $source to $target abra app cp $APP_NAME $asset_dir/$source $target
abra app cp $APP_NAME $asset_dir/$source $target done
done
} }
shell(){ shell() {
if [ -z "$1" ] if [ -z "$1" ]; then
then echo "Usage: ... shell <python code>"
echo "Usage: ... shell <python code>" exit 1
exit 1 fi
fi ak shell -c "$1" 2>&1 | quieten
ak shell -c "$1" 2>&1 | quieten
} }
import_user() { import_user() {
if [ -z "$1" ] if [ -z "$1" ]; then
then echo "Usage: ... import_user <users.csv>"
echo "Usage: ... import_user <users.csv>" exit 1
exit 1 fi
fi source_file=$1
source_file=$1 filename=$(basename $source_file)
filename=$(basename $source_file) abra app cp $APP_NAME $source_file worker:/tmp/
abra app cp $APP_NAME $source_file worker:/tmp/ abra app cmd -T $APP_NAME worker _import_user $filename
abra app cmd -T $APP_NAME worker _import_user $filename
} }
_import_user() { _import_user() {
/manage.py shell -c """ /manage.py shell -c """
import csv import csv
new_user = User() new_user = User()
with open('/tmp/$1', newline='') as file: with open('/tmp/$1', newline='') as file:
@ -85,9 +82,9 @@ with open('/tmp/$1', newline='') as file:
} }
set_admin_pass() { set_admin_pass() {
password=$(cat /run/secrets/admin_pass) password=$(cat /run/secrets/admin_pass)
token=$(cat /run/secrets/admin_token) token=$(cat /run/secrets/admin_token)
/manage.py shell -c """ /manage.py shell -c """
import time import time
i = 0 i = 0
while (not User.objects.filter(username='akadmin')): while (not User.objects.filter(username='akadmin')):
@ -122,45 +119,45 @@ else:
} }
rotate_db_pass() { rotate_db_pass() {
db_password=$(cat /run/secrets/db_password) db_password=$(cat /run/secrets/db_password)
psql -U authentik -c """ALTER USER authentik WITH PASSWORD '$db_password';""" psql -U authentik -c """ALTER USER authentik WITH PASSWORD '$db_password';"""
} }
# This function is for blueprints that are overwriting custom blueprints # This function is for blueprints that are overwriting custom blueprints
# It deactivates the affected custom blueprints to avoid changes to be reverted # It deactivates the affected custom blueprints to avoid changes to be reverted
apply_blueprints() { apply_blueprints() {
update_and_disable_blueprint default/flow-password-change.yaml update_and_disable_blueprint default/flow-password-change.yaml
update_and_disable_blueprint default/flow-default-authentication-flow.yaml update_and_disable_blueprint default/flow-default-authentication-flow.yaml
update_and_disable_blueprint default/flow-default-user-settings-flow.yaml update_and_disable_blueprint default/flow-default-user-settings-flow.yaml
update_and_disable_blueprint default/flow-default-source-enrollment.yaml update_and_disable_blueprint default/flow-default-source-enrollment.yaml
apply_blueprint 3_flow_translation.yaml apply_blueprint 3_flow_translation.yaml
apply_blueprint 2_flow_authentication.yaml apply_blueprint 2_flow_authentication.yaml
} }
update_and_disable_blueprint() { update_and_disable_blueprint() {
enable_blueprint $@ 2>&1 | quieten enable_blueprint $@ 2>&1 | quieten
sleep 1 sleep 1
apply_blueprint $@ apply_blueprint $@
sleep 1 sleep 1
disable_blueprint $@ 2>&1 | quieten disable_blueprint $@ 2>&1 | quieten
} }
disable_blueprint() { disable_blueprint() {
blueprint_state False $@ blueprint_state False $@
} }
enable_blueprint() { enable_blueprint() {
blueprint_state True $@ blueprint_state True $@
} }
apply_blueprint() { apply_blueprint() {
echo apply blueprint $@ echo apply blueprint $@
ak apply_blueprint $@ 2>&1 | quieten ak apply_blueprint $@ 2>&1 | quieten
} }
blueprint_state() { blueprint_state() {
/manage.py shell -c """ /manage.py shell -c """
import time import time
blueprint_state=$1 blueprint_state=$1
blueprint_path='$2' blueprint_path='$2'
@ -178,9 +175,9 @@ print(f'{blueprint.name} enabled: {blueprint.enabled}')
} }
# This function adds each application with its name, slug and group if passed # This function adds each application with its name, slug and group if passed
add_applications(){ add_applications() {
export APPLICATIONS export APPLICATIONS
/manage.py shell -c """ /manage.py shell -c """
import json import json
import os import os
if os.environ['APPLICATIONS'] == '': if os.environ['APPLICATIONS'] == '':
@ -207,7 +204,7 @@ for name, details in applications.items():
## This function is for renaming apps - usage: rename "old name" "new name" ## This function is for renaming apps - usage: rename "old name" "new name"
rename() { rename() {
/manage.py shell -c """ /manage.py shell -c """
old_name = '$1' old_name = '$1'
new_name = '$2' if '$2' else old_name new_name = '$2' if '$2' else old_name
@ -221,55 +218,51 @@ else:
""" 2>&1 | quieten """ 2>&1 | quieten
} }
quieten() {
# 'SyntaxWarning|version_regex|"http\['
quieten(){ # is a workaround to get rid of some verbose syntax warnings, this might be fixed with another version
# 'SyntaxWarning|version_regex|"http\[' grep -Pv '"level": "(info|debug)"|SyntaxWarning|version_regex|"http\[|RuntimeWarning:'
# is a workaround to get rid of some verbose syntax warnings, this might be fixed with another version
grep -Pv '"level": "(info|debug)"|SyntaxWarning|version_regex|"http\[|RuntimeWarning:'
} }
add_email_templates(){ add_email_templates() {
for file_path in "$@"; do for file_path in "$@"; do
echo copy template $file_path echo copy template $file_path
abra app cp $APP_NAME $file_path app:/templates/ abra app cp $APP_NAME $file_path app:/templates/
done done
} }
set_icons(){ set_icons() {
if [ -n "$1" ] if [ -n "$1" ]; then
then APP_ICONS="$1"
APP_ICONS="$1" fi
fi for icon in $APP_ICONS; do
for icon in $APP_ICONS; do
app=$(echo $icon | cut -d ":" -f1) app=$(echo $icon | cut -d ":" -f1)
file_path=$(eval echo $(echo $icon | cut -d ":" -f2)) file_path=$(eval echo $(echo $icon | cut -d ":" -f2))
file=$(basename $file_path) file=$(basename $file_path)
echo copy icon $file_path for $app echo copy icon $file_path for $app
abra app cp $APP_NAME $file_path app:/media/ abra app cp $APP_NAME $file_path app:/media/
abra app cmd -T $APP_NAME app set_app_icon $app /media/$file abra app cmd -T $APP_NAME app set_app_icon $app /media/$file
done done
} }
set_extra_icons(){ set_extra_icons() {
if [ -z "$EXTRA_ICONS" ] if [ -z "$EXTRA_ICONS" ]; then
then echo "Variable EXTRA_ICONS is not set"
echo "Variable EXTRA_ICONS is not set" exit 1
exit 1 fi
fi export EXTRA_ICONS
export EXTRA_ICONS icon_key_values=$(python3 -c "
icon_key_values=$(python3 -c "
import json import json
import os import os
for key, value in json.loads(os.environ['EXTRA_ICONS']).items(): for key, value in json.loads(os.environ['EXTRA_ICONS']).items():
print(f'{key}:{value}') print(f'{key}:{value}')
") ")
set_icons "$icon_key_values" set_icons "$icon_key_values"
} }
set_app_icon() { set_app_icon() {
TOKEN=$(cat /run/secrets/admin_token) TOKEN=$(cat /run/secrets/admin_token)
python -c """ python -c """
import requests import requests
import os import os
my_token = '$TOKEN' my_token = '$TOKEN'
@ -288,18 +281,18 @@ with open(icon_path, 'rb') as img:
} }
blueprint_cleanup() { blueprint_cleanup() {
/manage.py shell -c """ /manage.py shell -c """
delete_flows = ['default-recovery-flow' , 'custom-authentication-flow' , 'invitation-enrollment-flow' , 'initial-setup'] delete_flows = ['default-recovery-flow' , 'custom-authentication-flow' , 'invitation-enrollment-flow' , 'initial-setup']
Flow.objects.filter(slug__in=delete_flows).delete() Flow.objects.filter(slug__in=delete_flows).delete()
Stage.objects.filter(flow=None).delete() Stage.objects.filter(flow=None).delete()
Prompt.objects.filter(promptstage=None).delete() Prompt.objects.filter(promptstage=None).delete()
Brand.objects.filter(default=True).delete() Brand.objects.filter(default=True).delete()
""" 2>&1 | quieten """ 2>&1 | quieten
apply_blueprints apply_blueprints
} }
get_certificate() { get_certificate() {
/manage.py shell -c """ /manage.py shell -c """
provider_name='$1' provider_name='$1'
if not provider_name: if not provider_name:
print('no Provider Name given') print('no Provider Name given')
@ -312,7 +305,14 @@ print(''.join(cert.certificate_data.splitlines()[1:-1]))
} }
get_user_uid() { get_user_uid() {
/manage.py shell -c """ /manage.py shell -c """
print(User.objects.filter(username='$1').first().uid) print(User.objects.filter(username='$1').first().uid)
""" 2>&1 | quieten """ 2>&1 | quieten
} }
fix_collation_mismatch() {
psql -U ${POSTGRES_USER} -d authentik -c "ALTER DATABASE authentik REFRESH COLLATION VERSION;"
psql -U ${POSTGRES_USER} -d authentik -c "REINDEX DATABASE authentik;"
psql -U ${POSTGRES_USER} -d postgres -c "ALTER DATABASE postgres REFRESH COLLATION VERSION;"
psql -U ${POSTGRES_USER} -d postgres -c "REINDEX DATABASE postgres;"
}