mirror of
https://github.com/YunoHost-Apps/snserver_ynh.git
synced 2024-09-03 20:26:22 +02:00
340 lines
14 KiB
Bash
Executable file
340 lines
14 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
source ynh_install_ruby__2
|
|
source ynh_redis
|
|
source ynh_send_readme_to_admin__2
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# MANAGE SCRIPT FAILURE
|
|
#=================================================
|
|
|
|
ynh_clean_setup () {
|
|
ynh_clean_check_starting
|
|
}
|
|
# Exit if an error occurs during the execution of the script
|
|
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
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
access_domain=$YNH_APP_ARG_ACCESS_DOMAIN
|
|
if [ -z "$access_domain" ]
|
|
then
|
|
access_domain=$domain
|
|
fi
|
|
access_domain=$(echo $access_domain | sed "s@;@ @g")
|
|
mail="$app@$domain"
|
|
|
|
#=================================================
|
|
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
|
|
#=================================================
|
|
final_path=/opt/yunohost/$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=3
|
|
|
|
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=access_domain --value=$access_domain
|
|
ynh_app_setting_set --app=$app --key=mail --value=$mail
|
|
redis_db=$(ynh_redis_get_free_db)
|
|
ynh_app_setting_set --app=$app --key=redis_db --value="$redis_db"
|
|
|
|
#=================================================
|
|
# STANDARD MODIFICATIONS
|
|
#=================================================
|
|
# FIND AND OPEN A PORT
|
|
#=================================================
|
|
ynh_script_progression --message="Finding an available port..." --weight=1
|
|
|
|
# Find an available port
|
|
port=$(ynh_find_port --port=3000)
|
|
# Open the port
|
|
ynh_app_setting_set --app=$app --key=port --value=$port
|
|
|
|
#=================================================
|
|
# INSTALL DEPENDENCIES
|
|
#=================================================
|
|
ynh_script_progression --message="Installing dependencies..." --weight=17
|
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
|
ynh_install_ruby --ruby_version=$RUBY_VERSION
|
|
|
|
#=================================================
|
|
# CREATE A MYSQL DATABASE
|
|
#=================================================
|
|
ynh_script_progression --message="Creating a MySQL database..." --weight=2
|
|
|
|
db_name=$(ynh_sanitize_dbid --db_name=$app)
|
|
db_user=$db_name
|
|
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
|
|
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
|
|
|
|
#=================================================
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
#=================================================
|
|
ynh_script_progression --message="Setting up source files..." --weight=2
|
|
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
|
# Download, check integrity, uncompress and patch the source from app.src
|
|
mkdir -p $final_path
|
|
ynh_setup_source --dest_dir="$final_path/live"
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring nginx web server..." --weight=3
|
|
|
|
# Create a dedicated nginx config
|
|
ynh_add_nginx_config "\
|
|
port \
|
|
access_domain \
|
|
"
|
|
|
|
#=================================================
|
|
# CREATE DEDICATED USER
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring system user..." --weight=1
|
|
|
|
# Create a system user
|
|
ynh_system_user_create --username=$app --home_dir=$final_path
|
|
|
|
#=================================================
|
|
# SPECIFIC SETUP
|
|
#=================================================
|
|
# INSTALLING RUBY
|
|
#=================================================
|
|
ynh_script_progression --message="Installing Ruby...( This may take a while... )" --weight=100 #331
|
|
|
|
pushd "$final_path/live"
|
|
ynh_use_ruby
|
|
ynh_gem update --system --no-document
|
|
ynh_gem install bundler --no-document
|
|
popd
|
|
|
|
#=================================================
|
|
# Setup
|
|
#=================================================
|
|
#=================================================
|
|
# MODIFY A CONFIG FILE
|
|
#=================================================
|
|
ynh_script_progression --message="Modifying a config file..." --weight=2
|
|
|
|
config_file="$final_path/live/.env"
|
|
cp -f ../conf/env.sample $config_file
|
|
ynh_replace_string --match_string="EXPOSED_PORT=.*$" --replace_string="EXPOSED_PORT=$port" --target_file="$config_file"
|
|
secret_key=$(ynh_string_random --length=48 | base64)
|
|
ynh_replace_string --match_string="SECRET_KEY_BASE=.*$" --replace_string="SECRET_KEY_BASE=$secret_key" --target_file="$config_file"
|
|
pseudo_key=$(ynh_string_random --length=48 | base64)
|
|
ynh_replace_string --match_string="PSEUDO_KEY_PARAMS_KEY=.*$" --replace_string="PSEUDO_KEY_PARAMS_KEY=$pseudo_key" --target_file="$config_file"
|
|
ynh_replace_string --match_string="RAILS_ENV=.*$" --replace_string="RAILS_ENV=production" --target_file="$config_file"
|
|
ynh_replace_string --match_string="DB_DATABASE=.*$" --replace_string="DB_DATABASE=$db_name" --target_file="$config_file"
|
|
ynh_replace_string --match_string="DB_USERNAME=.*$" --replace_string="DB_USERNAME=$db_user" --target_file="$config_file"
|
|
ynh_replace_string --match_string="DB_PASSWORD=.*$" --replace_string="DB_PASSWORD=$db_pwd" --target_file="$config_file"
|
|
ynh_replace_string --match_string="RAILS_RELATIVE_URL_ROOT=.*$" --replace_string="RAILS_RELATIVE_URL_ROOT=$path_url" --target_file="$config_file"
|
|
ynh_replace_string --match_string="SMTP_HOST=.*$" --replace_string="SMTP_HOST=localhost" --target_file="$config_file"
|
|
ynh_replace_string --match_string="SMTP_PORT=.*$" --replace_string="SMTP_PORT=25" --target_file="$config_file"
|
|
ynh_replace_string --match_string="SMTP_DOMAIN=.*$" --replace_string="SMTP_DOMAIN=localhost" --target_file="$config_file"
|
|
ynh_replace_string --match_string="SMTP_STARTTLS=.*$" --replace_string="SMTP_STARTTLS=" --target_file="$config_file"
|
|
ynh_replace_string --match_string="RAILS_SERVE_STATIC_FILES=.*$" --replace_string="RAILS_SERVE_STATIC_FILES=true" --target_file="$config_file"
|
|
ynh_replace_string --match_string="REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_file"
|
|
|
|
ynh_replace_string --match_string="__MAIL__" --replace_string="$mail" --target_file="$final_path/live/app/mailers/application_mailer.rb"
|
|
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$final_path/live/app/views/user_mailer/welcome.html.erb"
|
|
ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$final_path/live/app/views/user_mailer/welcome.html.erb"
|
|
|
|
#=================================================
|
|
# INSTALLING Standard Notes - Syncing Server
|
|
#=================================================
|
|
ynh_script_progression --message="Installing Standard Notes - Syncing Server..." --weight=93
|
|
|
|
chown -R $app: "$final_path"
|
|
|
|
pushd "$final_path/live"
|
|
ynh_exec_as $app $ynh_ruby_load_path bin/bundle config set --local path 'vendor/bundle'
|
|
ynh_exec_as $app $ynh_ruby_load_path bin/bundle config set with 'development'
|
|
ynh_exec_as $app $ynh_ruby_load_path bin/bundle install -j$(getconf _NPROCESSORS_ONLN)
|
|
ynh_exec_as $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails db:migrate --quiet
|
|
ynh_exec_as $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails db:seed --quiet
|
|
# ynh_exec_as $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails assets:precompile --quiet
|
|
popd
|
|
|
|
#=================================================
|
|
# INSTALLING Standard Notes - Extensions
|
|
#=================================================
|
|
ynh_script_progression --message="Installing Standard Notes - Extensions..." --weight=1
|
|
|
|
|
|
if [ $path_url = "/" ]
|
|
then
|
|
path=""
|
|
else
|
|
path=$path_url
|
|
fi
|
|
|
|
ynh_replace_string --match_string="__DOMAIN__PATH__" --replace_string="$domain$path" --target_file="$final_path/live/public/extensions/repo.json"
|
|
|
|
find "$final_path/live/public/extensions/src/" -name "*.json" -print0 | while read -d $'\0' file
|
|
do
|
|
ynh_replace_string --match_string="__DOMAIN__PATH__" --replace_string="$domain$path" --target_file="$file"
|
|
done
|
|
|
|
find "../conf/" -name "ext_*.src" -print0 | while read -d $'\0' file
|
|
do
|
|
basename=$(basename -as .src $file)
|
|
ynh_setup_source --dest_dir="$final_path/live/public/extensions/src/${basename#'ext_'}" --source_id="$basename"
|
|
done
|
|
|
|
#=================================================
|
|
# SETUP SYSTEMD
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring a systemd service..." --weight=4
|
|
|
|
# Create a dedicated systemd config
|
|
ynh_add_systemd_config --others_var="\
|
|
port \
|
|
RBENV_ROOT \
|
|
"
|
|
|
|
#=================================================
|
|
# STORE THE CONFIG FILE CHECKSUM
|
|
#=================================================
|
|
ynh_script_progression --message="Storing the config file checksum..." --weight=1
|
|
|
|
# Calculate and store the config file checksum into the app settings
|
|
ynh_store_file_checksum --file="$config_file"
|
|
ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.d/$app.conf"
|
|
|
|
#=================================================
|
|
# GENERIC FINALIZATION
|
|
#=================================================
|
|
#=================================================
|
|
# SECURE FILES AND DIRECTORIES
|
|
#=================================================
|
|
ynh_script_progression --message="Securing files and directories..." --weight=1
|
|
|
|
# Set permissions to app files
|
|
chmod 750 "$final_path"
|
|
chmod -R o-rwx "$final_path"
|
|
chown -R $app: "$final_path"
|
|
|
|
mkdir -p "$final_path/live/log"
|
|
chown -R $app: "$final_path/live/log/"
|
|
mkdir -p "$final_path/live/tmp"
|
|
chown -R $app: "$final_path/live/tmp/"
|
|
|
|
mkdir -p "/var/log/$app"
|
|
chown -R $app: "/var/log/$app"
|
|
|
|
#=================================================
|
|
# SETUP LOGROTATE
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring log rotation..." --weight=1
|
|
|
|
# Use logrotate to manage application logfile(s)
|
|
ynh_use_logrotate --logfile="$final_path/live/log/production.log"
|
|
ynh_use_logrotate --logfile="/var/log/$app/$app.log"
|
|
|
|
#=================================================
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
#=================================================
|
|
ynh_script_progression --message="Integrate $app service in Yunohost..." --weight=1
|
|
|
|
yunohost service add $app --description "Standard Notes - Syncing Server" --log "/var/log/$app/$app.log"
|
|
|
|
#=================================================
|
|
# SETUP FAIL2BAN
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring fail2ban..." --weight=1
|
|
|
|
# Create a dedicated fail2ban config
|
|
touch "/var/log/$app/$app.log"
|
|
ynh_add_fail2ban_config --use_template --others_var="\
|
|
domain \
|
|
path_url \
|
|
"
|
|
|
|
#=================================================
|
|
# SETUP SSOWAT
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring SSOwat..." --weight=3
|
|
|
|
# Make app public if necessary
|
|
if [ $is_public -eq 1 ]
|
|
then
|
|
# Everyone can access the app.
|
|
# The "main" permission is automatically created before the install script.
|
|
ynh_permission_update --permission="main" --add="visitors" --remove="all_users"
|
|
fi
|
|
|
|
#=================================================
|
|
# START SYSTEMD SERVICE
|
|
#=================================================
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=1
|
|
|
|
# Start a systemd service
|
|
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
ynh_script_progression --message="Reloading nginx web server..." --weight=1
|
|
|
|
ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
#=================================================
|
|
# WAITING FOR SERVICE
|
|
#=================================================
|
|
ynh_script_progression --message="Waiting for service..." --weight=1
|
|
|
|
is_service_ready
|
|
|
|
#=================================================
|
|
# SEND A README FOR THE ADMIN
|
|
#=================================================
|
|
ynh_script_progression --message="Sending a readme for the admin..."
|
|
|
|
admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
|
|
config_panel="You can find some specific configurations for this app by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__"
|
|
|
|
admin_mail_info="\
|
|
Standard Notes extensions are hosted with this package.\n\
|
|
New users will get an e-mail with install instructions.\n\
|
|
With the current SMTP settings you can not send e-mails to all e-mail providers. For example GMail will probably not recive this e-mails.\n\
|
|
Please setup the SMTP account in this file: \"$final_path/live/.env\" under \"#Mailer settings\" if you want to fix this.\n\
|
|
Please setup the Access-Domain for the extensions in the __URL_TAG1__config-panel__URL_TAG2__$admin_panel/config-panel__URL_TAG3__ if not already done during the installation settings.\
|
|
"
|
|
|
|
echo -e "\
|
|
Standard Notes - Syncing Server was successfully installed.\n\
|
|
Please configure the Standard Notes web app or mobile app to use this syning server: https://$domain$path_url/\n\
|
|
$config_panel\n\n\
|
|
$admin_mail_info\
|
|
" > message
|
|
|
|
ynh_send_readme_to_admin --app_message="message" --type='install'
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|