2020-01-18 10:28:47 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
2021-05-01 21:37:41 +02:00
source ynh_install_ruby__2
2021-03-06 21:39:49 +01:00
source ynh_redis
2021-05-02 11:57:39 +02:00
source ynh_send_readme_to_admin__2
2020-01-18 10:28:47 +01:00
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
2020-12-27 23:18:55 +01:00
ynh_clean_check_starting
2020-01-18 10:28:47 +01:00
}
# 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
2020-12-24 12:11:42 +01:00
path_url=$YNH_APP_ARG_PATH
2020-01-18 10:28:47 +01:00
is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME
2021-02-06 02:01:06 +01:00
access_domain=$YNH_APP_ARG_ACCESS_DOMAIN
if [ -z "$access_domain" ]
2021-01-08 19:44:37 +01:00
then
access_domain=$domain
fi
2021-05-04 20:37:10 +02:00
access_domain=$(echo $access_domain | sed "s@;@ @g")
2021-02-06 20:01:03 +01:00
mail="$app@$domain"
2020-01-18 10:28:47 +01:00
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2020-12-20 14:22:36 +01:00
final_path=/opt/yunohost/$app
2020-01-18 10:28:47 +01:00
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
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Storing installation settings..." --weight=3
2020-01-18 10:28:47 +01:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
2021-01-08 19:44:37 +01:00
ynh_app_setting_set --app=$app --key=access_domain --value=$access_domain
2021-02-06 01:44:31 +01:00
ynh_app_setting_set --app=$app --key=mail --value=$mail
2021-03-06 21:39:49 +01:00
redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app=$app --key=redis_db --value="$redis_db"
2020-01-18 10:28:47 +01:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
2021-01-09 22:30:32 +01:00
ynh_script_progression --message="Finding an available port..." --weight=1
2020-01-18 10:28:47 +01:00
# Find an available port
2021-07-04 21:45:43 +02:00
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)))
2021-07-04 22:46:56 +02:00
port_api_gateway=$(ynh_find_port --port=$((port_auth_worker+1)))
2020-01-18 10:28:47 +01:00
# Open the port
2021-07-04 21:45:43 +02:00
ynh_app_setting_set --app=$app --key=port --value=$port_syncing_server_js
ynh_app_setting_set --app=$app --key=port --value=$port_syncing_server_js_worker
ynh_app_setting_set --app=$app --key=port --value=$port_auth
ynh_app_setting_set --app=$app --key=port --value=$port_auth_worker
ynh_app_setting_set --app=$app --key=port --value=$port_api_gateway
2020-01-18 10:28:47 +01:00
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Installing dependencies..." --weight=17
2020-01-18 10:28:47 +01:00
ynh_install_app_dependencies $pkg_dependencies
2021-07-04 21:45:43 +02:00
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"
2020-01-18 10:28:47 +01:00
2021-07-05 19:08:00 +02:00
#=================================================
# 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
2020-01-18 10:28:47 +01:00
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Creating a MySQL database..." --weight=2
2020-01-18 10:28:47 +01:00
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
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Setting up source files..." --weight=2
2020-01-18 10:28:47 +01:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
2021-07-04 21:45:43 +02:00
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"
2020-01-18 10:28:47 +01:00
2021-07-05 19:08:00 +02:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path"
2020-01-18 10:28:47 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Configuring nginx web server..." --weight=3
2020-01-18 10:28:47 +01:00
# Create a dedicated nginx config
2021-05-09 14:50:38 +02:00
ynh_add_nginx_config
2020-01-18 10:28:47 +01:00
#=================================================
# SPECIFIC SETUP
#=================================================
#=================================================
# MODIFY A CONFIG FILE
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Modifying a config file..." --weight=2
2020-01-18 10:28:47 +01:00
2021-07-04 22:46:56 +02:00
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"
2021-07-04 21:45:43 +02:00
2021-07-05 19:08:00 +02:00
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)
2021-07-04 21:45:43 +02:00
pseudo_key_params_key=$(ynh_string_random --length=48 | base64)
2021-07-05 20:50:52 +02:00
encryption_server_key=$(hexdump -n 32 -e '4/4 "%08X" 1 "\n"' /dev/random) # 32bytes hex key is required
2021-07-04 21:45:43 +02:00
2021-07-05 19:08:00 +02:00
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
2021-07-04 21:45:43 +02:00
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_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_add_config --template="env_syncing-server-js.env.sample" --destination="$config_syncing_server_js"
2021-07-04 22:46:56 +02:00
ynh_add_config --template="env_syncing-server-js-worker.env.sample" --destination="$config_syncing_server_js_worker"
2021-07-04 21:45:43 +02:00
ynh_add_config --template="env_auth.env.sample" --destination="$config_auth"
2021-07-04 22:46:56 +02:00
ynh_add_config --template="env_auth-worker.env.sample" --destination="$config_auth_worker"
2021-07-04 21:45:43 +02:00
ynh_add_config --template="env_api-gateway.env.sample" --destination="$config_api_gateway"
#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"
2020-01-18 10:28:47 +01:00
#=================================================
2021-05-01 21:48:58 +02:00
# INSTALLING Standard Notes - Syncing Server
2020-01-18 10:28:47 +01:00
#=================================================
2021-05-01 21:48:58 +02:00
ynh_script_progression --message="Installing Standard Notes - Syncing Server..." --weight=93
2020-01-18 10:28:47 +01:00
2021-07-04 21:45:43 +02:00
ynh_use_nodejs
#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)
pushd "$final_path/live/syncing-server-js"
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn install --pure-lockfile
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn build
popd
pushd "$final_path/live/syncing-server-js-worker"
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn install --pure-lockfile
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn build
popd
pushd "$final_path/live/auth"
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn install --pure-lockfile
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn build
popd
pushd "$final_path/live/auth-worker"
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn install --pure-lockfile
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn build
popd
pushd "$final_path/live/api-gateway"
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn install --pure-lockfile
ynh_exec_as $app env PATH=$ynh_node_load_PATH yarn build
2020-01-18 10:28:47 +01:00
popd
2021-01-08 19:44:37 +01:00
#=================================================
# INSTALLING Standard Notes - Extensions
#=================================================
2021-07-04 21:45:43 +02:00
#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
#
2020-01-18 10:28:47 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Configuring a systemd service..." --weight=4
2020-01-18 10:28:47 +01:00
# Create a dedicated systemd config
2021-07-05 20:50:52 +02:00
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"
2020-01-18 10:28:47 +01:00
#=================================================
# STORE THE CONFIG FILE CHECKSUM
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Storing the config file checksum..." --weight=1
2020-01-18 10:28:47 +01:00
# Calculate and store the config file checksum into the app settings
2021-07-05 19:08:00 +02:00
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"
2020-12-20 14:22:36 +01:00
ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.d/$app.conf"
2020-01-18 10:28:47 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2020-12-20 14:22:36 +01:00
#=================================================
2020-01-18 10:28:47 +01:00
# SECURE FILES AND DIRECTORIES
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Securing files and directories..." --weight=1
2020-01-18 10:28:47 +01:00
# Set permissions to app files
2021-05-01 22:04:34 +02:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
2021-05-01 23:10:36 +02:00
chown -R $app: "$final_path"
2021-05-01 22:04:34 +02:00
2021-07-04 21:45:43 +02:00
#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/"
2020-12-20 00:27:44 +01:00
2020-12-27 23:18:55 +01:00
mkdir -p "/var/log/$app"
chown -R $app: "/var/log/$app"
2020-01-18 10:28:47 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Configuring log rotation..." --weight=1
2020-01-18 10:28:47 +01:00
# Use logrotate to manage application logfile(s)
2021-07-04 21:45:43 +02:00
#ynh_use_logrotate --logfile="$final_path/live/log/production.log"
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"
2020-01-18 10:28:47 +01:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Integrate $app service in Yunohost..." --weight=1
2020-01-18 10:28:47 +01:00
2021-07-04 21:45:43 +02:00
yunohost service add $app --description "Standard Notes - Syncing Server" --log "/var/log/$app/syncing-server-js.log"
yunohost service add $app --description "Standard Notes - Syncing Server - Worker" --log "/var/log/$app/syncing-server-js-worker.log"
yunohost service add $app --description "Standard Notes - Auth" --log "/var/log/$app/auth.log"
yunohost service add $app --description "Standard Notes - Auth - Worker" --log "/var/log/$app/auth-worker.log"
yunohost service add $app --description "Standard Notes - API Gateway" --log "/var/log/$app/api-gateway.log"
2020-01-18 10:28:47 +01:00
#=================================================
# SETUP FAIL2BAN
#=================================================
2021-01-03 15:45:28 +01:00
ynh_script_progression --message="Configuring fail2ban..." --weight=1
2020-01-18 10:28:47 +01:00
# Create a dedicated fail2ban config
2021-05-09 14:50:38 +02:00
ynh_add_fail2ban_config --use_template
2020-01-18 10:28:47 +01:00
#=================================================
# SETUP SSOWAT
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Configuring SSOwat..." --weight=3
2021-03-14 18:51:33 +01:00
# Make app public if necessary
2020-01-18 10:28:47 +01:00
if [ $is_public -eq 1 ]
then
2021-03-14 18:51:33 +01:00
# 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"
2020-01-18 10:28:47 +01:00
fi
2020-12-20 14:22:36 +01:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
# Start a systemd service
2021-07-05 19:08:00 +02:00
ynh_systemd_action --service_name="$app-syncing-server-js" --action="start" --log_path="/var/log/$app/syncing-server-js.log"
ynh_systemd_action --service_name="$app-syncing-server-js-worker" --action="start" --log_path="/var/log/$app/syncing-server-js-worker.log"
2021-07-04 21:45:43 +02:00
ynh_systemd_action --service_name="$app-auth" --action="start" --log_path="/var/log/$app/auth.log"
ynh_systemd_action --service_name="$app-auth-worker" --action="start" --log_path="/var/log/$app/auth-worker.log"
ynh_systemd_action --service_name="$app-api-gateway" --action="start" --log_path="/var/log/$app/api-gateway.log"
2020-12-20 14:22:36 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# RELOAD NGINX
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Reloading nginx web server..." --weight=1
2020-01-18 10:28:47 +01:00
ynh_systemd_action --service_name=nginx --action=reload
2021-01-15 19:58:23 +01:00
#=================================================
2021-01-25 13:08:34 +01:00
# WAITING FOR SERVICE
#=================================================
2021-07-04 21:45:43 +02:00
ynh_script_progression --message="Waiting for services..." --weight=1
2021-01-25 13:08:34 +01:00
is_service_ready
#=================================================
2021-01-15 19:58:23 +01:00
# 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"
2021-01-22 23:47:08 +01:00
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__"
2021-01-15 19:58:23 +01:00
2021-02-10 22:14:53 +01:00
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\
2021-02-10 22:53:12 +01:00
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.\
"
2021-01-15 19:58:23 +01:00
2021-02-10 22:53:12 +01:00
echo -e "\
Standard Notes - Syncing Server was successfully installed.\n\
2021-07-05 19:08:00 +02:00
Please configure the Standard Notes web app or mobile app to use this syncing server: https://$domain$path_url/\n\
2021-02-10 22:53:12 +01:00
$config_panel\n\n\
$admin_mail_info\
" > message
2021-01-15 19:58:23 +01:00
2021-02-10 22:53:12 +01:00
ynh_send_readme_to_admin --app_message="message" --type='install'
2021-01-15 19:58:23 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# END OF SCRIPT
#=================================================
2020-12-20 01:26:25 +01:00
ynh_script_progression --message="Installation of $app completed" --last