2022-11-23 10:08:54 +01:00
#!/bin/bash
#=================================================
2024-08-31 02:59:28 +02:00
# COMMON VARIABLES AND CUSTOM HELPERS
2024-07-22 16:17:56 +02:00
#=================================================
# Install/Upgrade SearXNG in virtual environement
myynh_source_searxng ( ) {
# Retrieve info from manifest
2024-08-31 02:59:28 +02:00
repo_fullpath = $( ynh_read_manifest
commit_sha = $( ynh_read_manifest | xargs basename --suffix= ".tar.gz" )
2024-07-22 16:17:56 +02:00
# Download source
sudo -H -u $app -i bash << EOF
mkdir " $install_dir /searxng-src "
git clone -n " $repo_fullpath " " $install_dir /searxng-src " 2>& 1
EOF
# Checkout commit
pushd " $install_dir /searxng-src "
sudo -H -u $app -i bash << EOF
cd " $install_dir /searxng-src "
git checkout " $commit_sha " 2>& 1
EOF
popd
}
myynh_install_searxng ( ) {
# Create the virtual environment
sudo -H -u $app -i bash << EOF
python3 -m venv " $install_dir /searxng-pyenv "
echo " . $install_dir /searxng-pyenv/bin/activate " > " $install_dir /.profile "
EOF
# Check if virtualenv was sourced from the login
sudo -H -u $app -i bash << EOF
command -v python && python --version
EOF
sudo -H -u $app -i bash << EOF
pip install --upgrade pip
pip install --upgrade setuptools
pip install --upgrade wheel
pip install --upgrade pyyaml
cd " $install_dir /searxng-src "
pip install -e .
EOF
}
# Set permissions
myynh_set_permissions ( ) {
2024-08-31 02:59:28 +02:00
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown -R $app: "$install_dir"
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod 750 "$install_dir"
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod -R o-rwx "$install_dir"
2024-07-22 16:17:56 +02:00
}
2022-11-23 10:08:54 +01:00
#=================================================
2022-11-23 23:16:21 +01:00
# UWSGI HELPERS
2022-11-23 10:08:54 +01:00
#=================================================
2022-11-23 23:16:21 +01:00
# Check if system wide templates are available and correcly configured
#
# usage: ynh_check_global_uwsgi_config
ynh_check_global_uwsgi_config ( ) {
2024-08-31 02:59:28 +02:00
uwsgi --version || ynh_die "You need to add uwsgi (and appropriate plugin) as a dependency"
2022-11-23 23:16:21 +01:00
2023-09-18 18:10:55 +02:00
cat > "/etc/systemd/system/uwsgi-app@.service" <<EOF
2022-11-23 23:16:21 +01:00
[ Unit]
Description = %i uWSGI app
After = syslog.target
2023-02-08 00:06:00 +01:00
2022-11-23 23:16:21 +01:00
[ Service]
RuntimeDirectory = %i
ExecStart = /usr/bin/uwsgi \
--ini /etc/uwsgi/apps-available/%i.ini \
2023-09-18 18:10:55 +02:00
--socket /run/%i/app.socket \
2022-11-23 23:16:21 +01:00
--logto /var/log/uwsgi/%i/%i.log
User = %i
Group = www-data
2023-09-18 18:10:55 +02:00
Restart = always
RestartSec = 10
2022-11-23 23:16:21 +01:00
KillSignal = SIGQUIT
Type = notify
NotifyAccess = all
2023-02-08 00:06:00 +01:00
2022-11-23 23:16:21 +01:00
[ Install]
WantedBy = multi-user.target
EOF
2023-09-18 18:10:55 +02:00
systemctl daemon-reload
2022-11-23 23:16:21 +01:00
}
# Create a dedicated uwsgi ini file to use with generic uwsgi service
#
# To be able to customise the settings of the systemd unit you can override the rules with the file "conf/uwsgi-app@override.service".
# This file will be automatically placed on the good place
2024-08-31 02:59:28 +02:00
#
2023-09-18 18:10:55 +02:00
# Note that the service need to be started manually at the end of the installation.
# Generally you can start the service with this command:
2024-08-31 02:59:28 +02:00
# ynh_systemctl --service "uwsgi-app@$app.service" --wait_until "WSGI app 0 \(mountpoint='[/[:alnum:]_-]*'\) ready in [[:digit:]]* seconds on interpreter" --log_path "/var/log/uwsgi/$app/$app.log"
2022-11-23 23:16:21 +01:00
#
# usage: ynh_add_uwsgi_service
#
# to interact with your service: `systemctl <action> uwsgi-app@app`
ynh_add_uwsgi_service ( ) {
2023-09-18 18:10:55 +02:00
ynh_check_global_uwsgi_config
local finaluwsgiini = " /etc/uwsgi/apps-available/ $app .ini "
# www-data group is needed since it is this nginx who will start the service
2024-08-31 02:59:28 +02:00
usermod --append --groups www-data " $app " || ynh_die " It wasn't possible to add user $app to group www-data "
2023-09-18 18:10:55 +02:00
2024-08-31 02:59:28 +02:00
ynh_config_add --template= "uwsgi.ini" --destination= " $finaluwsgiini "
ynh_store_file_checksum " $finaluwsgiini "
2023-09-18 18:10:55 +02:00
chown $app :root " $finaluwsgiini "
# make sure the folder for logs exists and set authorizations
mkdir -p " /var/log/uwsgi/ $app "
chown $app :root " /var/log/uwsgi/ $app "
chmod -R u = rwX,g= rX,o= " /var/log/uwsgi/ $app "
# Setup specific Systemd rules if necessary
mkdir -p " /etc/systemd/system/uwsgi-app@ $app .service.d "
2024-07-22 16:17:56 +02:00
if [ -e "../conf/uwsgi-app@override.service" ]
then
2024-08-31 02:59:28 +02:00
ynh_config_add --template= "uwsgi-app@override.service" --destination= " /etc/systemd/system/uwsgi-app@ $app .service.d/override.conf "
2023-09-18 18:10:55 +02:00
fi
systemctl daemon-reload
2024-08-31 02:59:28 +02:00
ynh_systemctl --service= " uwsgi-app@ $app .service " --action= "enable"
2023-09-18 18:10:55 +02:00
# Add as a service
yunohost service add " uwsgi-app@ $app " --description= "uWSGI service for searxng" --log " /var/log/uwsgi/ $app / $app .log "
2022-11-23 23:16:21 +01:00
}
# Remove the dedicated uwsgi ini file
#
# usage: ynh_remove_uwsgi_service
ynh_remove_uwsgi_service ( ) {
2023-09-18 18:10:55 +02:00
local finaluwsgiini = " /etc/uwsgi/apps-available/ $app .ini "
2024-07-22 16:17:56 +02:00
if [ -e " $finaluwsgiini " ]
then
2023-09-18 18:10:55 +02:00
yunohost service remove " uwsgi-app@ $app "
2024-08-31 02:59:28 +02:00
ynh_systemctl --service= " uwsgi-app@ $app .service " --action= "stop"
ynh_systemctl --service= " uwsgi-app@ $app .service " --action= "disable"
2023-09-18 18:10:55 +02:00
2024-08-31 02:59:28 +02:00
ynh_safe_rm " $finaluwsgiini "
ynh_safe_rm " /var/log/uwsgi/ $app "
ynh_safe_rm " /etc/systemd/system/uwsgi-app@ $app .service.d "
2023-09-18 18:10:55 +02:00
fi
2022-11-23 23:16:21 +01:00
}
2023-09-18 18:10:55 +02:00
# Backup the dedicated uwsgi config
# Should be used in backup script
2022-11-23 23:16:21 +01:00
#
2023-09-18 18:10:55 +02:00
# usage: ynh_backup_uwsgi_service
ynh_backup_uwsgi_service ( ) {
2024-08-31 02:59:28 +02:00
ynh_backup " /etc/uwsgi/apps-available/ $app .ini "
ynh_backup " /etc/systemd/system/uwsgi-app@ $app .service.d " || true
2023-09-18 18:10:55 +02:00
}
# Restore the dedicated uwsgi config
# Should be used in restore script
2022-11-23 23:16:21 +01:00
#
2023-09-18 18:10:55 +02:00
# usage: ynh_restore_uwsgi_service
ynh_restore_uwsgi_service ( ) {
ynh_check_global_uwsgi_config
2024-08-31 02:59:28 +02:00
ynh_restore " /etc/uwsgi/apps-available/ $app .ini "
ynh_restore " /etc/systemd/system/uwsgi-app@ $app .service.d " || true
2023-09-18 18:10:55 +02:00
mkdir -p " /var/log/uwsgi/ $app "
chown $app :root " /var/log/uwsgi/ $app "
chmod -R u = rwX,g= rX,o= " /var/log/uwsgi/ $app "
2024-08-31 02:59:28 +02:00
ynh_systemctl --service= " uwsgi-app@ $app .service " --action= "enable"
2023-09-18 18:10:55 +02:00
yunohost service add " uwsgi-app@ $app " --description= "uWSGI service for searxng" --log " /var/log/uwsgi/ $app / $app .log "
2022-11-23 23:16:21 +01:00
}