From 3a500d8457adb54a62bc657501e39ea77b9c53e4 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 27 May 2024 23:38:22 +0200 Subject: [PATCH] helpers 2.1: remove ynh_require_ram and unused --ignore_swap and --only_swap options in ynh_get_ram --- helpers/helpers.v2.1.d/hardware | 103 -------------------------------- helpers/helpers.v2.1.d/utils | 38 ++++++++++++ 2 files changed, 38 insertions(+), 103 deletions(-) delete mode 100644 helpers/helpers.v2.1.d/hardware diff --git a/helpers/helpers.v2.1.d/hardware b/helpers/helpers.v2.1.d/hardware deleted file mode 100644 index 678f381b1..000000000 --- a/helpers/helpers.v2.1.d/hardware +++ /dev/null @@ -1,103 +0,0 @@ -#!/bin/bash - -# Get the total or free amount of RAM+swap on the system -# -# [packagingv1] -# -# usage: ynh_get_ram [--free|--total] [--ignore_swap|--only_swap] -# | arg: -f, --free - Count free RAM+swap -# | arg: -t, --total - Count total RAM+swap -# | arg: -s, --ignore_swap - Ignore swap, consider only real RAM -# | arg: -o, --only_swap - Ignore real RAM, consider only swap -# | ret: the amount of free ram, in MB (MegaBytes) -# -# Requires YunoHost version 3.8.1 or higher. -ynh_get_ram() { - # ============ Argument parsing ============= - local -A args_array=([f]=free [t]=total [s]=ignore_swap [o]=only_swap) - local free - local total - local ignore_swap - local only_swap - ynh_handle_getopts_args "$@" - ignore_swap=${ignore_swap:-0} - only_swap=${only_swap:-0} - free=${free:-0} - total=${total:-0} - # =========================================== - - if [ $free -eq $total ]; then - ynh_print_warn --message="You have to choose --free or --total when using ynh_get_ram" - ram=0 - # Use the total amount of ram - elif [ $free -eq 1 ]; then - local free_ram=$(LC_ALL=C vmstat --stats --unit M | grep "free memory" | awk '{print $1}') - local free_swap=$(LC_ALL=C vmstat --stats --unit M | grep "free swap" | awk '{print $1}') - local free_ram_swap=$((free_ram + free_swap)) - - # Use the total amount of free ram - local ram=$free_ram_swap - if [ $ignore_swap -eq 1 ]; then - # Use only the amount of free ram - ram=$free_ram - elif [ $only_swap -eq 1 ]; then - # Use only the amount of free swap - ram=$free_swap - fi - elif [ $total -eq 1 ]; then - local total_ram=$(LC_ALL=C vmstat --stats --unit M | grep "total memory" | awk '{print $1}') - local total_swap=$(LC_ALL=C vmstat --stats --unit M | grep "total swap" | awk '{print $1}') - local total_ram_swap=$((total_ram + total_swap)) - - local ram=$total_ram_swap - if [ $ignore_swap -eq 1 ]; then - # Use only the amount of free ram - ram=$total_ram - elif [ $only_swap -eq 1 ]; then - # Use only the amount of free swap - ram=$total_swap - fi - fi - - echo $ram -} - -# Return 0 or 1 depending if the system has a given amount of RAM+swap free or total -# -# [packagingv1] -# -# usage: ynh_require_ram --required=RAM [--free|--total] [--ignore_swap|--only_swap] -# | arg: -r, --required= - The amount to require, in MB -# | arg: -f, --free - Count free RAM+swap -# | arg: -t, --total - Count total RAM+swap -# | arg: -s, --ignore_swap - Ignore swap, consider only real RAM -# | arg: -o, --only_swap - Ignore real RAM, consider only swap -# | ret: 1 if the ram is under the requirement, 0 otherwise. -# -# Requires YunoHost version 3.8.1 or higher. -ynh_require_ram() { - # ============ Argument parsing ============= - local -A args_array=([r]=required= [f]=free [t]=total [s]=ignore_swap [o]=only_swap) - local required - local free - local total - local ignore_swap - local only_swap - ynh_handle_getopts_args "$@" - # 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? - # If the variable $free is not empty, set it to '--free' - 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 $required ]; then - return 1 - else - return 0 - fi -} diff --git a/helpers/helpers.v2.1.d/utils b/helpers/helpers.v2.1.d/utils index 5c3e2b2f6..f82a7c265 100644 --- a/helpers/helpers.v2.1.d/utils +++ b/helpers/helpers.v2.1.d/utils @@ -1068,3 +1068,41 @@ except socket.error: sys.exit(0) EOF } + +# Get the total or free amount of RAM+swap on the system +# +# [packagingv1] +# +# usage: ynh_get_ram [--free|--total] +# | arg: -f, --free - Count free RAM+swap +# | arg: -t, --total - Count total RAM+swap +# | ret: the amount of free ram, in MB (MegaBytes) +# +# Requires YunoHost version 3.8.1 or higher. +ynh_get_ram() { + # ============ Argument parsing ============= + local -A args_array=([f]=free [t]=total) + local free + local total + ynh_handle_getopts_args "$@" + free=${free:-0} + total=${total:-0} + # =========================================== + + if [ $free -eq $total ]; then + ynh_print_warn --message="You have to choose --free or --total when using ynh_get_ram" + ram=0 + elif [ $free -eq 1 ]; then + local free_ram=$(LC_ALL=C vmstat --stats --unit M | grep "free memory" | awk '{print $1}') + local free_swap=$(LC_ALL=C vmstat --stats --unit M | grep "free swap" | awk '{print $1}') + local free_ram_swap=$((free_ram + free_swap)) + local ram=$free_ram_swap + elif [ $total -eq 1 ]; then + local total_ram=$(LC_ALL=C vmstat --stats --unit M | grep "total memory" | awk '{print $1}') + local total_swap=$(LC_ALL=C vmstat --stats --unit M | grep "total swap" | awk '{print $1}') + local total_ram_swap=$((total_ram + total_swap)) + local ram=$total_ram_swap + fi + + echo $ram +}