_common.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. 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. }
  116. # Operations needed by both 'install' and 'upgrade' scripts
  117. function vpnclient_deploy_files_and_services()
  118. {
  119. # Install IPv6 scripts
  120. sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  121. sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  122. # Install command-line cube file loader
  123. sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient-loadcubefile.sh /usr/local/bin/
  124. # Copy confs
  125. sudo mkdir -pm 0755 /var/log/nginx/
  126. sudo chown root:admins /etc/openvpn/
  127. sudo chmod 775 /etc/openvpn/
  128. sudo mkdir -pm 0755 /etc/yunohost/hooks.d/post_iptable_rules/
  129. sudo install -b -o root -g admins -m 0664 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl
  130. sudo install -o root -g root -m 0644 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl.restore
  131. sudo install -b -o root -g root -m 0644 ../conf/nginx_vpnadmin.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  132. sudo install -b -o root -g root -m 0644 ../conf/phpfpm_vpnadmin.conf /etc/php5/fpm/pool.d/${app}.conf
  133. sudo install -b -o root -g root -m 0755 ../conf/hook_post-iptable-rules /etc/yunohost/hooks.d/90-vpnclient.tpl
  134. sudo install -b -o root -g root -m 0644 ../conf/openvpn@.service /etc/systemd/system/
  135. # Copy web sources
  136. sudo mkdir -pm 0755 /var/www/${app}/
  137. sudo cp -a ../sources/* /var/www/${app}/
  138. sudo chown -R root: /var/www/${app}/
  139. sudo chmod -R 0644 /var/www/${app}/*
  140. sudo find /var/www/${app}/ -type d -exec chmod +x {} \;
  141. # Create certificates directory
  142. sudo mkdir -pm 0770 /etc/openvpn/keys/
  143. sudo chown root:admins /etc/openvpn/keys/
  144. #=================================================
  145. # NGINX CONFIGURATION
  146. #=================================================
  147. sudo sed "s|<TPL:NGINX_LOCATION>|${path_url}|g" -i "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  148. sudo sed "s|<TPL:NGINX_REALPATH>|/var/www/${app}/|g" -i "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  149. sudo sed "s|<TPL:PHP_NAME>|${app}|g" -i "/etc/nginx/conf.d/${domain}.d/${app}.conf"
  150. #=================================================
  151. # PHP-FPM CONFIGURATION
  152. #=================================================
  153. sudo sed "s|<TPL:PHP_NAME>|${app}|g" -i /etc/php5/fpm/pool.d/${app}.conf
  154. sudo sed "s|<TPL:PHP_USER>|admin|g" -i /etc/php5/fpm/pool.d/${app}.conf
  155. sudo sed "s|<TPL:PHP_GROUP>|admins|g" -i /etc/php5/fpm/pool.d/${app}.conf
  156. sudo sed "s|<TPL:NGINX_REALPATH>|/var/www/${app}/|g" -i /etc/php5/fpm/pool.d/${app}.conf
  157. # Fix sources
  158. sudo sed "s|<TPL:NGINX_LOCATION>|${path_url}|g" -i /var/www/${app}/config.php
  159. # Copy init script
  160. sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient /usr/local/bin/
  161. sudo install -o root -g root -m 0644 ../conf/ynh-vpnclient.service /etc/systemd/system/
  162. # Copy checker timer
  163. sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient-checker.sh /usr/local/bin/
  164. sudo install -o root -g root -m 0644 ../conf/ynh-vpnclient-checker.service /etc/systemd/system/
  165. sudo install -o root -g root -m 0644 ../conf/ynh-vpnclient-checker.timer /etc/systemd/system/
  166. sudo systemctl daemon-reload
  167. }