From aeef0080cd559f7ba6a132d409b95078d8ff3ed6 Mon Sep 17 00:00:00 2001 From: Moul Date: Sat, 20 Aug 2016 02:31:08 +0200 Subject: [PATCH] [enh] close #19: use functions for duplicate code on scripts --- scripts/functions.sh | 33 +++++++++++++++++++++++++++++++++ scripts/install | 32 ++++++-------------------------- scripts/upgrade | 32 ++++++-------------------------- 3 files changed, 45 insertions(+), 52 deletions(-) create mode 100644 scripts/functions.sh diff --git a/scripts/functions.sh b/scripts/functions.sh new file mode 100644 index 0000000..cbdb653 --- /dev/null +++ b/scripts/functions.sh @@ -0,0 +1,33 @@ +#/bin/bash + +INSTALL_DUNITER_DEBIAN_PACKAGE () { +# Retrieve url of last version and version number +url=$(curl -s https://api.github.com/repos/duniter/duniter/releases | grep "browser_" | grep $arch | head -1 | cut -d\" -f4) +version=$(curl -s https://api.github.com/repos/duniter/duniter/releases | grep "browser_" | grep $arch | head -1 | cut -d/ -f8) + +# Retrieve debian package and install it +wget -nc --quiet $url -P /tmp +sudo dpkg -i /tmp/duniter-$version-linux-$arch.deb +sudo rm -f /tmp/duniter-$version-linux-$arch.deb +} + +CONFIG_SSOWAT_FOR_RESTRICTED_ACCESS () { +# Add admin to the allowed users +sudo yunohost app addaccess $app -u $admin + +# Allow only allowed users to access admin panel +ynh_app_setting_set "$app" protected_uris "/" + +# SSOwat Configuration +#ynh_app_setting_set "$app" unprotected_uris "/api/" +} + +CONFIG_NGINX_FOR_WEB_ADMIN () { +# Configure Nginx +nginx_conf="../conf/nginx.conf" +#sudo sed -i "s@YNH_EXAMPLE_PATH@$path@g" $nginx_conf +sudo sed -i "s@YNH_EXAMPLE_PORT@$port@" $nginx_conf +sudo sed -i "s@YNH_EXAMPLE_DOMAIN@$domain@" $nginx_conf +sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf +sudo nginx -t && sudo service nginx reload +} diff --git a/scripts/install b/scripts/install index 083b75d..d993ea9 100755 --- a/scripts/install +++ b/scripts/install @@ -14,8 +14,9 @@ salt=$YNH_APP_ARG_SALT password=$YNH_APP_ARG_PASSWORD admin=$YNH_APP_ARG_ADMIN -# Source app helpers +# Source app helpers and functions source /usr/share/yunohost/helpers +source functions.sh # Check domain/path availability sudo yunohost app checkurl "${domain}${path}" -a "$app" \ @@ -48,7 +49,7 @@ if [ $arch == "x86_64" ]; then arch="x64" fi if [[ $arch != "x64" && $arch != "armv7l" ]]; then - ynh_die "$arch is not currently supported." 2 + ynh_die "$arch is not supported." 2 fi # Open port on firewall @@ -59,14 +60,7 @@ ynh_app_setting_set $app port $port ynh_app_setting_set $app arch $arch ynh_app_setting_set "$app" admin "$admin" -# Retrieve url of last version and version number -url=$(curl -s https://api.github.com/repos/duniter/duniter/releases | grep "browser_" | grep $arch | head -1 | cut -d\" -f4) -version=$(curl -s https://api.github.com/repos/duniter/duniter/releases | grep "browser_" | grep $arch | head -1 | cut -d/ -f8) - -# Retrieve debian package and install it -wget -nc --quiet $url -P /tmp -sudo dpkg -i /tmp/duniter-$version-linux-$arch.deb -#sudo rm -f /tmp/duniter-$version-linux-$arch.deb +INSTALL_DUNITER_DEBIAN_PACKAGE # Configure Duniter node sudo $app init --autoconf @@ -82,19 +76,5 @@ sudo $app webstart # Add Duniter service to the YunoHost monitoring sudo yunohost service add $app --log /home/admin/.config/$app/"$app"_default/"$app".log -# Add admin to the allowed users -sudo yunohost app addaccess $app -u $admin - -# Allow only allowed users to access admin panel -ynh_app_setting_set "$app" protected_uris "/" - -# SSOwat Configuration -#ynh_app_setting_set "$app" unprotected_uris "/api/" - -# Configure Nginx -nginx_conf="../conf/nginx.conf" -#sudo sed -i "s@YNH_EXAMPLE_PATH@$path@g" $nginx_conf -sudo sed -i "s@YNH_EXAMPLE_PORT@$port@" $nginx_conf -sudo sed -i "s@YNH_EXAMPLE_DOMAIN@$domain@" $nginx_conf -sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf -sudo nginx -t && sudo service nginx reload +CONFIG_SSOWAT_FOR_RESTRICTED_ACCESS +CONFIG_NGINX_FOR_WEB_ADMIN diff --git a/scripts/upgrade b/scripts/upgrade index 524b9fe..c5f9308 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -3,8 +3,9 @@ # Exit on command errors and treat unset variables as an error set -eu -# Source app helpers +# Source app helpers and functions source /usr/share/yunohost/helpers +source functions.sh # Retrive arguments app=$YNH_APP_INSTANCE_NAME @@ -16,7 +17,7 @@ admin=$(ynh_app_setting_get "$app" admin) # Check CPU arch if [[ $arch != "x64" && $arch != "armv7l" ]]; then - ynh_die "$arch is not currently supported." 2 + ynh_die "$arch is not supported." 2 fi # Stop duniter daemon @@ -25,14 +26,7 @@ sudo $app stop # Remove Duniter package sudo dpkg -r duniter -# Retrieve url of latest version and number -url=$(curl -s https://api.github.com/repos/duniter/duniter/releases | grep "browser_" | grep $arch | head -1 | cut -d\" -f4) -version=$(curl -s https://api.github.com/repos/duniter/duniter/releases | grep "browser_" | grep $arch | head -1 | cut -d/ -f8) - -# Retrieve Debian package and install it -wget -nc --quiet $url -P /tmp -sudo dpkg -i /tmp/duniter-$version-linux-$arch.deb -sudo rm -f /tmp/duniter-$version-linux-$arch.deb +INSTALL_DUNITER_DEBIAN_PACKAGE # Start duniter daemon sudo $app webstart @@ -40,19 +34,5 @@ sudo $app webstart # Remove trailing "/" for next command path=${path%/} -# Add admin to the allowed users -sudo yunohost app addaccess $app -u $admin - -# Allow only allowed users to access admin panel -ynh_app_setting_set "$app" protected_uris "/" - -# SSOwat Configuration -#ynh_app_setting_set "$app" unprotected_uris "/api/" - -# Upgrade Nginx configuration -nginx_conf="../conf/nginx.conf" -#sudo sed -i "s@YNH_EXAMPLE_PATH@$path@g" $nginx_conf -sudo sed -i "s@YNH_EXAMPLE_PORT@$port@" $nginx_conf -sudo sed -i "s@YNH_EXAMPLE_DOMAIN@$domain@" $nginx_conf -sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf -sudo nginx -t && sudo service nginx reload +CONFIG_SSOWAT_FOR_RESTRICTED_ACCESS +CONFIG_NGINX_FOR_WEB_ADMIN