2018-05-27 21:20:39 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-03-16 09:09:09 +01:00
|
|
|
#=================================================
|
|
|
|
# COMMON VARIABLES
|
|
|
|
#=================================================
|
|
|
|
|
2020-08-14 12:29:45 +02:00
|
|
|
YNH_PHP_VERSION="7.3"
|
|
|
|
|
|
|
|
extra_php_dependencies="php${YNH_PHP_VERSION}-cli php${YNH_PHP_VERSION}-mbstring php${YNH_PHP_VERSION}-xml"
|
2019-03-16 09:09:09 +01:00
|
|
|
|
2018-05-27 21:20:39 +02:00
|
|
|
#=================================================
|
|
|
|
# EXPERIMENTAL HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
2019-05-01 20:35:48 +02:00
|
|
|
# Check available space before creating a temp directory.
|
2018-05-27 21:20:39 +02:00
|
|
|
#
|
2019-05-01 20:35:48 +02:00
|
|
|
# usage: ynh_smart_mktemp --min_size="Min size"
|
2018-05-27 21:20:39 +02:00
|
|
|
#
|
2019-05-01 20:35:48 +02:00
|
|
|
# | arg: -s, --min_size= - Minimal size needed for the temporary directory, in Mb
|
2019-03-17 12:34:20 +01:00
|
|
|
ynh_smart_mktemp () {
|
2019-05-01 20:35:48 +02:00
|
|
|
# Declare an array to define the options of this helper.
|
|
|
|
declare -Ar args_array=( [s]=min_size= )
|
|
|
|
local min_size
|
|
|
|
# Manage arguments with getopts
|
|
|
|
ynh_handle_getopts_args "$@"
|
|
|
|
|
|
|
|
min_size="${min_size:-300}"
|
2019-03-17 12:34:20 +01:00
|
|
|
# Transform the minimum size from megabytes to kilobytes
|
|
|
|
min_size=$(( $min_size * 1024 ))
|
|
|
|
|
|
|
|
# Check if there's enough free space in a directory
|
|
|
|
is_there_enough_space () {
|
|
|
|
local free_space=$(df --output=avail "$1" | sed 1d)
|
|
|
|
test $free_space -ge $min_size
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_there_enough_space /tmp; then
|
|
|
|
local tmpdir=/tmp
|
|
|
|
elif is_there_enough_space /var; then
|
|
|
|
local tmpdir=/var
|
|
|
|
elif is_there_enough_space /; then
|
2019-05-01 20:35:48 +02:00
|
|
|
local tmpdir=/
|
2019-03-17 12:34:20 +01:00
|
|
|
elif is_there_enough_space /home; then
|
|
|
|
local tmpdir=/home
|
|
|
|
else
|
2019-05-01 20:35:48 +02:00
|
|
|
ynh_die "Insufficient free space to continue..."
|
2019-03-17 12:34:20 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$(sudo mktemp --directory --tmpdir="$tmpdir")"
|
|
|
|
}
|
2019-05-01 20:35:48 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Execute a command as another user
|
|
|
|
# usage: ynh_exec_as USER COMMAND [ARG ...]
|
|
|
|
ynh_exec_as() {
|
|
|
|
local USER=$1
|
|
|
|
shift 1
|
|
|
|
|
|
|
|
if [[ $USER = $(whoami) ]]; then
|
|
|
|
eval "$@"
|
|
|
|
else
|
|
|
|
sudo -u "$USER" "$@"
|
|
|
|
fi
|
2018-05-27 21:20:39 +02:00
|
|
|
}
|