-
Notifications
You must be signed in to change notification settings - Fork 0
/
F-check-env.sh
56 lines (49 loc) · 1.45 KB
/
F-check-env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function check_env() {
# $1 is hostname, $2 is boolean if we should ignore FTP_.* variables
# assert $1 is hostname
if [ -z "$1" ]; then
echo "Usage: $0 <Hostname>"
exit 1
fi
# assert $1.env exists
if [ ! -f $PATH_ROOT/$1.env ]; then
echo "[WARN] File $PATH_ROOT/$1.env does not exist"
echo " Copying a template..."
cp $PATH_CORE/template.env $PATH_ROOT/$1.env
echo ""
echo "Please fill in the required environment variables in $PATH_ROOT/$1.env"
exit 2
fi
# load $PATH_ROOT/$1.env
local pwd_path=$(pwd)
cd $PATH_ROOT
set -o allexport
source $1.env
set +o allexport
cd $pwd_path
# assert $1 == $HOSTNAME
if [ "$1" != "$HOSTNAME" ]; then
echo "Hostname mismatch: $1 != $HOSTNAME"
exit 1
fi
# assert required environment variables
all_vars=$(grep -oP '^[A-Z_]+(?==)' $PATH_CORE/template.env | sort | uniq)
echo "Checking for required environment variables..."
all_set=true
for var in $all_vars; do
var=$(echo $var | sed 's/[\$\{\}]//g')
if [ -z "${!var}" ]; then
if [ "$2" = true ] && [[ $var == FTP_* ]]; then
echo "[WARN] Environment variable $var is not set"
continue
fi
echo "[ERR] Environment variable $var is not set"
all_set=false
fi
done
if [ "$all_set" = false ]; then
echo "Please fill in the required environment variables in $PATH_ROOT/$1.env"
exit 2
fi
echo "[OK] All required environment variables are set"
}