_common.sh 6.8 KB

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