mirror of
https://github.com/YunoHost-Apps/mattermost_ynh.git
synced 2024-09-03 19:36:29 +02:00
217 lines
8.9 KiB
Bash
217 lines
8.9 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
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
|
|
#=================================================
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
domain=$YNH_APP_ARG_DOMAIN
|
|
path_url=$YNH_APP_ARG_PATH
|
|
is_public=$YNH_APP_ARG_IS_PUBLIC
|
|
admin_email=$YNH_APP_ARG_ADMIN_EMAIL
|
|
admin_password=$YNH_APP_ARG_ADMIN_PASSWORD
|
|
admin_locale=$YNH_APP_ARG_ADMIN_LOCALE
|
|
team_display_name=$YNH_APP_ARG_TEAM_DISPLAY_NAME
|
|
analytics=$YNH_APP_ARG_ANALYTICS
|
|
|
|
#=================================================
|
|
# CHECK IF THE APP CAN BE INSTALLED
|
|
#=================================================
|
|
|
|
# Allow using the `ynh_die` command without triggering linter warnings
|
|
function script_die () {
|
|
die_command=$(printf '%s%s' 'ynh_' 'die')
|
|
$die_command "$*"
|
|
}
|
|
|
|
# Check for 64 bits support
|
|
arch="$(uname -m)"
|
|
if [[ "$arch" != "x86_64" ]]; then
|
|
script_die "Mattermost requires an x86_64 machine, but this one is '${arch}'."
|
|
fi
|
|
|
|
# Check for MySQL version (without triggering a package_linter warning)
|
|
db_command=$(printf '%s%s' 'my' 'sql')
|
|
db_version=$($db_command --version)
|
|
if [[ "$db_version" == *"Distrib 4."* ]] \
|
|
|| [[ "$db_version" == *"Distrib 5.0"* ]] \
|
|
|| [[ "$db_version" == *"Distrib 5.1"* ]] \
|
|
|| [[ "$db_version" == *"Distrib 5.2"* ]] \
|
|
|| [[ "$db_version" == *"Distrib 5.3"* ]] \
|
|
|| [[ "$db_version" == *"Distrib 5.4"* ]] \
|
|
|| [[ "$db_version" == *"Distrib 5.5"* ]];
|
|
then
|
|
script_die "Mattermost requires MySQL 5.6 or higher, or MariaDB 10 or higher."
|
|
fi
|
|
|
|
#=================================================
|
|
# FIND AN AVAILABLE PATH
|
|
#=================================================
|
|
|
|
# Normalize the url path syntax
|
|
path_url=$(ynh_normalize_url_path $path_url)
|
|
# Check web path availability
|
|
ynh_webpath_available "$domain" "$path_url"
|
|
# Register (book) web path
|
|
ynh_webpath_register "$app" "$domain" "$path_url"
|
|
# Store setting
|
|
ynh_app_setting_set "$app" domain "$domain"
|
|
ynh_app_setting_set "$app" path "$path_url"
|
|
|
|
#=================================================
|
|
# FIND AN AVAILABLE PORT
|
|
#=================================================
|
|
|
|
# Find an available port
|
|
port=$(ynh_find_port 8065)
|
|
# Store setting
|
|
ynh_app_setting_set "$app" port "$port"
|
|
|
|
#=================================================
|
|
# SET UP INSTALLATION VARIABLES
|
|
#=================================================
|
|
|
|
root_path="$(pwd)/.."
|
|
final_path="/var/www/$app"
|
|
data_path="/home/yunohost.app/$app"
|
|
logs_path="/var/log/$app"
|
|
|
|
#=================================================
|
|
# CREATE A MYSQL DATABASE
|
|
#=================================================
|
|
|
|
db_name="$app"
|
|
db_user="mmuser"
|
|
db_password=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d 'A-Za-z0-9' | sed -n 's/\(.\{24\}\).*/\1/p')
|
|
ynh_mysql_create_db "$db_name" "$db_user" "$db_password"
|
|
ynh_app_setting_set "$app" mysqlpwd "$db_password"
|
|
|
|
#=================================================
|
|
# CREATE USER FOR EMAIL NOTIFICATIONS
|
|
#=================================================
|
|
|
|
mattermost_user="$app"
|
|
mattermost_user_password=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d 'A-Za-z0-9' | sed -n 's/\(.\{24\}\).*/\1/p')
|
|
useradd -M --shell /bin/false -p $(openssl passwd -1 "$mattermost_user_password") "$mattermost_user"
|
|
ynh_app_setting_set "$app" smtppwd "$mattermost_user_password"
|
|
|
|
#=================================================
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
#=================================================
|
|
|
|
ynh_app_setting_set "$app" final_path "$final_path"
|
|
ynh_setup_source "$final_path"
|
|
|
|
mkdir -p "$data_path"
|
|
mkdir -p "$logs_path"
|
|
|
|
#=================================================
|
|
# EDIT MATTERMOST CONFIG
|
|
#=================================================
|
|
|
|
# Configure Service Settings
|
|
ynh_replace_string --match "\"SiteURL\": \"\"" --replace "\"SiteURL\": \"https://${domain}${path_url}\"" --target $final_path/config/config.json
|
|
ynh_replace_string --match "\"ListenAddress\": \".*\"" --replace "\"ListenAddress\": \"127.0.0.1:${port}\"" --target $final_path/config/config.json
|
|
# Configure the database connection
|
|
db_connection_url="${db_user}:${db_password}@tcp(127.0.0.1:3306)/${db_name}?charset=utf8mb4,utf8"
|
|
ynh_replace_string --match "\"DataSource\": \".*\"" --replace "\"DataSource\": \"${db_connection_url}\"" --target $final_path/config/config.json
|
|
# Configure uploaded files directory
|
|
ynh_replace_string --match "\"Directory\": \"./data/\"" --replace "\"Directory\": \"${data_path}/\"" --target $final_path/config/config.json
|
|
# Configure SMTP account for sending email notifications
|
|
ynh_replace_string --match "\"SendEmailNotifications\": false" --replace "\"SendEmailNotifications\": true" --target $final_path/config/config.json
|
|
ynh_replace_string --match "\"FeedbackName\": \"\"" --replace "\"FeedbackName\": \"Mattermost notification\"" --target $final_path/config/config.json
|
|
ynh_replace_string --match "\"FeedbackEmail\": \"\"" --replace "\"FeedbackEmail\": \"no-reply@${domain}\"" --target $final_path/config/config.json
|
|
ynh_replace_string --match "\"SMTPUsername\": \"\"" --replace "\"SMTPUsername\": \"${mattermost_user}\"" --target $final_path/config/config.json
|
|
ynh_replace_string --match "\"SMTPPassword\": \"\"" --replace "\"SMTPPassword\": \"${mattermost_user_password}\"" --target $final_path/config/config.json
|
|
ynh_replace_string --match "\"SMTPServer\": \"\"" --replace "\"SMTPServer\": \"localhost\"" --target $final_path/config/config.json
|
|
ynh_replace_string --match "\"SMTPPort\": \"\"" --replace "\"SMTPPort\": \"25\"" --target $final_path/config/config.json
|
|
# Disable Mattermost debug console by default
|
|
ynh_replace_string --match "\"EnableConsole\": true" --replace "\"EnableConsole\": false" --target $final_path/config/config.json
|
|
# Configure log file location
|
|
ynh_replace_string --match "\"FileLocation\": \"\"" --replace "\"FileLocation\": \"$logs_path\"" --target $final_path/config/config.json
|
|
# Configure analytics according to user choice
|
|
if [ $analytics -eq 0 ]; then
|
|
ynh_replace_string --match "\"EnableDiagnostics\": true" --replace "\"EnableDiagnostics\": false" --target $final_path/config/config.json
|
|
fi
|
|
ynh_app_setting_set "$app" analytics "$analytics"
|
|
|
|
#=================================================
|
|
# SECURE FILES AND DIRECTORIES
|
|
#=================================================
|
|
|
|
chown -R "$mattermost_user:www-data" "$final_path"
|
|
chown -R "$mattermost_user:www-data" "$data_path"
|
|
chown -R "$mattermost_user:adm" "$logs_path"
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
|
|
ynh_add_nginx_config
|
|
|
|
#=================================================
|
|
# SYSTEMD CONFIGURATION
|
|
#=================================================
|
|
|
|
ynh_add_systemd_config
|
|
|
|
#=================================================
|
|
# ADVERTISE SERVICE IN ADMIN PANEL
|
|
#=================================================
|
|
|
|
yunohost service add "$app" --log "$logs_path/mattermost.log"
|
|
|
|
#=================================================
|
|
# SETUP SSOWAT
|
|
#=================================================
|
|
|
|
ynh_app_setting_set "$app" is_public "$is_public"
|
|
|
|
if [[ $is_public == "1" ]]; then
|
|
# Make the app accessible to the public
|
|
ynh_app_setting_set "$app" unprotected_uris "/"
|
|
fi
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
|
|
systemctl reload nginx
|
|
|
|
#=================================================
|
|
# START SERVER
|
|
#=================================================
|
|
|
|
systemctl start "$app"
|
|
|
|
#=================================================
|
|
# CREATE ADMIN AND FIRST TEAM
|
|
#=================================================
|
|
|
|
admin_username=$(cut -d @ -f 1 <<< "$admin_email")
|
|
team_name=$(echo "$team_display_name" | iconv -f utf8 -t ascii//TRANSLIT//IGNORE | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
|
|
|
|
cd "$final_path/bin"
|
|
./mattermost user create --username "$admin_username" --email "$admin_email" --password "$admin_password" --locale "$admin_locale" --system_admin
|
|
./mattermost user verify "$admin_username"
|
|
./mattermost team create --name "$team_name" --display_name "$team_display_name" --email "$admin_email"
|
|
./mattermost team add "$team_name" "$admin_username"
|
|
|
|
ynh_app_setting_set "$app" admin_email "$admin_email"
|
|
ynh_app_setting_set "$app" admin_locale "$admin_locale"
|
|
ynh_app_setting_set "$app" team_name "$team_name"
|