1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/lutim_ynh.git synced 2024-09-03 19:36:24 +02:00
lutim_ynh/scripts/install
2020-04-24 20:16:59 +02:00

267 lines
11 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
# Load common variables for all scripts.
source _variables
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE FAILURE OF THE SCRIPT
#=================================================
ynh_clean_setup () {
# Clean installation remaining that are not handle by the remove script.
ynh_clean_check_starting
}
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC
always_encrypt=$YNH_APP_ARG_ALWAYS_ENCRYPT
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=2
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
# 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=2
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=is_public --value=$is_public
ynh_app_setting_set --app=$app --key=always_encrypt --value=$always_encrypt
ynh_app_setting_set --app=$app --key=overwrite_settings --value=1
ynh_app_setting_set --app=$app --key=overwrite_nginx --value=1
ynh_app_setting_set --app=$app --key=overwrite_systemd --value=1
ynh_app_setting_set --app=$app --key=admin_mail_html --value=1
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Configuring firewall..." --weight=3
# Find a free port
port=$(ynh_find_port --port=8095)
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..."
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=60
ynh_install_app_dependencies $app_depencencies
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring nginx web server..." --weight=2
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=2
# Create a dedicated system user
ynh_system_user_create $app
#=================================================
# SPECIFIC SETUP
#=================================================
# SETUP LUTIM
#=================================================
ynh_script_progression --message="Configure Lutim" --weight=2
# Configure Lutim
cp ../conf/lutim.conf.template "$final_path/lutim.conf"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$final_path/lutim.conf"
ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$final_path/lutim.conf"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$final_path/lutim.conf"
ynh_replace_string --match_string="__ENCRYPT__" --replace_string="$always_encrypt" --target_file="$final_path/lutim.conf"
secret=$(ynh_string_random)
ynh_replace_string --match_string="__SECRET__" --replace_string="$secret" --target_file="$final_path/lutim.conf"
ynh_app_setting_set --app=$app --key=secret --value="$secret"
# Set the number of process for Lutim to twice the number of CPU core.
ynh_replace_string --match_string="__WORKERS__" --replace_string="$(( $(nproc) * 2 ))" --target_file="$final_path/lutim.conf"
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$final_path/lutim.conf"
#=================================================
# SETUP HOOKS FILE
#=================================================
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="../hooks/post_app_addaccess"
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="../hooks/post_app_removeaccess"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=2
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# SETUP CRON
#=================================================
cp ../conf/cron_lutim /etc/cron.d/$app
ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path/" --target_file=/etc/cron.d/$app
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file=/etc/cron.d/$app
chmod +x $final_path/script/lutim
#=================================================
# INSTALL LUTIM WITH CARTON
#=================================================
ynh_script_progression --message="Installing Lutim with Carton..." --weight=60
mkdir -p /var/log/$app/
(cd $final_path
carton install 2>&1 | tee -a "/var/log/$app/setup_carton.log")
# Use a perl path adapted to the system architecture
arch_dir=$(ls -1 $final_path/local/lib/perl5/ | grep linux-gnu)
if [ "$?" -ne 0 ]
then
ynh_die --message="Unable to find the perl directory for your architecture."
fi
ynh_replace_string --match_string="__ARCHDIR__" --replace_string="$arch_dir" --target_file="$final_path/script/lutim"
#=================================================
# SETUP LOG FILE
#=================================================
# Making log a symbolic link to /var/log
touch /var/log/$app/production.log
chown $app -R /var/log/$app
ln -s /var/log/$app/production.log "$final_path/log/production.log"
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
# Set permissions to app files
chown -R $app: $final_path
#=================================================
# GENERIC FINALISATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=2
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add $app --log $final_path/log/production.log
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring SSOwat..."
ynh_permission_update --permission="main" --add="visitors"
# This is a fake permission without any URL.
# The purpose of this permission is only to trigger hooks post_app_add/removeaccess when it's modified.
# We can't use a real permission for now because the actual permision system doesn't support regex.
ynh_permission_create --permission="upload images" --allowed="visitors"
if [ $is_public -eq 0 ]
then
# If the app is private, viewing images stays publicly accessible.
if [ "$path_url" == "/" ]; then
# If the path is /, clear it to prevent any error with the regex.
path_url=""
fi
# Modify the domain to be used in a regex
domain_regex=$(echo "$domain" | sed 's@-@.@g')
ynh_app_setting_set --app=$app --key=protected_regex --value="$domain_regex$path_url/stats/?$","$domain_regex$path_url/manifest.webapp/?$","$domain_regex$path_url/?$","$domain_regex$path_url/[d-m]/.*$"
# If the app is not public, then the "visitors" group doesn't have this permission
ynh_permission_update --permission="upload images" --remove="visitors"
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --weight=2
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# CHECK LUTIM BOOTING
#=================================================
ynh_script_progression --message="Restarting Lutim..." --weight=6
# Wait for lutim to be fully started
ynh_systemd_action --action=restart --line_match="Manager.*started" --log_path="/var/log/$app/production.log" --timeout="120"
ynh_systemd_action --action=stop
# Set right permissions on new files created at first start
chown -R $app: $final_path
# Wait for lutim to be fully started
ynh_systemd_action --action=restart --line_match="Manager.*started" --log_path="/var/log/$app/production.log" --timeout="120"
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
# Get main domain and buid the url of the admin panel of the app.
admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
echo "You can find a config file at $final_path/lutim.conf
You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.
If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/lutim_ynh__URL_TAG3__." > mail_to_send
ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="root" --type=install
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last