1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/question2answer_ynh.git synced 2024-09-03 20:16:07 +02:00
question2answer_ynh/scripts/install

279 lines
11 KiB
Text
Raw Normal View History

2014-10-20 18:55:53 +02:00
#!/bin/bash
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# GENERIC START
2017-06-02 18:23:51 +02:00
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2017-06-05 13:11:48 +02:00
source _common.sh
2017-06-02 18:23:51 +02:00
source /usr/share/yunohost/helpers
#=================================================
2017-06-17 17:49:26 +02:00
# MANAGE SCRIPT FAILURE
2017-06-02 18:23:51 +02:00
#=================================================
2018-06-28 22:05:35 +02:00
ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
true
}
2017-06-17 17:49:26 +02:00
# Exit if an error occurs during the execution of the script
2017-06-02 18:23:51 +02:00
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN
email=`ynh_user_get_info "$admin" 'mail'`
2017-06-02 18:23:51 +02:00
is_public=$YNH_APP_ARG_IS_PUBLIC
language=$YNH_APP_ARG_LANGUAGE
2018-06-28 22:05:35 +02:00
password=$YNH_APP_ARG_PASSWORD
q2a_name=${YNH_APP_ARG_Q2A_NAME// /%20}
2018-06-28 22:05:35 +02:00
### If it's a multi-instance app, meaning it can be installed several times independently
### The id of the app as stated in the manifest is available as $YNH_APP_ID
2020-08-02 16:00:17 +02:00
### The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2"...)
2018-06-28 22:05:35 +02:00
### The app instance name is available as $YNH_APP_INSTANCE_NAME
### - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample
### - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2
2020-08-02 16:00:17 +02:00
### - ynhexample__{N} for the subsequent installations, with N=3,4...
2018-07-01 09:57:16 +02:00
### The app instance name is probably what interests you most, since this is
2018-06-28 22:05:35 +02:00
### guaranteed to be unique. This is a good unique identifier to define installation path,
2020-08-02 16:00:17 +02:00
### db names...
app=$YNH_APP_INSTANCE_NAME
2014-10-20 18:55:53 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
2017-06-02 18:23:51 +02:00
#=================================================
2019-04-18 19:58:47 +02:00
### About --weight and --time
### ynh_script_progression will show to your final users the progression of each scripts.
### In order to do that, --weight will represent the relative time of execution compared to the other steps in the script.
### --time is a packager option, it will show you the execution time since the previous call.
### This option should be removed before releasing your app.
### Use the execution time, given by --time, to estimate the weight of a step.
### A common way to do it is to set a weight equal to the execution time in second +1.
### The execution time is given for the duration since the previous call. So the weight should be applied to this previous call.
ynh_script_progression --message="Validating installation parameters..." --weight=2
2014-10-20 18:55:53 +02:00
2020-08-02 16:00:17 +02:00
### If the app uses NGINX as web server (written in HTML/PHP in most cases), the final path should be "/var/www/$app".
### If the app provides an internal web server (or uses another application server such as uWSGI), the final path should be "/opt/yunohost/$app"
2017-08-28 23:55:51 +02:00
final_path=/var/www/$app
2019-05-07 12:03:25 +02:00
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
2017-08-28 23:55:51 +02:00
2017-06-17 17:49:26 +02:00
# Register (book) web path
2019-04-16 00:32:39 +02:00
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
2014-10-20 18:55:53 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=3
2019-04-16 00:32:39 +02: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=admin --value=$admin
ynh_app_setting_set --app=$app --key=language --value=$language
2017-06-02 18:23:51 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
2017-06-17 17:49:26 +02:00
# CREATE A MYSQL DATABASE
2017-06-02 18:23:51 +02:00
#=================================================
ynh_script_progression --message="Creating a MySQL database..." --weight=3
2018-06-28 22:05:35 +02:00
### Use these lines if you need a database for the application.
### `ynh_mysql_setup_db` will create a database, an associated user and a ramdom password.
### The password will be stored as 'mysqlpwd' into the app settings,
### and will be available as $db_pwd
### If you're not using these lines:
### - Remove the section "BACKUP THE MYSQL DATABASE" in the backup script
### - Remove also the section "REMOVE THE MYSQL DATABASE" in the remove script
### - As well as the section "RESTORE THE MYSQL DATABASE" in the restore script
2017-06-02 18:23:51 +02:00
2019-05-07 12:03:25 +02:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
2019-05-02 21:04:03 +02:00
db_user=$db_name
2019-04-16 00:32:39 +02:00
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
2019-05-02 21:04:03 +02:00
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
2017-06-02 18:23:51 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=3
2017-06-02 18:23:51 +02:00
2018-06-28 22:05:35 +02:00
### `ynh_setup_source` is used to install an app from a zip or tar.gz file,
2018-07-01 09:57:16 +02:00
### downloaded from an upstream source, like a git repository.
2018-06-28 22:05:35 +02:00
### `ynh_setup_source` use the file conf/app.src
2019-04-16 00:32:39 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2017-06-02 18:23:51 +02:00
# Download, check integrity, uncompress and patch the source from app.src
2019-04-16 00:32:39 +02:00
ynh_setup_source --dest_dir="$final_path"
2017-06-02 18:23:51 +02:00
2021-03-25 17:37:29 +01:00
#=================================================
# DOWNLOAD AND UNPACK LDAP PLUGIN
#=================================================
ynh_script_progression --message="Setting up LDAP plugin..." --weight=3
wget -O qa-ldap-login.zip https://github.com/zakkak/qa-ldap-login/archive/$ldap_login_commit.zip 2>&1
unzip qa-ldap-login.zip -d $final_path/qa-plugin
mv $final_path/qa-plugin/qa-ldap-login-$ldap_login_commit $final_path/qa-plugin/qa-ldap-login
ynh_replace_string\
--match_string="require_once QA_INCLUDE_DIR . 'db/selects.php';"\
--replace_string="require_once QA_INCLUDE_DIR . 'db/selects.php';\r\n require_once QA_INCLUDE_DIR . '../qa-plugin/qa-ldap-login/qa-ldap-process.php';"\
--target_file="$final_path/qa-include/pages/login.php"
2017-06-02 18:23:51 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..." --weight=14
2017-06-02 18:23:51 +02:00
2018-06-28 22:05:35 +02:00
### `ynh_add_nginx_config` will use the file conf/nginx.conf
2020-08-02 15:41:28 +02:00
# Create a dedicated NGINX config
ynh_add_nginx_config
2017-06-02 18:23:51 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=5
2017-06-02 18:23:51 +02:00
# Create a system user
2019-04-16 00:32:39 +02:00
ynh_system_user_create --username=$app
2017-06-02 18:23:51 +02:00
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring PHP-FPM..." --weight=1
2017-06-02 18:23:51 +02:00
### `ynh_add_fpm_config` will use the files conf/php-fpm.conf
2018-06-28 22:05:35 +02:00
2020-08-02 15:41:28 +02:00
# Create a dedicated PHP-FPM config
2017-08-28 23:55:51 +02:00
ynh_add_fpm_config
2017-06-02 18:23:51 +02:00
#=================================================
# DOWNLOAD TRANSLATIONS
2017-06-02 18:23:51 +02:00
#=================================================
ynh_script_progression --message="Installing Translations..." --weight=6
### French
if [ $language == "fr" ]; then
ynh_print_OFF
git clone https://github.com/mrflos/q2a-lang-fr $final_path/qa-lang/fr
ynh_print_ON
fi
2017-06-02 18:23:51 +02:00
#=================================================
# MODIFY A CONFIG FILE
2017-06-02 18:23:51 +02:00
#=================================================
mv $final_path/qa-config-example.php $final_path/qa-config.php
mv $final_path/.htaccess-example $final_path/.htaccess
2018-06-28 22:05:35 +02:00
ynh_replace_string --match_string="your-mysql-username" --replace_string=$db_user --target_file="$final_path/qa-config.php"
ynh_replace_string --match_string="your-mysql-password" --replace_string=$db_pwd --target_file="$final_path/qa-config.php"
ynh_replace_string --match_string="your-mysql-db-name" --replace_string=$db_name --target_file="$final_path/qa-config.php"
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# SETUP APPLICATION WITH CURL
2017-06-02 18:23:51 +02:00
#=================================================
2018-07-01 09:57:16 +02:00
### Use these lines only if the app installation needs to be finalized through
### web forms. We generally don't want to ask the final user,
### so we're going to use curl to automatically fill the fields and submit the
### forms.
2018-06-28 22:05:35 +02:00
2017-06-02 18:23:51 +02:00
# Set right permissions for curl install
chown -R $app: $final_path
2017-06-02 18:23:51 +02:00
2017-06-17 17:49:26 +02:00
# Set the app as temporarily public for curl call
ynh_script_progression --message="Configuring SSOwat..." --weight=4
2021-01-06 13:31:32 +01:00
# Making the app public for curl
ynh_permission_update --permission="main" --add="visitors"
2017-06-02 18:23:51 +02:00
# Reload SSOwat config
yunohost app ssowatconf
2017-06-02 18:23:51 +02:00
2020-08-02 16:00:17 +02:00
# Reload NGINX
2019-04-18 19:58:47 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2017-06-02 18:23:51 +02:00
# Installation with curl
ynh_script_progression --message="Finalizing installation..." --weight=4
ynh_local_curl "/index.php?qa=install" "create=Set+up+the+Database+including+User+Management"
ynh_local_curl "/index.php?qa=install" "handle=$admin" "password=$password" "email=$email" "super=Set+up+the+Super+Administrator"
ynh_local_curl_csrf "/index.php?qa=admin&qa_1=general"\
"option_site_title=$q2a_name"\
"option_site_url=https%3A%2F%2F$domain$path_url"\
"option_neat_urls=0"\
"option_site_language=$language"\
"option_site_theme=SnowFlat"\
"option_site_theme_mobile=SnowFlat"\
"option_site_text_direction=ltr"\
"option_tags_or_categories=tc"\
"dosaveoptions=1"\
"has_js=0"
2017-06-02 18:23:51 +02:00
2018-06-28 22:05:35 +02:00
# Remove the public access
2021-01-06 13:31:32 +01:00
ynh_permission_update --permission="main" --remove="visitors"
2018-06-28 22:05:35 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
2018-07-01 09:57:16 +02:00
# STORE THE CONFIG FILE CHECKSUM
2017-06-02 18:23:51 +02:00
#=================================================
2018-06-28 22:05:35 +02:00
### `ynh_store_file_checksum` is used to store the checksum of a file.
### That way, during the upgrade script, by using `ynh_backup_if_checksum_is_different`,
### you can make a backup of this file before modifying it again if the admin had modified it.
2017-06-02 18:23:51 +02:00
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$final_path/qa-config.php"
ynh_store_file_checksum --file="$final_path/.htaccess"
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# GENERIC FINALIZATION
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# SECURE FILES AND DIRECTORIES
2017-06-02 18:23:51 +02:00
#=================================================
2018-07-01 09:57:16 +02:00
### For security reason, any app should set the permissions to root: before anything else.
### Then, if write authorization is needed, any access should be given only to directories
### that really need such authorization.
2018-06-28 22:05:35 +02:00
2017-06-02 18:23:51 +02:00
# Set permissions to app files
chown -R root: $final_path
2017-06-02 18:23:51 +02:00
# Remove database initialization file
#rm $final_path/qa-include/qa-install.php
2019-04-18 19:58:47 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring permissions..." --weight=2
2017-06-02 18:23:51 +02:00
# Make app public if necessary
if [ $is_public -eq 1 ]
then
2020-02-08 17:16:25 +01:00
# Everyone can access the app.
# The "main" permission is automatically created before the install script.
2021-01-06 13:31:32 +01:00
ynh_permission_update --permission="main" --add="visitors"
2014-10-20 18:55:53 +02:00
fi
2020-02-08 17:16:25 +01:00
# Only the admin can access the admin panel of the app (if the app has an admin panel)
ynh_permission_create --permission="admin" --url="/index.php/admin" --allowed=$admin
2020-02-08 17:16:25 +01:00
2017-06-02 18:23:51 +02:00
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=5
2019-02-10 15:02:38 +01:00
2019-04-18 19:58:47 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2019-02-10 15:02:38 +01:00
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last