2021-03-19 04:09:26 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
true
}
# 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=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN
is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Validating installation parameters..." --weight=1
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
final_path=/opt/yunohost/$app
2021-03-19 04:09:26 +01:00
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
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Storing installation settings..." --weight=1
2021-03-19 04:09:26 +01: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
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Finding available ports..." --weight=1
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
# Find available ports
guacd_port=$(ynh_find_port --port=4822)
ynh_app_setting_set --app=$app --key=guacd_port --value=$guacd_port
port=$(ynh_find_port --port=8080)
2021-03-19 04:09:26 +01:00
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Installing dependencies..." --weight=1
2021-03-19 04:09:26 +01:00
2021-07-30 08:25:22 +02:00
# Guacamole depends on Apache Tomcat.
2021-03-21 05:17:45 +01:00
# But installing Tomcat from the Debian repos automatically enables and starts it.
# So we stop and disable it, unless Tomcat was already enabled or started by the user beforehand
tomcat_enabled=""
tomcat_active=""
if [ "$(systemctl cat tomcat9 --quiet)" ]; then
if (systemctl is-enabled tomcat9 --quiet); then
tomcat_enabled=1
fi
if (systemctl is-active tomcat9 --quiet); then
tomcat_active=1
fi
fi
2021-03-19 04:09:26 +01:00
ynh_install_app_dependencies $pkg_dependencies
2021-03-21 05:17:45 +01:00
if [ ! $tomcat_enabled ]; then
systemctl disable tomcat9 --quiet
fi
if [ ! $tomcat_active ]; then
systemctl stop tomcat9 --quiet
fi
2021-03-19 04:09:26 +01:00
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Creating a MySQL database..." --weight=1
2021-03-19 04:09:26 +01:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
#=================================================
2021-03-21 05:17:45 +01:00
# DOWNLOAD, CHECK AND UNPACK SOURCES
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Setting up source files..." --weight=3
2021-03-19 04:09:26 +01:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
2021-03-21 05:17:45 +01:00
ynh_setup_source --source_id="server" --dest_dir="$final_path/.guacd-src"
tomcat_guac_dir="$path_url"
if [ "$path_url" == "/" -o -z "$path_url" ]; then
tomcat_guac_dir="ROOT"
fi
ynh_setup_source --source_id="client" --dest_dir="$final_path/var/lib/tomcat9/webapps/$tomcat_guac_dir"
ynh_setup_source --source_id="auth-ldap" --dest_dir="$final_path/etc/guacamole/extensions/ldap"
mv "$final_path/etc/guacamole/extensions/ldap/guacamole-auth-ldap-1.3.0.jar" "$final_path/etc/guacamole/extensions/guacamole-auth-ldap.jar"
ynh_secure_remove --file="$final_path/etc/guacamole/extensions/ldap"
ynh_setup_source --source_id="auth-header" --dest_dir="$final_path/etc/guacamole/extensions/header"
mv "$final_path/etc/guacamole/extensions/header/guacamole-auth-header-1.2.0.jar" "$final_path/etc/guacamole/extensions/guacamole-auth-header.jar"
ynh_secure_remove --file="$final_path/etc/guacamole/extensions/header"
ynh_setup_source --source_id="auth-jdbc" --dest_dir="$final_path/etc/guacamole/extensions/jdbc"
mv "$final_path/etc/guacamole/extensions/jdbc/mysql/guacamole-auth-jdbc-mysql-1.3.0.jar" "$final_path/etc/guacamole/extensions/guacamole-auth-jdbc-mysql.jar"
mv "$final_path/etc/guacamole/extensions/jdbc/mysql/schema" "$final_path/etc/guacamole/extensions/mysql-schema"
ynh_secure_remove --file="$final_path/etc/guacamole/extensions/jdbc"
ynh_setup_source --source_id="mariadb-java-client" --dest_dir="$final_path/etc/guacamole/lib/"
mv "$final_path/etc/guacamole/lib/mariadb-java-client-2.7.2.jar" "$final_path/etc/guacamole/lib/mariadb-java-client.jar"
ynh_setup_source --source_id="tomcat9_deb" --dest_dir="$final_path/"
pushd "$final_path" || ynh_die
2021-07-30 08:25:22 +02:00
ar x "$final_path/tomcat9.deb" "data.tar.xz"
2021-03-21 05:17:45 +01:00
popd || ynh_die
2021-07-30 08:25:22 +02:00
ynh_secure_remove --file="$final_path/tomcat9.deb"
2021-03-21 05:17:45 +01:00
mkdir -p "$final_path/tomcat9-data"
tar -C "$final_path/tomcat9-data" -xJf "$final_path/data.tar.xz"
cp -r "$final_path/tomcat9-data/usr/share/tomcat9/etc" -T "$final_path/etc/tomcat9/"
2021-07-30 08:25:22 +02:00
cp -r "$final_path/tomcat9-data/etc/tomcat9/" -T "$final_path/etc/tomcat9/"
2021-03-21 05:17:45 +01:00
ynh_secure_remove --file="$final_path/data.tar.xz"
ynh_secure_remove --file="$final_path/tomcat9-data"
#=================================================
# FINISH SETTING UP DATABASE
#=================================================
ynh_script_progression --message="Setting up database files..." --weight=2
ynh_mysql_connect_as --user=$db_user --password="$db_pwd" --database="$db_name" < "$final_path/etc/guacamole/extensions/mysql-schema/001-create-schema.sql"
ynh_replace_string --match_string="guacadmin" --replace_string="$admin" -f "$final_path/etc/guacamole/extensions/mysql-schema/002-create-admin-user.sql"
ynh_replace_string --match_string="CA458A7D494E3BE824F5E1E175A1556C0F8EEF2C2D7DF3633BEC4A29C4411960" --replace_string="$(hexdump -n 32 -e '4/4 "%08X"' /dev/urandom)" -f "$final_path/etc/guacamole/extensions/mysql-schema/002-create-admin-user.sql"
ynh_replace_string --match_string="FE24ADC5E11E2B25288D1704ABE67A79E342ECC26064CE69C5B3177795A82264" --replace_string="$(hexdump -n 32 -e '4/4 "%08X"' /dev/urandom)" -f "$final_path/etc/guacamole/extensions/mysql-schema/002-create-admin-user.sql"
ynh_mysql_connect_as --user=$db_user --password="$db_pwd" --database="$db_name" < "$final_path/etc/guacamole/extensions/mysql-schema/002-create-admin-user.sql"
ynh_secure_remove --file="$final_path/etc/guacamole/extensions/mysql-schema"
2021-03-19 04:09:26 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Configuring NGINX web server..." --weight=1
2021-03-19 04:09:26 +01:00
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
2021-03-21 05:17:45 +01:00
# CREATE DEDICATED USERS
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Configuring system users..." --weight=1
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
# Create system users
ynh_system_user_create --username="$app-guacd"
ynh_system_user_create --username="$app-tomcat"
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
# SPECIFIC SETUP
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
# COMPILE GUACD
#=================================================
ynh_script_progression --message="Compiling guacd..." --weight=30
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
pushd "$final_path/.guacd-src" || ynh_die
./configure --enable-allow-freerdp-snapshots --prefix="$final_path" --datadir="$final_path" --with-freerdp-plugin-dir="$final_path/lib/x86_64-linux-gnu/freerdp2"
2021-07-30 08:25:22 +02:00
ynh_exec_warn_less make
ynh_exec_warn_less make install
2021-03-21 05:17:45 +01:00
popd || ynh_die
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
# SETUP TOMCAT
#=================================================
mkdir -p "$final_path/etc/"
2021-03-24 03:58:16 +01:00
ln -s -f "$final_path/etc/tomcat9" "$final_path/var/lib/tomcat9/conf"
ln -s -f "/var/log/$app/tomcat/" "$final_path/var/lib/tomcat9/logs"
ln -s -f "/var/cache/$app-tomcat9" "$final_path/var/lib/tomcat9/work"
2021-03-21 05:17:45 +01:00
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
# CREATE LOG DIRS
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
mkdir -p "/var/log/$app/tomcat/" "/var/log/$app/guacd/" "/var/log/$app/tomcat/"
2021-03-19 04:09:26 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Configuring systemd services..." --weight=1
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
# Create dedicated systemd configs
ynh_add_systemd_config --service="$app-guacd" --template="guacd.service" -v guacd_port
ynh_add_systemd_config --service="$app-tomcat" --template="tomcat.service"
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
# ADD CONFIGURATIONS
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
ynh_add_config --template="guacamole.properties" --destination="$final_path/etc/guacamole/guacamole.properties"
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
ynh_replace_string --match_string="8080" --replace_string="$port" --target_file="$final_path/etc/tomcat9/server.xml"
2021-03-19 04:09:26 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2021-03-21 05:17:45 +01:00
# Set permissions to app files
2021-03-22 21:39:41 +01:00
chown -R root:root $final_path
chmod -R o-rwx $final_path
setfacl -n -R -m user:$app-guacd:rx -m default:user:$app-guacd:rx $final_path
setfacl -n -R -m user:$app-tomcat:rx -m default:user:$app-tomcat:rx $final_path
chown -R $app-guacd:$app-guacd "/var/log/$app/guacd/"
chown -R $app-tomcat:$app-tomcat "/var/log/$app/tomcat/"
chown -R root:$app-tomcat "$final_path/etc/tomcat9/" "$final_path/etc/guacamole/"
2021-03-21 05:17:45 +01:00
chown -R "$app-tomcat":"$app-tomcat" "$final_path/var/lib/tomcat9/webapps"
2021-03-22 21:39:41 +01:00
setfacl -n -R -m user:$app-guacd:- -m default:user:$app-guacd:- "$final_path/var/lib/tomcat9/" "$final_path/etc/guacamole/" "$final_path/etc/tomcat9/"
2021-03-19 04:09:26 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Configuring log rotation..." --weight=1
2021-03-19 04:09:26 +01:00
# Use logrotate to manage application logfile(s)
2021-03-21 05:17:45 +01:00
ynh_use_logrotate --logfile="/var/log/$app/tomcat/catalina.out" --specific_user="$app-tomcat/$app-tomcat"
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
# INTEGRATE SERVICES IN YUNOHOST
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Integrating services in YunoHost..." --weight=1
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
yunohost service add "$app-guacd" --description="Guacamole server" --log="/var/log/$app/guacd.log"
yunohost service add "$app-tomcat" --description="Guacamole client" --log="/var/log/$app/tomcat.log"
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
# START SYSTEMD SERVICES
2021-03-19 04:09:26 +01:00
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Starting systemd services..." --weight=1
2021-03-19 04:09:26 +01:00
2021-03-21 05:17:45 +01:00
# Start systemd services
ynh_systemd_action --service_name="$app-guacd" --action="start" --log_path="/var/log/$app/guacd.log"
ynh_systemd_action --service_name="$app-tomcat" --action="start" --log_path="/var/log/$app/tomcat.log"
2021-03-19 04:09:26 +01:00
#=================================================
# SETUP FAIL2BAN
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Configuring Fail2Ban..." --weight=1
2021-03-19 04:09:26 +01:00
# Create a dedicated Fail2Ban config
2021-03-21 05:17:45 +01:00
ynh_add_fail2ban_config --logpath="/var/log/syslog" --failregex='o.a.g.r.auth.AuthenticationService - Authentication attempt from <HOST> for user "[^"]*" failed\.$'
2021-03-19 04:09:26 +01:00
#=================================================
# SETUP SSOWAT
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Configuring permissions..." --weight=1
2021-03-19 04:09:26 +01:00
# Make app public if necessary
if [ $is_public -eq 1 ]
then
# Everyone can access the app.
# The "main" permission is automatically created before the install script.
ynh_permission_update --permission="main" --add="visitors"
fi
#=================================================
# RELOAD NGINX
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2021-03-19 04:09:26 +01:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2021-03-21 05:17:45 +01:00
ynh_script_progression --message="Installation of $app completed" --last