_common.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/bin/bash
  2. #
  3. # Common variables and helpers
  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. # Experimental helper
  12. # Cf. https://github.com/YunoHost-Apps/Experimental_helpers/blob/72b0bc77c68d4a4a2bf4e95663dbc05e4a762a0a/ynh_read_manifest/ynh_read_manifest
  13. read_manifest () {
  14. if [ -f '../manifest.json' ] ; then
  15. read_json '../manifest.json' "$1"
  16. else
  17. read_json '../settings/manifest.json' "$1"
  18. fi
  19. }
  20. # Experimental helper
  21. # cf. https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_abort_if_up_to_date/ynh_abort_if_up_to_date
  22. ynh_abort_if_up_to_date () {
  23. version=$(read_json "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" 'version' 2> /dev/null || echo '20160501-7')
  24. last_version=$(read_manifest 'version')
  25. if [ "${version}" = "${last_version}" ]; then
  26. info "Up-to-date, nothing to do"
  27. ynh_die "" 0
  28. fi
  29. }
  30. # Helper to start/stop/.. a systemd service from a yunohost context,
  31. # *and* the systemd service itself needs to be able to run yunohost
  32. # commands.
  33. #
  34. # Hence the need to release the lock during the operation
  35. #
  36. # usage : ynh_systemctl yolo restart
  37. #
  38. function ynh_systemctl()
  39. {
  40. local ACTION="$1"
  41. local SERVICE="$2"
  42. local LOCKFILE="/var/run/moulinette_yunohost.lock"
  43. # Launch the action
  44. sudo systemctl "$ACTION" "$SERVICE" &
  45. local SYSCTLACTION=$!
  46. # Save and release the lock...
  47. cp $LOCKFILE $LOCKFILE.bkp.$$
  48. rm $LOCKFILE
  49. # Wait for the end of the action
  50. wait $SYSCTLACTION
  51. # Make sure the lock is released...
  52. while [ -f $LOCKFILE ]
  53. do
  54. sleep 0.1
  55. done
  56. # Restore the old lock
  57. mv $LOCKFILE.bkp.$$ $LOCKFILE
  58. }
  59. # Read the value of a key in a ynh manifest file
  60. #
  61. # usage: ynh_read_manifest manifest key
  62. # | arg: manifest - Path of the manifest to read
  63. # | arg: key - Name of the key to find
  64. ynh_read_manifest () {
  65. manifest="$1"
  66. key="$2"
  67. python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])"
  68. }
  69. # Read the upstream version from the manifest
  70. # The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
  71. # For example : 4.3-2~ynh3
  72. # This include the number before ~ynh
  73. # In the last example it return 4.3-2
  74. #
  75. # usage: ynh_app_upstream_version
  76. ynh_app_upstream_version () {
  77. manifest_path="../manifest.json"
  78. if [ ! -e "$manifest_path" ]; then
  79. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  80. fi
  81. version_key=$(ynh_read_manifest "$manifest_path" "version")
  82. echo "${version_key/~ynh*/}"
  83. }
  84. # Read package version from the manifest
  85. # The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
  86. # For example : 4.3-2~ynh3
  87. # This include the number after ~ynh
  88. # In the last example it return 3
  89. #
  90. # usage: ynh_app_package_version
  91. ynh_app_package_version () {
  92. manifest_path="../manifest.json"
  93. if [ ! -e "$manifest_path" ]; then
  94. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  95. fi
  96. version_key=$(ynh_read_manifest "$manifest_path" "version")
  97. echo "${version_key/*~ynh/}"
  98. }
  99. # Exit without error if the package is up to date
  100. #
  101. # This helper should be used to avoid an upgrade of a package
  102. # when it's not needed.
  103. #
  104. # To force an upgrade, even if the package is up to date,
  105. # you have to set the variable YNH_FORCE_UPGRADE before.
  106. # example: sudo YNH_FORCE_UPGRADE=1 yunohost app upgrade MyApp
  107. #
  108. # usage: ynh_abort_if_up_to_date
  109. ynh_abort_if_up_to_date () {
  110. local force_upgrade=${YNH_FORCE_UPGRADE:-0}
  111. local package_check=${PACKAGE_CHECK_EXEC:-0}
  112. local version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0)
  113. local last_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0)
  114. if [ "$version" = "$last_version" ]
  115. then
  116. if [ "$force_upgrade" != "0" ]
  117. then
  118. echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2
  119. unset YNH_FORCE_UPGRADE
  120. elif [ "$package_check" != "0" ]
  121. then
  122. echo "Upgrade forced for package check." >&2
  123. else
  124. ynh_die "Up-to-date, nothing to do" 0
  125. fi
  126. fi
  127. }
  128. # Operations needed by both 'install' and 'upgrade' scripts
  129. function vpnclient_deploy_files_and_services()
  130. {
  131. # Install IPv6 scripts
  132. sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  133. sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  134. # Install command-line cube file loader
  135. sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient-loadcubefile.sh /usr/local/bin/
  136. # Copy confs
  137. sudo mkdir -pm 0755 /var/log/nginx/
  138. sudo chown root:admins /etc/openvpn/
  139. sudo chmod 775 /etc/openvpn/
  140. sudo mkdir -pm 0755 /etc/yunohost/hooks.d/post_iptable_rules/
  141. sudo install -b -o root -g admins -m 0664 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl
  142. sudo install -o root -g root -m 0644 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl.restore
  143. sudo install -b -o root -g root -m 0644 ../conf/nginx_vpnadmin.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  144. sudo install -b -o root -g root -m 0644 ../conf/phpfpm_vpnadmin.conf /etc/php5/fpm/pool.d/${app}.conf
  145. sudo install -b -o root -g root -m 0755 ../conf/hook_post-iptable-rules /etc/yunohost/hooks.d/90-vpnclient.tpl
  146. sudo install -b -o root -g root -m 0644 ../conf/openvpn@.service /etc/systemd/system/
  147. # Copy web sources
  148. sudo mkdir -pm 0755 /var/www/${app}/
  149. sudo cp -a ../sources/* /var/www/${app}/
  150. sudo chown -R root: /var/www/${app}/
  151. sudo chmod -R 0644 /var/www/${app}/*
  152. sudo find /var/www/${app}/ -type d -exec chmod +x {} \;
  153. # Create certificates directory
  154. sudo mkdir -pm 0770 /etc/openvpn/keys/
  155. sudo chown root:admins /etc/openvpn/keys/
  156. #=================================================
  157. # NGINX CONFIGURATION
  158. #=================================================
  159. sudo sed "s|<TPL:NGINX_LOCATION>|${path_url}|g" -i "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  160. sudo sed "s|<TPL:NGINX_REALPATH>|/var/www/${app}/|g" -i "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  161. sudo sed "s|<TPL:PHP_NAME>|${app}|g" -i "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  162. #=================================================
  163. # PHP-FPM CONFIGURATION
  164. #=================================================
  165. sudo sed "s|<TPL:PHP_NAME>|${app}|g" -i /etc/php5/fpm/pool.d/${app}.conf
  166. sudo sed "s|<TPL:PHP_USER>|admin|g" -i /etc/php5/fpm/pool.d/${app}.conf
  167. sudo sed "s|<TPL:PHP_GROUP>|admins|g" -i /etc/php5/fpm/pool.d/${app}.conf
  168. sudo sed "s|<TPL:NGINX_REALPATH>|/var/www/${app}/|g" -i /etc/php5/fpm/pool.d/${app}.conf
  169. # Fix sources
  170. sudo sed "s|<TPL:NGINX_LOCATION>|${path_url}|g" -i /var/www/${app}/config.php
  171. # Copy init script
  172. sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient /usr/local/bin/
  173. sudo install -o root -g root -m 0644 ../conf/ynh-vpnclient.service /etc/systemd/system/
  174. # Copy checker timer
  175. sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient-checker.sh /usr/local/bin/
  176. sudo install -o root -g root -m 0644 ../conf/ynh-vpnclient-checker.service /etc/systemd/system/
  177. sudo install -o root -g root -m 0644 ../conf/ynh-vpnclient-checker.timer /etc/systemd/system/
  178. sudo systemctl daemon-reload
  179. }