Try to improve the semantic of RAM helper

This commit is contained in:
Alexandre Aubin 2020-04-05 23:57:43 +02:00
parent 810e5b0d09
commit cbf573c346

View file

@ -1,24 +1,25 @@
#!/bin/bash #!/bin/bash
# Check the amount of available RAM # Get the total or free amount of RAM+swap on the system
# #
# usage: ynh_check_ram [--required=RAM required in Mb] [--ignore_swap|--only_swap] [--free_ram] # usage: ynh_get_ram [--free|--total] [--ignore_swap|--only_swap]
# | arg: -r, --required= - Amount of RAM required in Mb. The helper will return 0 is there's enough RAM, or 1 otherwise. # | arg: -f, --free - Count free RAM+swap
# If --required isn't set, the helper will print the amount of RAM, in Mb. # | arg: -t, --total - Count total RAM+swap
# | arg: -s, --ignore_swap - Ignore swap # | arg: -s, --ignore_swap - Ignore swap, consider only real RAM
# | arg: -o, --only_swap - Ignore real RAM, consider only swap. # | arg: -o, --only_swap - Ignore real RAM, consider only swap
# | arg: -f, --free_ram - Count only free RAM, not the total amount of RAM available. ynh_get_ram () {
ynh_available_ram () {
# Declare an array to define the options of this helper. # Declare an array to define the options of this helper.
declare -Ar args_array=( [r]=required= [s]=ignore_swap [o]=only_swap [f]=free_ram ) declare -Ar args_array=( [f]=free [t]=total [s]=ignore_swap [o]=only_swap )
local required local free
local total
local ignore_swap local ignore_swap
local only_swap local only_swap
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
required=${required:-}
ignore_swap=${ignore_swap:-0} ignore_swap=${ignore_swap:-0}
only_swap=${only_swap:-0} only_swap=${only_swap:-0}
free=${free:-0}
total=${total:-0}
local total_ram=$(vmstat --stats --unit M | grep "total memory" | awk '{print $1}') local total_ram=$(vmstat --stats --unit M | grep "total memory" | awk '{print $1}')
local total_swap=$(vmstat --stats --unit M | grep "total swap" | awk '{print $1}') local total_swap=$(vmstat --stats --unit M | grep "total swap" | awk '{print $1}')
@ -29,11 +30,10 @@ ynh_available_ram () {
local free_ram_swap=$(( free_ram + free_swap )) local free_ram_swap=$(( free_ram + free_swap ))
# Use the total amount of ram # Use the total amount of ram
local ram=$total_ram_swap if [ $free -eq 1 ]
if [ $free_ram -eq 1 ]
then then
# Use the total amount of free ram # Use the total amount of free ram
ram=$free_ram_swap local ram=$free_ram_swap
if [ $ignore_swap -eq 1 ] if [ $ignore_swap -eq 1 ]
then then
# Use only the amount of free ram # Use only the amount of free ram
@ -43,7 +43,9 @@ ynh_available_ram () {
# Use only the amount of free swap # Use only the amount of free swap
ram=$free_swap ram=$free_swap
fi fi
else elif [ $total -eq 1 ]
then
local ram=$total_ram_swap
if [ $ignore_swap -eq 1 ] if [ $ignore_swap -eq 1 ]
then then
# Use only the amount of free ram # Use only the amount of free ram
@ -53,20 +55,46 @@ ynh_available_ram () {
# Use only the amount of free swap # Use only the amount of free swap
ram=$total_swap ram=$total_swap
fi fi
else
echo "Uhoh, you should choose --free or --total when using ynh_get_ram" >&2
ram=0
fi fi
if [ -n "$required" ] echo $ram
then }
# Return 1 if the amount of ram isn't enough.
if [ $ram -lt $required ] # Return 0 or 1 depending if the system has a given amount of RAM+swap free or total
then #
return 1 # usage: ynh_require_ram [--amount=RAM required in Mb] [--free|--total] [--ignore_swap|--only_swap]
else # | arg: -a, --amount - The amount to require, in Mb
return 0 # | arg: -f, --free - Count free RAM+swap
fi # | arg: -t, --total - Count total RAM+swap
# | arg: -s, --ignore_swap - Ignore swap, consider only real RAM
# If no RAM is required, return the amount of available ram. # | arg: -o, --only_swap - Ignore real RAM, consider only swap
else ynh_require_ram () {
echo $ram # Declare an array to define the options of this helper.
fi declare -Ar args_array=( [a]=amount= [f]=free [t]=total [s]=ignore_swap [o]=only_swap )
local amount
local free
local total
local ignore_swap
local only_swap
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
amount=${amount:-0}
# Dunno if that's the right way to do, but that's some black magic to be able to
# forward the bool args to ynh_get_ram easily?
free=${free:+--free}
total=${total:+--total}
ignore_swap=${ignore_swap:+--ignore_swap}
only_swap=${only_swap:+--only_swap}
local ram=$(ynh_get_ram $free $total $ignore_swap $only_swap)
if [ $ram -lt $amount ]
then
return 1
else
return 0
fi
} }