-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.sh
executable file
·139 lines (111 loc) · 2.99 KB
/
init.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
#!/bin/bash
set -eu -o pipefail
#set -x
ENV_FILE_TEMPLATE="$1"
ENV_FILE_COMMON="template-common.env"
ENV_FILE_OVERRIDE="template-override.env"
ENV_FILE_MAIN="./config.env"
SECRET_DIR="./secrets"
DB_PASS_FILE="${SECRET_DIR}/db_password"
NEXTCLOUD_ADMIN_PASS_FILE="${SECRET_DIR}/nextcloud_admin_password"
COMPOSE_OVERRIDE="docker-compose.override.yml"
COMPOSE_HTTPS="docker-compose.override.yml.https"
COMPOSE_HTTP="docker-compose.override.yml.http"
REQUIRED="__REQUIRED__"
check_existence() {
FILE="$1"
if [ -s "${FILE}" ]; then
echo "${FILE}: already exists"
return 0
fi
return 1
}
gen_pass_stdout() {
openssl rand -base64 36
}
create_pass_file() {
FILE="$1"
VAL="$2"
touch "${FILE}"
chmod 600 "${FILE}"
echo "${VAL}" > "${FILE}"
echo "${FILE}: created"
}
read_input() {
MSG="$1"
DEFAULT="$2"
read -p "${MSG} [${DEFAULT}]: " VAL
if [ -n "${VAL}" ]; then
echo "${VAL}"
else
if [ "${DEFAULT}" = "${REQUIRED}" ]; then
return 1
else
echo "${DEFAULT}"
fi
fi
return 0
}
if ! check_existence "${NEXTCLOUD_ADMIN_PASS_FILE}"; then
PASS=$(read_input "Enter Nextcloud admin password (default auto-generated)" "")
if [ -z "${PASS}" ]; then
PASS=$(gen_pass_stdout)
fi
create_pass_file "${NEXTCLOUD_ADMIN_PASS_FILE}" "${PASS}"
fi
check_existence "${DB_PASS_FILE}" \
|| create_pass_file "${DB_PASS_FILE}" "$(gen_pass_stdout)"
chmod 700 "${SECRET_DIR}"
check_existence "${ENV_FILE_MAIN}" && exit 0
TMPFILE=$(mktemp)
cleanup() {
rm -f "${TMPFILE}"
}
trap cleanup 1 2 15 ERR
PROTOCOL=
echo "Please input parameters"
echo "default is shown in the brackets."
echo "Specifying \"\" means empty (disabled)."
if [ -f "${ENV_FILE_OVERRIDE}" ]; then
ENV_FILE_TEMPLATE="${ENV_FILE_OVERRIDE}"
fi
NEXTCLOUD_GFARM_USE_GFARM_FOR_DATADIR=0
IFS='
'
for kv in $(cat "${ENV_FILE_COMMON}" "${ENV_FILE_TEMPLATE}"); do
[[ "$kv" =~ "=" ]] || continue
echo "$kv" | grep -q "^\s*#.*$" && continue
k=${kv%=*}
v=${kv#*=}
if [ ${NEXTCLOUD_GFARM_USE_GFARM_FOR_DATADIR} = "0" ]; then
# skip unnecessary keys
case "${k}" in
MYPROXY_USER) continue;;
GFARM_USER) continue;;
GFARM_DATA_PATH) continue;;
GFARM_BACKUP_PATH) continue;;
esac
fi
if VAL=$(read_input "$k" "$v"); then
:
else
echo "ERROR" 1>&2
exit 1
fi
echo "${k}=${VAL}" >> "${TMPFILE}"
if [ "${k}" = "PROTOCOL" ]; then
PROTOCOL="${VAL}"
elif [ "${k}" = "NEXTCLOUD_GFARM_USE_GFARM_FOR_DATADIR" ]; then
NEXTCLOUD_GFARM_USE_GFARM_FOR_DATADIR="${VAL}"
fi
done
if [ ! -e "${COMPOSE_OVERRIDE}" ]; then
if [ "${PROTOCOL}" = "http" ]; then
ln -s "${COMPOSE_HTTP}" "${COMPOSE_OVERRIDE}"
else
ln -s "${COMPOSE_HTTPS}" "${COMPOSE_OVERRIDE}"
fi
fi
mv "${TMPFILE}" "${ENV_FILE_MAIN}"
echo "${ENV_FILE_MAIN}: created. Please check and correct parameters."
cleanup