YunoHost includes a hook mechanism triggered on a lot of operation changing the system. You can use this mechanism in order to extend the behaviour of a YunoHost command.
The most obvious case is adding a user. If you had a post_user_create
hook, this hook will be triggered as soon as a user is added.
Below we imagine we want to run a command after each user creation to add the user to samba user.
You should create a directory with the name of the hooks into /etc/yunohost/hooks.d/
:
mkdir -p /etc/yunohost/hooks.d/post_user_create
Next create a bash script inside this directory prefixed by 2 numbers and a dash:
nano /etc/yunohost/hooks.d/post_user_create/05-add-user-to-samba
By default, the directory must be readable and traversable by root, but if you notice your hook is not run at all by YunoHost, you can check permissions with ls -l /etc/yunohost/hooks.d/
and apply these commands if needed:
chown root:root /etc/yunohost/hooks.d/post_user_create
chmod u+rx /etc/yunohost/hooks.d/post_user_create
If you are packaging an app, you should not set by yourself the hook into /etc/yunohost/hooks.d
instead you should create a hooks dir at the root of your package.
.
├── conf
├── hooks
├── scripts
In the hooks dir, create a bash script called with the type of hook you want to create for example post_create_user
.
This hook is run at the end of the command yunohost user create
or equivalent action in webadmin.
#!/bin/bash
domain=$(cat /etc/hostname)
message="Hello $YNH_USER_FIRSTNAME,
Welcome on $domain !
Feel free to <a href='https://$domain/yunohost/sso/edit.html'>change your password</a>.
Let me know if you have a problem,
The admin of $domain
"
echo $message | mail -s "Welcome on $domain !" $YNH_USER_MAIL
This hook is run at the end of the command yunohost user delete
or equivalent action in webadmin.
#!/bin/bash
This hook is run at the end of the command yunohost user update
or equivalent action in webadmin.
Only arguments given to the cli/api are given as environment variable.
#!/bin/bash
"
domain=$(cat /etc/hostname)
message="Hello,
Your password has been successfully changed on $domain.
If you have not asked for changing your password, you probably should contact the admin of $domain.
"
echo $message | mail -s "Your password has been changed on $domain !" $YNH_USER_USERNAME
This hook is run at the end of the command yunohost user permission add
or equivalent action in webadmin.
main
, admin
, etc.)This hook is run at the end of the command yunohost user permission remove
or equivalent action in webadmin.
main
, admin
, etc.)This hook is run at the end of the command yunohost domain add
or equivalent action in webadmin.
This hook is run at the end of the command yunohost domain remove
or equivalent action in webadmin.
This hook is run at the end of the command yunohost domain cert update
or equivalent action in webadmin.
#!/bin/bash
systemctl restart gemserv
This hook is run at the end of the command yunohost domain dns suggest
or equivalent action in webadmin.
Thanks to This hook you can customize
The script should return a JSON array with dictionnary in this format:
[
{
'type': 'SRV',
'name': 'stuff.foo.bar',
'value': 'yoloswag',
'ttl': 3600
}
]
#!/bin/bash
if [[ "$1" == "XXXX.nohost.me" ]] ; then
echo "[
{
'type': 'TXT',
'name': '_acme-challenge',
'value': 'LETSENCRYPT_VALUE',
'ttl': 3600
}
]"
fi
This hook is run at the end of the command yunohost app change-url
or equivalent action in webadmin.
This hook is run at the end of the command yunohost app upgrade
or equivalent action in webadmin.
dpkg --print-architecture
#!/bin/bash
source /usr/share/yunohost/helpers
app=$YNH_APP_INSTANCE_NAME
if [[ "$app" == "etherpad_mypads" ]]; then
ynh_write_var_in_file --file=/var/www/etherpad_mypads/settings.json --key=max --value=100 --after=importExportRateLimiting
systemctl restart etherpad_mypads
fi
This hook is run at the end of the command yunohost app install
or equivalent action in webadmin.
dpkg --print-architecture
This hook is run at the end of the command yunohost app remove
or equivalent action in webadmin.
dpkg --print-architecture
This hook is run at the end of the command yunohost backup create
or equivalent action in webadmin.
#!/bin/bash
# Source YNH helpers
source /usr/share/yunohost/helpers
ynh_backup_dest (){
YNH_CWD="${YNH_BACKUP_DIR%/}/$1"
mkdir -p $YNH_CWD
cd "$YNH_CWD"
}
# Exit hook on subcommand error or unset variable
ynh_abort_if_errors
# MISC
ynh_backup_dest "conf/custom/misc"
ynh_backup "/etc/sysctl.d/noipv6.conf"
ynh_backup "/usr/local/bin/"
ynh_backup "/etc/yunohost/hooks.d/backup/99-conf_custom"
ynh_backup "/etc/yunohost/hooks.d/restore/99-conf_custom"
This hook is run at the end of the command yunohost backup restore
or equivalent action in webadmin.
#!/bin/bash
# Source YNH helpers
source /usr/share/yunohost/helpers
ynh_restore_dest (){
YNH_CWD="${YNH_BACKUP_DIR%/}/$1"
cd "$YNH_CWD"
}
# Exit hook on subcommand error or unset variable
ynh_abort_if_errors
# MISC
ynh_restore_dest "conf/custom/misc"
ynh_restore_file "/etc/sysctl.d/noipv6.conf"
ynh_restore_file "/usr/local/bin/"
ynh_restore_file "/etc/yunohost/hooks.d/backup/99-conf_custom"
ynh_restore_file "/etc/yunohost/hooks.d/restore/99-conf_custom"
This hook is run during the command yunohost backup create
or equivalent action in webadmin.
This hook is called several times with different action keywords.
#!/bin/bash
set -euo pipefail
work_dir="$2"
name="$3"
repo="$4"
size="$5"
description="$6"
case "$1" in
need_mount)
# Set false if your method can itself put files in good place in your archive
true
;;
backup)
mount /dev/sda1 /mnt/hdd
if [[ "$(df /mnt/hdd | tail -n1 | cut -d" " -f1)" != "/dev/sda1" ]]
then
exit 1
fi
pushd "$work_dir"
current_date=$(date +"%Y-%m-%d_%H:%M")
cp -a "${work_dir}" "/mnt/hdd/${current_date}_$name"
popd
umount /mnt/hdd
;;
*)
echo "hook called with unknown argument \`$1'" >&2
exit 1
;;
esac
exit 0
This hook is run at the end of the command yunohost firewall reload
or equivalent action in webadmin.
#!/bin/bash
iptables -A OUTPUT -p tcp --dport 25 -m owner --uid-owner postfix -j ACCEPT
iptables -A OUTPUT -p tcp --dport 25 -m tcp -j REJECT --reject-with icmp-port-unreachable
This hook is run during the command yunohost tools regen-conf
or equivalent action in webadmin.
This hook is called several times with different actions keywords (pre and post operations).
#!/bin/bash
action=$1
pending_dir=$4
postfix_conf=$pending_dir/../postfix/etc/postfix/main.cf
[[ "$action" == "pre" ]] || exit 0
[[ -e $postfix_conf ]] || exit 0
echo '
compatibility_level = 2' >> $postfix_conf
Fehler gefunden? Möchten Sie diese Seite verbessern? Einfach dem Edit Link am Anfang der Seite folgen und dann auf das Symbol auf Github clicken um Änderungen vorzuschlagen.
Powered by Grav + with by Trilby Media. • Terms of Service