From 0d39d52d539506e33d09939504e2e1f73295b7a1 Mon Sep 17 00:00:00 2001 From: frju365 Date: Sun, 2 Sep 2018 11:08:06 +0200 Subject: [PATCH] Update install --- scripts/install | 250 ++++++++++++++++++++++++++++++------------------ 1 file changed, 156 insertions(+), 94 deletions(-) diff --git a/scripts/install b/scripts/install index 3b189bd..9f58647 100644 --- a/scripts/install +++ b/scripts/install @@ -1,145 +1,207 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -source .fonctions # Loads the generic functions usually used in the script -source /usr/share/yunohost/helpers # Source app helpers +source _common.sh +source /usr/share/yunohost/helpers -CLEAN_SETUP () { - # Clean installation residues that are not supported by the remove script. - # Clean hosts - echo "" +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +ynh_clean_setup () { + ### Remove this function if there's nothing to clean before calling the remove script. + true } -TRAP_ON # Active trap to stop the script if an error is detected. +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= -# Retrieve arguments domain=$YNH_APP_ARG_DOMAIN path=$YNH_APP_ARG_PATH admin=$YNH_APP_ARG_ADMIN is_public=$YNH_APP_ARG_IS_PUBLIC app=$YNH_APP_INSTANCE_NAME +secret=$(ynh_string_random 24) script_dir=$PWD -# Check variable is not empty -CHECK_VAR "$app" "app name not set" -CHECK_VAR "$script_dir" "script_dir not set" +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS +#================================================= -CHECK_USER "$admin" # Check username +final_path=/var/www/$app +test ! -e "$final_path" || ynh_die "This path already contains a folder" -CHECK_PATH # Check and fix path syntax -CHECK_DOMAINPATH # Check and fix domain disponibility - -CHECK_FINALPATH # Check final path - -# Check domain with regex domain_regex=$(echo "$domain" | sed 's@-@.@g') -CHECK_VAR "$domain_regex" "domain_regex empty" -port=$(ynh_find_port 8096) # Check port availability +# Normalize the url path syntax +path_url=$(ynh_normalize_url_path $path_url) + +# Check web path availability +ynh_webpath_available $domain $path_url +# Register (book) web path +ynh_webpath_register $app $domain $path_url + +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= -# Save app settings ynh_app_setting_set $app admin $admin ynh_app_setting_set $app domain $domain ynh_app_setting_set $app is_public $is_public ynh_app_setting_set $app port $port +ynh_app_setting_set $app secret $secret -# Install build-essential -ynh_package_update -ynh_package_install build-essential +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# FIND AND OPEN A PORT +#================================================= + +# Find a free port +port=$(ynh_find_port 8096) +# Open this port +yunohost firewall allow --no-upnp TCP $port 2>&1 +ynh_app_setting_set $app port $port + +#================================================= +# INSTALL DEPENDENCIES +#================================================= + +ynh_install_app_dependencies build-essential cpanminus + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= -# Copy files to the right place -sudo mkdir "${final_path}" 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" -# Get source -SETUP_SOURCE +#================================================= +# NGINX CONFIGURATION +#================================================= -# Copy it to Nginx conf directory -sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf - -if [ "$is_public" = "Yes" ]; +# Create a dedicated nginx config +ynh_add_nginx_config +if [ "$is_public" = true ]; then sudo sed -i "s@#--PRIVATE--@@g" /etc/nginx/conf.d/$domain.d/$app.conf fi -## Copy and fix variable into lufi config +#================================================= +# CREATE DEDICATED USER +#================================================= + +# Create a system user +ynh_system_user_create $app + +#================================================= +# Copy and fix variable into lufi config +#================================================= + sudo cp ../conf/lufi.conf.template "${final_path}/lufi.conf" -sudo sed -i "s@__DOMAIN__@$domain@g" "${final_path}/lufi.conf" -sudo sed -i "s@__PATH__@$path@g" "${final_path}/lufi.conf" -sudo sed -i "s@__PORT__@$port@g" "${final_path}/lufi.conf" +ynh_replace_string "__DOMAIN__" "$domain" "${final_path}/lufi.conf" +ynh_replace_string "__PATH__" "$path" "${final_path}/lufi.conf" +ynh_replace_string "__PORT__" "$port" "${final_path}/lufi.conf" +ynh_replace_string "__SECRET__" "$secret" "${final_path}/lufi.conf" -secret=$(ynh_string_random 24) -CHECK_VAR "$secret" "secret empty" -sudo sed -i "s@__SECRET__@$secret@g" "${final_path}/lufi.conf" -STORE_MD5_CONFIG "lufi.conf" "${final_path}/lufi.conf" +#================================================= +# Set right permissions on new files created at first start +#================================================= -# Install systemd script -sudo cp ../conf/lufi.service /etc/systemd/system/lufi.service -sudo chown root: /etc/systemd/system/lufi.service -sudo sed -i "s@__FINALPATH__@$final_path/@g" /etc/systemd/system/lufi.service -## Start service auto -sudo systemctl enable lufi.service - -## Install cron -sudo cp ../conf/cron_lufi /etc/cron.d/$app -sudo sed -i "s@__FINALPATH__@$final_path/@g" /etc/cron.d/$app -sudo chmod +x $final_path/script/lufi - -# Install logrotate -sed -i "s@__FINALPATH__@$final_path@g" ../conf/logrotate -sudo cp ../conf/logrotate /etc/logrotate.d/$app +sudo chown -R $app:$app "$final_path" +#================================================= # Install Carton -sudo apt-get install cpanminus -y +#================================================= + echo yes | sudo cpanm Carton +#================================================= # Install lufi via carton -sudo mkdir -p /var/log/$app/ -cd $final_path -sudo carton install 2>&1 | sudo tee -a "/var/log/$app/setup_carton.log" +#================================================= -# Change variables in nginx configuration -sudo sed -i "s@__PATH__@$path@g" /etc/nginx/conf.d/$domain.d/$app.conf -sudo sed -i "s@__PORT__@$port@g" /etc/nginx/conf.d/$domain.d/$app.conf +mkdir -p /var/log/$app/ +pushd $final_path +carton install 2>&1 | sudo tee -a "/var/log/$app/setup_carton.log" +popd -# Make app public or private -ynh_app_setting_set $app skipped_uris "/" -if [ "$is_public" = "No" ]; +#================================================= +# STORE THE CONFIG FILE CHECKSUM +#================================================= + +ynh_store_file_checksum "${final_path}/lufi.conf" + +#================================================= +# SETUP SYSTEMD +#================================================= + +# Create a dedicated systemd config +ynh_add_systemd_config + +#================================================= +## Install cron +#================================================= + +cp ../conf/cron_lufi /etc/cron.d/$app +ynh_replace_string "__FINALPATH__" "$final_path/" "/etc/cron.d/$app" +chmod +x $final_path/script/lufi + +#================================================= +# Making log symbolic link to /var/log +#================================================= + +touch /var/log/$app/production.log +chown www-data: /var/log/$app/production.log +ln -s /var/log/$app/production.log "$final_path/log/production.log" + +#================================================= +# Start lufi +#================================================= + +sudo systemctl start $app.service + +#================================================= +# SETUP LOGROTATE +#================================================= + +# Use logrotate to manage application logfile(s) +ynh_use_logrotate + +#================================================= +# ADVERTISE SERVICE IN ADMIN PANEL +#================================================= + +yunohost service add NAME_INIT.D --log "/var/log/FILE.log" + +#================================================= +# SETUP SSOWAT +#================================================= + +# Make app public if necessary +if [ $is_public -eq 1 ] then + # unprotected_uris allows SSO credentials to be passed anyway. + ynh_app_setting_set $app unprotected_uris "/" +else if [ "$path" == "/" ]; then path="" fi ynh_app_setting_set $app protected_regex "$domain_regex$path/stats$","$domain_regex$path/manifest.webapp$","$domain_regex$path/$","$domain_regex$path/d/.*$","$domain_regex$path/m/.*$" fi -# Making log symbolic link to /var/log -sudo touch /var/log/$app/production.log -sudo chown www-data: /var/log/$app/production.log -sudo ln -s /var/log/$app/production.log "$final_path/log/production.log" +#================================================= +# RELOAD NGINX +#================================================= -# Configure owner -sudo chown -R www-data: $final_path - -# Start lufi -sudo systemctl daemon-reload -sudo systemctl start lufi.service -sudo systemctl enable lufi.service - -# Set right permissions on new files created at first start -sudo chown -R www-data: "$final_path" - -# Add lufi as a service -sudo yunohost service add lufi -l $final_path/log/production.log - -if [ "$is_public" = "No" ]; -then - # Delete public access - ynh_app_setting_delete $app unprotected_uris - sudo yunohost app ssowatconf -fi - -# Reload Nginx -sudo service nginx reload +systemctl reload nginx