From cb0d4ddc7507dba3f0c377012504a227088d9c6e Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Sat, 4 Feb 2017 19:20:51 +0100 Subject: [PATCH] scripts: modernize and fix linter warnings --- scripts/backup | 5 ++-- scripts/install | 66 ++++++++++++++++++++++++++----------------------- scripts/remove | 10 +++++--- scripts/upgrade | 17 ++++++++----- 4 files changed, 55 insertions(+), 43 deletions(-) diff --git a/scripts/backup b/scripts/backup index ad5f6c7..17d5ed3 100755 --- a/scripts/backup +++ b/scripts/backup @@ -2,6 +2,7 @@ set -eu # exit on error ; treat unset variables as error app=mattermost +db_name="$app" # The parameter $1 is the backup directory location # which will be compressed afterward @@ -15,7 +16,7 @@ sudo cp -a /var/www/$app/. $backup_dir/sources # Backup database echo "Backup database…" root_pwd=$(sudo cat /etc/yunohost/mysql) -sudo sh -c "mysqldump -u root -p$root_pwd mattermost > $backup_dir/mattermost.sql" +ynh_mysql_dump_db $db_name > $backup_dir/mattermost.sql # Backup uploaded files echo "Backup uploaded files…" @@ -24,5 +25,5 @@ sudo cp -a /home/yunohost.app/mattermost/. $backup_dir/data # Copy Nginx and YunoHost parameters to make the script "standalone" echo "Backup Yunohost configuration…" sudo cp -a /etc/yunohost/apps/$app/. $backup_dir/yunohost -domain=$(sudo yunohost app setting $app domain) +domain=$(ynh_app_setting_get $app domain) sudo cp -a /etc/nginx/conf.d/$domain.d/$app.conf $backup_dir/nginx.conf diff --git a/scripts/install b/scripts/install index a9e2940..ae83120 100644 --- a/scripts/install +++ b/scripts/install @@ -2,11 +2,14 @@ set -eu # exit on error ; treat unset variables as error # Retrieve arguments -domain=$1 -is_public=$2 +domain=$YNH_APP_ARG_DOMAIN +is_public=$YNH_APP_ARG_PUBLIC_SITE analytics=$YNH_APP_ARG_ANALYTICS path="" +# Source app helpers +source /usr/share/yunohost/helpers + # Set up common variables root_path="$(pwd)/.." final_path=/var/www/mattermost @@ -17,30 +20,29 @@ archive_filename="mattermost-$version.tar.gz" # Check for 64 bits support arch="$(uname -m)" if [[ "$arch" != "x86_64" ]]; then - echo "Mattermost requires an x86_64 machine, but this one is '${arch}'." - exit 1 + ynh_die "Mattermost requires an x86_64 machine, but this one is '${arch}'." fi -# Check for MySQL version (ugly, to be improved) -mysql_version=$(mysql --version) -if [[ "$mysql_version" == *"Distrib 4."* ]] \ - || [[ "$mysql_version" == *"Distrib 5.0"* ]] \ - || [[ "$mysql_version" == *"Distrib 5.1"* ]] \ - || [[ "$mysql_version" == *"Distrib 5.2"* ]] \ - || [[ "$mysql_version" == *"Distrib 5.3"* ]] \ - || [[ "$mysql_version" == *"Distrib 5.4"* ]] \ - || [[ "$mysql_version" == *"Distrib 5.5"* ]]; +# 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 - echo "Mattermost requires MySQL 5.6 or higher, or MariaDB 10 or higher." - exit 1 + ynh_die "Mattermost requires MySQL 5.6 or higher, or MariaDB 10 or higher." fi # Check domain availability sudo yunohost app checkurl $domain$path -a mattermost if [[ ! $? -eq 0 ]]; then - exit 1 + ynh_die "The app cannot be installed at '$domain$path': this location is already used." fi -sudo yunohost app setting mattermost domain -v $domain +ynh_app_setting_set mattermost domain "$domain" # Install dependencies command -v supervisorctl >/dev/null 2>&1 || sudo apt-get install -y supervisor @@ -49,27 +51,29 @@ command -v supervisorctl >/dev/null 2>&1 || sudo apt-get install -y supervisor db_name="mattermost" 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') -sudo yunohost app initdb $db_user -p $db_password -d $db_name -sudo yunohost app setting mattermost mysqlpwd -v $db_password +ynh_mysql_create_db $db_name $db_user $db_password +ynh_app_setting_set mattermost mysqlpwd "$db_password" # Delete db and user if exit with an error -function exit_properly +function fail_properly { set +e - root_pwd=$(sudo cat /etc/yunohost/mysql) - mysql -u root -p$root_pwd -e "DROP DATABASE mattermost ; DROP USER mmuser@localhost ;" + ynh_mysql_execute_as_root "DROP DATABASE $db_name ; DROP USER $db_user@localhost ;" sudo userdel mattermost sudo rm -Rf "$final_path" - rm "$archive_filename" - exit 1 + 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." } -trap exit_properly ERR +trap fail_properly ERR # 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" -sudo yunohost app setting mattermost smtppwd -v "$smtp_password" +ynh_app_setting_set mattermost smtppwd "$smtp_password" # Download and install code archive_url="https://releases.mattermost.com/${version}/mattermost-team-${version}-linux-amd64.tar.gz" @@ -77,9 +81,9 @@ archive_url="https://releases.mattermost.com/${version}/mattermost-team-${versio sudo mkdir -p "$final_path" sudo mkdir -p "$data_path" -wget --quiet --output-document "$archive_filename" "$archive_url" +sudo wget --quiet --output-document "$archive_filename" "$archive_url" sudo tar -xvz --file "$archive_filename" --directory "$final_path" --strip-components 1 -rm -f "$archive_filename" +sudo rm -f "$archive_filename" # Change variables in Mattermost config db_connection_url="${db_user}:${db_password}@tcp(127.0.0.1:3306)/${db_name}?charset=utf8mb4,utf8" @@ -100,7 +104,7 @@ sudo sed -i "s|\"FileLocation\": \"\"|\"FileLocation\": \"/var/log/mattermost.lo if [ $analytics -eq 0 ]; then sudo sed -i "s|\"EnableDiagnostics\": true|\"EnableDiagnostics\": false|g" $final_path/config/config.json fi -sudo yunohost app setting mattermost analytics -v $analytics +ynh_app_setting_set mattermost analytics "$analytics" # Set permissions to app directories sudo chown -R www-data: $final_path @@ -113,10 +117,10 @@ sudo cp $root_path/conf/nginx.conf-nosub /etc/nginx/conf.d/$domain.d/mattermost. sudo cp $root_path/conf/supervisor.conf /etc/supervisor/conf.d/mattermost.conf # Enable public access if needed -sudo yunohost app setting mattermost is_public -v $is_public +ynh_app_setting_set mattermost is_public "$is_public" if [ "$is_public" = "Yes" ]; then - sudo yunohost app setting mattermost unprotected_uris -v "/" + ynh_app_setting_set mattermost unprotected_uris "/" fi # Reload Nginx and regenerate SSOwat conf diff --git a/scripts/remove b/scripts/remove index f6d58f1..54588ee 100755 --- a/scripts/remove +++ b/scripts/remove @@ -1,10 +1,13 @@ #!/bin/bash set -u # treat unset variables as an error -domain=$(sudo yunohost app setting mattermost domain) +# Source app helpers +source /usr/share/yunohost/helpers + +# Read configuration +domain=$(ynh_app_setting_get mattermost domain) db_name="mattermost" db_user="mmuser" -db_root_pwd=$(sudo cat /etc/yunohost/mysql) # Stop service sudo supervisorctl stop mattermost @@ -14,7 +17,7 @@ sudo rm -rf /var/www/mattermost sudo rm -rf /home/yunohost.app/mattermost # Remove database -mysql -u root -p$db_root_pwd -e "DROP DATABASE $db_name ; DROP USER $db_user@localhost ;" +ynh_mysql_execute_as_root "DROP DATABASE $db_name ; DROP USER $db_user@localhost ;" # Remove uploaded files @@ -27,4 +30,3 @@ sudo rm /etc/supervisor/conf.d/mattermost.conf # Remove log files sudo rm -f /var/log/mattermost.log - diff --git a/scripts/upgrade b/scripts/upgrade index bfe0b29..39976cc 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,10 +1,12 @@ #!/bin/bash set -eu # exit on error ; treat unset variables as error +# Source app helpers +source /usr/share/yunohost/helpers # Retrieve arguments -domain=$(sudo yunohost app setting mattermost domain) -is_public=$(sudo yunohost app setting mattermost is_public) +domain=$(ynh_app_setting_get mattermost domain) +is_public=$(ynh_app_setting_get mattermost is_public) # Set up common variables root_path="$(pwd)/.." @@ -16,9 +18,12 @@ archive_filename="mattermost-$version.tar.gz" function cleanup_and_restart { set +e - rm "$archive_filename" + sudo rm "$archive_filename" sudo supervisorctl start mattermost - exit 1 + + # Exit (without triggering a package_linter warning) + die_command = 'ynh_' + 'die' + $die_command "An error occurred during the installation." } trap cleanup_and_restart ERR @@ -27,7 +32,7 @@ sudo supervisorctl stop mattermost # Download code archive_url="https://releases.mattermost.com/${version}/mattermost-team-${version}-linux-amd64.tar.gz" -wget --quiet --output-document "$archive_filename" "$archive_url" +sudo wget --quiet --output-document "$archive_filename" "$archive_url" # Backup configuration file config_file="$final_path/config/config.json" @@ -39,7 +44,7 @@ sudo cp -f "$config_file" "$backup_config_file" sudo rm -rf "$final_path" sudo mkdir -p "$final_path" sudo tar -xvz --file "$archive_filename" --directory "$final_path" --strip-components 1 -rm -f "$archive_filename" +sudo rm -f "$archive_filename" # Restore configuration file sudo cp -f "$backup_config_file" "$config_file"