_common.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #!/bin/bash
  2. #=================================================
  3. # COMMON VARIABLES
  4. #=================================================
  5. YNH_PHP_VERSION="7.4"
  6. php_dependencies="php$YNH_PHP_VERSION-common php$YNH_PHP_VERSION-cgi php$YNH_PHP_VERSION-sqlite3 php$YNH_PHP_VERSION-xml php$YNH_PHP_VERSION-intl"
  7. # dependencies used by the app (must be on a single line)
  8. pkg_dependencies="cmake build-essential libgmp-dev libidn11-dev nettle-dev libreadline-dev sqlite3 cron curl iputils-ping psmisc unzip idn2 libcap2-bin dns-root-data libcap2 netcat-openbsd procps jq $php_dependencies"
  9. pihole_adminlte_version=5.18
  10. pihole_flt_version=5.20
  11. PI_HOLE_LOCAL_REPO="/etc/.pihole"
  12. PI_HOLE_INSTALL_DIR="/opt/pihole"
  13. PI_HOLE_CONFIG_DIR="/etc/pihole"
  14. PI_HOLE_BIN_DIR="/usr/local/bin"
  15. #=================================================
  16. # PERSONAL HELPERS
  17. #=================================================
  18. #=================================================
  19. # EXPERIMENTAL HELPERS
  20. #=================================================
  21. # Send an email to inform the administrator
  22. #
  23. # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type]
  24. # | arg: -m --app_message= - The file with the content to send to the administrator.
  25. # | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root
  26. # example: "root admin@domain"
  27. # If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
  28. # example: "root admin@domain user1 user2"
  29. # | arg: -t, --type= - Type of mail, could be 'backup', 'change_url', 'install', 'remove', 'restore', 'upgrade'
  30. ynh_send_readme_to_admin() {
  31. # Declare an array to define the options of this helper.
  32. declare -Ar args_array=( [m]=app_message= [r]=recipients= [t]=type= )
  33. local app_message
  34. local recipients
  35. local type
  36. # Manage arguments with getopts
  37. ynh_handle_getopts_args "$@"
  38. app_message="${app_message:-}"
  39. recipients="${recipients:-root}"
  40. type="${type:-install}"
  41. # Get the value of admin_mail_html
  42. admin_mail_html=$(ynh_app_setting_get $app admin_mail_html)
  43. admin_mail_html="${admin_mail_html:-0}"
  44. # Retrieve the email of users
  45. find_mails () {
  46. local list_mails="$1"
  47. local mail
  48. local recipients=" "
  49. # Read each mail in argument
  50. for mail in $list_mails
  51. do
  52. # Keep root or a real email address as it is
  53. if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
  54. then
  55. recipients="$recipients $mail"
  56. else
  57. # But replace an user name without a domain after by its email
  58. if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
  59. then
  60. recipients="$recipients $mail"
  61. fi
  62. fi
  63. done
  64. echo "$recipients"
  65. }
  66. recipients=$(find_mails "$recipients")
  67. # Subject base
  68. local mail_subject="☁️🆈🅽🅷☁️: \`$app\`"
  69. # Adapt the subject according to the type of mail required.
  70. if [ "$type" = "backup" ]; then
  71. mail_subject="$mail_subject has just been backup."
  72. elif [ "$type" = "change_url" ]; then
  73. mail_subject="$mail_subject has just been moved to a new URL!"
  74. elif [ "$type" = "remove" ]; then
  75. mail_subject="$mail_subject has just been removed!"
  76. elif [ "$type" = "restore" ]; then
  77. mail_subject="$mail_subject has just been restored!"
  78. elif [ "$type" = "upgrade" ]; then
  79. mail_subject="$mail_subject has just been upgraded!"
  80. else # install
  81. mail_subject="$mail_subject has just been installed!"
  82. fi
  83. local mail_message="This is an automated message from your beloved YunoHost server.
  84. Specific information for the application $app.
  85. $(if [ -n "$app_message" ]
  86. then
  87. cat "$app_message"
  88. else
  89. echo "...No specific information..."
  90. fi)
  91. ---
  92. Automatic diagnosis data from YunoHost
  93. __PRE_TAG1__$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')__PRE_TAG2__"
  94. # Store the message into a file for further modifications.
  95. echo "$mail_message" > mail_to_send
  96. # If a html email is required. Apply html tags to the message.
  97. if [ "$admin_mail_html" -eq 1 ]
  98. then
  99. # Insert 'br' tags at each ending of lines.
  100. ynh_replace_string "$" "<br>" mail_to_send
  101. # Insert starting HTML tags
  102. sed --in-place '1s@^@<!DOCTYPE html>\n<html>\n<head></head>\n<body>\n@' mail_to_send
  103. # Keep tabulations
  104. ynh_replace_string " " "\&#160;\&#160;" mail_to_send
  105. ynh_replace_string "\t" "\&#160;\&#160;" mail_to_send
  106. # Insert url links tags
  107. ynh_replace_string "__URL_TAG1__\(.*\)__URL_TAG2__\(.*\)__URL_TAG3__" "<a href=\"\2\">\1</a>" mail_to_send
  108. # Insert pre tags
  109. ynh_replace_string "__PRE_TAG1__" "<pre>" mail_to_send
  110. ynh_replace_string "__PRE_TAG2__" "<\pre>" mail_to_send
  111. # Insert finishing HTML tags
  112. echo -e "\n</body>\n</html>" >> mail_to_send
  113. # Otherwise, remove tags to keep a plain text.
  114. else
  115. # Remove URL tags
  116. ynh_replace_string "__URL_TAG[1,3]__" "" mail_to_send
  117. ynh_replace_string "__URL_TAG2__" ": " mail_to_send
  118. # Remove PRE tags
  119. ynh_replace_string "__PRE_TAG[1-2]__" "" mail_to_send
  120. fi
  121. # Define binary to use for mail command
  122. if [ -e /usr/bin/bsd-mailx ]
  123. then
  124. local mail_bin=/usr/bin/bsd-mailx
  125. else
  126. local mail_bin=/usr/bin/mail.mailutils
  127. fi
  128. if [ "$admin_mail_html" -eq 1 ]
  129. then
  130. content_type="text/html"
  131. else
  132. content_type="text/plain"
  133. fi
  134. # Send the email to the recipients
  135. cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients"
  136. }
  137. #=================================================
  138. ynh_maintenance_mode_ON () {
  139. # Load value of $path_url and $domain from the config if their not set
  140. if [ -z $path_url ]; then
  141. path_url=$(ynh_app_setting_get $app path)
  142. fi
  143. if [ -z $domain ]; then
  144. domain=$(ynh_app_setting_get $app domain)
  145. fi
  146. mkdir -p /var/www/html/
  147. # Create an html to serve as maintenance notice
  148. echo "<!DOCTYPE html>
  149. <html>
  150. <head>
  151. <meta http-equiv="refresh" content="3">
  152. <title>Your app $app is currently under maintenance!</title>
  153. <style>
  154. body {
  155. width: 70em;
  156. margin: 0 auto;
  157. }
  158. </style>
  159. </head>
  160. <body>
  161. <h1>Your app $app is currently under maintenance!</h1>
  162. <p>This app has been put under maintenance by your administrator at $(date)</p>
  163. <p>Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.</p>
  164. </body>
  165. </html>" > "/var/www/html/maintenance.$app.html"
  166. # Create a new nginx config file to redirect all access to the app to the maintenance notice instead.
  167. echo "# All request to the app will be redirected to ${path_url}_maintenance and fall on the maintenance notice
  168. rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/? redirect;
  169. # Use another location, to not be in conflict with the original config file
  170. location ${path_url}_maintenance/ {
  171. alias /var/www/html/ ;
  172. try_files maintenance.$app.html =503;
  173. # Include SSOWAT user panel.
  174. include conf.d/yunohost_panel.conf.inc;
  175. }" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  176. # The current config file will redirect all requests to the root of the app.
  177. # To keep the full path, we can use the following rewrite rule:
  178. # rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/\$1? redirect;
  179. # The difference will be in the $1 at the end, which keep the following queries.
  180. # But, if it works perfectly for a html request, there's an issue with any php files.
  181. # This files are treated as simple files, and will be downloaded by the browser.
  182. # Would be really be nice to be able to fix that issue. So that, when the page is reloaded after the maintenance, the user will be redirected to the real page he was.
  183. systemctl reload nginx
  184. }
  185. ynh_maintenance_mode_OFF () {
  186. # Load value of $path_url and $domain from the config if their not set
  187. if [ -z $path_url ]; then
  188. path_url=$(ynh_app_setting_get $app path)
  189. fi
  190. if [ -z $domain ]; then
  191. domain=$(ynh_app_setting_get $app domain)
  192. fi
  193. # Rewrite the nginx config file to redirect from ${path_url}_maintenance to the real url of the app.
  194. echo "rewrite ^${path_url}_maintenance/(.*)$ ${path_url}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  195. systemctl reload nginx
  196. # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
  197. sleep 4
  198. # Then remove the temporary files used for the maintenance.
  199. rm "/var/www/html/maintenance.$app.html"
  200. rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  201. systemctl reload nginx
  202. }
  203. #=================================================
  204. # Create a changelog for an app after an upgrade from the file CHANGELOG.md.
  205. #
  206. # usage: ynh_app_changelog [--format=markdown/html/plain] [--output=changelog_file] --changelog=changelog_source]
  207. # | arg: -f --format= - Format in which the changelog will be printed
  208. # markdown: Default format.
  209. # html: Turn urls into html format.
  210. # plain: Plain text changelog
  211. # | arg: -o --output= - Output file for the changelog file (Default ./changelog)
  212. # | arg: -c --changelog= - CHANGELOG.md source (Default ../CHANGELOG.md)
  213. #
  214. # The changelog is printed into the file ./changelog and ./changelog_lite
  215. ynh_app_changelog () {
  216. # Declare an array to define the options of this helper.
  217. local legacy_args=foc
  218. declare -Ar args_array=( [f]=format= [o]=output= [c]=changelog= )
  219. local format
  220. local output
  221. local changelog
  222. # Manage arguments with getopts
  223. ynh_handle_getopts_args "$@"
  224. format=${format:-markdown}
  225. output=${output:-changelog}
  226. changelog=${changelog:-../CHANGELOG.md}
  227. local original_changelog="$changelog"
  228. local temp_changelog="changelog_temp"
  229. local final_changelog="$output"
  230. if [ ! -n "$original_changelog" ]
  231. then
  232. echo "No changelog available..." > "$final_changelog"
  233. echo "No changelog available..." > "${final_changelog}_lite"
  234. return 0
  235. fi
  236. local current_version=$(ynh_read_manifest --manifest="/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" --manifest_key="version")
  237. local update_version=$(ynh_read_manifest --manifest="../manifest.json" --manifest_key="version")
  238. # Get the line of the version to update to into the changelog
  239. local update_version_line=$(grep --max-count=1 --line-number "^## \[$update_version" "$original_changelog" | cut -d':' -f1)
  240. # If there's no entry for this version yet into the changelog
  241. # Get the first available version
  242. if [ -z "$update_version_line" ]
  243. then
  244. update_version_line=$(grep --max-count=1 --line-number "^##" "$original_changelog" | cut -d':' -f1)
  245. fi
  246. # Get the length of the complete changelog.
  247. local changelog_length=$(wc --lines "$original_changelog" | awk '{print $1}')
  248. # Cut the file before the version to update to.
  249. tail --lines=$(( $changelog_length - $update_version_line + 1 )) "$original_changelog" > "$temp_changelog"
  250. # Get the length of the troncated changelog.
  251. changelog_length=$(wc --lines "$temp_changelog" | awk '{print $1}')
  252. # Get the line of the current version into the changelog
  253. # Keep only the last line found
  254. local current_version_line=$(grep --line-number "^## \[$current_version" "$temp_changelog" | cut -d':' -f1 | tail --lines=1)
  255. # If there's no entry for this version into the changelog
  256. # Get the last available version
  257. if [ -z "$current_version_line" ]
  258. then
  259. current_version_line=$(grep --line-number "^##" "$original_changelog" | cut -d':' -f1 | tail --lines=1)
  260. fi
  261. # Cut the file before the current version.
  262. # Then grep the previous version into the changelog to get the line number of the previous version
  263. local previous_version_line=$(tail --lines=$(( $changelog_length - $current_version_line )) \
  264. "$temp_changelog" | grep --max-count=1 --line-number "^## " | cut -d':' -f1)
  265. # If there's no previous version into the changelog
  266. # Go until the end of the changelog
  267. if [ -z "$previous_version_line" ]
  268. then
  269. previous_version_line=$changelog_length
  270. fi
  271. # Cut the file after the previous version to keep only the changelog between the current version and the version to update to.
  272. head --lines=$(( $current_version_line + $previous_version_line - 1 )) "$temp_changelog" | tee "$final_changelog"
  273. if [ "$format" = "html" ]
  274. then
  275. # Replace markdown links by html links
  276. ynh_replace_string --match_string="\[\(.*\)\](\(.*\)))" --replace_string="<a href=\"\2\">\1</a>)" --target_file="$final_changelog"
  277. ynh_replace_string --match_string="\[\(.*\)\](\(.*\))" --replace_string="<a href=\"\2\">\1</a>" --target_file="$final_changelog"
  278. elif [ "$format" = "plain" ]
  279. then
  280. # Change title format.
  281. ynh_replace_string --match_string="^##.*\[\(.*\)\](\(.*\)) - \(.*\)$" --replace_string="## \1 (\3) - \2" --target_file="$final_changelog"
  282. # Change modifications lines format.
  283. ynh_replace_string --match_string="^\([-*]\).*\[\(.*\)\]\(.*\)" --replace_string="\1 \2 \3" --target_file="$final_changelog"
  284. fi
  285. # else markdown. As the file is already in markdown, nothing to do.
  286. # Keep only important changes into the changelog
  287. # Remove all minor changes
  288. sed '/^-/d' "$final_changelog" > "${final_changelog}_lite"
  289. # Remove all blank lines (to keep a clear workspace)
  290. sed --in-place '/^$/d' "${final_changelog}_lite"
  291. # Add a blank line at the end
  292. echo "" >> "${final_changelog}_lite"
  293. # Clean titles if there's no significative changes
  294. local line
  295. local previous_line=""
  296. while read line <&3
  297. do
  298. if [ -n "$previous_line" ]
  299. then
  300. # Remove the line if it's a title or a blank line, and the previous one was a title as well.
  301. if ( [ "${line:0:1}" = "#" ] || [ ${#line} -eq 0 ] ) && [ "${previous_line:0:1}" = "#" ]
  302. then
  303. ynh_replace_special_string --match_string="${previous_line//[/.}" --replace_string="" --target_file="${final_changelog}_lite"
  304. fi
  305. fi
  306. previous_line="$line"
  307. done 3< "${final_changelog}_lite"
  308. # Remove all blank lines again
  309. sed --in-place '/^$/d' "${final_changelog}_lite"
  310. # Restore changelog format with blank lines
  311. ynh_replace_string --match_string="^##.*" --replace_string="\n\n&\n" --target_file="${final_changelog}_lite"
  312. # Remove the 2 first blank lines
  313. sed --in-place '1,2d' "${final_changelog}_lite"
  314. # Add a blank line at the end
  315. echo "" >> "${final_changelog}_lite"
  316. # If changelog are empty, add an info
  317. if [ $(wc --words "$final_changelog" | awk '{print $1}') -eq 0 ]
  318. then
  319. echo "No changes from the changelog..." > "$final_changelog"
  320. fi
  321. if [ $(wc --words "${final_changelog}_lite" | awk '{print $1}') -eq 0 ]
  322. then
  323. echo "No significative changes from the changelog..." > "${final_changelog}_lite"
  324. fi
  325. }
  326. #=================================================
  327. # FUTURE OFFICIAL HELPERS
  328. #=================================================