_common.sh 22 KB

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