Explorar o código

[enh] Make use of YunoHost helpers and harden set options

Jérôme Lebleu %!s(int64=8) %!d(string=hai) anos
pai
achega
4fa6a82d8d
Modificáronse 5 ficheiros con 122 adicións e 85 borrados
  1. 19 14
      scripts/backup
  2. 33 22
      scripts/install
  3. 17 13
      scripts/remove
  4. 28 14
      scripts/restore
  5. 25 22
      scripts/upgrade

+ 19 - 14
scripts/backup

@@ -1,26 +1,31 @@
 #!/bin/bash
 
-# causes the shell to exit if any subcommand or pipeline returns a non-zero status
-set -e
+# Exit on command errors and treat unset variables as an error
+set -eu
 
+# See comments in install script
 app=$YNH_APP_INSTANCE_NAME
 
-# The first argument is the backup directory location for the app
-# from where the script is executed and which will be compressed afterward
-backup_dir=$YNH_APP_BACKUP_DIR
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
 
 # Backup sources & data
-sudo cp -a "/var/www/${app}" ./sources
+# Note: the last argument is where to save this path, see the restore script.
+ynh_backup "/var/www/${app}" "sources"
 
-# Backup mysql database if needed
-# db_pwd=$(sudo yunohost app setting $app mysqlpwd)
-# sudo mysqldump -u $app -p$db_pwd $app > ./dump.sql
+# If a MySQL database is used:
+#
+# # Dump the database
+# dbname=$app
+# dbuser=$app
+# dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
+# mysqldump -u "$dbuser" -p"$dbpass" --no-create-db "$dbname" > ./dump.sql
 
 # Copy NGINX configuration
-domain=$(sudo yunohost app setting "$app" domain)
-sudo cp -a "/etc/nginx/conf.d/${domain}.d/${app}.conf" ./nginx.conf
+domain=$(ynh_app_setting_get "$app" domain)
+ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf"
 
-# If a dedicated php-fpm process is used :
-# Copy dedicated php-fpm process to backup folder
+# If a dedicated php-fpm process is used:
 #
-#sudo cp -a "/etc/php5/fpm/pool.d/${app}.conf" ./php-fpm.conf
+# # Copy PHP-FPM pool configuration
+# ynh_backup "/etc/php5/fpm/pool.d/${app}.conf" "php-fpm.conf"

+ 33 - 22
scripts/install

@@ -1,7 +1,7 @@
 #!/bin/bash
 
-# causes the shell to exit if any subcommand or pipeline returns a non-zero status
-set -e
+# Exit on command errors and treat unset variables as an error
+set -eu
 
 # This is a multi-instance app, meaning it can be installed several times independently
 # The id of the app as stated in the manifest is available as $YNH_APP_ID
@@ -22,14 +22,17 @@ admin=$YNH_APP_ARG_ADMIN
 is_public=$YNH_APP_ARG_IS_PUBLIC
 language=$YNH_APP_ARG_LANGUAGE
 
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
 # Save app settings
-sudo yunohost app setting $app admin -v "$admin"
-sudo yunohost app setting $app is_public -v "$is_public"
-sudo yunohost app setting $app language -v "$language"
+ynh_app_setting_set "$app" admin "$admin"
+ynh_app_setting_set "$app" is_public "$is_public"
+ynh_app_setting_set "$app" language "$language"
 
 # Check domain/path availability
-sudo yunohost app checkurl $domain$path -a $app \
-	|| (echo "Path not available: $domain$path" && exit 1)
+sudo yunohost app checkurl "${domain}${path}" -a "$app" \
+    || ynh_die "Path not available: ${domain}${path}"
 
 # Copy source files
 final_path=/var/www/$app
@@ -41,10 +44,18 @@ sudo cp -a ../sources/. $final_path
 sudo chown -R root:root $final_path
 
 # If your app use a MySQL database you can use these lines to bootstrap
-# a database, an associated user and save the password in app settings
-# db_pwd=$(openssl rand -hex 15)
-# sudo yunohost app initdb $app -p $db_pwd
-# sudo yunohost app setting $app mysqlpwd -v $db_pwd
+# a database, an associated user and save the password in app settings.
+#
+# # Generate MySQL password and create database
+# dbuser=$app
+# dbname=$app
+# dbpass=$(ynh_string_random 12)
+# ynh_app_setting_set "$app" mysqlpwd "$dbpass"
+# ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
+#
+# # Load initial SQL into the new database
+# ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" \
+#     < "../sources/sql/mysql.init.sql"
 
 # Modify Nginx configuration file and copy it to Nginx conf directory
 sed -i "s@YNH_WWW_PATH@$path@g" ../conf/nginx.conf
