Parcourir la source

[fix] upgrade script requires some helpers.

```
Warning: Upgrading app vpnclient...
Warning: ./upgrade: line 35: ynh_abort_if_up_to_date: command not found
Warning: !!
Warning:   vpnclient's script has encountered an error. Its execution was cancelled.
Warning: !!
Warning:
Error: Unable to upgrade vpnclient
```
pitchum il y a 6 ans
Parent
commit
7800953960
1 fichiers modifiés avec 73 ajouts et 0 suppressions
  1. 73 0
      scripts/_common.sh

+ 73 - 0
scripts/_common.sh

@@ -41,3 +41,76 @@ function ynh_systemctl()
   mv $LOCKFILE.bkp.$$ $LOCKFILE
 }
 
+# Read the value of a key in a ynh manifest file
+#
+# usage: ynh_read_manifest manifest key
+# | arg: manifest - Path of the manifest to read
+# | arg: key - Name of the key to find
+ynh_read_manifest () {
+	manifest="$1"
+	key="$2"
+	python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])"
+}
+
+# Read the upstream version from the manifest
+# The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
+# For example : 4.3-2~ynh3
+# This include the number before ~ynh
+# In the last example it return 4.3-2
+#
+# usage: ynh_app_upstream_version
+ynh_app_upstream_version () {
+    manifest_path="../manifest.json"
+    if [ ! -e "$manifest_path" ]; then
+        manifest_path="../settings/manifest.json"	# Into the restore script, the manifest is not at the same place
+    fi
+    version_key=$(ynh_read_manifest "$manifest_path" "version")
+    echo "${version_key/~ynh*/}"
+}
+
+# Read package version from the manifest
+# The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
+# For example : 4.3-2~ynh3
+# This include the number after ~ynh
+# In the last example it return 3
+#
+# usage: ynh_app_package_version
+ynh_app_package_version () {
+    manifest_path="../manifest.json"
+    if [ ! -e "$manifest_path" ]; then
+        manifest_path="../settings/manifest.json"	# Into the restore script, the manifest is not at the same place
+    fi
+    version_key=$(ynh_read_manifest "$manifest_path" "version")
+    echo "${version_key/*~ynh/}"
+}
+
+# Exit without error if the package is up to date
+#
+# This helper should be used to avoid an upgrade of a package
+# when it's not needed.
+#
+# To force an upgrade, even if the package is up to date,
+# you have to set the variable YNH_FORCE_UPGRADE before.
+# example: sudo YNH_FORCE_UPGRADE=1 yunohost app upgrade MyApp
+#
+# usage: ynh_abort_if_up_to_date
+ynh_abort_if_up_to_date () {
+	local force_upgrade=${YNH_FORCE_UPGRADE:-0}
+	local package_check=${PACKAGE_CHECK_EXEC:-0}
+
+	local version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0)
+	local last_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0)
+	if [ "$version" = "$last_version" ]
+	then
+		if [ "$force_upgrade" != "0" ]
+		then
+			echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2
+			unset YNH_FORCE_UPGRADE
+		elif [ "$package_check" != "0" ]
+		then
+			echo "Upgrade forced for package check." >&2
+		else
+			ynh_die "Up-to-date, nothing to do" 0
+		fi
+	fi
+}