2023-01-05 14:48:22 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC START
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Load common variables and helpers
|
|
|
|
source ./_common.sh
|
|
|
|
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# MANAGE SCRIPT FAILURE
|
|
|
|
#=================================================
|
|
|
|
|
2023-01-05 14:48:22 +01:00
|
|
|
# Exit if an error occurs during the execution of the script
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_abort_if_errors
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# RETRIEVE ARGUMENTS FROM THE MANIFEST
|
|
|
|
#=================================================
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
# Retrieve arguments
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? domain=$YNH_APP_ARG_DOMAIN
|
|
|
|
#REMOVEME? path=$YNH_APP_ARG_PATH
|
|
|
|
#REMOVEME? admin=$YNH_APP_ARG_ADMIN
|
|
|
|
#REMOVEME? is_public=$YNH_APP_ARG_IS_PUBLIC
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? app=$YNH_APP_INSTANCE_NAME
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Validating installation parameters..." --weight=1
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? install_dir="/var/www/$app"
|
|
|
|
#REMOVEME? test ! -e "$install_dir" || ynh_die --message="This path already contains a folder"
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
ynh_user_exists "$admin" || ynh_die --message "The chosen admin user does not exist."
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
# Register (book) web path
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_webpath_register --app=$app --domain=$domain --path=$path
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STORE SETTINGS FROM MANIFEST
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Storing installation settings..." --weight=1
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=domain --value=$domain
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=admin --value=$admin
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=path --value=$path
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STANDARD MODIFICATIONS
|
|
|
|
#=================================================
|
|
|
|
# FIND AND OPEN A PORT
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Finding an available port..." --weight=1
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
# Find an available port
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? port=$(ynh_find_port --port=6000)
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=port --value=$port
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# INSTALL DEPENDENCIES
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Installing dependencies..." --weight=20
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CREATE A DATABASE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Creating a database..." --weight=3
|
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? db_name=$(ynh_sanitize_dbid --db_name=$app)
|
|
|
|
#REMOVEME? db_user=$db_name
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=db_name --value=$db_name
|
|
|
|
#REMOVEME? ynh_psql_test_if_first_run
|
|
|
|
#REMOVEME? ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# CREATE DEDICATED USER
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Configuring system user..." --weight=1
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? data_dir=/home/yunohost.app/$app
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=data_dir --value=$data_dir
|
2023-01-11 19:46:20 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Create a system user
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_system_user_create --username=$app --home_dir=$data_dir --groups ssh.app --use_shell
|
2023-01-11 10:32:49 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Installing sources files..." --weight=10
|
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=install_dir --value=$install_dir
|
|
|
|
ynh_setup_source --dest_dir=$install_dir --source_id=$YNH_ARCH
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
mkdir -p "$install_dir/custom/conf"
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
chmod 750 "$install_dir"
|
|
|
|
chmod -R o-rwx "$install_dir"
|
|
|
|
chown -R $app:$app "$install_dir"
|
|
|
|
chmod +x "$install_dir/forgejo"
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-01-22 08:10:43 +01:00
|
|
|
#=================================================
|
|
|
|
# KEYS GENERATION
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
secret_key=$($install_dir/forgejo generate secret SECRET_KEY)
|
|
|
|
lfs_jwt_secret=$($install_dir/forgejo generate secret JWT_SECRET)
|
|
|
|
internal_token=$($install_dir/forgejo generate secret INTERNAL_TOKEN)
|
2023-01-22 08:10:43 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=secret_key --value=$secret_key
|
|
|
|
ynh_app_setting_set --app=$app --key=lfs_jwt_secret --value=$lfs_jwt_secret
|
|
|
|
ynh_app_setting_set --app=$app --key=internal_token --value=$internal_token
|
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# NGINX CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring NGINX web server..." --weight=2
|
|
|
|
ynh_add_nginx_config
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CREATE DATA DIRECTORY
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Creating a data directory..." --weight=1
|
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
if [ -e "$data_dir" ]; then
|
|
|
|
old_data_dir_path="$data_dir$(date '+%Y%m%d.%H%M%S')"
|
2023-01-05 14:48:22 +01:00
|
|
|
ynh_print_warn "A data directory already exist. Data was renamed to $old_data_dir_path"
|
2023-03-10 17:13:54 +01:00
|
|
|
mv "$data_dir" "$old_data_dir_path"
|
2023-01-05 14:48:22 +01:00
|
|
|
fi
|
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
mkdir -p $data_dir
|
|
|
|
mkdir -p "$data_dir/.ssh"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
chmod 750 "$data_dir"
|
|
|
|
chmod -R o-rwx "$data_dir"
|
|
|
|
chown -R $app:$app "$data_dir"
|
|
|
|
chmod u=rwx,g=,o= "$data_dir/.ssh"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
# ADD A CONFIGURATION
|
2023-01-05 14:48:22 +01:00
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
ynh_script_progression --message="Adding a configuration file..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
ssh_port=$(grep -P "Port\s+\d+" /etc/ssh/sshd_config | grep -P -o "\d+")
|
2023-03-10 17:13:54 +01:00
|
|
|
ynh_add_config --template="app.ini" --destination="$install_dir/custom/conf/app.ini"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
chmod 640 "$install_dir/custom/conf/app.ini"
|
|
|
|
chown $app:$app "$install_dir/custom/conf/app.ini"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# SETUP SYSTEMD
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring a systemd service..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
ynh_add_systemd_config
|
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# SETUP LOGROTATE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring log rotation..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Configure logrotate
|
2023-01-11 19:46:20 +01:00
|
|
|
ynh_use_logrotate --logfile "/var/log/$app/forgejo.log"
|
|
|
|
|
|
|
|
chown $app:$app /var/log/$app
|
|
|
|
chmod u=rwX,g=rX,o= "/var/log/$app"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
yunohost service add "$app" --description="Forgejo" --log="/var/log/$app/forgejo.log"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# START SYSTEMD SERVICE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=3
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/forgejo.log" --line_match="Starting new Web server: tcp:127.0.0.1:"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
2023-01-05 14:48:22 +01:00
|
|
|
# SETUP FAIL2BAN
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring Fail2Ban..." --weight=1
|
|
|
|
|
2023-01-06 12:17:27 +01:00
|
|
|
ynh_add_fail2ban_config --logpath "/var/log/$app/forgejo.log" --failregex ".*Failed authentication attempt for .* from <HOST>" --max_retry 5
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
# SETUP SSOWAT
|
2023-01-05 14:48:22 +01:00
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Configuring permissions..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Make app public if necessary or protect it
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? if [ $is_public -eq 1 ]
|
2023-01-05 14:48:22 +01:00
|
|
|
then
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_permission_update --permission="main" --add="visitors"
|
2023-01-05 14:48:22 +01:00
|
|
|
fi
|
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Only the admin can access the admin panel of the app (if the app has an admin panel)
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_permission_create --permission="admin" --allowed=$admin
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# RELOAD NGINX
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Reloading NGINX web server..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# LDAP CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Adding LDAP configuration..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
pushd "$install_dir"
|
2023-01-11 10:32:49 +01:00
|
|
|
ynh_exec_as $app ./forgejo admin auth add-ldap --security-protocol "Unencrypted" --name "YunoHost LDAP" --host "localhost" --port "389" --skip-tls-verify --user-search-base "ou=users,dc=yunohost,dc=org" --user-filter "(&(uid=%s)(objectClass=posixAccount)(permission=cn=$app.main,ou=permission,dc=yunohost,dc=org))" --firstname-attribute "givenName" --surname-attribute "sn" --email-attribute "mail" --admin-filter "(permission=cn=$app.admin,ou=permission,dc=yunohost,dc=org)"
|
|
|
|
popd
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# END OF SCRIPT
|
|
|
|
#=================================================
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|