From 9acff9df8df566e49083f04802a7a711a10fe460 Mon Sep 17 00:00:00 2001 From: aymhce Date: Thu, 28 Sep 2017 01:29:05 +0200 Subject: [PATCH] apply recommandations YunoHost-Apps/ampache_ynh#21 --- manifest.json | 2 +- scripts/_common | 62 ---------------------------------------------- scripts/_common.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/backup | 6 +++-- scripts/install | 22 +++++++--------- scripts/remove | 15 ++++++++--- scripts/restore | 15 +++++------ scripts/upgrade | 4 +-- 8 files changed, 98 insertions(+), 90 deletions(-) delete mode 100644 scripts/_common create mode 100644 scripts/_common.sh mode change 100755 => 100644 scripts/backup mode change 100755 => 100644 scripts/restore diff --git a/manifest.json b/manifest.json index be4474f..4f32b84 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,7 @@ }, "multi_instance": true, "requirements": { - "yunohost": ">> 2.3.12.1" + "yunohost": ">= 2.7.2" }, "services" : [ "nginx", diff --git a/scripts/_common b/scripts/_common deleted file mode 100644 index 8665191..0000000 --- a/scripts/_common +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/bash - -# Copy files to the right place -ampache_ynh_getsources () { - version=$(cat upstream_version) - - final_path=/var/www/$app - sudo mkdir -p $final_path/log - - sudo wget -O ../ampache.tar.gz https://github.com/ampache/ampache/archive/$version.tar.gz > /dev/null 2>&1 - sudo tar xvzf ../ampache.tar.gz -C .. > /dev/null 2>&1 - sudo cp -ar ../ampache-$version/. $final_path - - sudo cp ../conf/ampache.cfg.php $final_path/config/ampache.cfg.php -} - -# Change variables in Ampache configuration -ampache_ynh_prepareconfig () { - sudo sed -i "s/yunouser/$app/g" $final_path/config/ampache.cfg.php - sudo sed -i "s/yunopass/$db_pwd/g" $final_path/config/ampache.cfg.php - sudo sed -i "s/yunobase/$app/g" $final_path/config/ampache.cfg.php - sudo sed -i "s@PATHTOCHANGE@${path}@g" $final_path/config/ampache.cfg.php - sudo sed -i "s@DOMAINTOCHANGE@$domain@g" $final_path/config/ampache.cfg.php -} - -# Modify Nginx configuration file and copy it to Nginx conf directory -ampache_ynh_preparenginx () { - path2=$(if [ "$path" == "/" ] ; then echo "" ; else echo "$path" ; fi) - sed -i "s@PATHTOCHANGE@${path}@g" ../conf/nginx.conf* - sed -i "s@PATH2TOCHANGE@${path2}@g" ../conf/nginx.conf* - sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf* - sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf -} - -# Ampache installation -ampache_ynh_install () { - ORIPATH=$(pwd) - cd $final_path - sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" - sudo php composer-setup.php - sudo php composer.phar install --prefer-source --no-interaction - # Set permissions to ampache directory - sudo chown -R www-data: $final_path - cd $ORIPATH -} - -# Reload Nginx and regenerate SSOwat conf -ampache_ynh_reloadservices () { - sudo service nginx reload - ynh_app_setting_set $app skipped_uris "/" - sudo yunohost app ssowatconf -} - -# Pre config ampache -ampache_ynh_doconfig () { - echo "127.0.0.1 $domain #yunoampache" | sudo tee -a /etc/hosts - sleep 1 - curl -kL -X POST http://$domain${path%/}/update.php?action=update > /dev/null 2>&1 - sleep 5 - sudo sed '/yunoampache/d' /etc/hosts > /tmp/hosts.tmp - sudo cp /tmp/hosts.tmp /etc/hosts ; sudo rm -f /tmp/hosts.tmp -} diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..f602f2f --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# Copy files to the right place +ampache_ynh_getsources () { + version=$(cat upstream_version) + + final_path=/var/www/$app + mkdir -p $final_path/log + + wget -O ../ampache.tar.gz https://github.com/ampache/ampache/archive/$version.tar.gz > /dev/null 2>&1 + tar xvzf ../ampache.tar.gz -C .. > /dev/null 2>&1 + cp -ar ../ampache-$version/. $final_path + + cp ../conf/ampache.cfg.php $final_path/config/ampache.cfg.php +} + +# Change variables in Ampache configuration +ampache_ynh_prepareconfig () { + ynh_replace_string "yunouser" "$app" $final_path/config/ampache.cfg.php + ynh_replace_string "yunopass" "$db_pwd" $final_path/config/ampache.cfg.php + ynh_replace_string "yunobase" "$app" $final_path/config/ampache.cfg.php + ynh_replace_string "PATHTOCHANGE" "${path}" $final_path/config/ampache.cfg.php + ynh_replace_string "DOMAINTOCHANGE" "$domain" $final_path/config/ampache.cfg.php +} + +# Modify Nginx configuration file and copy it to Nginx conf directory +ampache_ynh_preparenginx () { + path2=$(if [ "$path" == "/" ] ; then echo "" ; else echo "$path" ; fi) + ynh_replace_string "PATHTOCHANGE" "${path}" ../conf/nginx.conf* + ynh_replace_string "PATH2TOCHANGE" "${path2}" ../conf/nginx.conf* + ynh_replace_string "ALIASTOCHANGE" "$final_path/" ../conf/nginx.conf* + cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf +} + +# Ampache installation +ampache_ynh_install () { + ORIPATH=$(pwd) + cd $final_path + php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" + php composer-setup.php + php composer.phar install --prefer-source --no-interaction + # Set permissions to ampache directory + chown -R www-data: $final_path + cd $ORIPATH +} + +# Reload Nginx and regenerate SSOwat conf +ampache_ynh_reloadservices () { + systemctl reload nginx + ynh_app_setting_set $app skipped_uris "/" + yunohost app ssowatconf +} + +# Pre config ampache +ampache_ynh_doconfig () { + echo "127.0.0.1 $domain #yunoampache" | tee -a /etc/hosts + sleep 1 + curl -kL -X POST http://$domain${path%/}/update.php?action=update > /dev/null 2>&1 + sleep 5 + sed '/yunoampache/d' /etc/hosts > /tmp/hosts.tmp + cp /tmp/hosts.tmp /etc/hosts ; ynh_secure_remove /tmp/hosts.tmp +} diff --git a/scripts/backup b/scripts/backup old mode 100755 new mode 100644 index 76aa69b..cba12a8 --- a/scripts/backup +++ b/scripts/backup @@ -23,17 +23,19 @@ domain=$(ynh_app_setting_get "$app" domain) path=$(ynh_app_setting_get "$app" path) admin=$(ynh_app_setting_get "$app" admin) +final_path=/var/www/$app + #================================================= # BACKUP THE APP MAIN DIR #================================================= -ynh_backup "/var/www/$app" "sources" 1 +ynh_backup "$final_path" #================================================= # BACKUP THE NGINX CONFIGURATION #================================================= -ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" "conf/nginx.conf" +ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # BACKUP THE MYSQL DATABASE diff --git a/scripts/install b/scripts/install index 3984f41..65c5f63 100644 --- a/scripts/install +++ b/scripts/install @@ -18,7 +18,7 @@ admin_ampache=$YNH_APP_ARG_ADMIN # Source helpers . /usr/share/yunohost/helpers -. _common +. _common.sh #================================================= # MANAGE SCRIPT FAILURE @@ -30,9 +30,6 @@ ynh_abort_if_errors # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS #================================================= -# Check if admin exists -sudo yunohost user list --json | grep -qi "\"username\": \"$admin_ampache\"" || ynh_die "wrong admin username" - # Check domain/path availability path=$(ynh_normalize_url_path $path) ynh_webpath_available $domain $path @@ -50,14 +47,14 @@ ynh_app_setting_set $app admin $admin_ampache # get sources and copy files to the right place ampache_ynh_getsources -sudo cp ../conf/admin.sql /tmp/ +cp ../conf/admin.sql /tmp/ #================================================= # CREATE A MYSQL DATABASE #================================================= # Generate random password -db_pwd=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d '[A-Za-z0-9]' | sed -n 's/\(.\{24\}\).*/\1/p') +db_pwd=$(ynh_string_random 24) # Initialize database and store mysql password for upgrade ynh_app_setting_set $app mysqlpwd $db_pwd @@ -71,9 +68,9 @@ ynh_mysql_connect_as "$app" "$db_pwd" "$app" < "$final_path/sql/ampache.sql" # Change variables in Ampache configuration ampache_ynh_prepareconfig -sudo sed -i "s/yunoadmin/$admin_ampache/g" /tmp/admin.sql -random_key=db_pwd=$(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 sed -i "s@RANDOMKEYTOCHANGE@$random_key@g" $final_path/config/ampache.cfg.php +ynh_replace_string "yunoadmin" "$admin_ampache" /tmp/admin.sql +random_key=db_pwd=$(ynh_string_random 24) +ynh_replace_string "RANDOMKEYTOCHANGE" "$random_key" $final_path/config/ampache.cfg.php #================================================= # NGINX CONFIGURATION @@ -87,8 +84,7 @@ ampache_ynh_preparenginx #================================================= # Install dependency -sudo apt-get update -sudo apt-get install libav-tools -y +ynh_install_dependencies libav-tools #================================================= # SPECIFIC SETUP @@ -117,5 +113,5 @@ ynh_mysql_connect_as "$app" "$db_pwd" "$app" < /tmp/admin.sql #================================================= # Clean install -sudo rm -rf ../ampache-$version -sudo rm /tmp/admin.sql +ynh_secure_remove ../ampache-$version +ynh_secure_remove /tmp/admin.sql diff --git a/scripts/remove b/scripts/remove index 979a55f..7d30d29 100644 --- a/scripts/remove +++ b/scripts/remove @@ -15,6 +15,8 @@ set -u app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get $app domain) +final_path=/var/www/$app + #================================================= # REMOVE THE MYSQL DATABASE #================================================= @@ -27,17 +29,24 @@ ynh_mysql_drop_user $app #================================================= # Remove sources -sudo rm -rf /var/www/$app +ynh_secure_remove $final_path #================================================= # REMOVE NGINX CONFIGURATION #================================================= # Remove Nginx configuration and reload Nginx conf -sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf +ynh_secure_remove /etc/nginx/conf.d/$domain.d/$app.conf + +#================================================= +# REMOVE DEPENDENCIES +#================================================= + +# Remove dependency +ynh_remove_dependencies libav-tools #================================================= # RELOAD NGINX #================================================= -sudo service nginx reload +systemctl reload nginx diff --git a/scripts/restore b/scripts/restore old mode 100755 new mode 100644 index 7d74d47..2c55002 --- a/scripts/restore +++ b/scripts/restore @@ -23,13 +23,15 @@ domain=$(ynh_app_setting_get "$app" domain) path=$(ynh_app_setting_get "$app" path) admin=$(ynh_app_setting_get "$app" admin) +final_path=/var/www/$app + #================================================= # CHECK IF THE APP CAN BE RESTORED #================================================= # Check domain/path availability -test ! -d /var/www/$app \ - || ynh_die "There is already a directory: /var/www/$app " +test ! -d $final_path \ + || ynh_die "There is already a directory: $final_path" #================================================= # CHECK THE PATH @@ -42,20 +44,19 @@ path=$(ynh_normalize_url_path $path) #================================================= # Install dependency -sudo apt-get update -sudo apt-get install libav-tools -y +ynh_install_dependencies libav-tools #================================================= # RESTORE THE APP MAIN DIR #================================================= -sudo cp -a ./sources "/var/www/$app" +ynh_restore_file "$final_path" #================================================= # RESTORE THE NGINX CONFIGURATION #================================================= -sudo cp -a ./conf/nginx.conf "/etc/nginx/conf.d/$domain.d/$app.conf" +ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # RESTORE THE MYSQL DATABASE @@ -69,4 +70,4 @@ ynh_mysql_connect_as $app $db_pwd $app < ./db.sql # RELOAD NGINX #================================================= -sudo service nginx reload +systemctl reload nginx diff --git a/scripts/upgrade b/scripts/upgrade index 6a9de09..f476650 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,7 +7,7 @@ set -eu #================================================= . /usr/share/yunohost/helpers -. _common +. _common.sh #================================================= # MANAGE SCRIPT FAILURE @@ -75,4 +75,4 @@ ampache_ynh_doconfig #================================================= # Clean install -sudo rm -rf ../ampache-$version +ynh_secure_remove ../ampache-$version