forgejo_ynh/scripts/install

231 lines
8.5 KiB
Text
Raw Normal View History

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
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
2023-01-05 14:48:22 +01:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
2023-01-05 14:48:22 +01:00
# Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN
is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
final_path="/var/www/$app"
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
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
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=1
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=admin --value=$admin
ynh_app_setting_set --app=$app --key=path --value=$path_url
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Finding an available port..." --weight=1
# Find an available port
port=$(ynh_find_port --port=6000)
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=20
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE A DATABASE
#=================================================
ynh_script_progression --message="Creating a database..." --weight=3
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_psql_test_if_first_run
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
2023-01-05 14:48:22 +01:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=1
2023-01-11 19:46:20 +01:00
datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
# Create a system user
2023-01-11 19:46:20 +01:00
ynh_system_user_create --username=$app --home_dir=$datadir --groups ssh.app --use_shell
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Installing sources files..." --weight=10
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2023-01-11 19:46:20 +01:00
ynh_setup_source --dest_dir=$final_path --source_id=$YNH_ARCH
2023-01-05 14:48:22 +01:00
mkdir -p "$final_path/custom/conf"
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path"
chmod +x "$final_path/forgejo"
#=================================================
# KEYS GENERATION
#=================================================
secret_key=$($final_path/forgejo generate secret SECRET_KEY)
lfs_jwt_secret=$($final_path/forgejo generate secret JWT_SECRET)
internal_token=$($final_path/forgejo generate secret INTERNAL_TOKEN)
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
#=================================================
# 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-01-05 14:48:22 +01:00
if [ -e "$datadir" ]; then
old_data_dir_path="$datadir$(date '+%Y%m%d.%H%M%S')"
ynh_print_warn "A data directory already exist. Data was renamed to $old_data_dir_path"
mv "$datadir" "$old_data_dir_path"
fi
mkdir -p $datadir
mkdir -p "$datadir/.ssh"
2023-01-05 14:48:22 +01:00
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R $app:$app "$datadir"
chmod u=rwx,g=,o= "$datadir/.ssh"
2023-01-05 14:48:22 +01:00
#=================================================
# ADD A CONFIGURATION
2023-01-05 14:48:22 +01:00
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=1
2023-01-05 14:48:22 +01:00
ssh_port=$(grep -P "Port\s+\d+" /etc/ssh/sshd_config | grep -P -o "\d+")
ynh_add_config --template="app.ini" --destination="$final_path/custom/conf/app.ini"
2023-01-05 14:48:22 +01:00
chmod 640 "$final_path/custom/conf/app.ini"
chown $app:$app "$final_path/custom/conf/app.ini"
2023-01-05 14:48:22 +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
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=1
2023-01-05 14:48:22 +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
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2023-01-05 14:48:22 +01:00
yunohost service add "$app" --description="Forgejo" --log="/var/log/$app/forgejo.log"
2023-01-05 14:48:22 +01:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=3
2023-01-05 14:48:22 +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-05 14:48:22 +01:00
# SETUP FAIL2BAN
#=================================================
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
#=================================================
# SETUP SSOWAT
2023-01-05 14:48:22 +01:00
#=================================================
ynh_script_progression --message="Configuring permissions..." --weight=1
2023-01-05 14:48:22 +01:00
# Make app public if necessary or protect it
if [ $is_public -eq 1 ]
2023-01-05 14:48:22 +01:00
then
ynh_permission_update --permission="main" --add="visitors"
2023-01-05 14:48:22 +01:00
fi
# Only the admin can access the admin panel of the app (if the app has an admin panel)
2023-01-05 14:48:22 +01:00
ynh_permission_create --permission="admin" --allowed=$admin
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2023-01-05 14:48:22 +01:00
ynh_systemd_action --service_name=nginx --action=reload
2023-01-05 14:48:22 +01:00
#=================================================
# LDAP CONFIGURATION
#=================================================
ynh_script_progression --message="Adding LDAP configuration..." --weight=1
2023-01-05 14:48:22 +01:00
pushd "$final_path"
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
#=================================================
# END OF SCRIPT
#=================================================
2023-01-05 14:48:22 +01:00
ynh_script_progression --message="Installation of $app completed" --last