2020-01-18 10:28:47 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC START
|
|
|
|
#=================================================
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
source _common.sh
|
2020-12-20 00:27:44 +01:00
|
|
|
source ynh_install_ruby
|
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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
|
|
|
ynh_app_setting_set --app=$app --key=is_public --value=$is_public
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STANDARD MODIFICATIONS
|
|
|
|
#=================================================
|
|
|
|
# FIND AND OPEN A PORT
|
|
|
|
#=================================================
|
2020-12-20 01:26:25 +01:00
|
|
|
ynh_script_progression --message="Configuring firewall..." --weight=1
|
2020-01-18 10:28:47 +01:00
|
|
|
|
|
|
|
# Find an available port
|
2020-12-24 12:11:42 +01:00
|
|
|
port=$(ynh_find_port --port=3000)
|
2020-01-18 10:28:47 +01:00
|
|
|
# Open the port
|
2020-12-24 12:11:42 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=port --value=$port
|
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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
2020-12-24 12:11:42 +01:00
|
|
|
mkdir -p $final_path
|
2020-01-18 10:28:47 +01:00
|
|
|
ynh_setup_source --dest_dir="$final_path/live"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
2020-12-27 23:18:55 +01:00
|
|
|
ynh_add_nginx_config "port"
|
2020-01-18 10:28:47 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CREATE DEDICATED USER
|
|
|
|
#=================================================
|
2020-12-20 01:26:25 +01:00
|
|
|
ynh_script_progression --message="Configuring system user..." --weight=1
|
2020-01-18 10:28:47 +01:00
|
|
|
|
|
|
|
# Create a system user
|
|
|
|
ynh_system_user_create --username=$app --home_dir=$final_path
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SPECIFIC SETUP
|
|
|
|
#=================================================
|
2020-12-24 12:11:42 +01:00
|
|
|
# INSTALLING RUBY
|
2020-01-18 10:28:47 +01:00
|
|
|
#=================================================
|
2020-12-27 23:18:55 +01:00
|
|
|
ynh_script_progression --message="Installing Ruby...( This may take a while... )" --weight=100 #331
|
2020-01-18 10:28:47 +01:00
|
|
|
|
2020-12-20 00:27:44 +01:00
|
|
|
ynh_install_ruby --ruby_version=$RUBY_VERSION
|
|
|
|
/opt/rbenv/versions/$RUBY_VERSION/bin/gem update --system --no-document
|
2020-01-18 10:28:47 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
|
|
|
|
|
|
config_file="$final_path/live/.env"
|
2020-12-20 14:22:36 +01:00
|
|
|
cp -f ../conf/env.sample $config_file
|
2020-12-24 12:11:42 +01:00
|
|
|
ynh_replace_string --match_string="EXPOSED_PORT=3000" --replace_string="EXPOSED_PORT=$port" --target_file="$config_file"
|
2020-01-18 10:28:47 +01:00
|
|
|
secret_key=$(ynh_string_random --length=48 | base64)
|
|
|
|
ynh_replace_string --match_string="SECRET_KEY_BASE=changeme123" --replace_string="SECRET_KEY_BASE=$secret_key" --target_file="$config_file"
|
2020-12-20 00:27:44 +01:00
|
|
|
pseudo_key=$(ynh_string_random --length=48 | base64)
|
|
|
|
ynh_replace_string --match_string="PSEUDO_KEY_PARAMS_KEY=changeme456" --replace_string="PSEUDO_KEY_PARAMS_KEY=$pseudo_key" --target_file="$config_file"
|
2020-01-18 10:28:47 +01:00
|
|
|
ynh_replace_string --match_string="RAILS_ENV=development" --replace_string="RAILS_ENV=production" --target_file="$config_file"
|
|
|
|
ynh_replace_string --match_string="DB_DATABASE=standard_notes_db" --replace_string="DB_DATABASE=$db_name" --target_file="$config_file"
|
|
|
|
ynh_replace_string --match_string="DB_USERNAME=std_notes_user" --replace_string="DB_USERNAME=$db_user" --target_file="$config_file"
|
|
|
|
ynh_replace_string --match_string="DB_PASSWORD=changeme123" --replace_string="DB_PASSWORD=$db_pwd" --target_file="$config_file"
|
2020-12-27 23:18:55 +01:00
|
|
|
ynh_replace_string --match_string="RAILS_RELATIVE_URL_ROOT=/" --replace_string="RAILS_RELATIVE_URL_ROOT=$path_url" --target_file="$config_file"
|
2020-01-18 10:28:47 +01:00
|
|
|
|
|
|
|
#=================================================
|
2020-12-27 23:18:55 +01:00
|
|
|
# INSTALLING Standard Notes - Synicing Server
|
2020-01-18 10:28:47 +01:00
|
|
|
#=================================================
|
2020-12-20 01:26:25 +01:00
|
|
|
ynh_script_progression --message="Installing Standard Notes - Synicing Server..." --weight=93
|
2020-01-18 10:28:47 +01:00
|
|
|
|
|
|
|
chown -R "$app": "$final_path"
|
|
|
|
|
|
|
|
pushd "$final_path/live"
|
2020-12-27 23:18:55 +01:00
|
|
|
exec_as "$app" env PATH=$PATH /opt/rbenv/versions/$RUBY_VERSION/bin/bundle config set path 'vendor/bundle'
|
|
|
|
exec_as "$app" env PATH=$PATH /opt/rbenv/versions/$RUBY_VERSION/bin/bundle config set with 'development'
|
|
|
|
exec_as "$app" env PATH=$PATH /opt/rbenv/versions/$RUBY_VERSION/bin/bundle install
|
|
|
|
exec_as "$app" env PATH=$PATH RAILS_ENV=production /opt/rbenv/versions/$RUBY_VERSION/bin/bundle exec rails db:create db:migrate --quiet
|
2020-01-18 10:28:47 +01:00
|
|
|
popd
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
2020-12-27 23:18:55 +01:00
|
|
|
ynh_add_systemd_config --others_var="\
|
|
|
|
port \
|
|
|
|
RUBY_VERSION \
|
|
|
|
"
|
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
|
|
|
|
ynh_store_file_checksum --file="$config_file"
|
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
|
|
|
|
chown -R root: $final_path
|
|
|
|
chown $app: $final_path
|
2020-12-27 23:18:55 +01: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)
|
2020-12-20 14:22:36 +01:00
|
|
|
ynh_use_logrotate --logfile="$final_path/live/log/production.log"
|
|
|
|
ynh_use_logrotate --logfile="/var/log/$app/$app.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
|
|
|
|
2020-12-24 12:11:42 +01:00
|
|
|
yunohost service add $app --description "Standard Notes - Syncing Server" --log "/var/log/$app/$app.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-01-02 19:42:20 +01:00
|
|
|
touch "/var/log/$app/$app.log"
|
|
|
|
ynh_add_fail2ban_config --use_template --others_var="\
|
|
|
|
domain \
|
|
|
|
path_url \
|
|
|
|
"
|
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
|
2020-12-20 00:27:44 +01:00
|
|
|
# Make app public if necessary or protect it
|
2020-01-18 10:28:47 +01:00
|
|
|
if [ $is_public -eq 1 ]
|
|
|
|
then
|
2020-12-27 23:18:55 +01:00
|
|
|
# Create the visitors permission if needed
|
|
|
|
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
|
|
|
|
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
|
2021-01-24 22:04:27 +01:00
|
|
|
curl --silent --insecure --resolve $domain:443:127.0.0.1 https://$domain$path_url/ -o /dev/null --write-out "%{http_code}" --retry 10 --retry-delay 3
|
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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# END OF SCRIPT
|
|
|
|
#=================================================
|
|
|
|
|
2020-12-20 01:26:25 +01:00
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|