_common.sh 14 KB

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