_common.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/bash
  2. #
  3. # Common variables and helpers
  4. #
  5. pkg_dependencies="php7.0-fpm sipcalc dnsutils openvpn curl fake-hwclock"
  6. to_logs() {
  7. # When yunohost --verbose or bash -x
  8. if $_ISVERBOSE; then
  9. cat
  10. else
  11. cat > /dev/null
  12. fi
  13. }
  14. # Experimental helpers
  15. # Cf. https://github.com/YunoHost-Apps/Experimental_helpers/blob/72b0bc77c68d4a4a2bf4e95663dbc05e4a762a0a/ynh_read_manifest/ynh_read_manifest
  16. read_json () {
  17. python3 -c "import sys, json;print(json.load(open('$1'))['$2'])"
  18. }
  19. # Experimental helper
  20. # Cf. https://github.com/YunoHost-Apps/Experimental_helpers/blob/72b0bc77c68d4a4a2bf4e95663dbc05e4a762a0a/ynh_read_manifest/ynh_read_manifest
  21. read_manifest () {
  22. if [ -f '../manifest.json' ] ; then
  23. read_json '../manifest.json' "$1"
  24. else
  25. read_json '../settings/manifest.json' "$1"
  26. fi
  27. }
  28. # Experimental helper
  29. # cf. https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_abort_if_up_to_date/ynh_abort_if_up_to_date
  30. ynh_abort_if_up_to_date () {
  31. version=$(read_json "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" 'version' 2> /dev/null || echo '20160501-7')
  32. last_version=$(read_manifest 'version')
  33. if [ "${version}" = "${last_version}" ]; then
  34. ynh_print_info "Up-to-date, nothing to do"
  35. ynh_die "" 0
  36. fi
  37. }
  38. # Read the value of a key in a ynh manifest file
  39. #
  40. # usage: ynh_read_manifest manifest key
  41. # | arg: manifest - Path of the manifest to read
  42. # | arg: key - Name of the key to find
  43. ynh_read_manifest () {
  44. manifest="$1"
  45. key="$2"
  46. python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])"
  47. }
  48. # Read the upstream version from the manifest
  49. # The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
  50. # For example : 4.3-2~ynh3
  51. # This include the number before ~ynh
  52. # In the last example it return 4.3-2
  53. #
  54. # usage: ynh_app_upstream_version
  55. ynh_app_upstream_version () {
  56. manifest_path="../manifest.json"
  57. if [ ! -e "$manifest_path" ]; then
  58. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  59. fi
  60. version_key=$(ynh_read_manifest "$manifest_path" "version")
  61. echo "${version_key/~ynh*/}"
  62. }
  63. # Read package version from the manifest
  64. # The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
  65. # For example : 4.3-2~ynh3
  66. # This include the number after ~ynh
  67. # In the last example it return 3
  68. #
  69. # usage: ynh_app_package_version
  70. ynh_app_package_version () {
  71. manifest_path="../manifest.json"
  72. if [ ! -e "$manifest_path" ]; then
  73. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  74. fi
  75. version_key=$(ynh_read_manifest "$manifest_path" "version")
  76. echo "${version_key/*~ynh/}"
  77. }
  78. # Exit without error if the package is up to date
  79. #
  80. # This helper should be used to avoid an upgrade of a package
  81. # when it's not needed.
  82. #
  83. # To force an upgrade, even if the package is up to date,
  84. # you have to set the variable YNH_FORCE_UPGRADE before.
  85. # example: YNH_FORCE_UPGRADE=1 yunohost app upgrade MyApp
  86. #
  87. # usage: ynh_abort_if_up_to_date
  88. ynh_abort_if_up_to_date () {
  89. local force_upgrade=${YNH_FORCE_UPGRADE:-0}
  90. local package_check=${PACKAGE_CHECK_EXEC:-0}
  91. local version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0)
  92. local last_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0)
  93. if [ "$version" = "$last_version" ]
  94. then
  95. if [ "$force_upgrade" != "0" ]
  96. then
  97. echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2
  98. unset YNH_FORCE_UPGRADE
  99. elif [ "$package_check" != "0" ]
  100. then
  101. echo "Upgrade forced for package check." >&2
  102. else
  103. ynh_die "Up-to-date, nothing to do" 0
  104. fi
  105. fi
  106. }
  107. # Operations needed by both 'install' and 'upgrade' scripts
  108. function vpnclient_deploy_files_and_services()
  109. {
  110. local domain=$1
  111. local app=$2
  112. local service_name=$3
  113. local sysuser="${app}"
  114. local service_checker_name="$service_name-checker"
  115. # Ensure vpnclient_ynh has its own system user
  116. if ! ynh_system_user_exists ${sysuser}
  117. then
  118. ynh_system_user_create ${sysuser}
  119. fi
  120. # Ensure the system user has enough permissions
  121. install -b -o root -g root -m 0440 ../conf/sudoers.conf /etc/sudoers.d/${app}_ynh
  122. ynh_replace_string "__VPNCLIENT_SYSUSER__" "${sysuser}" /etc/sudoers.d/${app}_ynh
  123. # Install IPv6 scripts
  124. install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  125. install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  126. # Install command-line cube file loader
  127. install -o root -g root -m 0755 ../conf/$service_name-loadcubefile.sh /usr/local/bin/
  128. # Copy confs
  129. mkdir -pm 0755 /var/log/nginx/
  130. chown root:${sysuser} /etc/openvpn/
  131. chmod 775 /etc/openvpn/
  132. mkdir -pm 0755 /etc/yunohost/hooks.d/post_iptable_rules/
  133. install -b -o root -g ${sysuser} -m 0664 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl
  134. install -o root -g root -m 0644 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl.restore
  135. install -b -o root -g root -m 0755 ../conf/hook_post-iptable-rules /etc/yunohost/hooks.d/90-vpnclient.tpl
  136. install -b -o root -g root -m 0644 ../conf/openvpn@.service /etc/systemd/system/
  137. # Copy web sources
  138. mkdir -pm 0755 /var/www/${app}/
  139. cp -a ../sources/* /var/www/${app}/
  140. chown -R root: /var/www/${app}/
  141. chmod -R 0644 /var/www/${app}/*
  142. find /var/www/${app}/ -type d -exec chmod +x {} \;
  143. # Create certificates directory
  144. mkdir -pm 0770 /etc/openvpn/keys/
  145. chown root:${sysuser} /etc/openvpn/keys/
  146. #=================================================
  147. # NGINX CONFIGURATION
  148. #=================================================
  149. ynh_print_info "Configuring nginx web server..."
  150. ynh_add_nginx_config
  151. #=================================================
  152. # PHP-FPM CONFIGURATION
  153. #=================================================
  154. ynh_print_info "Configuring php-fpm..."
  155. ynh_add_fpm_config
  156. #=================================================
  157. # Fix sources
  158. ynh_replace_string "__PATH__" "${path_url}" "/var/www/${app}/config.php"
  159. # Copy init script
  160. install -o root -g root -m 0755 ../conf/$service_name /usr/local/bin/
  161. # Copy checker timer
  162. install -o root -g root -m 0755 ../conf/$service_checker_name.sh /usr/local/bin/
  163. install -o root -g root -m 0644 ../conf/$service_checker_name.timer /etc/systemd/system/
  164. #=================================================
  165. # SETUP SYSTEMD
  166. #=================================================
  167. ynh_print_info "Configuring a systemd service..."
  168. ynh_add_systemd_config $service_name "$service_name.service"
  169. ynh_add_systemd_config $service_checker_name "$service_checker_name.service"
  170. }
  171. function service_is_managed_by_yunohost() {
  172. yunohost service status $1 >/dev/null 2>&1
  173. }