1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/peertube_ynh.git synced 2024-09-03 19:56:29 +02:00
peertube_ynh/scripts/install
2019-06-06 23:16:31 +02:00

269 lines
9.7 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source ynh_add_extra_apt_repos__3
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="/"
admin_email=$YNH_APP_ARG_EMAIL
admin_pass=$(ynh_string_random --length=24)
is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_print_info --message="Validating installation parameters..."
final_path=/var/www/$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_print_info --message="Storing installation settings..."
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=admin_email --value=$admin_email
ynh_app_setting_set --app=$app --key=admin_pass --value=$admin_pass
ynh_app_setting_set --app=$app --key=is_public --value=$is_public
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_print_info --message="Configuring firewall..."
# Find a free port
port=$(ynh_find_port --port=9000)
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_print_info --message="Installing dependencies..."
# Install nodejs
ynh_install_nodejs --nodejs_version=8
# Install dependencies
ynh_install_app_dependencies $pkg_dependencies
# Install ffmpeg from backports for Debian Jessie and from main for others
if [ "$(lsb_release --codename --short)" == "jessie" ]; then
ynh_install_extra_app_dependencies --repo="deb http://httpredir.debian.org/debian jessie-backports main" --package="ffmpeg"
else
ynh_add_app_dependencies --package="ffmpeg"
fi
# Install Yarn
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 A POSTGRESQL DATABASE
#=================================================
ynh_print_info --message="Creating a PostgreSQL database..."
db_name="peertube_${app}"
db_user=$app
db_pwd=$(ynh_string_random --length=30)
ynh_app_setting_set --app="$app" --key=psql_db --value="$db_name"
ynh_app_setting_set --app="$app" --key=psqlpwd --value="$db_pwd"
ynh_psql_test_if_first_run
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS unaccent;" --database=$db_name
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS pg_trgm;" --database=$db_name
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_print_info --message="Setting up source files..."
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_print_info --message="Configuring nginx web server..."
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_print_info --message="Configuring system user..."
# Create a system user
ynh_system_user_create --username=$app
#=================================================
# SPECIFIC SETUP
#=================================================
# CREATE THE DATA DIRECTORY
#=================================================
ynh_print_info --message="Create the data directory..."
# Define app's data directory
datadir="/home/yunohost.app/${app}/storage"
# Create app folders
mkdir -p "$datadir"
# Give permission to the datadir
chown -R "$app":"$app" "$datadir"
#=================================================
# MODIFY A CONFIG FILE
#=================================================
cp ../conf/production.yaml "$final_path/config/production.yaml"
ynh_replace_string --match_string="__domain__" --replace_string="$domain" --target_file="$final_path/config/production.yaml"
ynh_replace_string --match_string="__db_name__" --replace_string="$app" --target_file="$final_path/config/production.yaml"
ynh_replace_string --match_string="__app__" --replace_string="$app" --target_file="$final_path/config/production.yaml"
ynh_replace_string --match_string="__db_pwd__" --replace_string="$db_pwd" --target_file="$final_path/config/production.yaml"
ynh_replace_string --match_string="__email__" --replace_string="$admin_email" --target_file="$final_path/config/production.yaml"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$final_path/config/production.yaml"
#Create the admin settings file
touch "$final_path/config/local-production.json"
#=================================================
# FIX NGINX DOMAIN CONFIGURATION
#=================================================
ynh_replace_string --match_string="X-Frame-Options : SAMEORIGIN" --replace_string="X-Frame-Options : ALLOWALL" --target_file="/etc/nginx/conf.d/$domain.conf"
#=================================================
# STORE THE CHECKSUM OF THE CONFIG FILE
#=================================================
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$final_path/config/production.yaml"
ynh_store_file_checksum --file="$final_path/config/local-production.json"
#=================================================
# BUILD YARN DEPENDENCIES
#=================================================
pushd "$final_path"
yarn install --production --pure-lockfile
popd
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_replace_string --match_string="__ENV_PATH__" --replace_string="$PATH" --target_file="../conf/systemd.service"
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions to app files
chown -R "$app":"$app" $final_path
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_print_info --message="Configuring log rotation..."
# Use logrotate to manage application logfile(s)
ynh_use_logrotate --logfile="/home/yunohost.app/${app}/storage/logs/peertube.log"
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
# if using yunohost version 3.2 or more in the 'manifest.json', a description can be added
yunohost service add $app --description "$app daemon for Peertube" --log "/home/yunohost.app/${app}/storage/logs/peertube.log"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_print_info --message="Starting a systemd service..."
ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" --line_match="Server listening on localhost"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_print_info --message="Configuring SSOwat..."
# Make app public if necessary
if [ $is_public -eq 1 ]
then
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_print_info --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# CHANGE PEERTUBE ADMIN PASSWORD AFTER INITIAL GEN
#=================================================
# we need to wait for the service to init peertube's database
pushd "$final_path"
echo $admin_pass | NODE_CONFIG_DIR="$final_path/config" NODE_ENV=production npm run reset-password -- -u root
popd
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
message=" $app was successfully installed :)
Please open your $app domain: https://$domain$path_url
The admin username is: root
The admin password is: $admin_pass
If you are facing any problem or want to improve this app, please open a new issue here: https://github.com/YunoHost-Apps/peertube_ynh"
ynh_send_readme_to_admin --app_message="$message"
#=================================================
# END OF SCRIPT
#=================================================
ynh_print_info --message="Installation of $app completed"