mirror of
https://github.com/YunoHost-Apps/snserver_ynh.git
synced 2024-09-03 20:26:22 +02:00
380 lines
16 KiB
Bash
Executable file
380 lines
16 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
source ynh_add_swap
|
|
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
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
#=================================================
|
|
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
|
|
#=================================================
|
|
final_path=/opt/yunohost/$app
|
|
final_path_www=/var/www/$app
|
|
final_path_extensions="$final_path_www/extensions"
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
|
ynh_app_setting_set --app=$app --key=final_path_www --value=$final_path_www
|
|
ynh_app_setting_set --app=$app --key=final_path_extensions --value=$final_path_extensions
|
|
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
|
|
test ! -e "$final_path_www" || 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
|
|
redis_db=$(ynh_redis_get_free_db)
|
|
ynh_app_setting_set --app=$app --key=redis_db --value="$redis_db"
|
|
|
|
#=================================================
|
|
# STORE SETTINGS
|
|
#=================================================
|
|
ynh_app_setting_set --app=$app --key=syncing_server_js_version --value=$syncing_server_js_version
|
|
ynh_app_setting_set --app=$app --key=auth_version --value=$auth_version
|
|
ynh_app_setting_set --app=$app --key=api_gateway_version --value=$api_gateway_version
|
|
ynh_app_setting_set --app=$app --key=extensions_version --value=$extensions_version
|
|
ynh_app_setting_set --app=$app --key=standalone --value="true"
|
|
|
|
#=================================================
|
|
# STANDARD MODIFICATIONS
|
|
#=================================================
|
|
# FIND AND OPEN A PORT
|
|
#=================================================
|
|
ynh_script_progression --message="Finding an available port..." --weight=1
|
|
|
|
# Find an available port
|
|
port_syncing_server_js=$(ynh_find_port --port=3000)
|
|
port_syncing_server_js_worker=$(ynh_find_port --port=$((port_syncing_server_js+1)))
|
|
port_auth=$(ynh_find_port --port=$((port_syncing_server_js_worker+1)))
|
|
port_auth_worker=$(ynh_find_port --port=$((port_auth+1)))
|
|
port_api_gateway=$(ynh_find_port --port=$((port_auth_worker+1)))
|
|
ynh_app_setting_set --app=$app --key=port_syncing_server_js --value=$port_syncing_server_js
|
|
ynh_app_setting_set --app=$app --key=port_syncing_server_js_worker --value=$port_syncing_server_js_worker
|
|
ynh_app_setting_set --app=$app --key=port_auth --value=$port_auth
|
|
ynh_app_setting_set --app=$app --key=port_auth_worker --value=$port_auth_worker
|
|
ynh_app_setting_set --app=$app --key=port_api_gateway --value=$port_api_gateway
|
|
|
|
#=================================================
|
|
# INSTALL DEPENDENCIES
|
|
#=================================================
|
|
ynh_script_progression --message="Installing dependencies..." --weight=17
|
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
|
ynh_install_nodejs --nodejs_version=$NODEJS_VERSION
|
|
ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key="https://dl.yarnpkg.com/debian/pubkey.gpg"
|
|
|
|
#=================================================
|
|
# 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
|
|
|
|
#=================================================
|
|
# 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
|
|
|
|
# Download, check integrity, uncompress and patch the source from app.src
|
|
mkdir -p "$final_path/live"
|
|
ynh_setup_source --source_id=app_syncing-server-js --dest_dir="$final_path/live/syncing-server-js"
|
|
ynh_setup_source --source_id=app_syncing-server-js --dest_dir="$final_path/live/syncing-server-js-worker"
|
|
ynh_setup_source --source_id=app_auth --dest_dir="$final_path/live/auth"
|
|
ynh_setup_source --source_id=app_auth --dest_dir="$final_path/live/auth-worker"
|
|
ynh_setup_source --source_id=app_api-gateway --dest_dir="$final_path/live/api-gateway"
|
|
|
|
chmod 750 "$final_path"
|
|
chmod -R o-rwx "$final_path"
|
|
chown -R $app:$app "$final_path"
|
|
|
|
# Copy help file
|
|
mkdir -p "$final_path_www/help"
|
|
if test -e "$YNH_APP_BASEDIR/sources/extra_files/help"; then
|
|
cp --archive "$YNH_APP_BASEDIR/sources/extra_files/help/." "$final_path_www/help"
|
|
fi
|
|
ynh_replace_vars --file="$final_path_www/help/index.html"
|
|
|
|
# Extensions
|
|
mkdir -p "$final_path_extensions"
|
|
|
|
if test -e "$YNH_APP_BASEDIR/sources/extra_files/extensions"; then
|
|
cp --archive "$YNH_APP_BASEDIR/sources/extra_files/extensions/." "$final_path_extensions"
|
|
fi
|
|
|
|
find "../conf/" -name "ext_*.src" -print0 | while read -d $'\0' file
|
|
do
|
|
basename=$(basename -as .src $file)
|
|
ynh_setup_source --dest_dir="$final_path_extensions/src/${basename#'ext_'}" --source_id="$basename"
|
|
done
|
|
|
|
chmod 750 "$final_path_www"
|
|
chmod -R o-rwx "$final_path_www"
|
|
chown -R "www-data":"www-data" "$final_path_www"
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring nginx web server..." --weight=3
|
|
|
|
# Create a dedicated nginx config
|
|
ynh_add_nginx_config
|
|
|
|
#=================================================
|
|
# SPECIFIC SETUP
|
|
#=================================================
|
|
#=================================================
|
|
# ADD SWAP
|
|
#=================================================
|
|
ynh_script_progression --message="Adding swap..."
|
|
|
|
ynh_add_swap --size=$swap_needed
|
|
|
|
#=================================================
|
|
# MODIFY A CONFIG FILE
|
|
#=================================================
|
|
ynh_script_progression --message="Modifying a config file..." --weight=2
|
|
|
|
config_syncing_server_js="$final_path/live/syncing-server-js.env"
|
|
config_syncing_server_js_worker="$final_path/live/syncing-server-js-worker.env"
|
|
config_auth="$final_path/live/auth.env"
|
|
config_auth_worker="$final_path/live/auth-worker.env"
|
|
config_api_gateway="$final_path/live/api-gateway.env"
|
|
|
|
jwt_secret=$(ynh_string_random --length=48 | base64)
|
|
legacy_jwt_secret=$(ynh_string_random --length=48 | base64)
|
|
auth_jwt_secret=$(ynh_string_random --length=48 | base64)
|
|
pseudo_key_params_key=$(ynh_string_random --length=48 | base64)
|
|
encryption_server_key=$(hexdump -n 32 -e '4/4 "%08X"' /dev/random) # 32bytes hex key is required
|
|
|
|
ynh_app_setting_set --app=$app --key=jwt_secret --value=$jwt_secret
|
|
ynh_app_setting_set --app=$app --key=legacy_jwt_secret --value=$legacy_jwt_secret
|
|
ynh_app_setting_set --app=$app --key=auth_jwt_secret --value=$auth_jwt_secret
|
|
ynh_app_setting_set --app=$app --key=pseudo_key_params_key --value=$pseudo_key_params_key
|
|
ynh_app_setting_set --app=$app --key=encryption_server_key --value=$encryption_server_key
|
|
|
|
ynh_add_config --template="env_syncing-server-js.env.sample" --destination="$config_syncing_server_js"
|
|
ynh_add_config --template="env_syncing-server-js-worker.env.sample" --destination="$config_syncing_server_js_worker"
|
|
ynh_add_config --template="env_auth.env.sample" --destination="$config_auth"
|
|
ynh_add_config --template="env_auth-worker.env.sample" --destination="$config_auth_worker"
|
|
ynh_add_config --template="env_api-gateway.env.sample" --destination="$config_api_gateway"
|
|
|
|
#=================================================
|
|
# INSTALLING Standard Notes - Syncing Server
|
|
#=================================================
|
|
ynh_script_progression --message="Installing Standard Notes - Syncing Server..." --weight=93
|
|
|
|
|
|
ynh_use_nodejs
|
|
pushd "$final_path/live/syncing-server-js"
|
|
ynh_print_info "Installing ... [1/3]"
|
|
ynh_exec_as $app env NODE_OPTIONS="--max-old-space-size=4096" PATH=$ynh_node_load_PATH yarn install --pure-lockfile
|
|
ynh_exec_as $app env NODE_OPTIONS="--max-old-space-size=4096" PATH=$ynh_node_load_PATH yarn build
|
|
popd
|
|
pushd "$final_path/live/auth"
|
|
ynh_print_info "Installing ... [2/3]"
|
|
ynh_exec_as $app env NODE_OPTIONS="--max-old-space-size=4096" PATH=$ynh_node_load_PATH yarn install --pure-lockfile
|
|
ynh_exec_as $app env NODE_OPTIONS="--max-old-space-size=4096" PATH=$ynh_node_load_PATH yarn build
|
|
popd
|
|
pushd "$final_path/live/api-gateway"
|
|
ynh_print_info "Installing ... [3/3]"
|
|
ynh_exec_as $app env NODE_OPTIONS="--max-old-space-size=4096" PATH=$ynh_node_load_PATH yarn install --pure-lockfile
|
|
ynh_exec_as $app env NODE_OPTIONS="--max-old-space-size=4096" PATH=$ynh_node_load_PATH yarn build
|
|
popd
|
|
#=================================================
|
|
# INSTALLING Standard Notes - Extensions
|
|
#=================================================
|
|
ynh_script_progression --message="Setup 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_extensions/repo.json"
|
|
|
|
find "$final_path_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
|
|
|
|
|
|
#=================================================
|
|
# SETUP SYSTEMD
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring a systemd service..." --weight=4
|
|
|
|
# Create a dedicated systemd config
|
|
ynh_add_systemd_config --service="$app-syncing-server-js" --template="systemd_syncing-server-js.service"
|
|
ynh_add_systemd_config --service="$app-syncing-server-js-worker" --template="systemd_syncing-server-js-worker.service"
|
|
ynh_add_systemd_config --service="$app-auth" --template="systemd_auth.service"
|
|
ynh_add_systemd_config --service="$app-auth-worker" --template="systemd_auth-worker.service"
|
|
ynh_add_systemd_config --service="$app-api-gateway" --template="systemd_api-gateway.service"
|
|
|
|
#=================================================
|
|
# 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_syncing_server_js"
|
|
ynh_store_file_checksum --file="$config_syncing_server_js_worker"
|
|
ynh_store_file_checksum --file="$config_auth"
|
|
ynh_store_file_checksum --file="$config_auth_worker"
|
|
ynh_store_file_checksum --file="$config_api_gateway"
|
|
ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.d/$app.conf"
|
|
|
|
#=================================================
|
|
# GENERIC FINALIZATION
|
|
#=================================================
|
|
#=================================================
|
|
# SETUP LOGROTATE
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring log rotation..." --weight=1
|
|
|
|
mkdir -p "/var/log/$app"
|
|
chown -R "$app": "/var/log/$app"
|
|
|
|
# Use logrotate to manage application logfile(s)
|
|
ynh_use_logrotate --logfile="/var/log/$app/syncing-server.log"
|
|
ynh_use_logrotate --logfile="/var/log/$app/syncing-server-worker.log"
|
|
ynh_use_logrotate --logfile="/var/log/$app/auth.log"
|
|
ynh_use_logrotate --logfile="/var/log/$app/auth-worker.log"
|
|
ynh_use_logrotate --logfile="/var/log/$app/api-gateway.log"
|
|
|
|
#=================================================
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
#=================================================
|
|
ynh_script_progression --message="Integrate $app service in Yunohost..." --weight=1
|
|
|
|
yunohost service add "$app-syncing-server-js" --description="Standard Notes - Syncing Server" --log="/var/log/$app/syncing-server-js.log"
|
|
yunohost service add "$app-syncing-server-js-worker" --description="Standard Notes - Syncing Server - Worker" --log="/var/log/$app/syncing-server-js-worker.log"
|
|
yunohost service add "$app-auth" --description="Standard Notes - Auth" --log="/var/log/$app/auth.log"
|
|
yunohost service add "$app-auth-worker" --description="Standard Notes - Auth - Worker" --log="/var/log/$app/auth-worker.log"
|
|
yunohost service add "$app-api-gateway" --description="Standard Notes - API Gateway" --log="/var/log/$app/api-gateway.log"
|
|
|
|
|
|
#=================================================
|
|
# START SYSTEMD SERVICE
|
|
#=================================================
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=1
|
|
|
|
# Start a systemd service
|
|
ynh_systemd_action \
|
|
--service_name="$app-syncing-server-js" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/syncing-server-js.log" \
|
|
--line_match='{"message":"Server started on port '$port_syncing_server_js'","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-syncing-server-js-worker" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/syncing-server-js-worker.log" \
|
|
--line_match='{"message":"Starting worker...","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-auth" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/auth.log" \
|
|
--line_match='{"message":"Server started on port '$port_auth'","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-auth-worker" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/auth-worker.log" \
|
|
--line_match='{"message":"Starting worker...","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-api-gateway" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/api-gateway.log" \
|
|
--line_match='{"message":"Server started on port '$port_api_gateway'","level":"info"}'
|
|
|
|
#=================================================
|
|
# SETUP FAIL2BAN
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring fail2ban..." --weight=1
|
|
|
|
# Create a dedicated fail2ban config
|
|
ynh_add_fail2ban_config --use_template
|
|
|
|
#=================================================
|
|
# SETUP SSOWAT
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring SSOwat..." --weight=3
|
|
|
|
# Everyone can access the app.
|
|
# The "main" permission is automatically created before the install script.
|
|
ynh_permission_update --permission="main" --add="visitors" --show_tile="false"
|
|
ynh_permission_create --permission="help" --url="/help/" --allowed="all_users" --show_tile="true" --label="Help"
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
ynh_script_progression --message="Reloading nginx web server..." --weight=1
|
|
|
|
ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
#=================================================
|
|
# 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"
|
|
|
|
echo -e "\
|
|
Standard Notes - Syncing Server was successfully installed.\n\
|
|
Please configure the Standard Notes web app or mobile app to use this syncing server: https://$domain$path_url\n\
|
|
\n\
|
|
Standard Notes extensions are hosted with this package.\n\
|
|
An Help page for setting-up the Standard Notes Server and Extensions have been created under: https://$domain$path_url/help\n\
|
|
The Help page is accessible via the Yunohost Portal.\n\
|
|
You can deactivate the Help page under: $admin_panel\
|
|
" > message
|
|
|
|
ynh_send_readme_to_admin --app_message="message" --type='install'
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|