forked from coppit/docker-no-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_config_file.sh
182 lines (137 loc) · 4.97 KB
/
parse_config_file.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
# Container-specific stuff first
REQUIRED_SETTINGS="USERNAME PASSWORD DOMAINS"
DEFAULT_SETTINGS="INTERVAL=30m DEBUG=false"
TEMPLATE_CONFIG_FILE=/files/noip.conf
CONFIG_FILE=/config/noip.conf
GENERATED_CONFIG_FILE=/config/no-ip2.generated.conf
BREADCRUMB_FILE=/config/no-ip2.generated.conf.breadcrumb
EXPECT_SCRIPT=/files/create_config.exp
#-----------------------------------------------------------------------------------------------------------------------
validate_values() {
if [ $(all_required_settings_exist) != true ]
then
echo "Missing required settings, which must be provided in the config file or by environment variables:"
echo "$REQUIRED_SETTINGS"
exit 0
fi
if [[ ! "$INTERVAL" =~ ^[0-9]+[mhd]$ ]]; then
echo "INTERVAL must be a number followed by m, h, or d. Example: 5m"
exit 2
fi
if [[ "${INTERVAL: -1}" == 'm' && "${INTERVAL:0:-1}" -lt 5 ]]; then
echo "The shortest allowed INTERVAL is 5 minutes"
exit 2
fi
if [ ${INTERVAL: -1} == 'm' ]
then
INTERVAL="${INTERVAL:: -1}"
elif [ ${INTERVAL: -1} == 'h' ]
then
INTERVAL="$((${INTERVAL:: -1} * 60))"
# Days
else
INTERVAL="$((${INTERVAL:: -1} * 60 * 24))"
fi
}
#-----------------------------------------------------------------------------------------------------------------------
print_config() {
echo "Configuration:"
echo " USERNAME=$USERNAME"
echo " PASSWORD=<hidden>"
echo " DOMAINS=$DOMAINS"
echo " INTERVAL=$INTERVAL"
echo " DEBUG=$DEBUG"
}
#-----------------------------------------------------------------------------------------------------------------------
generate_binary_config() {
current_expect_script_md5=$(md5sum "$EXPECT_SCRIPT" | awk '{print $1}')
current_md5=$(echo "$current_expect_script_md5|$USERNAME|$PASSWORD|$DOMAINS|$INTERVAL" | md5sum | awk '{print $1}')
if [[ -f "$BREADCRUMB_FILE" ]]
then
breadcrumb_md5=$(cat "$BREADCRUMB_FILE")
else
breadcrumb_md5=""
fi
if [[ "$current_md5" == "$breadcrumb_md5" ]]
then
echo "Settings have not changed, so no need to regenerate the binary config file."
return
fi
if ! expect "$EXPECT_SCRIPT" "$USERNAME" "$PASSWORD" "$DOMAINS" "$INTERVAL"
then
echo "Failed to create noip2 configuration file $GENERATED_CONFIG_FILE. Exiting"
exit 4
fi
echo -n "$current_md5" > "$BREADCRUMB_FILE"
}
########################################################################################################################
ENV_VARS=/etc/envvars
MERGED_ENV_VARS=/etc/envvars.merged
#-----------------------------------------------------------------------------------------------------------------------
all_required_settings_exist() {
ALL_REQUIRED_SETTINGS_EXIST=true
for required_setting in $REQUIRED_SETTINGS
do
if [ -z "$(eval "echo \$$required_setting")" ]
then
ALL_REQUIRED_SETTINGS_EXIST=false
break
fi
done
echo $ALL_REQUIRED_SETTINGS_EXIST
}
#-----------------------------------------------------------------------------------------------------------------------
create_and_validate_config_file() {
# Search for config file. If it doesn't exist, copy the default one
if [ ! -f "$CONFIG_FILE" ]; then
echo "Creating config file. Please do not forget to edit it to specify your settings!"
cp "$TEMPLATE_CONFIG_FILE" "$CONFIG_FILE"
chmod a+w "$CONFIG_FILE"
exit 1
fi
# Check to see if they didn't edit the config file
if diff "$TEMPLATE_CONFIG_FILE" "$CONFIG_FILE" >/dev/null
then
echo "Please edit the config file to specify your settings"
exit 3
fi
# Translate line endings, since they may have edited the file in Windows
PROCESSED_CONFIG_FILE=$(mktemp)
tr -d '\r' < "$CONFIG_FILE" > "$PROCESSED_CONFIG_FILE"
echo "$PROCESSED_CONFIG_FILE"
}
#-----------------------------------------------------------------------------------------------------------------------
merge_config_vars_and_env_vars() {
SAFE_CONFIG_FILE=$1
. "$SAFE_CONFIG_FILE"
export $(grep = "$SAFE_CONFIG_FILE" | grep -v '^ *#' | cut -d= -f1)
# Env vars take precedence
. "$ENV_VARS"
export $(cut -d= -f1 "$ENV_VARS")
}
#-----------------------------------------------------------------------------------------------------------------------
set_default_values() {
# Handle defaults now
for KEY_VALUE in $DEFAULT_SETTINGS
do
KEY=$(echo "$KEY_VALUE" | cut -d= -f1)
VALUE=$(echo "$KEY_VALUE" | cut -d= -f2)
eval "export $KEY=\${$KEY:=$VALUE}"
done
}
########################################################################################################################
. "$ENV_VARS"
if [ $(all_required_settings_exist) = false ]
then
echo "Not all required settings passed as environment variables."
exit 0
fi
SAFE_CONFIG_FILE=$(create_and_validate_config_file)
merge_config_vars_and_env_vars $SAFE_CONFIG_FILE
validate_values
set_default_values
print_config
generate_binary_config
# Now dump the envvars, in the style that boot.sh does. exec to avoid SHLVL=2
exec sh -c "export > \"$MERGED_ENV_VARS\""