@@ -52,31 +63,31 @@ sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/nginx.conf
 # If a dedicated php-fpm process is used:
 # Don't forget to modify ../conf/nginx.conf accordingly or your app will not work!
 #
-#sudo sed -i "s@YNH_WWW_APP@$app@g" ../conf/nginx.conf
+# sudo sed -i "s@YNH_WWW_APP@$app@g" ../conf/nginx.conf
 sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
 
 # If a dedicated php-fpm process is used:
-# Adjustment and copy dedicated php-fpm conf file
 # Don't forget to modify ../conf/php-fpm.conf accordingly or your app will not work!
 #
-#sed -i "s@YNH_WWW_APP@$app@g" ../conf/php-fpm.conf
-#sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/php-fpm.conf
-#finalphpconf=/etc/php5/fpm/pool.d/$app.conf
-#sudo cp ../conf/php-fpm.conf $finalphpconf
-#sudo chown root: $finalphpconf
-#sudo chmod 644 $finalphpconf
+# # Modify PHP-FPM pool configuration and copy it to the pool directory
+# sed -i "s@YNH_WWW_APP@$app@g" ../conf/php-fpm.conf
+# sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/php-fpm.conf
+# finalphpconf=/etc/php5/fpm/pool.d/$app.conf
+# sudo cp ../conf/php-fpm.conf $finalphpconf
+# sudo chown root: $finalphpconf
+# sudo chmod 644 $finalphpconf
 
 # If app is public, add url to SSOWat conf as skipped_uris
 if [[ $is_public -eq 1 ]];
 then
   # unprotected_uris allows SSO credentials to be passed anyway.
-  sudo yunohost app setting $app unprotected_uris -v "/"
+  ynh_app_setting_set "$app" unprotected_uris "/"
 fi
 
 
-# If dedicated php-fpm process:
+# If a dedicated php-fpm process is used:
 #
-#sudo service php5-fpm reload
+# sudo service php5-fpm reload
 
 # Restart services
 sudo service nginx reload

+ 17 - 13
scripts/remove

@@ -1,13 +1,13 @@
 #!/bin/bash
 
