2
0

_common.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. #!/bin/bash
  2. #=================================================
  3. # FUTUR OFFICIAL HELPERS
  4. #=================================================
  5. # Install or update the main directory yunohost.multimedia
  6. #
  7. # usage: ynh_multimedia_build_main_dir
  8. ynh_multimedia_build_main_dir () {
  9. local ynh_media_release="v1.2"
  10. local checksum="806a827ba1902d6911095602a9221181"
  11. # Download yunohost.multimedia scripts
  12. wget -nv https://github.com/YunoHost-Apps/yunohost.multimedia/archive/${ynh_media_release}.tar.gz
  13. # Check the control sum
  14. echo "${checksum} ${ynh_media_release}.tar.gz" | md5sum -c --status \
  15. || ynh_die "Corrupt source"
  16. # Check if the package acl is installed. Or install it.
  17. ynh_package_is_installed 'acl' \
  18. || ynh_package_install acl
  19. # Extract
  20. mkdir yunohost.multimedia-master
  21. tar -xf ${ynh_media_release}.tar.gz -C yunohost.multimedia-master --strip-components 1
  22. ./yunohost.multimedia-master/script/ynh_media_build.sh
  23. }
  24. # Add a directory in yunohost.multimedia
  25. # This "directory" will be a symbolic link to a existing directory.
  26. #
  27. # usage: ynh_multimedia_addfolder "Source directory" "Destination directory"
  28. #
  29. # | arg: -s, --source_dir= - Source directory - The real directory which contains your medias.
  30. # | arg: -d, --dest_dir= - Destination directory - The name and the place of the symbolic link, relative to "/home/yunohost.multimedia"
  31. ynh_multimedia_addfolder () {
  32. # Declare an array to define the options of this helper.
  33. declare -Ar args_array=( [s]=source_dir= [d]=dest_dir= )
  34. local source_dir
  35. local dest_dir
  36. # Manage arguments with getopts
  37. ynh_handle_getopts_args "$@"
  38. ./yunohost.multimedia-master/script/ynh_media_addfolder.sh --source="$source_dir" --dest="$dest_dir"
  39. }
  40. # Move a directory in yunohost.multimedia, and replace by a symbolic link
  41. #
  42. # usage: ynh_multimedia_movefolder "Source directory" "Destination directory"
  43. #
  44. # | arg: -s, --source_dir= - Source directory - The real directory which contains your medias.
  45. # It will be moved to "Destination directory"
  46. # A symbolic link will replace it.
  47. # | arg: -d, --dest_dir= - Destination directory - The new name and place of the directory, relative to "/home/yunohost.multimedia"
  48. ynh_multimedia_movefolder () {
  49. # Declare an array to define the options of this helper.
  50. declare -Ar args_array=( [s]=source_dir= [d]=dest_dir= )
  51. local source_dir
  52. local dest_dir
  53. # Manage arguments with getopts
  54. ynh_handle_getopts_args "$@"
  55. ./yunohost.multimedia-master/script/ynh_media_addfolder.sh --inv --source="$source_dir" --dest="$dest_dir"
  56. }
  57. # Allow an user to have an write authorisation in multimedia directories
  58. #
  59. # usage: ynh_multimedia_addaccess user_name
  60. #
  61. # | arg: -u, --user_name= - The name of the user which gain this access.
  62. ynh_multimedia_addaccess () {
  63. # Declare an array to define the options of this helper.
  64. declare -Ar args_array=( [u]=user_name=)
  65. local user_name
  66. # Manage arguments with getopts
  67. ynh_handle_getopts_args "$@"
  68. groupadd -f multimedia
  69. usermod -a -G multimedia $user_name
  70. }
  71. #=================================================
  72. # Get the total or free amount of RAM+swap on the system
  73. #
  74. # usage: ynh_get_ram [--free|--total] [--ignore_swap|--only_swap]
  75. # | arg: -f, --free - Count free RAM+swap
  76. # | arg: -t, --total - Count total RAM+swap
  77. # | arg: -s, --ignore_swap - Ignore swap, consider only real RAM
  78. # | arg: -o, --only_swap - Ignore real RAM, consider only swap
  79. ynh_get_ram () {
  80. # Declare an array to define the options of this helper.
  81. local legacy_args=ftso
  82. declare -Ar args_array=( [f]=free [t]=total [s]=ignore_swap [o]=only_swap )
  83. local free
  84. local total
  85. local ignore_swap
  86. local only_swap
  87. # Manage arguments with getopts
  88. ynh_handle_getopts_args "$@"
  89. ignore_swap=${ignore_swap:-0}
  90. only_swap=${only_swap:-0}
  91. free=${free:-0}
  92. total=${total:-0}
  93. local total_ram=$(vmstat --stats --unit M | grep "total memory" | awk '{print $1}')
  94. local total_swap=$(vmstat --stats --unit M | grep "total swap" | awk '{print $1}')
  95. local total_ram_swap=$(( total_ram + total_swap ))
  96. local free_ram=$(vmstat --stats --unit M | grep "free memory" | awk '{print $1}')
  97. local free_swap=$(vmstat --stats --unit M | grep "free swap" | awk '{print $1}')
  98. local free_ram_swap=$(( free_ram + free_swap ))
  99. # Use the total amount of ram
  100. if [ $free -eq 1 ]
  101. then
  102. # Use the total amount of free ram
  103. local ram=$free_ram_swap
  104. if [ $ignore_swap -eq 1 ]
  105. then
  106. # Use only the amount of free ram
  107. ram=$free_ram
  108. elif [ $only_swap -eq 1 ]
  109. then
  110. # Use only the amount of free swap
  111. ram=$free_swap
  112. fi
  113. elif [ $total -eq 1 ]
  114. then
  115. local ram=$total_ram_swap
  116. if [ $ignore_swap -eq 1 ]
  117. then
  118. # Use only the amount of free ram
  119. ram=$total_ram
  120. elif [ $only_swap -eq 1 ]
  121. then
  122. # Use only the amount of free swap
  123. ram=$total_swap
  124. fi
  125. else
  126. ynh_print_warn --message="You have to choose --free or --total when using ynh_get_ram"
  127. ram=0
  128. fi
  129. echo $ram
  130. }
  131. # Return 0 or 1 depending if the system has a given amount of RAM+swap free or total
  132. #
  133. # usage: ynh_require_ram --required=RAM required in Mb [--free|--total] [--ignore_swap|--only_swap]
  134. # | arg: -r, --required - The amount to require, in Mb
  135. # | arg: -f, --free - Count free RAM+swap
  136. # | arg: -t, --total - Count total RAM+swap
  137. # | arg: -s, --ignore_swap - Ignore swap, consider only real RAM
  138. # | arg: -o, --only_swap - Ignore real RAM, consider only swap
  139. ynh_require_ram () {
  140. # Declare an array to define the options of this helper.
  141. local legacy_args=rftso
  142. declare -Ar args_array=( [r]=required= [f]=free [t]=total [s]=ignore_swap [o]=only_swap )
  143. local required
  144. local free
  145. local total
  146. local ignore_swap
  147. local only_swap
  148. # Manage arguments with getopts
  149. ynh_handle_getopts_args "$@"
  150. # Dunno if that's the right way to do, but that's some black magic to be able to
  151. # forward the bool args to ynh_get_ram easily?
  152. # If the variable $free is not empty, set it to '--free'
  153. free=${free:+--free}
  154. total=${total:+--total}
  155. ignore_swap=${ignore_swap:+--ignore_swap}
  156. only_swap=${only_swap:+--only_swap}
  157. local ram=$(ynh_get_ram $free $total $ignore_swap $only_swap)
  158. if [ $ram -lt $required ]
  159. then
  160. return 1
  161. else
  162. return 0
  163. fi
  164. }
  165. #=================================================
  166. # Define the values to configure php-fpm
  167. #
  168. # usage: ynh_get_scalable_phpfpm --usage=usage --footprint=footprint [--print]
  169. # | arg: -f, --footprint - Memory footprint of the service (low/medium/high).
  170. # low - Less than 20Mb of ram by pool.
  171. # medium - Between 20Mb and 40Mb of ram by pool.
  172. # high - More than 40Mb of ram by pool.
  173. # Or specify exactly the footprint, the load of the service as Mb by pool instead of having a standard value.
  174. # To have this value, use the following command and stress the service.
  175. # watch -n0.5 ps -o user,cmd,%cpu,rss -u APP
  176. #
  177. # | arg: -u, --usage - Expected usage of the service (low/medium/high).
  178. # low - Personal usage, behind the sso.
  179. # medium - Low usage, few people or/and publicly accessible.
  180. # high - High usage, frequently visited website.
  181. #
  182. # | arg: -p, --print - Print the result
  183. #
  184. #
  185. #
  186. # The footprint of the service will be used to defined the maximum footprint we can allow, which is half the maximum RAM.
  187. # So it will be used to defined 'pm.max_children'
  188. # A lower value for the footprint will allow more children for 'pm.max_children'. And so for
  189. # 'pm.start_servers', 'pm.min_spare_servers' and 'pm.max_spare_servers' which are defined from the
  190. # value of 'pm.max_children'
  191. # NOTE: 'pm.max_children' can't exceed 4 times the number of processor's cores.
  192. #
  193. # The usage value will defined the way php will handle the children for the pool.
  194. # A value set as 'low' will set the process manager to 'ondemand'. Children will start only if the
  195. # service is used, otherwise no child will stay alive. This config gives the lower footprint when the
  196. # service is idle. But will use more proc since it has to start a child as soon it's used.
  197. # Set as 'medium', the process manager will be at dynamic. If the service is idle, a number of children
  198. # equal to pm.min_spare_servers will stay alive. So the service can be quick to answer to any request.
  199. # The number of children can grow if needed. The footprint can stay low if the service is idle, but
  200. # not null. The impact on the proc is a little bit less than 'ondemand' as there's always a few
  201. # children already available.
  202. # Set as 'high', the process manager will be set at 'static'. There will be always as many children as
  203. # 'pm.max_children', the footprint is important (but will be set as maximum a quarter of the maximum
  204. # RAM) but the impact on the proc is lower. The service will be quick to answer as there's always many
  205. # children ready to answer.
  206. ynh_get_scalable_phpfpm () {
  207. local legacy_args=ufp
  208. # Declare an array to define the options of this helper.
  209. declare -Ar args_array=( [u]=usage= [f]=footprint= [p]=print )
  210. local usage
  211. local footprint
  212. local print
  213. # Manage arguments with getopts
  214. ynh_handle_getopts_args "$@"
  215. # Set all characters as lowercase
  216. footprint=${footprint,,}
  217. usage=${usage,,}
  218. print=${print:-0}
  219. if [ "$footprint" = "low" ]
  220. then
  221. footprint=20
  222. elif [ "$footprint" = "medium" ]
  223. then
  224. footprint=35
  225. elif [ "$footprint" = "high" ]
  226. then
  227. footprint=50
  228. fi
  229. # Define the way the process manager handle child processes.
  230. if [ "$usage" = "low" ]
  231. then
  232. php_pm=ondemand
  233. elif [ "$usage" = "medium" ]
  234. then
  235. php_pm=dynamic
  236. elif [ "$usage" = "high" ]
  237. then
  238. php_pm=static
  239. else
  240. ynh_die --message="Does not recognize '$usage' as an usage value."
  241. fi
  242. # Get the total of RAM available, except swap.
  243. local max_ram=$(ynh_get_ram --total --ignore_swap)
  244. at_least_one() {
  245. # Do not allow value below 1
  246. if [ $1 -le 0 ]
  247. then
  248. echo 1
  249. else
  250. echo $1
  251. fi
  252. }
  253. # Define pm.max_children
  254. # The value of pm.max_children is the total amount of ram divide by 2 and divide again by the footprint of a pool for this app.
  255. # So if php-fpm start the maximum of children, it won't exceed half of the ram.
  256. php_max_children=$(( $max_ram / 2 / $footprint ))
  257. # If process manager is set as static, use half less children.
  258. # Used as static, there's always as many children as the value of pm.max_children
  259. if [ "$php_pm" = "static" ]
  260. then
  261. php_max_children=$(( $php_max_children / 2 ))
  262. fi
  263. php_max_children=$(at_least_one $php_max_children)
  264. # To not overload the proc, limit the number of children to 4 times the number of cores.
  265. local core_number=$(nproc)
  266. local max_proc=$(( $core_number * 4 ))
  267. if [ $php_max_children -gt $max_proc ]
  268. then
  269. php_max_children=$max_proc
  270. fi
  271. if [ "$php_pm" = "dynamic" ]
  272. then
  273. # Define pm.start_servers, pm.min_spare_servers and pm.max_spare_servers for a dynamic process manager
  274. php_min_spare_servers=$(( $php_max_children / 8 ))
  275. php_min_spare_servers=$(at_least_one $php_min_spare_servers)
  276. php_max_spare_servers=$(( $php_max_children / 2 ))
  277. php_max_spare_servers=$(at_least_one $php_max_spare_servers)
  278. php_start_servers=$(( $php_min_spare_servers + ( $php_max_spare_servers - $php_min_spare_servers ) /2 ))
  279. php_start_servers=$(at_least_one $php_start_servers)
  280. else
  281. php_min_spare_servers=0
  282. php_max_spare_servers=0
  283. php_start_servers=0
  284. fi
  285. if [ $print -eq 1 ]
  286. then
  287. ynh_debug --message="Footprint=${footprint}Mb by pool."
  288. ynh_debug --message="Process manager=$php_pm"
  289. ynh_debug --message="Max RAM=${max_ram}Mb"
  290. if [ "$php_pm" != "static" ]; then
  291. ynh_debug --message="\nMax estimated footprint=$(( $php_max_children * $footprint ))"
  292. ynh_debug --message="Min estimated footprint=$(( $php_min_spare_servers * $footprint ))"
  293. fi
  294. if [ "$php_pm" = "dynamic" ]; then
  295. ynh_debug --message="Estimated average footprint=$(( $php_max_spare_servers * $footprint ))"
  296. elif [ "$php_pm" = "static" ]; then
  297. ynh_debug --message="Estimated footprint=$(( $php_max_children * $footprint ))"
  298. fi
  299. ynh_debug --message="\nRaw php-fpm values:"
  300. ynh_debug --message="pm.max_children = $php_max_children"
  301. if [ "$php_pm" = "dynamic" ]; then
  302. ynh_debug --message="pm.start_servers = $php_start_servers"
  303. ynh_debug --message="pm.min_spare_servers = $php_min_spare_servers"
  304. ynh_debug --message="pm.max_spare_servers = $php_max_spare_servers"
  305. fi
  306. fi
  307. }
  308. #=================================================
  309. # EXPERIMENTAL HELPERS
  310. #=================================================
  311. # Send an email to inform the administrator
  312. #
  313. # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type]
  314. # | arg: -m --app_message= - The file with the content to send to the administrator.
  315. # | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root
  316. # example: "root admin@domain"
  317. # If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
  318. # example: "root admin@domain user1 user2"
  319. # | arg: -t, --type= - Type of mail, could be 'backup', 'change_url', 'install', 'remove', 'restore', 'upgrade'
  320. ynh_send_readme_to_admin() {
  321. # Declare an array to define the options of this helper.
  322. declare -Ar args_array=( [m]=app_message= [r]=recipients= [t]=type= )
  323. local app_message
  324. local recipients
  325. local type
  326. # Manage arguments with getopts
  327. ynh_handle_getopts_args "$@"
  328. app_message="${app_message:-}"
  329. recipients="${recipients:-root}"
  330. type="${type:-install}"
  331. # Get the value of admin_mail_html
  332. admin_mail_html=$(ynh_app_setting_get $app admin_mail_html)
  333. admin_mail_html="${admin_mail_html:-0}"
  334. # Retrieve the email of users
  335. find_mails () {
  336. local list_mails="$1"
  337. local mail
  338. local recipients=" "
  339. # Read each mail in argument
  340. for mail in $list_mails
  341. do
  342. # Keep root or a real email address as it is
  343. if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
  344. then
  345. recipients="$recipients $mail"
  346. else
  347. # But replace an user name without a domain after by its email
  348. if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
  349. then
  350. recipients="$recipients $mail"
  351. fi
  352. fi
  353. done
  354. echo "$recipients"
  355. }
  356. recipients=$(find_mails "$recipients")
  357. # Subject base
  358. local mail_subject="☁️🆈🅽🅷☁️: \`$app\`"
  359. # Adapt the subject according to the type of mail required.
  360. if [ "$type" = "backup" ]; then
  361. mail_subject="$mail_subject has just been backup."
  362. elif [ "$type" = "change_url" ]; then
  363. mail_subject="$mail_subject has just been moved to a new URL!"
  364. elif [ "$type" = "remove" ]; then
  365. mail_subject="$mail_subject has just been removed!"
  366. elif [ "$type" = "restore" ]; then
  367. mail_subject="$mail_subject has just been restored!"
  368. elif [ "$type" = "upgrade" ]; then
  369. mail_subject="$mail_subject has just been upgraded!"
  370. else # install
  371. mail_subject="$mail_subject has just been installed!"
  372. fi
  373. local mail_message="This is an automated message from your beloved YunoHost server.
  374. Specific information for the application $app.
  375. $(if [ -n "$app_message" ]
  376. then
  377. cat "$app_message"
  378. else
  379. echo "...No specific information..."
  380. fi)
  381. ---
  382. Automatic diagnosis data from YunoHost
  383. __PRE_TAG1__$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')__PRE_TAG2__"
  384. # Store the message into a file for further modifications.
  385. echo "$mail_message" > mail_to_send
  386. # If a html email is required. Apply html tags to the message.
  387. if [ "$admin_mail_html" -eq 1 ]
  388. then
  389. # Insert 'br' tags at each ending of lines.
  390. ynh_replace_string "$" "<br>" mail_to_send
  391. # Insert starting HTML tags
  392. sed --in-place '1s@^@<!DOCTYPE html>\n<html>\n<head></head>\n<body>\n@' mail_to_send
  393. # Keep tabulations
  394. ynh_replace_string " " "\&#160;\&#160;" mail_to_send
  395. ynh_replace_string "\t" "\&#160;\&#160;" mail_to_send
  396. # Insert url links tags
  397. ynh_replace_string "__URL_TAG1__\(.*\)__URL_TAG2__\(.*\)__URL_TAG3__" "<a href=\"\2\">\1</a>" mail_to_send
  398. # Insert pre tags
  399. ynh_replace_string "__PRE_TAG1__" "<pre>" mail_to_send
  400. ynh_replace_string "__PRE_TAG2__" "<\pre>" mail_to_send
  401. # Insert finishing HTML tags
  402. echo -e "\n</body>\n</html>" >> mail_to_send
  403. # Otherwise, remove tags to keep a plain text.
  404. else
  405. # Remove URL tags
  406. ynh_replace_string "__URL_TAG[1,3]__" "" mail_to_send
  407. ynh_replace_string "__URL_TAG2__" ": " mail_to_send
  408. # Remove PRE tags
  409. ynh_replace_string "__PRE_TAG[1-2]__" "" mail_to_send
  410. fi
  411. # Define binary to use for mail command
  412. if [ -e /usr/bin/bsd-mailx ]
  413. then
  414. local mail_bin=/usr/bin/bsd-mailx
  415. else
  416. local mail_bin=/usr/bin/mail.mailutils
  417. fi
  418. if [ "$admin_mail_html" -eq 1 ]
  419. then
  420. content_type="text/html"
  421. else
  422. content_type="text/plain"
  423. fi
  424. # Send the email to the recipients
  425. cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients"
  426. }
  427. #=================================================
  428. ynh_maintenance_mode_ON () {
  429. # Load value of $path_url and $domain from the config if their not set
  430. if [ -z $path_url ]; then
  431. path_url=$(ynh_app_setting_get $app path)
  432. fi
  433. if [ -z $domain ]; then
  434. domain=$(ynh_app_setting_get $app domain)
  435. fi
  436. mkdir -p /var/www/html/
  437. # Create an html to serve as maintenance notice
  438. echo "<!DOCTYPE html>
  439. <html>
  440. <head>
  441. <meta http-equiv="refresh" content="3">
  442. <title>Your app $app is currently under maintenance!</title>
  443. <style>
  444. body {
  445. width: 70em;
  446. margin: 0 auto;
  447. }
  448. </style>
  449. </head>
  450. <body>
  451. <h1>Your app $app is currently under maintenance!</h1>
  452. <p>This app has been put under maintenance by your administrator at $(date)</p>
  453. <p>Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.</p>
  454. </body>
  455. </html>" > "/var/www/html/maintenance.$app.html"
  456. # Create a new nginx config file to redirect all access to the app to the maintenance notice instead.
  457. echo "# All request to the app will be redirected to ${path_url}_maintenance and fall on the maintenance notice
  458. rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/? redirect;
  459. # Use another location, to not be in conflict with the original config file
  460. location ${path_url}_maintenance/ {
  461. alias /var/www/html/ ;
  462. try_files maintenance.$app.html =503;
  463. # Include SSOWAT user panel.
  464. include conf.d/yunohost_panel.conf.inc;
  465. }" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  466. # The current config file will redirect all requests to the root of the app.
  467. # To keep the full path, we can use the following rewrite rule:
  468. # rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/\$1? redirect;
  469. # The difference will be in the $1 at the end, which keep the following queries.
  470. # But, if it works perfectly for a html request, there's an issue with any php files.
  471. # This files are treated as simple files, and will be downloaded by the browser.
  472. # 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.
  473. systemctl reload nginx
  474. }
  475. ynh_maintenance_mode_OFF () {
  476. # Load value of $path_url and $domain from the config if their not set
  477. if [ -z $path_url ]; then
  478. path_url=$(ynh_app_setting_get $app path)
  479. fi
  480. if [ -z $domain ]; then
  481. domain=$(ynh_app_setting_get $app domain)
  482. fi
  483. # Rewrite the nginx config file to redirect from ${path_url}_maintenance to the real url of the app.
  484. echo "rewrite ^${path_url}_maintenance/(.*)$ ${path_url}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  485. systemctl reload nginx
  486. # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
  487. sleep 4
  488. # Then remove the temporary files used for the maintenance.
  489. rm "/var/www/html/maintenance.$app.html"
  490. rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  491. systemctl reload nginx
  492. }
  493. #=================================================
  494. # Create a changelog for an app after an upgrade from the file CHANGELOG.md.
  495. #
  496. # usage: ynh_app_changelog [--format=markdown/html/plain] [--output=changelog_file] --changelog=changelog_source]
  497. # | arg: -f --format= - Format in which the changelog will be printed
  498. # markdown: Default format.
  499. # html: Turn urls into html format.
  500. # plain: Plain text changelog
  501. # | arg: -o --output= - Output file for the changelog file (Default ./changelog)
  502. # | arg: -c --changelog= - CHANGELOG.md source (Default ../CHANGELOG.md)
  503. #
  504. # The changelog is printed into the file ./changelog and ./changelog_lite
  505. ynh_app_changelog () {
  506. # Declare an array to define the options of this helper.
  507. local legacy_args=foc
  508. declare -Ar args_array=( [f]=format= [o]=output= [c]=changelog= )
  509. local format
  510. local output
  511. local changelog
  512. # Manage arguments with getopts
  513. ynh_handle_getopts_args "$@"
  514. format=${format:-markdown}
  515. output=${output:-changelog}
  516. changelog=${changelog:-../CHANGELOG.md}
  517. local original_changelog="$changelog"
  518. local temp_changelog="changelog_temp"
  519. local final_changelog="$output"
  520. if [ ! -n "$original_changelog" ]
  521. then
  522. echo "No changelog available..." > "$final_changelog"
  523. echo "No changelog available..." > "${final_changelog}_lite"
  524. return 0
  525. fi
  526. local current_version=$(ynh_read_manifest --manifest="/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" --manifest_key="version")
  527. local update_version=$(ynh_read_manifest --manifest="../manifest.json" --manifest_key="version")
  528. # Get the line of the version to update to into the changelog
  529. local update_version_line=$(grep --max-count=1 --line-number "^## \[$update_version" "$original_changelog" | cut -d':' -f1)
  530. # If there's no entry for this version yet into the changelog
  531. # Get the first available version
  532. if [ -z "$update_version_line" ]
  533. then
  534. update_version_line=$(grep --max-count=1 --line-number "^##" "$original_changelog" | cut -d':' -f1)
  535. fi
  536. # Get the length of the complete changelog.
  537. local changelog_length=$(wc --lines "$original_changelog" | awk '{print $1}')
  538. # Cut the file before the version to update to.
  539. tail --lines=$(( $changelog_length - $update_version_line + 1 )) "$original_changelog" > "$temp_changelog"
  540. # Get the length of the troncated changelog.
  541. changelog_length=$(wc --lines "$temp_changelog" | awk '{print $1}')
  542. # Get the line of the current version into the changelog
  543. # Keep only the last line found
  544. local current_version_line=$(grep --line-number "^## \[$current_version" "$temp_changelog" | cut -d':' -f1 | tail --lines=1)
  545. # If there's no entry for this version into the changelog
  546. # Get the last available version
  547. if [ -z "$current_version_line" ]
  548. then
  549. current_version_line=$(grep --line-number "^##" "$original_changelog" | cut -d':' -f1 | tail --lines=1)
  550. fi
  551. # Cut the file before the current version.
  552. # Then grep the previous version into the changelog to get the line number of the previous version
  553. local previous_version_line=$(tail --lines=$(( $changelog_length - $current_version_line )) \
  554. "$temp_changelog" | grep --max-count=1 --line-number "^## " | cut -d':' -f1)
  555. # If there's no previous version into the changelog
  556. # Go until the end of the changelog
  557. if [ -z "$previous_version_line" ]
  558. then
  559. previous_version_line=$changelog_length
  560. fi
  561. # Cut the file after the previous version to keep only the changelog between the current version and the version to update to.
  562. head --lines=$(( $current_version_line + $previous_version_line - 1 )) "$temp_changelog" | tee "$final_changelog"
  563. if [ "$format" = "html" ]
  564. then
  565. # Replace markdown links by html links
  566. ynh_replace_string --match_string="\[\(.*\)\](\(.*\)))" --replace_string="<a href=\"\2\">\1</a>)" --target_file="$final_changelog"
  567. ynh_replace_string --match_string="\[\(.*\)\](\(.*\))" --replace_string="<a href=\"\2\">\1</a>" --target_file="$final_changelog"
  568. elif [ "$format" = "plain" ]
  569. then
  570. # Change title format.
  571. ynh_replace_string --match_string="^##.*\[\(.*\)\](\(.*\)) - \(.*\)$" --replace_string="## \1 (\3) - \2" --target_file="$final_changelog"
  572. # Change modifications lines format.
  573. ynh_replace_string --match_string="^\([-*]\).*\[\(.*\)\]\(.*\)" --replace_string="\1 \2 \3" --target_file="$final_changelog"
  574. fi
  575. # else markdown. As the file is already in markdown, nothing to do.
  576. # Keep only important changes into the changelog
  577. # Remove all minor changes
  578. sed '/^-/d' "$final_changelog" > "${final_changelog}_lite"
  579. # Remove all blank lines (to keep a clear workspace)
  580. sed --in-place '/^$/d' "${final_changelog}_lite"
  581. # Add a blank line at the end
  582. echo "" >> "${final_changelog}_lite"
  583. # Clean titles if there's no significative changes
  584. local line
  585. local previous_line=""
  586. while read line <&3
  587. do
  588. if [ -n "$previous_line" ]
  589. then
  590. # Remove the line if it's a title or a blank line, and the previous one was a title as well.
  591. if ( [ "${line:0:1}" = "#" ] || [ ${#line} -eq 0 ] ) && [ "${previous_line:0:1}" = "#" ]
  592. then
  593. ynh_replace_special_string --match_string="${previous_line//[/.}" --replace_string="" --target_file="${final_changelog}_lite"
  594. fi
  595. fi
  596. previous_line="$line"
  597. done 3< "${final_changelog}_lite"
  598. # Remove all blank lines again
  599. sed --in-place '/^$/d' "${final_changelog}_lite"
  600. # Restore changelog format with blank lines
  601. ynh_replace_string --match_string="^##.*" --replace_string="\n\n&\n" --target_file="${final_changelog}_lite"
  602. # Remove the 2 first blank lines
  603. sed --in-place '1,2d' "${final_changelog}_lite"
  604. # Add a blank line at the end
  605. echo "" >> "${final_changelog}_lite"
  606. # If changelog are empty, add an info
  607. if [ $(wc --words "$final_changelog" | awk '{print $1}') -eq 0 ]
  608. then
  609. echo "No changes from the changelog..." > "$final_changelog"
  610. fi
  611. if [ $(wc --words "${final_changelog}_lite" | awk '{print $1}') -eq 0 ]
  612. then
  613. echo "No significative changes from the changelog..." > "${final_changelog}_lite"
  614. fi
  615. }
  616. #=================================================
  617. # Execute a command as another user
  618. # usage: exec_as USER COMMAND [ARG ...]
  619. ynh_exec_as() {
  620. local USER=$1
  621. shift 1
  622. if [[ $USER = $(whoami) ]]; then
  623. eval "$@"
  624. else
  625. sudo -u "$USER" "$@"
  626. fi
  627. }