_common.sh 14 KB

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