_common.sh 8.0 KB

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