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

198 lines
6.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
#=================================================
2018-11-03 21:46:35 +01:00
ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
true
}
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
#=================================================
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:41:28 +01:00
password=$YNH_APP_ARG_PASSWORD
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
#=================================================
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
### If the app uses nginx as web server (written in HTML/PHP in most cases), the final path should be "/var/www/$app".
### If the app provides an internal web server (or uses another application server such as uwsgi), the final path should be "/opt/yunohost/$app"
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"
2017-02-02 19:21:27 +01:00
domain_regex=$(echo "$domain" | sed 's@-@.@g')
2018-11-03 21:46:35 +01:00
# 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
2017-02-02 19:21:27 +01:00
CHECK_VAR "$domain_regex" "domain_regex empty"
2018-11-03 21:46:35 +01:00
#=================================================
# FIND AND OPEN A PORT
#=================================================
# Find a free port
port=$(ynh_find_port 8095)
# Open this port
yunohost firewall allow --no-upnp TCP $port 2>&1
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2017-02-02 19:21:27 +01:00
ynh_app_setting_set $app domain $domain
ynh_app_setting_set $app is_public $is_public
ynh_app_setting_set $app port $port
2019-02-03 00:19:57 +01:00
ynh_app_setting_set $app path $path_url
2019-02-14 23:41:28 +01:00
hashed_password=$(echo $password | sha256sum $password)
ynh_app_setting_set $app hashed_password $hashed_password
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2018-11-04 11:00:03 +01:00
ynh_install_app_dependencies build-essential libssl-dev zlib1g-dev libpng-dev libpq-dev memcached postgresql
2018-11-03 21:46:35 +01:00
# Install Carton
echo yes | sudo cpan Carton
2018-11-04 11:00:03 +01:00
#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
# Create postgresql database
ynh_psql_test_if_first_run
db_name=$(ynh_sanitize_dbid "$app")
db_user=$db_name
# Initialize database and store postgres password for upgrade
ynh_psql_setup_db "$db_name" "$db_user"
2019-02-03 00:19:57 +01:00
ynh_app_setting_set "$app" db_name "$db_name"
db_pwd=$(ynh_app_setting_get $app 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
#=================================================
2017-02-02 19:21:27 +01:00
ynh_app_setting_set $app final_path $final_path
2018-11-03 21:46:35 +01:00
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source "$final_path"
#=================================================
# NGINX CONFIGURATION
#=================================================
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
#=================================================
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
# Create a system user
ynh_system_user_create $app
#=================================================
2017-04-21 12:54:39 +02:00
## Copy and fix variable into lstu config
2018-11-03 21:46:35 +01:00
#=================================================
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
cp ../conf/lstu.conf.template "${final_path}/lstu.conf"
ynh_replace_string "__DOMAIN__" "$domain" "${final_path}/lstu.conf"
ynh_replace_string "__PATH__" "$path_url" "${final_path}/lstu.conf"
ynh_replace_string "__PORT__" "$port" "${final_path}/lstu.conf"
2018-11-04 11:00:03 +01:00
ynh_replace_string "__DB_NAME__" "$db_name" "${final_path}/lstu.conf"
ynh_replace_string "__DB_USER__" "$db_user" "${final_path}/lstu.conf"
ynh_replace_string "__DB_PWD__" "$db_pwd" "${final_path}/lstu.conf"
2019-02-14 23:41:28 +01:00
ynh_replace_string "__PASSWORD_HASHED__" "$hashed_password" "${final_path}/lstu.conf"
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
secret=$(ynh_string_random 24)
2019-02-03 20:44:59 +01:00
ynh_app_setting_set $app secret $secret
2018-11-03 21:46:35 +01:00
ynh_replace_string "__SECRET__" "$secret" "${final_path}/lstu.conf"
ynh_store_file_checksum "${final_path}/lstu.conf"
2017-02-02 20:04:10 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
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
#=================================================
2017-04-21 12:54:39 +02:00
# Install lstu's dependencies via carton
2018-11-03 21:46:35 +01:00
#=================================================
2019-02-03 00:19:57 +01:00
pushd $final_path
carton install --deployment --without=sqlite --without=mysql
popd
2018-11-03 21:46:35 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add $app --log "/var/log/$app.log"
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# SETUP SSOWAT
#=================================================
2017-02-02 19:21:27 +01:00
# Make app public or private
ynh_app_setting_set $app skipped_uris "/"
2017-04-21 12:54:39 +02:00
if [ $is_public -eq 0 ];
then # If the app is private, only the shortened URLs are publics
2019-02-03 00:19:57 +01:00
if [ "$path_url" == "/" ]; then
path_url=""
2017-02-02 19:21:27 +01:00
fi
2018-11-03 21:48:16 +01:00
ynh_app_setting_set $app protected_regex "$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/$"
2018-11-03 21:46:35 +01:00
else
ynh_replace_string "#--PRIVATE--" "" "/etc/nginx/conf.d/$domain.d/$app.conf"
2017-02-02 19:21:27 +01:00
fi
2018-11-03 21:46:35 +01:00
#=================================================
2017-04-21 12:54:39 +02:00
# Configure owner
2018-11-03 21:46:35 +01:00
#=================================================
2017-02-02 19:21:27 +01:00
2019-02-03 00:19:57 +01:00
sudo chown -R www-data $final_path
2017-04-21 12:54:39 +02:00
2018-11-03 21:46:35 +01:00
#=================================================
# Start lstu
#=================================================
2017-02-02 19:21:27 +01:00
2019-02-03 01:03:02 +01:00
systemctl enable $app.service
2019-02-03 21:24:02 +01:00
ynh_systemd_action -n $app -a start -l "Server available at" -p "systemd"
2017-02-02 19:21:27 +01:00
2018-11-03 21:46:35 +01:00
#=================================================
# RELOAD NGINX
#=================================================
2017-02-02 19:21:27 +01:00
2017-04-21 12:54:39 +02:00
# Reload Nginx
2019-02-03 21:24:02 +01:00
systemctl reload nginx