2015-10-21 13:03:49 +02:00
|
|
|
#!/bin/bash
|
2017-02-04 19:14:21 +01:00
|
|
|
set -eu # exit on error ; treat unset variables as error
|
2015-10-21 13:03:49 +02:00
|
|
|
|
|
|
|
# Retrieve arguments
|
2017-02-04 19:20:51 +01:00
|
|
|
domain=$YNH_APP_ARG_DOMAIN
|
|
|
|
is_public=$YNH_APP_ARG_PUBLIC_SITE
|
2017-05-25 13:23:26 +02:00
|
|
|
analytics=$YNH_APP_ARG_ANALYTICS
|
2015-10-21 13:03:49 +02:00
|
|
|
path=""
|
|
|
|
|
2017-02-04 19:20:51 +01:00
|
|
|
# Source app helpers
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
2016-04-17 18:35:42 +02:00
|
|
|
# Set up common variables
|
|
|
|
root_path="$(pwd)/.."
|
|
|
|
final_path=/var/www/mattermost
|
|
|
|
data_path=/home/yunohost.app/mattermost
|
|
|
|
version=$(cat "$root_path/VERSION")
|
|
|
|
archive_filename="mattermost-$version.tar.gz"
|
|
|
|
|
2015-10-21 16:24:51 +02:00
|
|
|
# Check for 64 bits support
|
|
|
|
arch="$(uname -m)"
|
|
|
|
if [[ "$arch" != "x86_64" ]]; then
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_die "Mattermost requires an x86_64 machine, but this one is '${arch}'."
|
2015-10-21 16:24:51 +02:00
|
|
|
fi
|
|
|
|
|
2017-02-04 19:20:51 +01:00
|
|
|
# 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"* ]];
|
2015-10-21 16:24:51 +02:00
|
|
|
then
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_die "Mattermost requires MySQL 5.6 or higher, or MariaDB 10 or higher."
|
2015-10-21 16:24:51 +02:00
|
|
|
fi
|
|
|
|
|
2015-10-21 13:03:49 +02:00
|
|
|
# Check domain availability
|
|
|
|
sudo yunohost app checkurl $domain$path -a mattermost
|
|
|
|
if [[ ! $? -eq 0 ]]; then
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_die "The app cannot be installed at '$domain$path': this location is already used."
|
2015-10-21 13:03:49 +02:00
|
|
|
fi
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_app_setting_set mattermost domain "$domain"
|
2015-10-21 13:03:49 +02:00
|
|
|
|
|
|
|
# Install dependencies
|
2015-10-21 16:24:51 +02:00
|
|
|
command -v supervisorctl >/dev/null 2>&1 || sudo apt-get install -y supervisor
|
2015-10-21 13:03:49 +02:00
|
|
|
|
|
|
|
# Initialize database and store mysql password for upgrade
|
2015-10-21 14:33:49 +02:00
|
|
|
db_name="mattermost"
|
2015-10-21 13:03:49 +02:00
|
|
|
db_user="mmuser"
|
2015-10-21 14:33:49 +02:00
|
|
|
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')
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_mysql_create_db $db_name $db_user $db_password
|
|
|
|
ynh_app_setting_set mattermost mysqlpwd "$db_password"
|
2015-10-21 13:03:49 +02:00
|
|
|
|
2015-10-21 14:33:49 +02:00
|
|
|
# Delete db and user if exit with an error
|
2017-02-04 19:20:51 +01:00
|
|
|
function fail_properly
|
2015-10-21 14:33:49 +02:00
|
|
|
{
|
|
|
|
set +e
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_mysql_execute_as_root "DROP DATABASE $db_name ; DROP USER $db_user@localhost ;"
|
2015-10-21 14:33:49 +02:00
|
|
|
|
|
|
|
sudo userdel mattermost
|
2016-04-17 18:35:42 +02:00
|
|
|
sudo rm -Rf "$final_path"
|
2017-02-04 19:20:51 +01:00
|
|
|
sudo rm "$archive_filename"
|
|
|
|
|
|
|
|
# Exit (without triggering a package_linter warning)
|
|
|
|
die_command=$(printf '%s%s' 'ynh_' 'die')
|
|
|
|
$die_command "An error occurred during the installation."
|
2015-10-21 14:33:49 +02:00
|
|
|
}
|
2017-02-04 19:20:51 +01:00
|
|
|
trap fail_properly ERR
|
2015-10-21 14:33:49 +02:00
|
|
|
|
|
|
|
# Create user for email notifications
|
|
|
|
smtp_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')
|
|
|
|
sudo useradd -M --shell /bin/false -p $(openssl passwd -1 "$smtp_password") "mattermost"
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_app_setting_set mattermost smtppwd "$smtp_password"
|
2016-05-23 07:41:12 +02:00
|
|
|
|
2015-10-31 18:00:13 +01:00
|
|
|
# Download and install code
|
2016-04-17 18:35:42 +02:00
|
|
|
archive_url="https://releases.mattermost.com/${version}/mattermost-team-${version}-linux-amd64.tar.gz"
|
|
|
|
|
|
|
|
sudo mkdir -p "$final_path"
|
|
|
|
sudo mkdir -p "$data_path"
|
2015-10-31 18:00:13 +01:00
|
|
|
|
2017-02-04 19:20:51 +01:00
|
|
|
sudo wget --quiet --output-document "$archive_filename" "$archive_url"
|
2016-04-17 18:35:42 +02:00
|
|
|
sudo tar -xvz --file "$archive_filename" --directory "$final_path" --strip-components 1
|
2017-02-04 19:20:51 +01:00
|
|
|
sudo rm -f "$archive_filename"
|
2015-10-21 14:33:49 +02:00
|
|
|
|
|
|
|
# Change variables in Mattermost config
|
|
|
|
db_connection_url="${db_user}:${db_password}@tcp(127.0.0.1:3306)/${db_name}?charset=utf8mb4,utf8"
|
|
|
|
sudo sed -i "s|\"DataSource\": \".*\"|\"DataSource\": \"${db_connection_url}\"|g" $final_path/config/config.json
|
2015-10-21 14:46:47 +02:00
|
|
|
|
2016-04-17 18:35:42 +02:00
|
|
|
sudo sed -i "s|\"Directory\": \"./data/\"|\"Directory\": \"${data_path}/\"|g" $final_path/config/config.json
|
2015-10-31 16:51:08 +01:00
|
|
|
|
2015-10-21 14:33:49 +02:00
|
|
|
sudo sed -i "s|\"SendEmailNotifications\": false|\"SendEmailNotifications\": true|g" $final_path/config/config.json
|
|
|
|
sudo sed -i "s|\"FeedbackName\": \"\"|\"FeedbackName\": \"Mattermost notification\"|g" $final_path/config/config.json
|
|
|
|
sudo sed -i "s|\"FeedbackEmail\": \"\"|\"FeedbackEmail\": \"no-reply@${domain}\"|g" $final_path/config/config.json
|
|
|
|
sudo sed -i "s|\"SMTPUsername\": \"\"|\"SMTPUsername\": \"mattermost\"|g" $final_path/config/config.json
|
|
|
|
sudo sed -i "s|\"SMTPPassword\": \"\"|\"SMTPPassword\": \"${smtp_password}\"|g" $final_path/config/config.json
|
|
|
|
sudo sed -i "s|\"SMTPServer\": \"\"|\"SMTPServer\": \"localhost\"|g" $final_path/config/config.json
|
|
|
|
sudo sed -i "s|\"SMTPPort\": \"\"|\"SMTPPort\": \"25\"|g" $final_path/config/config.json
|
2015-10-21 13:03:49 +02:00
|
|
|
|
2015-10-21 14:46:47 +02:00
|
|
|
sudo sed -i "s|\"EnableConsole\": true|\"EnableConsole\": false|g" $final_path/config/config.json
|
|
|
|
sudo sed -i "s|\"FileLocation\": \"\"|\"FileLocation\": \"/var/log/mattermost.log\"|g" $final_path/config/config.json
|
2017-05-25 13:23:26 +02:00
|
|
|
if [ $analytics -eq 0 ]; then
|
|
|
|
sudo sed -i "s|\"EnableDiagnostics\": true|\"EnableDiagnostics\": false|g" $final_path/config/config.json
|
|
|
|
fi
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_app_setting_set mattermost analytics "$analytics"
|
2015-10-21 14:46:47 +02:00
|
|
|
|
2015-10-31 16:51:08 +01:00
|
|
|
# Set permissions to app directories
|
2015-10-21 13:03:49 +02:00
|
|
|
sudo chown -R www-data: $final_path
|
2015-10-31 16:51:08 +01:00
|
|
|
sudo chown -R www-data: $data_path
|
2015-10-21 13:03:49 +02:00
|
|
|
|
|
|
|
# Modify Nginx configuration file and copy it to Nginx conf directory
|
|
|
|
sudo cp $root_path/conf/nginx.conf-nosub /etc/nginx/conf.d/$domain.d/mattermost.conf
|
|
|
|
|
|
|
|
# Copy supervisor script
|
|
|
|
sudo cp $root_path/conf/supervisor.conf /etc/supervisor/conf.d/mattermost.conf
|
|
|
|
|
2015-10-21 14:33:49 +02:00
|
|
|
# Enable public access if needed
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_app_setting_set mattermost is_public "$is_public"
|
2015-10-21 14:33:49 +02:00
|
|
|
if [ "$is_public" = "Yes" ];
|
|
|
|
then
|
2017-02-04 19:20:51 +01:00
|
|
|
ynh_app_setting_set mattermost unprotected_uris "/"
|
2015-10-21 14:33:49 +02:00
|
|
|
fi
|
|
|
|
|
2015-10-21 13:03:49 +02:00
|
|
|
# Reload Nginx and regenerate SSOwat conf
|
|
|
|
sudo service nginx reload
|
|
|
|
sudo yunohost app ssowatconf
|
|
|
|
|
|
|
|
# Start app
|
2015-10-21 16:24:51 +02:00
|
|
|
sudo supervisorctl reload
|