#!/bin/bash #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source /usr/share/yunohost/helpers #================================================= # INITIALIZE AND STORE SETTINGS #================================================= admin_mail=$(ynh_user_get_info --username="$admin" --key=mail) # Initialize config_panel settings :< max_toot_chars="500" max_pinned_toots="5" max_bio_chars="500" max_profile_fields="4" max_display_name_chars="30" max_poll_options="5" max_poll_option_chars="100" max_image_size="8388608" max_video_size="41943040" max_emoji_size="51200" max_remote_emoji_size="204800" ynh_app_setting_set --key="max_toot_chars" --value="$max_toot_chars" ynh_app_setting_set --key="max_pinned_toots" --value="$max_pinned_toots" ynh_app_setting_set --key="max_bio_chars" --value="$max_bio_chars" ynh_app_setting_set --key="max_profile_fields" --value="$max_profile_fields" ynh_app_setting_set --key="max_display_name_chars" --value="$max_display_name_chars" ynh_app_setting_set --key="max_poll_options" --value="$max_poll_options" ynh_app_setting_set --key="max_poll_option_chars" --value="$max_poll_option_chars" ynh_app_setting_set --key="max_image_size" --value="$max_image_size" ynh_app_setting_set --key="max_video_size" --value="$max_video_size" ynh_app_setting_set --key="max_emoji_size" --value="$max_emoji_size" ynh_app_setting_set --key="max_remote_emoji_size" --value="$max_remote_emoji_size" # Set `service` settings to support `yunohost app shell` command ynh_app_setting_set --key=service --value="$app-web.service" redis_namespace=${app}_production ynh_app_setting_set --key=redis_namespace --value="$redis_namespace" secret_key_base=$(ynh_string_random --length=128) ynh_app_setting_set --key=secret_key_base --value="$secret_key_base" otp_secret=$(ynh_string_random --length=128) ynh_app_setting_set --key=otp_secret --value="$otp_secret" active_record_encryption_primary_key=$(ynh_string_random --length=32) ynh_app_setting_set --key=active_record_encryption_primary_key --value="$active_record_encryption_primary_key" active_record_encryption_deterministic_key=$(ynh_string_random --length=32) ynh_app_setting_set --key=active_record_encryption_deterministic_key --value="$active_record_encryption_deterministic_key" active_record_encryption_key_derivation_salt=$(ynh_string_random --length=32) ynh_app_setting_set --key=active_record_encryption_key_derivation_salt --value="$active_record_encryption_key_derivation_salt" #================================================= # INSTALL DEPENDENCIES #================================================= ynh_script_progression "Installing Ruby..." ynh_ruby_install 2>&1 ynh_script_progression "Installing NodeJS..." ynh_nodejs_install 2>&1 #================================================= # ADD SWAP IF NEEDED #================================================= ynh_script_progression "Adding swap if needed..." total_memory=$(ynh_get_ram --total) swap_needed=0 if [ $total_memory -lt $memory_needed ]; then # Need a minimum of 2.5Go of memory swap_needed=$(($memory_needed - $total_memory)) fi ynh_script_progression "Adding $swap_needed Mo to swap..." ynh_add_swap --size=$swap_needed #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= ynh_script_progression "Setting up source files..." # Download, check integrity, uncompress and patch the source from app.src ynh_setup_source --dest_dir="$install_dir/live" #REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown -R "$app:www-data" "$install_dir" #================================================= # ADD A CONFIGURATION #================================================= ynh_script_progression "Adding $app's configuration..." config="$install_dir/live/.env.production" language="$(echo $language | head -c 2)" # We need rake to build vapid keys, we generate them later once the app is installed vapid_private_key="" vapid_public_key="" ynh_config_add --template=".env.production.sample" --destination="$config" ynh_replace --match="registrations_mode: 'open'" --replace="registrations_mode: 'none'" --file="$install_dir/live/config/settings.yml" ynh_replace --match="min_invite_role: 'admin'" --replace="min_invite_role: 'none'" --file="$install_dir/live/config/settings.yml" ynh_store_file_checksum "$install_dir/live/config/settings.yml" #================================================= # BUILD APP #================================================= ynh_script_progression "Building app..." pushd "$install_dir/live" # Building ruby packages gem update --system gem install bundler --no-document ynh_exec_as_app ruby_load_path" $ld_preload bin/bundle config deployment 'true' ynh_exec_as_app ruby_load_path" $ld_preload bin/bundle config without 'development test' ynh_exec_as_app ruby_load_path" $ld_preload bin/bundle config set force_ruby_platform true ynh_exec_as_app ruby_load_path" $ld_preload bin/bundle install -j$(getconf _NPROCESSORS_ONLN) # Building assets corepack enable # This export might be removed in yunohost 12 yarn install echo "SAFETY_ASSURED=1">> "$config" ynh_exec_as_app RAILS_ENV=production ruby_load_path" $ld_preload bin/bundle exec rails db:migrate --quiet 2>&1 ynh_exec_as_app RAILS_ENV=production ruby_load_path" $ld_preload bin/bundle exec rails assets:precompile --quiet 2>&1 # Generate vapid keys ynh_exec_as_app RAILS_ENV=production ruby_load_path" $ld_preload bin/bundle exec rake mastodon:webpush:generate_vapid_key > key.txt 2>&1 # Create the first admin user ynh_exec_as_app RAILS_ENV=production ruby_load_path" $ld_preload bin/tootctl accounts create "$admin" --email="$admin_mail" --confirmed --role=Owner > /dev/null 2>&1 popd # Re-generate config with vapid keys vapid_private_key=$(grep -oP "VAPID_PRIVATE_KEY=\K.+" "$install_dir/live/key.txt") ynh_app_setting_set --key=vapid_private_key --value="$vapid_private_key" vapid_public_key=$(grep -oP "VAPID_PUBLIC_KEY=\K.+" "$install_dir/live/key.txt") ynh_app_setting_set --key=vapid_public_key --value="$vapid_public_key" ynh_safe_rm "$install_dir/live/key.txt" ynh_delete_file_checksum "$config" ynh_config_add --template=".env.production.sample" --destination="$config" #REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod 400 "$config" #REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown $app:$app "$config" #================================================= # SYSTEM CONFIGURATION #================================================= ynh_script_progression "Adding system configurations related to $app..." # Create a dedicated NGINX config using the conf/nginx.conf template ynh_config_add_nginx # Create a dedicated systemd config ynh_config_add_systemd --service="$app-web" --template="glitchsoc-web.service" yunohost service add "$app-web" --description="$app web service" ynh_config_add_systemd --service="$app-sidekiq" --template="glitchsoc-sidekiq.service" yunohost service add "$app-sidekiq" --description="$app sidekiq service" ynh_config_add_systemd --service="$app-streaming" --template="glitchsoc-streaming.service" yunohost service add "$app-streaming" --description="$app streaming service" # Create a cron file ynh_config_add --template="cron" --destination="/etc/cron.d/$app" # Use logrotate to manage application logfile(s) ynh_config_add_logrotate #================================================= # START SYSTEMD SERVICE #================================================= ynh_script_progression "Starting all systemd services..." ynh_systemctl --service="${app}-web" --action="start" --log_path="/var/log/$app/$app-web.log" --wait_until="Listening on" ynh_systemctl --service="${app}-sidekiq" --action="start" --log_path="/var/log/$app/$app-sidekiq.log" --wait_until="Schedules Loaded" ynh_systemctl --service="${app}-streaming" --action="start" --log_path="/var/log/$app/$app-streaming.log" --wait_until="Streaming API now listening" #================================================= # END OF SCRIPT #================================================= ynh_script_progression "Installation of $app completed"