1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/lufi_ynh.git synced 2024-09-03 19:36:28 +02:00
lufi_ynh/scripts/install

239 lines
7.8 KiB
Text
Raw Normal View History

2017-02-07 22:51:47 +01:00
#!/bin/bash
2018-09-02 11:08:06 +02:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
2019-03-03 18:04:55 +01:00
ynh_clean_check_starting
2018-09-02 11:08:06 +02:00
true
2017-02-07 22:51:47 +01:00
}
2018-09-02 11:08:06 +02:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
2017-02-07 22:51:47 +01:00
domain=$YNH_APP_ARG_DOMAIN
2018-09-04 10:19:30 +02:00
path_url=$YNH_APP_ARG_PATH
2017-02-07 22:51:47 +01:00
is_public=$YNH_APP_ARG_IS_PUBLIC
2019-03-03 18:04:55 +01:00
max_file_size=$YNH_APP_ARG_MAX_FILE_SIZE
2017-02-07 22:51:47 +01:00
app=$YNH_APP_INSTANCE_NAME
2018-09-02 11:08:06 +02:00
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Validating installation parameters..."
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
# Normalize the url path syntax
path_url=$(ynh_normalize_url_path $path_url)
2017-02-07 22:51:47 +01:00
2019-03-03 18:52:39 +01:00
# Check if max_file_size is a number
if ! [[ $max_file_size =~ "^[\-0-9]+$" ]] && [ $max_file_size -lt 0 ]; then
ynh_die "Max file must be a number positive or zero"
fi
2018-09-02 11:08:06 +02:00
# Check web path availability
ynh_webpath_available $domain $path_url
# Register (book) web path
ynh_webpath_register $app $domain $path_url
2017-02-07 22:51:47 +01:00
2019-03-03 18:04:55 +01:00
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_print_info "Configuring firewall..."
# Find a free port
port=$(ynh_find_port 8095)
# Open this port
yunohost firewall allow --no-upnp TCP $port 2>&1
2018-09-02 11:08:06 +02:00
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Storing installation settings..."
2017-02-07 22:51:47 +01:00
ynh_app_setting_set $app domain $domain
ynh_app_setting_set $app is_public $is_public
2019-03-03 18:04:55 +01:00
ynh_app_setting_set $app port $port
ynh_app_setting_set $app path $path_url
ynh_app_setting_set $app max_file_size $max_file_size
2018-09-02 11:08:06 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
2019-03-03 18:04:55 +01:00
# INSTALL DEPENDENCIES
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Installing dependencies..."
2018-09-02 11:08:06 +02:00
2019-03-03 19:04:08 +01:00
ynh_install_app_dependencies build-essential libssl-dev libio-socket-ssl-perl liblwp-protocol-https-perl libpq-dev postgresql cpanminus
2019-03-03 18:04:55 +01:00
# Install Carton
echo yes | cpanm Carton
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# CREATE A POSTGRESQL DATABASE
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Creating a PostgreSQL database..."
2017-02-07 22:51:47 +01:00
2019-03-03 18:04:55 +01:00
# Create postgresql database
ynh_psql_test_if_first_run
db_name=$(ynh_sanitize_dbid "$app")
db_user=$db_name
ynh_app_setting_set "$app" db_name "$db_name"
# Initialize database and store postgres password for upgrade
ynh_psql_setup_db "$db_name" "$db_user"
db_pwd=$(ynh_app_setting_get $app psqlpwd) # Password created in ynh_psql_setup_db function
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Setting up source files..."
2018-09-02 11:08:06 +02:00
ynh_app_setting_set $app final_path $final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source "$final_path"
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Configuring nginx web server..."
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
# Create a dedicated nginx config
2019-03-03 18:04:55 +01:00
ynh_add_nginx_config max_file_size
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Configuring system user..."
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
# Create a system user
ynh_system_user_create $app
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
# Copy and fix variable into lufi config
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Configuring lufi..."
2017-02-07 22:51:47 +01:00
2019-03-03 18:04:55 +01:00
cp ../conf/lufi.conf.template "${final_path}/lufi.conf"
2018-09-02 11:08:06 +02:00
ynh_replace_string "__DOMAIN__" "$domain" "${final_path}/lufi.conf"
2018-09-04 10:19:30 +02:00
ynh_replace_string "__PATH__" "$path_url" "${final_path}/lufi.conf"
2018-09-02 11:08:06 +02:00
ynh_replace_string "__PORT__" "$port" "${final_path}/lufi.conf"
2019-03-03 18:04:55 +01:00
ynh_replace_string "__DB_NAME__" "$db_name" "${final_path}/lufi.conf"
ynh_replace_string "__DB_USER__" "$db_user" "${final_path}/lufi.conf"
ynh_replace_string "__DB_PWD__" "$db_pwd" "${final_path}/lufi.conf"
ynh_replace_string "__MAX_FILE_SIZE__" "$max_file_size" "${final_path}/lufi.conf"
2019-03-03 18:52:39 +01:00
if [ $max_file_size -eq 0 ]; then # Comment the limitation line if no limit
ynh_replace_string "max_file_size" "#max_file_size" "${final_path}/lufi.conf"
fi
2019-03-03 18:04:55 +01:00
secret=$(ynh_string_random 24)
ynh_app_setting_set $app secret $secret
2018-09-02 11:08:06 +02:00
ynh_replace_string "__SECRET__" "$secret" "${final_path}/lufi.conf"
2019-03-03 18:04:55 +01:00
if [ $is_public -eq 0 ];
then
ynh_replace_string "__IS_PUBLIC__" "" "${final_path}/lufi.conf"
else
ynh_replace_string "__IS_PUBLIC__" "#" "${final_path}/lufi.conf"
fi
ynh_store_file_checksum "${final_path}/lufi.conf"
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# SETUP CRON
2018-09-02 11:08:06 +02:00
#=================================================
2017-02-07 22:51:47 +01:00
2019-03-03 18:04:55 +01:00
cp ../conf/cron_lufi /etc/cron.d/$app
ynh_replace_string "__FINALPATH__" "$final_path/" "/etc/cron.d/$app"
chmod +x $final_path/script/lufi
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# SETUP SYSTEMD
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Configuring a systemd service..."
2018-09-02 11:08:06 +02:00
2019-03-03 18:04:55 +01:00
# Create a dedicated systemd config
ynh_add_systemd_config
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# Install lufi's dependencies via carton
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Installing lufi..."
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
pushd $final_path
2019-03-03 18:04:55 +01:00
carton install --deployment --without=sqlite --without=mysql
2018-09-02 11:08:06 +02:00
popd
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# SETUP LOGROTATE
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Configuring log rotation..."
2018-09-02 11:08:06 +02:00
2019-03-03 18:04:55 +01:00
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# ADVERTISE SERVICE IN ADMIN PANEL
2018-09-02 11:08:06 +02:00
#=================================================
2017-02-07 22:51:47 +01:00
2019-03-03 20:26:09 +01:00
yunohost service add $app --log "$final_path/log/production.log"
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# SETUP SSOWAT
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Configuring SSOwat..."
2018-09-02 11:08:06 +02:00
2019-03-03 18:04:55 +01:00
ynh_app_setting_set $app unprotected_uris "/"
2019-03-08 22:04:09 +01:00
if [ $is_public -eq 0 ]
2019-03-03 18:04:55 +01:00
then
2019-03-08 22:04:09 +01:00
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 protected_regex "$domain_regex$path_url/stats$","$domain_regex$path_url/manifest.webapp$","$domain_regex$path_url/$","$domain_regex$path_url/d/.*$","$domain_regex$path_url/m/.*$"
2019-03-03 18:04:55 +01:00
fi
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# Configure owner
2018-09-02 11:08:06 +02:00
#=================================================
2017-02-07 22:51:47 +01:00
2019-03-03 18:04:55 +01:00
chown -R $app:$app "$final_path"
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
2017-02-07 22:51:47 +01:00
# Start lufi
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
systemctl enable $app.service
ynh_systemd_action -n $app -a start -l "Creating process id file" -p "$final_path/log/production.log"
2019-03-03 20:26:09 +01:00
ln -sf "$final_path/log/production.log" "/var/log/$app/production.log"
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# RELOAD NGINX
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Reloading nginx web server..."
2018-09-02 11:08:06 +02:00
2019-03-03 18:04:55 +01:00
# Reload Nginx
systemctl reload nginx
yunohost app ssowatconf
2017-02-07 22:51:47 +01:00
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
# END OF SCRIPT
2018-09-02 11:08:06 +02:00
#=================================================
2019-03-03 18:04:55 +01:00
ynh_print_info "Installation of $app completed"