1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/dato_ynh.git synced 2024-09-03 18:16:33 +02:00
dato_ynh/scripts/install
2024-02-28 22:46:32 +01:00

143 lines
6.1 KiB
Bash
Executable file

#!/bin/bash
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# INITIALIZE AND STORE SETTINGS
#=================================================
# Convert to true/false
autosynchronize=$(bool_to_str "$autosynchronize")
ynh_app_setting_set --app="$app" --key=autosynchronize --value="$autosynchronize"
#=================================================
# INSTALL NODEJS
#=================================================
ynh_script_progression --message="Installing NodeJS..." --weight=6
ynh_install_nodejs --nodejs_version="$nodejs_version"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=2
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$install_dir"
chmod -R o-rwx "$install_dir"
chown -R "$app:www-data" "$install_dir"
#=================================================
# BUILD NODE DEPENDENCIES
#=================================================
ynh_script_progression --message="Building node dependencies..." --weight=90
pushd "$install_dir"
ynh_use_nodejs
ynh_exec_warn_less ynh_exec_as "$app" "$ynh_node_load_PATH" "$ynh_npm" install --loglevel warn
popd
#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=3
dato_setup_config_files
#=================================================
# CUSTOMIZE SETUP FOR AUTOSYNCHRONIZATION
#=================================================
if [[ $autosynchronize == true ]]; then
#=================================================
# CUSTOMIZE COUCHDB SETTINGS
#=================================================
ynh_script_progression --message="Customizing couch config..." --weight=2
# make sure that couchdb has CORS enabled and that it accepts requests from dato domain
ynh_add_config --template="../conf/couch.ini" --destination="/opt/couchdb/etc/local.d/$app.ini"
# restart couchdb service so that it takes into consideration the changes
yunohost service restart couchdb
#=================================================
# SETUP A COUCHDB DATO ADMIN USER
#=================================================
ynh_script_progression --message="Setting up admin user in couch..." --weight=2
# figure out couch url with password
couch_pw_url=$(echo "$couch_url" | sed -En "s+^https?://+https://$couch_admin_name:$couch_admin_password@+p")
# add admin user to couch users database
addDatoAdmin_curlResult=$(curl -X PUT "$couch_pw_url/_users/org.couchdb.user:$couch_datoadmin_name" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "{\"name\": \"$couch_datoadmin_name\", \"password\": \"$couch_datoadmin_password\", \"roles\": [\"dato-admin\"], \"type\": \"user\"}" 2> /dev/null)
# display error message if there was an error creating dato admin user in couch
if [[ $addDatoAdmin_curlResult != '{"ok":true'* ]]; then
ynh_print_err --message="There was an error creating the dato admin user for in couch. You will probably have to do it manually (check the last section of this page for instructions: https://squeak.eauchat.org/apps/dato/?setups)."
ynh_print_err --message="Here is the error message from couchdb:"
ynh_print_err --message="$addDatoAdmin_curlResult"
ynh_print_err --message="Please make sure that your couchdb instance is accessible from the url you provided, with a proper SSL certificate (not a self-signed one), otherwise you will not be able to login to dato!"
fi
# modify _users db _security document
usersSecDoc=$(curl -X GET "$couch_pw_url/_users/_security")
usersSecDocModified=$(echo $usersSecDoc | jq '.members.roles += ["dato", "dato-admin"]')
usersSecDocChange_curlResult=$(curl -X PUT "$couch_pw_url/_users/_security" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "$usersSecDocModified" 2> /dev/null)
# display error message if there was an error modifying _users db _security document
if [[ $usersSecDocChange_curlResult != '{"ok":true'* ]]; then
ynh_print_err --message="There was an error enabling dato users to access their user profile in couch. You will have to do it manually (check the 'Configure couchdb so that it accepts requests from dato' section in the following page for instructions: https://squeak.eauchat.org/apps/dato/?setups)."
ynh_print_err --message="Here is the error message from couchdb:"
ynh_print_err --message="$usersSecDocChange_curlResult"
fi
fi
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
[ -d "$install_dir/dist" ] || mkdir "$install_dir/dist"
chown -R $app:$app $install_dir/dist $install_dir/global $install_dir/config
#=================================================
# SYSTEM CONFIGURATION
#=================================================
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1
# Create a dedicated NGINX config
ynh_add_nginx_config
# Create a dedicated systemd config
ynh_add_systemd_config
yunohost service add "$app" --description="Data storage with a convenient and flexible interface" --log="/var/log/$app/$app.log"
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting $app's systemd service..." --weight=60
# Start a systemd service
ynh_systemd_action --service_name="$app" --action="start" --log_path="systemd" --line_match="electrode server listening on port" --timeout=600
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last