-# The last argument is the app instance name
-app=${!#}
+# See comments in install script
+app=$YNH_APP_INSTANCE_NAME
 
-# Retrieve arguments
-domain=$(sudo yunohost app setting $app domain)
-path=$(sudo yunohost app setting $app path)
-admin=$(sudo yunohost app setting $app admin)
-is_public=$(sudo yunohost app setting $app is_public)
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
+# Retrieve app settings
+domain=$(ynh_app_setting_get "$app" domain)
 
 # Remove sources
 sudo rm -rf /var/www/$app
@@ -15,14 +15,18 @@ sudo rm -rf /var/www/$app
 # Remove configuration files
 sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf
 
-# If a database is used, remove it
-# root_pwd=$(sudo cat /etc/yunohost/mysql)
-# mysql -u root -p$root_pwd -e "DROP DATABASE $app ; DROP USER $app@localhost ;"
+# If a dedicated php-fpm process is used:
+#
+# sudo rm -f /etc/php5/fpm/pool.d/$app.conf
+# sudo service php5-fpm reload
 
-# If a dedicated php-fpm process is used :
+# If a MySQL database is used:
 #
-#sudo rm -f /etc/php5/fpm/pool.d/$app.conf
-#sudo service php5-fpm reload
+# # Drop MySQL database and user
+# dbname=$app
+# dbuser=$app
+# ynh_mysql_drop_db "$dbname" || true
+# ynh_mysql_drop_user "$dbuser" || true
 
 # Restart services
 sudo service nginx reload

+ 28 - 14
scripts/restore

@@ -1,13 +1,24 @@
 #!/bin/bash
 
-# causes the shell to exit if any subcommand or pipeline returns a non-zero status
-set -e
+# Note: each files and directories you've saved using the ynh_backup helper
+# will be located in the current directory, regarding the last argument.
 
+# Exit on command errors and treat unset variables as an error
+set -eu
+
+# See comments in install script
 app=$YNH_APP_INSTANCE_NAME
 
-# The first argument is the backup directory location of the app
-# from where the script is executed
-backup_dir=$YNH_APP_BACKUP_DIR
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
+# Retrieve old app settings
+domain=$(ynh_app_setting_get "$app" domain)
+path=$(ynh_app_setting_get "$app" path)
+
+# Check domain/path availability
+sudo yunohost app checkurl "${domain}${path}" -a "$app" \
+    || ynh_die "Path not available: ${domain}${path}"
 
 # Restore sources & data
 final_path="/var/www/${app}"
@@ -17,20 +28,23 @@ sudo cp -a ./sources "$final_path"
 # you may need to make some file and/or directory writeable by www-data (nginx user)
 sudo chown -R root:root "$final_path"
 
-# Restore mysql database if needed
-# db_pwd=$(sudo yunohost app setting $app mysqlpwd)
-# sudo mysql -u $app -p$db_pwd $app < ./dump.sql
+# If a MySQL database is used:
+#
+# # Create and restore the database
+# dbname=$app
+# dbuser=$app
+# dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
+# ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
+# ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./dump.sql
 
 # Restore NGINX configuration
-domain=$(sudo yunohost app setting "$app" domain)
 sudo cp -a ./nginx.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf"
 
-# If a dedicated php-fpm process is used :
-# Copy dedicated php-fpm process from backup folder to the right location
-# And restart service
+# If a dedicated php-fpm process is used:
 #
-#sudo cp -a ./php-fpm.conf "/etc/php5/fpm/pool.d/${app}.conf"
-#sudo service php5-fpm reload
+# # Copy PHP-FPM pool configuration and reload the service
+# sudo cp -a ./php-fpm.conf "/etc/php5/fpm/pool.d/${app}.conf"
+# sudo service php5-fpm reload
 
 # Restart webserver
 sudo service nginx reload

+ 25 - 22
scripts/upgrade

@@ -1,17 +1,20 @@
 #!/bin/bash
 
-# causes the shell to exit if any subcommand or pipeline returns a non-zero status
-set -e
+# Exit on command errors and treat unset variables as an error
+set -eu
 
 # See comments in install script
 app=$YNH_APP_INSTANCE_NAME
 
-# Retrieve arguments
-domain=$(sudo yunohost app setting $app domain)
-path=$(sudo yunohost app setting $app path)
-admin=$(sudo yunohost app setting $app admin)
-is_public=$(sudo yunohost app setting $app is_public)
-language=$(sudo yunohost app setting $app language)
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
+# Retrieve app settings
+domain=$(ynh_app_setting_get "$app" domain)
+path=$(ynh_app_setting_get "$app" path)
+admin=$(ynh_app_setting_get "$app" admin)
+is_public=$(ynh_app_setting_get "$app" is_public)
+language=$(ynh_app_setting_get "$app" language)
 
 # Remove trailing "/" for next commands
 path=${path%/}
@@ -28,33 +31,33 @@ sudo chown -R root:root $final_path
 # Modify Nginx configuration file and copy it to Nginx conf directory
 sed -i "s@YNH_WWW_PATH@$path@g" ../conf/nginx.conf
 sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/nginx.conf
-# If a dedicated php-fpm process is used :
+# If a dedicated php-fpm process is used:
 #
-#sudo sed -i "s@YNH_WWW_APP@$app@g" ../conf/nginx.conf
+# sudo sed -i "s@YNH_WWW_APP@$app@g" ../conf/nginx.conf
 sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
 
-# If a dedicated php-fpm process is used :
-# Adjustment and copy dedicated php-fpm conf file
+# If a dedicated php-fpm process is used:
 #
-#sed -i "s@YNH_WWW_APP@$app@g" ../conf/php-fpm.conf
-#sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/php-fpm.conf
-#finalphpconf=/etc/php5/fpm/pool.d/$app.conf
-#sudo cp ../conf/php-fpm.conf $finalphpconf
-#sudo chown root: $finalphpconf
-#sudo chmod 644 $finalphpconf
+# # Modify PHP-FPM pool configuration and copy it to the pool directory
+# sed -i "s@YNH_WWW_APP@$app@g" ../conf/php-fpm.conf
+# sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/php-fpm.conf
+# finalphpconf=/etc/php5/fpm/pool.d/$app.conf
+# sudo cp ../conf/php-fpm.conf $finalphpconf
+# sudo chown root: $finalphpconf
+# sudo chmod 644 $finalphpconf
 
 # If app is public, add url to SSOWat conf as skipped_uris
 if [[ $is_public -eq 1 ]];
 then
   # See install script
-  sudo yunohost app setting $app unprotected_uris -v "/"
+  ynh_app_setting_set "$app" unprotected_uris "/"
   # Remove old settings
-  sudo yunohost app setting $app skipped_uris -d
+  ynh_app_setting_delete "$app" skipped_uris
 fi
 
-# If a dedicated php-fpm process is used :
+# If a dedicated php-fpm process is used:
 #
-#sudo service php5-fpm restart
+# sudo service php5-fpm restart
 
 # Restart services
 sudo service nginx reload