This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathserver.sh
executable file
·234 lines (209 loc) · 10.4 KB
/
server.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
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
233
234
#!/bin/bash
DOCKER_VERSION=`docker --version`
if [ "$?" -ne "0" ]; then
echo "Please install Docker before proceeding."
exit 1
fi
CURL_AVAILABLE=`which curl`
if [ "$?" -ne "0" ]; then
echo "Please install cURL before proceeding."
exit 1
fi
DOCKER_COMPOSE_COMMAND="docker compose"
if ! $DOCKER_COMPOSE_COMMAND > /dev/null 2>&1; then
DOCKER_COMPOSE_COMMAND="docker-compose"
fi
checkConfigFiles() {
if [ ! -f ".env" ]; then echo "Could not find syncing-server environment file. Please run the './server.sh setup' command and try again." && exit 1; fi
if [ ! -f "docker/api-gateway.env" ]; then echo "Could not find api-gateway environment file. Please run the './server.sh setup' command and try again." && exit 1; fi
if [ ! -f "docker/auth.env" ]; then echo "Could not find auth environment file. Please run the './server.sh setup' command and try again." && exit 1; fi
if [ ! -f "docker/files.env" ]; then echo "Could not find file service environment file. Please run the './server.sh setup' command and try again." && exit 1; fi
if [ ! -f "docker/revisions.env" ]; then echo "Could not find revisions service environment file. Please run the './server.sh setup' command and try again." && exit 1; fi
}
checkForConfigFileChanges() {
checkConfigFiles
compareLineCount
}
compareLineCount() {
MAIN_ENV_FILE_SAMPLE_LINES=$(wc -l .env.sample | awk '{ print $1 }')
MAIN_ENV_FILE_LINES=$(wc -l .env | awk '{ print $1 }')
if [ "$MAIN_ENV_FILE_SAMPLE_LINES" -ne "$MAIN_ENV_FILE_LINES" ]; then echo "The .env file contains different amount of lines than .env.sample. This may be caused by the fact that there is a new environment variable to configure. Please update your environment file and try again." && exit 1; fi
API_GATEWAY_ENV_FILE_SAMPLE_LINES=$(wc -l docker/api-gateway.env.sample | awk '{ print $1 }')
API_GATEWAY_ENV_FILE_LINES=$(wc -l docker/api-gateway.env | awk '{ print $1 }')
if [ "$API_GATEWAY_ENV_FILE_SAMPLE_LINES" -ne "$API_GATEWAY_ENV_FILE_LINES" ]; then echo "The docker/api-gateway.env file contains different amount of lines than docker/api-gateway.env.sample. This may be caused by the fact that there is a new environment variable to configure. Please update your environment file and try again." && exit 1; fi
AUTH_ENV_FILE_SAMPLE_LINES=$(wc -l docker/auth.env.sample | awk '{ print $1 }')
AUTH_ENV_FILE_LINES=$(wc -l docker/auth.env | awk '{ print $1 }')
if [ "$AUTH_ENV_FILE_SAMPLE_LINES" -ne "$AUTH_ENV_FILE_LINES" ]; then echo "The docker/auth.env file contains different amount of lines than docker/auth.env.sample. This may be caused by the fact that there is a new environment variable to configure. Please update your environment file and try again." && exit 1; fi
FILES_ENV_FILE_SAMPLE_LINES=$(wc -l docker/files.env.sample | awk '{ print $1 }')
FILES_ENV_FILE_LINES=$(wc -l docker/files.env | awk '{ print $1 }')
if [ "$FILES_ENV_FILE_SAMPLE_LINES" -ne "$FILES_ENV_FILE_LINES" ]; then echo "The docker/files.env file contains different amount of lines than docker/files.env.sample. This may be caused by the fact that there is a new environment variable to configure. Please update your environment file and try again." && exit 1; fi
REVISIONS_ENV_FILE_SAMPLE_LINES=$(wc -l docker/revisions.env.sample | awk '{ print $1 }')
REVISIONS_ENV_FILE_LINES=$(wc -l docker/revisions.env | awk '{ print $1 }')
if [ "$REVISIONS_ENV_FILE_SAMPLE_LINES" -ne "$REVISIONS_ENV_FILE_LINES" ]; then echo "The docker/revisions.env file contains different amount of lines than docker/files.env.sample. This may be caused by the fact that there is a new environment variable to configure. Please update your environment file and try again." && exit 1; fi
}
cleanup() {
local output_logs=$1
if [ $output_logs == 1 ]
then
echo "Outputing last 100 lines of logs"
$DOCKER_COMPOSE_COMMAND logs --tail=100
fi
}
waitForServices() {
attempt=0
while [ $attempt -le 180 ]; do
attempt=$(( $attempt + 1 ))
echo "# Waiting for all services to be up (attempt: $attempt) ..."
ping_api_gateway_result=`curl -s $($DOCKER_COMPOSE_COMMAND port api-gateway 3000)`
if [ "$?" -eq "0" ]; then
sleep 2 # for warmup
echo "# All services are up!"
break
fi
sleep 2
done
}
replaceConfigValue() {
local config_file=$1
local config_key=$2
local config_value=$3
if [[ $OSTYPE == 'darwin'* ]]; then
sed -i '' "s/$config_key=.*/$config_key=$config_value/g" $config_file
else
sed -i "s/$config_key=.*/$config_key=$config_value/g" $config_file
fi
}
COMMAND=$1 && shift 1
case "$COMMAND" in
'setup' )
echo "Initializing default configuration"
if [ ! -f ".env" ]; then cp .env.sample .env; fi
if [ ! -f "docker/api-gateway.env" ]; then cp docker/api-gateway.env.sample docker/api-gateway.env; fi
if [ ! -f "docker/auth.env" ]; then cp docker/auth.env.sample docker/auth.env; fi
if [ ! -f "docker/files.env" ]; then cp docker/files.env.sample docker/files.env; fi
if [ ! -f "docker/revisions.env" ]; then cp docker/revisions.env.sample docker/revisions.env; fi
echo "Default configuration files created as .env and docker/*.env files. Feel free to modify values if needed."
;;
'restore-config-defaults' )
echo "Initializing default configuration"
cp .env.sample .env
cp docker/api-gateway.env.sample docker/api-gateway.env
cp docker/auth.env.sample docker/auth.env
cp docker/files.env.sample docker/files.env
cp docker/revisions.env.sample docker/revisions.env
echo "Default configuration files restored as .env and docker/*.env files. Feel free to modify values if needed."
;;
'start' )
checkForConfigFileChanges
echo "Starting up infrastructure"
$DOCKER_COMPOSE_COMMAND up -d
echo "Infrastructure started. Give it a moment to warm up. If you wish please run the './server.sh logs' command to see details."
;;
'status' )
echo "Services State:"
$DOCKER_COMPOSE_COMMAND ps
;;
'logs' )
$DOCKER_COMPOSE_COMMAND logs -f
;;
'update' )
echo "Stopping all services."
$DOCKER_COMPOSE_COMMAND kill --remove-orphans || true
echo "Pulling changes from Git."
git pull origin $(git rev-parse --abbrev-ref HEAD)
echo "Checking for env file changes"
checkForConfigFileChanges
echo "Downloading latest images of Standard Notes services."
$DOCKER_COMPOSE_COMMAND pull
echo "Images up to date. Starting all services."
$DOCKER_COMPOSE_COMMAND up -d
echo "Infrastructure started. Give it a moment to warm up. If you wish please run the './server.sh logs' command to see details."
;;
'create-subscription' )
EMAIL=$1
if [[ "$EMAIL" = "" ]]; then
echo "Please provide an email for the subscription."
exit 1
fi
shift 1
$DOCKER_COMPOSE_COMMAND exec db sh -c "MYSQL_PWD=\$MYSQL_ROOT_PASSWORD mysql \$MYSQL_DATABASE -e \
'INSERT INTO user_roles (role_uuid , user_uuid) VALUES ((SELECT uuid FROM roles WHERE name=\"PRO_USER\" ORDER BY version DESC limit 1) ,(SELECT uuid FROM users WHERE email=\"$EMAIL\")) ON DUPLICATE KEY UPDATE role_uuid = VALUES(role_uuid);' \
"
$DOCKER_COMPOSE_COMMAND exec db sh -c "MYSQL_PWD=\$MYSQL_ROOT_PASSWORD mysql \$MYSQL_DATABASE -e \
'INSERT INTO user_subscriptions SET uuid=UUID(), plan_name=\"PRO_PLAN\", ends_at=8640000000000000, created_at=0, updated_at=0, user_uuid=(SELECT uuid FROM users WHERE email=\"$EMAIL\"), subscription_id=1, subscription_type=\"regular\";' \
"
echo "Subscription successfully created. Please consider donating if you do not plan on purchasing a subscription."
;;
'dump-db' )
echo "Dumping database to data/mysql/001-db-dump.sql"
$DOCKER_COMPOSE_COMMAND exec db sh -c "MYSQL_PWD=\$MYSQL_ROOT_PASSWORD mysqldump -cn -u root \$MYSQL_DATABASE > /var/lib/mysql/001-db-dump.sql"
echo "Database dump complete"
echo "Creating sql adjustments file to data/revisions-db/002-adjust.sql"
echo "ALTER TABLE \`revisions\` ADD COLUMN \`user_uuid\` varchar(36) NULL" > data/revisions-db/002-adjust.sql
echo "Creating sql adjustments file complete"
echo "Dumping revisions database to data/revisions-db/003-revisions-dump.sql"
$DOCKER_COMPOSE_COMMAND exec revisions-db sh -c "MYSQL_PWD=\$MYSQL_ROOT_PASSWORD mysqldump -ctn -u root --ignore-table=\$MYSQL_DATABASE.migrations \$MYSQL_DATABASE > /var/lib/mysql/003-revisions-dump.sql"
echo "Revisions Database dump complete"
echo "Creating sql revisions ownership file to data/revisions-db/004-revisions-ownership.sql"
echo "UPDATE revisions r SET r.user_uuid=(SELECT i.user_uuid FROM items i WHERE i.uuid = r.item_uuid LIMIT 1)" > data/revisions-db/004-revisions-ownership.sql
echo "Creating sql revisions ownership file complete"
;;
'stop' )
echo "Stopping all service"
$DOCKER_COMPOSE_COMMAND kill --remove-orphans
echo "Services stopped"
;;
'version' )
$DOCKER_COMPOSE_COMMAND images
;;
'cleanup' )
echo "WARNING: This will permanently delete all of your data! Are you sure?"
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y )
$DOCKER_COMPOSE_COMMAND kill --remove-orphans && $DOCKER_COMPOSE_COMMAND rm -fv
rm -rf data/mysql
rm -rf data/revisions-db
rm -rf data/redis
echo "Cleanup performed. You can start your server with a clean environment."
;;
n|N )
echo "Cleanup aborted"
exit 0
;;
* )
echo "Invalid option supplied. Aborted cleanup."
;;
esac
;;
'generate-keys' )
replaceConfigValue .env AUTH_JWT_SECRET $(openssl rand -hex 32)
replaceConfigValue .env VALET_TOKEN_SECRET $(openssl rand -hex 32)
replaceConfigValue .env DB_PASSWORD $(openssl rand -hex 32)
replaceConfigValue .env REVISIONS_DB_PASSWORD $(openssl rand -hex 32)
replaceConfigValue docker/auth.env LEGACY_JWT_SECRET $(openssl rand -hex 32)
replaceConfigValue docker/auth.env PSEUDO_KEY_PARAMS_KEY $(openssl rand -hex 32)
replaceConfigValue docker/auth.env ENCRYPTION_SERVER_KEY $(openssl rand -hex 32)
replaceConfigValue docker/auth.env JWT_SECRET $(openssl rand -hex 32)
;;
'wait-for-startup' )
waitForServices
;;
'test' )
waitForServices
echo "# Starting test suite ..."
npx mocha-headless-chrome --timeout 1200000 -f http://localhost:9001/mocha/test.html
test_result=$?
cleanup $test_result
if [[ $test_result == 0 ]]
then
exit 0
else
exit 1
fi
;;
* )
echo "Unknown command"
;;
esac
exec "$@"