_common.sh 3.3 KB

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