-
Notifications
You must be signed in to change notification settings - Fork 122
/
setup.sh
executable file
·345 lines (263 loc) · 8.35 KB
/
setup.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT_DIR=${DIR}/../..
# load values from .env into environment variables
# see https://stackoverflow.com/a/30969768/3868241
set -o allexport
# shellcheck source=../.env
source "$ROOT_DIR/dev/.env"
set +o allexport
# ---- START static variables that shouldn't be changed
export AWS_PAGER=""
export AWS_CBOR_DISABLE=true
LOCALSTACK_ENDPOINT=http://localhost:4566
CORE_STACK_FILE="$ROOT_DIR/dev/cloudformation/grid-dev-core.yml"
CORE_STACK_FILENAME=$(basename "$CORE_STACK_FILE")
AUTH_STACK_FILE="$ROOT_DIR/dev/cloudformation/grid-dev-auth.yml"
AUTH_STACK_FILENAME=$(basename "$AUTH_STACK_FILE")
# ---- END
LOCAL_AUTH=false
NO_AUTH=false
for arg in "$@"; do
if [ "$arg" == "--clean" ]; then
CLEAN=true
shift
fi
if [ "$arg" == "--with-local-auth" ]; then
LOCAL_AUTH=true
shift
fi
if [ "$arg" == "--no-auth" ]; then
LOCAL_AUTH=true
NO_AUTH=true
shift
fi
done
clean() {
if [[ $CLEAN != true ]]; then
return
fi
echo "removing all previous local infrastructure"
rm -rf "$ROOT_DIR/dev/.localstack"
echo " removed historical localstack data"
docker-compose down -v
echo " removed docker containers"
docker-compose build
echo " rebuilt docker containers"
}
startDocker() {
docker-compose up -d
echo "waiting for localstack to launch on $LOCALSTACK_ENDPOINT"
while ! curl -s $LOCALSTACK_ENDPOINT >/dev/null; do
echo " localstack not ready yet"
sleep 1 # wait for 1 second before check again
done
echo " localstack is now ready"
}
setupDevNginx() {
imageOriginBucket=$(getStackResource "$CORE_STACK_NAME" ImageOriginBucket)
imagesBucket=$(getStackResource "$CORE_STACK_NAME" ImageBucket)
target="$ROOT_DIR/dev/nginx-mappings.yml"
sed -e "s/@IMAGE-ORIGIN-BUCKET/$imageOriginBucket/g" \
-e "s/@IMAGE-BUCKET/$imagesBucket/g" \
-e "s/@DOMAIN_ROOT/$DOMAIN/g" \
"$ROOT_DIR/dev/nginx-mappings.yml.template" > "$target"
dev-nginx setup-app "$target"
}
setupPanDomainConfiguration() {
if [[ $LOCAL_AUTH != true ]]; then
return
fi
echo "setting up pan domain authentication configuration for local auth"
panDomainBucket=$(getStackResource "$AUTH_STACK_NAME" PanDomainBucket)
PANDA_COOKIE_NAME=gutoolsAuth-assym
OUTPUT_DIR=/tmp
PANDA_PRIVATE_SETTINGS_FILE="$OUTPUT_DIR/$DOMAIN.settings"
PANDA_PUBLIC_SETTINGS_FILE="$OUTPUT_DIR/$DOMAIN.settings.public"
PRIVATE_KEY_FILE=$(mktemp "$OUTPUT_DIR/private-key.XXXXXX")
PUBLIC_KEY_FILE=$(mktemp "$OUTPUT_DIR/public-key.XXXXXX")
openssl genrsa -out "$PRIVATE_KEY_FILE" 4096
openssl rsa -pubout -in "$PRIVATE_KEY_FILE" -out "$PUBLIC_KEY_FILE"
privateKey=$(sed -e '1d' -e '$d' < "$PRIVATE_KEY_FILE" | tr -d '\n')
publicKey=$(sed -e '1d' -e '$d' < "$PUBLIC_KEY_FILE" | tr -d '\n')
privateSettings=$(cat <<END
privateKey=${privateKey}
publicKey=${publicKey}
cookieName=${PANDA_COOKIE_NAME}
clientId=${OIDC_CLIENT_ID}
clientSecret=${OIDC_CLIENT_SECRET}
discoveryDocumentUrl=http://localhost:9014/.well-known/openid-configuration
END
)
publicSettings=$(cat <<END
publicKey=${publicKey}
END
)
echo "$privateSettings" > "$PANDA_PRIVATE_SETTINGS_FILE"
echo "$publicSettings" > "$PANDA_PUBLIC_SETTINGS_FILE"
filesToUpload=(
"$PANDA_PRIVATE_SETTINGS_FILE"
"$PANDA_PUBLIC_SETTINGS_FILE"
)
for file in "${filesToUpload[@]}"; do
aws s3 cp "$file" "s3://$panDomainBucket/" --endpoint-url $LOCALSTACK_ENDPOINT
echo " uploaded $file to bucket $panDomainBucket"
done
rm -f "$PUBLIC_KEY_FILE"
rm -f "$PRIVATE_KEY_FILE"
rm -f "$PANDA_PRIVATE_SETTINGS_FILE"
rm -f "$PANDA_PUBLIC_SETTINGS_FILE"
}
setupGuardianPermissionConfiguration() {
if [[ $LOCAL_AUTH != true ]]; then
return
fi
echo "setting up permissions configuration for Guardian local auth"
permissionsBucket=$(getStackResource "$AUTH_STACK_NAME" PermissionsBucket)
target="$ROOT_DIR/dev/config/permissions.json"
sed -e "s/@EMAIL_DOMAIN/$EMAIL_DOMAIN/g" \
"$target.template" > "$target"
aws s3 cp "$target" \
"s3://$permissionsBucket/" \
--endpoint-url $LOCALSTACK_ENDPOINT
echo " uploaded file to $permissionsBucket"
}
setupPhotographersConfiguration() {
echo "setting up photographers configuration"
configBucket=$(getStackResource "$CORE_STACK_NAME" ConfigBucket)
target="$ROOT_DIR/dev/config/photographers.json"
aws s3 cp "$target" \
"s3://$configBucket/" \
--endpoint-url $LOCALSTACK_ENDPOINT
echo " uploaded file to $configBucket"
}
setupQuotasConfiguration () {
echo "setting up quotas configuration"
configBucket=$(getStackResource "$CORE_STACK_NAME" ConfigBucket)
target="$ROOT_DIR/dev/config/rcs-quota.json"
aws s3 cp "$target" \
"s3://$configBucket/" \
--endpoint-url $LOCALSTACK_ENDPOINT
echo " uploaded file to $configBucket"
}
setupUsagesData () {
echo "setting up usages data"
usageBucket=$(getStackResource "$CORE_STACK_NAME" UsageMailBucket)
target="$ROOT_DIR/dev/config/usages.eml"
aws s3 cp "$target" \
"s3://$usageBucket/" \
--endpoint-url $LOCALSTACK_ENDPOINT
echo " uploaded file to $usageBucket"
}
setupUsageRightsConfiguration() {
echo "setting up usage rights configuration"
configBucket=$(getStackResource "$CORE_STACK_NAME" ConfigBucket)
target="$ROOT_DIR/dev/config/usage_rights.json"
aws s3 cp "$target" \
"s3://$configBucket/" \
--endpoint-url $LOCALSTACK_ENDPOINT
echo " uploaded file to $configBucket"
}
getStackResource() {
stackName=$1
resourceName=$2
stackResources=$(
aws cloudformation describe-stack-resources \
--stack-name "$stackName" \
--endpoint-url $LOCALSTACK_ENDPOINT
)
resource=$(
echo "$stackResources" \
| jq -r ".StackResources[] | select(.LogicalResourceId == \"$resourceName\") | .PhysicalResourceId"
)
echo "$resource"
}
createCoreStack() {
echo "creating local core cloudformation stack"
set -x
stackNames=$(
aws cloudformation list-stacks \
--endpoint-url http://localhost:4566 | \
jq -r '.StackSummaries[].StackName'
)
if echo ${stackNames} | grep -q '^grid-dev-core$'; then
aws cloudformation update-stack \
--stack-name "$CORE_STACK_NAME" \
--template-body "file://$CORE_STACK_FILE" \
--endpoint-url $LOCALSTACK_ENDPOINT > /dev/null
echo " updated stack $CORE_STACK_NAME using $CORE_STACK_FILENAME"
else
aws cloudformation create-stack \
--stack-name "$CORE_STACK_NAME" \
--template-body "file://$CORE_STACK_FILE" \
--endpoint-url $LOCALSTACK_ENDPOINT > /dev/null
echo " created stack $CORE_STACK_NAME using $CORE_STACK_FILENAME"
fi
set +x
# TODO - this should wait until the stack operation has completed
}
createGuardianLocalAuthStack() {
if [[ $LOCAL_AUTH != true ]]; then
return
fi
echo "creating local auth cloudformation stack"
aws cloudformation create-stack \
--stack-name "$AUTH_STACK_NAME" \
--template-body "file://$AUTH_STACK_FILE" \
--endpoint-url $LOCALSTACK_ENDPOINT > /dev/null
echo " created stack $AUTH_STACK_NAME using $AUTH_STACK_FILENAME"
}
generateConfig() {
CONF_HOME="${HOME}/.grid"
mkdir -p ${CONF_HOME}
echo "generating configuration files"
pushd "$ROOT_DIR/dev/script/generate-config"
npm install
NO_AUTH=$NO_AUTH npm run generate-config
popd
echo " configuration files created in ${CONF_HOME}"
}
uploadApiKey() {
echo "uploading an api key"
keyBucket=$(getStackResource "$CORE_STACK_NAME" KeyBucket)
target="/tmp/$API_KEY"
echo "DEV Key" > "$target"
aws s3 cp "$target" \
"s3://$keyBucket/" \
--endpoint-url $LOCALSTACK_ENDPOINT
rm "$target"
echo " uploaded"
}
checkForJavaHome() {
echo "Checking JAVA_HOME"
if [[ -z "$JAVA_HOME" ]]; then
echo " JAVA_HOME not set, please set it before continuing"
echo " This can be done by adding \"export JAVA_HOME=\$(/usr/libexec/java_home)\" to ~/.profile"
exit 1
else
echo " JAVA_HOME is set to $JAVA_HOME"
fi
}
main() {
checkForJavaHome
clean
startDocker
createCoreStack
if [[ $LOCAL_AUTH == true ]]; then
if [[ $NO_AUTH != true ]]; then
createGuardianLocalAuthStack
setupPanDomainConfiguration
setupGuardianPermissionConfiguration
fi
fi
setupPhotographersConfiguration
setupQuotasConfiguration
setupUsagesData
setupUsageRightsConfiguration
setupDevNginx
generateConfig
uploadApiKey
echo "Setup complete. You're now able to start Grid!"
}
main