_common.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. #
  3. # Common variables
  4. #
  5. pkg_dependencies="php5-fpm sipcalc dnsutils openvpn curl fake-hwclock"
  6. # Experimental helpers
  7. # Cf. https://github.com/YunoHost-Apps/Experimental_helpers/blob/72b0bc77c68d4a4a2bf4e95663dbc05e4a762a0a/ynh_read_manifest/ynh_read_manifest
  8. read_json () {
  9. sudo python3 -c "import sys, json;print(json.load(open('$1'))['$2'])"
  10. }
  11. read_manifest () {
  12. if [ -f '../manifest.json' ] ; then
  13. read_json '../manifest.json' "$1"
  14. else
  15. read_json '../settings/manifest.json' "$1"
  16. fi
  17. }
  18. # Helper to start/stop/.. a systemd service from a yunohost context,
  19. # *and* the systemd service itself needs to be able to run yunohost
  20. # commands.
  21. #
  22. # Hence the need to release the lock during the operation
  23. #
  24. # usage : ynh_systemctl yolo restart
  25. #
  26. function ynh_systemctl()
  27. {
  28. local ACTION="$1"
  29. local SERVICE="$2"
  30. local LOCKFILE="/var/run/moulinette_yunohost.lock"
  31. # Launch the action
  32. sudo systemctl "$ACTION" "$SERVICE" &
  33. local SYSCTLACTION=$!
  34. # Save and release the lock...
  35. cp $LOCKFILE $LOCKFILE.bkp.$$
  36. rm $LOCKFILE
  37. # Wait for the end of the action
  38. wait $SYSCTLACTION
  39. # Make sure the lock is released...
  40. while [ -f $LOCKFILE ]
  41. do
  42. sleep 0.1
  43. done
  44. # Restore the old lock
  45. mv $LOCKFILE.bkp.$$ $LOCKFILE
  46. }
  47. # Read the value of a key in a ynh manifest file
  48. #
  49. # usage: ynh_read_manifest manifest key
  50. # | arg: manifest - Path of the manifest to read
  51. # | arg: key - Name of the key to find
  52. ynh_read_manifest () {
  53. manifest="$1"
  54. key="$2"
  55. python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])"
  56. }
  57. # Read the upstream version from the manifest
  58. # The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
  59. # For example : 4.3-2~ynh3
  60. # This include the number before ~ynh
  61. # In the last example it return 4.3-2
  62. #
  63. # usage: ynh_app_upstream_version
  64. ynh_app_upstream_version () {
  65. manifest_path="../manifest.json"
  66. if [ ! -e "$manifest_path" ]; then
  67. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  68. fi
  69. version_key=$(ynh_read_manifest "$manifest_path" "version")
  70. echo "${version_key/~ynh*/}"
  71. }
  72. # Read package version from the manifest
  73. # The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
  74. # For example : 4.3-2~ynh3
  75. # This include the number after ~ynh
  76. # In the last example it return 3
  77. #
  78. # usage: ynh_app_package_version
  79. ynh_app_package_version () {
  80. manifest_path="../manifest.json"
  81. if [ ! -e "$manifest_path" ]; then
  82. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  83. fi
  84. version_key=$(ynh_read_manifest "$manifest_path" "version")
  85. echo "${version_key/*~ynh/}"
  86. }
  87. # Exit without error if the package is up to date
  88. #
  89. # This helper should be used to avoid an upgrade of a package
  90. # when it's not needed.
  91. #
  92. # To force an upgrade, even if the package is up to date,
  93. # you have to set the variable YNH_FORCE_UPGRADE before.
  94. # example: sudo YNH_FORCE_UPGRADE=1 yunohost app upgrade MyApp
  95. #
  96. # usage: ynh_abort_if_up_to_date
  97. ynh_abort_if_up_to_date () {
  98. local force_upgrade=${YNH_FORCE_UPGRADE:-0}
  99. local package_check=${PACKAGE_CHECK_EXEC:-0}
  100. local version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0)
  101. local last_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0)
  102. if [ "$version" = "$last_version" ]
  103. then
  104. if [ "$force_upgrade" != "0" ]
  105. then
  106. echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2
  107. unset YNH_FORCE_UPGRADE
  108. elif [ "$package_check" != "0" ]
  109. then
  110. echo "Upgrade forced for package check." >&2
  111. else
  112. ynh_die "Up-to-date, nothing to do" 0
  113. fi
  114. fi
  115. }