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

231 lines
8.5 KiB
Text
Raw Normal View History

2017-02-02 19:21:27 +01:00
#!/bin/bash
2018-02-20 17:33:19 +01:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2017-04-21 12:54:39 +02:00
2018-02-20 17:33:19 +01:00
source _common.sh
source /usr/share/yunohost/helpers
2017-02-02 19:21:27 +01:00
2018-02-20 17:33:19 +01:00
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Managing script failure..."
2018-02-20 17:33:19 +01:00
2018-11-03 21:46:35 +01:00
ynh_clean_setup () {
2019-02-18 11:47:25 +01:00
ynh_clean_check_starting
2018-11-03 21:46:35 +01:00
}
2018-02-20 17:33:19 +01:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Retrieving arguments from the manifest..."
2018-11-03 21:46:35 +01:00
2017-02-02 19:21:27 +01:00
domain=$YNH_APP_ARG_DOMAIN
2018-11-03 21:48:16 +01:00
path_url=$YNH_APP_ARG_PATH
2017-02-02 19:21:27 +01:00
is_public=$YNH_APP_ARG_IS_PUBLIC
2019-02-14 23:39:36 +01:00
theme=$YNH_APP_ARG_THEME
2019-02-14 23:41:28 +01:00
password=$YNH_APP_ARG_PASSWORD
2020-04-19 05:16:46 +02:00
secret=$(ynh_string_random 24)
hashed_password=$(echo -n $password | sha256sum | cut -d' ' -f1)
2017-02-02 19:21:27 +01:00
app=$YNH_APP_INSTANCE_NAME
2018-11-03 21:46:35 +01:00
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Validating installation parameters..."
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
final_path=/var/www/$app
2020-04-19 05:16:46 +02:00
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
# Normalize the url path syntax
2020-04-19 05:16:46 +02:00
path_url=$(ynh_normalize_url_path --path_url=$path_url)
2018-11-03 21:46:35 +01:00
# Check web path availability
2020-04-19 05:16:46 +02:00
ynh_webpath_available --domain=$domain --path_url=$path_url
2018-11-03 21:46:35 +01:00
# Register (book) web path
2020-04-19 05:16:46 +02:00
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
# STORE SETTINGS FROM MANIFEST
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Storing installation settings..."
2018-11-03 21:46:35 +01:00
2020-04-19 05:16:46 +02:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=is_public --value=$is_public
2020-04-19 05:16:46 +02:00
ynh_app_setting_set --app=$app --key=theme --value=$theme
ynh_app_setting_set --app=$app --key=secret --value=$secret
ynh_app_setting_set --app=$app --key=hashed_password --value=$hashed_password
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Configuring firewall..."
2017-02-02 19:21:27 +01:00
2020-04-19 05:16:46 +02:00
# Find an available port
port=$(ynh_find_port --port=8095)
ynh_app_setting_set --app=$app --key=port --value=$port
2020-04-19 05:16:46 +02:00
# Open this port
ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $port
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Installing dependencies..."
ynh_install_app_dependencies $pkg_dependencies
2018-11-03 21:46:35 +01:00
# Install Carton
2019-02-21 00:36:05 +01:00
echo yes | cpanm Carton
2018-11-03 21:46:35 +01:00
2018-11-04 11:00:03 +01:00
#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Creating a PostgreSQL database..."
2018-11-04 11:00:03 +01:00
# Create postgresql database
ynh_psql_test_if_first_run
2020-04-19 05:16:46 +02:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
2018-11-04 11:00:03 +01:00
db_user=$db_name
2020-04-19 05:16:46 +02:00
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd) # Password created in ynh_psql_setup_db function
2018-11-04 11:00:03 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Setting up source files..."
2017-02-02 19:21:27 +01:00
2020-04-19 05:16:46 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2018-11-03 21:46:35 +01:00
# Download, check integrity, uncompress and patch the source from app.src
2020-04-19 05:16:46 +02:00
ynh_setup_source --dest_dir="$final_path"
2018-11-03 21:46:35 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Configuring nginx web server..."
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
# Create a dedicated nginx config
ynh_add_nginx_config
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Configuring system user..."
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
# Create a system user
2020-04-19 05:16:46 +02:00
ynh_system_user_create --username=$app
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 21:54:21 +02:00
# Copy and fix variable into lstu config
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Configuring lstu..."
config="${final_path}/lstu.conf"
cp ../conf/lstu.conf.template "$config"
ynh_replace_string --match_string="__DOMAIN__" --replace_string"$domain" --target_file="$config"
ynh_replace_string --match_string="__PATH__" --replace_string"$path_url" --target_file="$config"
ynh_replace_string --match_string="__PORT__" --replace_string"$port" --target_file="$config"
ynh_replace_string --match_string="__DB_NAME__" --replace_string"$db_name" --target_file="$config"
ynh_replace_string --match_string="__DB_USER__" --replace_string"$db_user" --target_file="$config"
ynh_replace_string --match_string="__DB_PWD__" --replace_string"$db_pwd" --target_file="$config"
ynh_replace_string --match_string="__SELECTED_THEME__" --replace_string"$theme" --target_file="$config"
ynh_replace_string --match_string="__PASSWORD_HASHED__" --replace_string"$hashed_password" --target_file="$config"
ynh_replace_string --match_string="__SECRET__" --replace_string"$secret" --target_file="$config"
2019-02-16 00:11:00 +01:00
if [ $is_public -eq 0 ];
then
2020-04-19 05:16:46 +02:00
ynh_replace_string --match_string="__IS_PUBLIC__" --replace_string"" --target_file="$config"
2019-02-16 00:11:00 +01:00
else
2020-04-19 05:16:46 +02:00
ynh_replace_string --match_string="__IS_PUBLIC__" --replace_string"#" --target_file="$config"
2019-02-16 00:11:00 +01:00
fi
2020-04-19 05:16:46 +02:00
ynh_store_file_checksum --file="$config"
2017-02-02 20:04:10 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Configuring a systemd service..."
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
# Create a dedicated systemd config
ynh_add_systemd_config
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
# BUILD LSTU
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Building lstu..."
2018-11-03 21:46:35 +01:00
2019-02-03 00:19:57 +01:00
pushd $final_path
2020-04-19 05:16:46 +02:00
carton install --deployment --without=sqlite --without=mysql
2019-02-03 00:19:57 +01:00
popd
2018-11-03 21:46:35 +01:00
2020-04-19 05:16:46 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
ynh_print_info --message="Securing files and directories..."
# Set permissions to app files
chown -R www-data $final_path
2018-11-03 21:46:35 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Configuring log rotation..."
2018-11-03 21:46:35 +01:00
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
2020-04-19 05:16:46 +02:00
# INTEGRATE SERVICE IN YUNOHOST
2018-11-03 21:46:35 +01:00
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Integrating service in YunoHost..."
2018-11-03 21:46:35 +01:00
2019-02-14 23:35:21 +01:00
yunohost service add $app --log "/var/log/$app.log" --log "/var/www/$app/log/production.log"
2017-02-02 19:21:27 +01:00
2020-04-19 05:16:46 +02:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_print_info --message="Starting a systemd service..."
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" --line_match="Server available at"
2018-11-03 21:46:35 +01:00
#=================================================
# SETUP SSOWAT
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Configuring SSOwat..."
2017-02-02 19:21:27 +01:00
2020-04-19 05:16:46 +02:00
ynh_app_setting_set --app=$app --key=unprotected_uris--value= "/"
2019-03-08 21:50:41 +01:00
if [ $is_public -eq 0 ]
then
# If the app is private, only the shortened URLs are publics.
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')
2020-04-19 05:16:46 +02:00
ynh_app_setting_set --app=$app --key=protected_regex --value="$domain_regex$path_url/login$","$domain_regex$path_url/logout$","$domain_regex$path_url/api$","$domain_regex$path_url/extensions$","$domain_regex$path_url/stats$","$domain_regex$path_url/d/.*$","$domain_regex$path_url/a$","$domain_regex$path_url/$"
2017-02-02 19:21:27 +01:00
fi
2018-11-03 21:46:35 +01:00
#=================================================
# RELOAD NGINX
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Reloading nginx web server..."
2017-02-02 19:21:27 +01:00
2020-04-19 05:16:46 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2019-02-21 12:35:13 +01:00
#=================================================
# END OF SCRIPT
#=================================================
2020-04-19 05:16:46 +02:00
ynh_print_info --message="Installation of $app completed"