-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathshared-functions.sh
More file actions
232 lines (211 loc) · 9.05 KB
/
shared-functions.sh
File metadata and controls
232 lines (211 loc) · 9.05 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# shellcheck shell=bash
# shellcheck disable=SC2034
# shellcheck source-path=..
function read_config() {
source "$TOOLKIT_ROOT/lib/default.rc"
# shellcheck source=/dev/null
source "$TOOLKIT_ROOT/config/overleaf.rc"
if [[ $SERVER_PRO != "true" || $IMAGE_VERSION_MAJOR -lt 4 ]]; then
# Force git bridge to be disabled if not ServerPro >= 4
GIT_BRIDGE_ENABLED=false
fi
}
function read_image_version() {
IMAGE_VERSION="$(head -n 1 "$TOOLKIT_ROOT/config/version")"
if [[ ! "$IMAGE_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9])+(-RC[0-9]*)?(-with-texlive-full)?$ ]]; then
echo "ERROR: invalid version '${IMAGE_VERSION}'"
exit 1
fi
IMAGE_VERSION_MAJOR=${BASH_REMATCH[1]}
IMAGE_VERSION_MINOR=${BASH_REMATCH[2]}
IMAGE_VERSION_PATCH=${BASH_REMATCH[3]}
}
function read_mongo_version() {
local mongo_image=$(read_configuration "MONGO_IMAGE")
local mongo_version=$(read_configuration "MONGO_VERSION")
if [ -z "${mongo_version}" ]; then
if [[ "$mongo_image" =~ ^mongo:([0-9]+)\.(.*)$ ]]; then
# when running a chain of commands (example: bin/up -> bin/docker-compose) we're passing
# SKIP_WARNINGS=true to prevent the warning message to be printed several times
if [[ ${SKIP_WARNINGS:-null} != "true" ]]; then
echo "------------------- WARNING ----------------------"
echo " Deprecation warning: the mongo image is now split between MONGO_IMAGE"
echo " and MONGO_VERSION configurations. Please update your config/overleaf.rc as"
echo " your current configuration may stop working in future versions of the toolkit."
echo " Example: MONGO_IMAGE=mongo"
echo " MONGO_VERSION=6.0"
echo "------------------- WARNING ----------------------"
fi
MONGO_VERSION_MAJOR=${BASH_REMATCH[1]}
MONGO_DOCKER_IMAGE="$mongo_image"
else
echo "--------------------- ERROR -----------------------"
echo " The mongo image is now split between MONGO_IMAGE and MONGO_VERSION configurations."
echo " Please update your config/overleaf.rc."
echo ""
echo " MONGO_VERSION must start with the actual major version of mongo, followed by a dot."
echo " Example: MONGO_IMAGE=my.dockerhub.com/custom-mongo"
echo " MONGO_VERSION=6.0-custom"
echo "--------------------- ERROR -----------------------"
exit 1
fi
else
if [[ ! "$mongo_version" =~ ^([0-9]+)\.(.+)$ ]]; then
echo "--------------------- ERROR -----------------------"
echo " Invalid MONGO_VERSION: $mongo_version"
echo ""
echo " MONGO_VERSION must start with the actual major version of mongo, followed by a dot."
echo " Example: MONGO_IMAGE=my.dockerhub.com/custom-mongo"
echo " MONGO_VERSION=6.0-custom"
echo "--------------------- ERROR -----------------------"
exit 1
fi
MONGO_VERSION_MAJOR=${BASH_REMATCH[1]}
MONGO_DOCKER_IMAGE="$mongo_image:$mongo_version"
fi
if [[ "$MONGO_VERSION_MAJOR" -lt 6 ]]; then
MONGOSH="mongo"
else
MONGOSH="mongosh"
fi
}
function set_server_pro_image_name() {
local version=$1
local image_name
if [[ -n ${OVERLEAF_IMAGE_NAME:-} ]]; then
if [[ "$OVERLEAF_IMAGE_NAME" == *:* ]]; then
image_name="${OVERLEAF_IMAGE_NAME%%:*}"
version="${OVERLEAF_IMAGE_NAME##*:}"
else
image_name="$OVERLEAF_IMAGE_NAME"
fi
elif [[ $SERVER_PRO == "true" ]]; then
image_name="quay.io/sharelatex/sharelatex-pro"
else
image_name="sharelatex/sharelatex"
fi
export IMAGE="$image_name:$version"
}
function set_git_bridge_image_name() {
local version=$1
local image_name
if [[ -n ${GIT_BRIDGE_IMAGE:-} ]]; then
image_name="$GIT_BRIDGE_IMAGE"
else
image_name="quay.io/sharelatex/git-bridge"
fi
# since we're reusing the GIT_BRIDGE_IMAGE environment variable, we check here if the version
# has already been added to it, for scenarios where this function is called more than once
if [[ "$image_name" == *"$version" ]]; then
export GIT_BRIDGE_IMAGE="$image_name"
else
export GIT_BRIDGE_IMAGE="$image_name:$version"
fi
}
function check_retracted_version() {
if [[ "${OVERLEAF_SKIP_RETRACTION_CHECK:-null}" == "$IMAGE_VERSION" ]]; then
return
fi
local version="$IMAGE_VERSION_MAJOR.$IMAGE_VERSION_MINOR.$IMAGE_VERSION_PATCH"
if [[ "$version" == "5.0.1" ]]; then
echo "-------------------------------------------------------"
echo "--------------------- WARNING -----------------------"
echo "-------------------------------------------------------"
echo " You are currently using a retracted version, $version."
echo ""
echo " We have identified a critical bug in a database migration that causes data loss in the history system."
echo " A new release is available with a fix and an automated recovery process."
echo " Please follow the steps of the recovery process in the following wiki page:"
echo " https://github.com/overleaf/overleaf/wiki/Doc-version-recovery"
echo "-------------------------------------------------------"
echo "--------------------- WARNING -----------------------"
echo "-------------------------------------------------------"
exit 1
fi
}
prompt() {
read -p "$1 (y/n): " choice
if [[ ! "$choice" =~ [Yy] ]]; then
echo "Exiting."
exit 1
fi
}
rebrand_sharelatex_env_variables() {
local filename=$1
local silent=${2:-no}
sharelatex_occurrences=$(set +o pipefail && grep -o "SHARELATEX_" "$TOOLKIT_ROOT/config/$filename" | wc -l | sed 's/ //g')
if [ "$sharelatex_occurrences" -gt 0 ]; then
echo "Rebranding from ShareLaTeX to Overleaf"
echo " Found $sharelatex_occurrences lines with SHARELATEX_ in config/$filename"
local timestamp=$(date "+%Y.%m.%d-%H.%M.%S")
local backup_filename="__old-$filename.$timestamp"
echo " Will create backup of config/$filename at config/$backup_filename"
prompt " Proceed with the renaming in config/$filename?"
echo " Creating backup file config/$backup_filename"
cp "$TOOLKIT_ROOT/config/$filename" "$TOOLKIT_ROOT/config/$backup_filename"
echo " Replacing 'SHARELATEX_' with 'OVERLEAF_' in config/$filename"
sed -i "s/SHARELATEX_/OVERLEAF_/g" "$TOOLKIT_ROOT/config/$filename"
echo " Updated $sharelatex_occurrences lines in config/$filename"
else
if [[ "$silent" != "silent_if_no_match" ]]; then
echo "Rebranding from ShareLaTeX to Overleaf"
echo " No 'SHARELATEX_' occurrences found in config/$filename"
fi
fi
}
function check_sharelatex_env_vars() {
local rc_occurrences=$(set +o pipefail && grep -o SHARELATEX_ "$TOOLKIT_ROOT/config/overleaf.rc" | wc -l | sed 's/ //g')
if [ "$rc_occurrences" -gt 0 ]; then
echo "Rebranding from ShareLaTeX to Overleaf"
echo " The Toolkit has adopted to Overleaf brand for its variables."
echo " Your config/overleaf.rc still has $rc_occurrences variables using the previous ShareLaTeX brand."
echo " Moving forward the 'SHARELATEX_' prefixed variables must be renamed to 'OVERLEAF_'."
echo " You can migrate your config/overleaf.rc to use the Overleaf brand by running:"
echo ""
echo " toolkit$ bin/rename-rc-vars"
echo ""
exit 1
fi
local expected_prefix="OVERLEAF_"
local invalid_prefix="SHARELATEX_"
if [[ "$IMAGE_VERSION_MAJOR" -lt 5 ]]; then
expected_prefix="SHARELATEX_"
invalid_prefix="OVERLEAF_"
fi
local env_occurrences=$(set +o pipefail && grep -o "$invalid_prefix" "$TOOLKIT_ROOT/config/variables.env" | wc -l | sed 's/ //g')
if [ "$env_occurrences" -gt 0 ]; then
echo "Rebranding from ShareLaTeX to Overleaf"
echo " Starting with Overleaf CE and Server Pro version 5.0.0 the environment variables will use the Overleaf brand."
echo " Previous versions used the ShareLaTeX brand for environment variables."
echo " Your config/variables.env has $env_occurrences entries matching '$invalid_prefix', expected prefix '$expected_prefix'."
echo " Please align your config/version with the naming scheme of variables in config/variables.env."
if [[ ! "$IMAGE_VERSION_MAJOR" -lt 5 ]]; then
echo " You can migrate your config/variables.env to use the Overleaf brand by running:"
echo ""
echo " toolkit$ bin/rename-env-vars-5-0"
echo ""
fi
exit 1
fi
if [[ ! "$IMAGE_VERSION_MAJOR" -lt 5 ]]; then
if grep -q -e 'TEXMFVAR=/var/lib/sharelatex/tmp/texmf-var' "$TOOLKIT_ROOT/config/variables.env"; then
echo "Rebranding from ShareLaTeX to Overleaf"
echo " The 'TEXMFVAR' override is not needed since Server Pro/Overleaf CE version 3.2 (August 2022) and it conflicts with the rebranded paths."
echo " Please remove the following entry from your config/variables.env:"
echo ""
echo " TEXMFVAR=/var/lib/sharelatex/tmp/texmf-var"
echo ""
exit 1
fi
fi
}
function read_variable() {
local name=$1
grep -E "^$name=" "$TOOLKIT_ROOT/config/variables.env" \
| sed -r "s/^$name=([\"']?)(.+)\1\$/\2/"
}
function read_configuration() {
local name=$1
grep -E "^$name=" "$TOOLKIT_ROOT/config/overleaf.rc" \
| sed -r "s/^$name=([\"']?)(.+)\1\$/\2/"
}