_common.sh 14 KB

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