1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/codimd_ynh.git synced 2024-09-03 18:16:32 +02:00
codimd_ynh/scripts/install
2020-03-09 15:20:44 +01:00

178 lines
6.1 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source ynh_add_extra_apt_repos__3
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
# 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="/"
is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..."
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"
# Register (book) web path
ynh_webpath_register "$app" "$domain" $path_url
port=$(ynh_find_port 3000)
db_user=$app
db_name=$app
db_pass=$(ynh_string_random 20)
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..."
ynh_app_setting_set "$app" domain "$domain"
ynh_app_setting_set "$app" path $path_url
ynh_app_setting_set "$app" is_public "$is_public"
ynh_app_setting_set "$app" port "$port"
ynh_app_setting_set "$app" db_pass "$db_pass"
ynh_app_setting_set "$app" final_path "$final_path"
#==============================================
# INSTALL POSTGRES
#==============================================
ynh_script_progression --message="Installing dependencies..."
ynh_install_app_dependencies postgresql apt-transport-https
# 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"
ynh_install_nodejs 8
#==============================================
# CREATE DB
#==============================================
ynh_script_progression --message="Creating a database..."
ynh_psql_test_if_first_run
ynh_psql_setup_db --db_user="$db_user" --db_name="$db_name" --db_pwd="$db_pass"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..."
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source "$final_path"
#=================================================
# MODIFY A CONFIG FILE
#=================================================
# Main config File
ynh_replace_string "__PORT__" "$port" "../conf/config.json.example"
ynh_replace_string "__DOMAIN__" "$domain" "../conf/config.json.example"
ynh_replace_string "__PATH__" "${path_url:1}" "../conf/config.json.example"
ynh_replace_string "__DB_USER__" "$db_user" "../conf/config.json.example"
ynh_replace_string "__DB_NAME__" "$db_name" "../conf/config.json.example"
ynh_replace_string "__DB_PASS__" "$db_pass" "../conf/config.json.example"
cp ../conf/config.json.example "$final_path"/config.json
ynh_store_file_checksum "$final_path/config.json"
# DB Config File
ynh_replace_string "__DB_USER__" "$db_user" "../conf/.sequelizerc.example"
ynh_replace_string "__DB_NAME__" "$db_name" "../conf/.sequelizerc.example"
ynh_replace_string "__DB_PASS__" "$db_pass" "../conf/.sequelizerc.example"
cp ../conf/.sequelizerc.example "$final_path"/.sequelizerc
#==============================================
# INSTALL CODIMD
#==============================================
ynh_script_progression --message="Building application... (this may take some time and resources!)"
pushd "$final_path" || exit
./bin/setup
yarn run build
#node_modules/.bin/sequelize db:migrate
popd || exit
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring nginx web server..."
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..."
# Create a system user
ynh_system_user_create "$app"
chown -R "$app":"$app" "$final_path"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..."
ynh_replace_string "__APP__" "$app" "../conf/systemd.service"
ynh_replace_string "__FINALPATH__" "$final_path" "../conf/systemd.service"
ynh_replace_string "__ENV_PATH__" "$PATH" "../conf/systemd.service"
ynh_replace_string "__NODE__" "$nodejs_path" "../conf/systemd.service"
ynh_add_systemd_config
yunohost service add "$app" --description "CodiMD daemon"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting $app..."
ynh_systemd_action --service_name="$app" --action="start"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring SSOwat..."
# If app is public, add url to SSOWat conf as skipped_uris
if [ "$is_public" -eq 1 ]; then
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set "$app" unprotected_uris "/"
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last