This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
manage
379 lines (312 loc) · 10.3 KB
/
manage
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/bash
export MSYS_NO_PATHCONV=1
# getDockerHost; for details refer to https://github.com/bcgov/DITP-DevOps/tree/main/code/snippets#getdockerhost
. /dev/stdin <<<"$(cat <(curl -s --raw https://raw.githubusercontent.com/bcgov/DITP-DevOps/main/code/snippets/getDockerHost))"
export DOCKERHOST=$(getDockerHost)
set -e
function echoError (){
_msg=${1}
_red='\e[31m'
_nc='\e[0m' # No Color
echo -e "${_red}${_msg}${_nc}"
}
function echoWarning (){
_msg=${1}
_yellow='\e[33m'
_nc='\e[0m' # No Color
echo -e "${_yellow}${_msg}${_nc}"
}
function echoSuccess (){
_msg=${1}
_green='\e[32m'
_nc='\e[0m' # No Color
echo -e "${_green}${_msg}${_nc}"
}
function echoInfo (){
_msg=${1}
_gray='\e[36m'
_nc='\e[0m' # No Color
echo -e "${_gray}${_msg}${_nc}"
}
function generateKey(){
(
_length=${1:-48}
# Format can be `-base64` or `-hex`
_format=${2:--base64}
echo $(openssl rand ${_format} ${_length})
)
}
function generateSeed(){
(
_prefix=${1}
_seed=$(echo "${_prefix}$(generateKey 32)" | fold -w 32 | head -n 1 )
_seed=$(echo -n "${_seed}")
echo ${_seed}
)
}
SCRIPT_HOME="$(cd "$(dirname "$0")" && pwd)"
# =================================================================================================================
# Usage:
# -----------------------------------------------------------------------------------------------------------------
usage() {
cat <<-EOF
Usage: $0 [command] [options]
Commands:
build - Build the docker images for the project.
You need to do this first.
$0 build
up - Creates the application containers from the built images
and starts the services based on the docker-compose.yml file.
You can pass in a list of containers to start.
By default all containers will be started.
$0 start
start - Same as up.
logs - Display the logs from the docker compose run (ctrl-c to exit).
stop - Stops the services. This is a non-destructive process. The volumes and containers
are not deleted so they will be reused the next time you run start.
down - Brings down the services and removes the volumes (storage) and containers.
rm - Same as down
EOF
exit 1
}
# -----------------------------------------------------------------------------------------------------------------
# Default Settings:
# -----------------------------------------------------------------------------------------------------------------
DEFAULT_CONTAINERS="controller-db"
ACAPY_CONTAINERS="aca-py wallet-db"
PROD_CONTAINERS="controller"
# -----------------------------------------------------------------------------------------------------------------
# Functions:
# -----------------------------------------------------------------------------------------------------------------
build-dav-controller() {
echo -e "\nBuilding dav-controller image..."
docker build \
-t 'dav-controller' \
-f './dav-controller/Dockerfile' '..'
}
buildImages() {
build-dav-controller
}
configureEnvironment() {
if [ -f .env ]; then
while read line; do
if [[ ! "$line" =~ ^\# ]] && [[ "$line" =~ .*= ]]; then
export ${line//[$'\r\n']}
fi
done <.env
fi
for arg in "$@"; do
# Remove recognized arguments from the list after processing.
shift
# echo "arg: ${arg}"
# echo "Remaining: ${@}"
case "$arg" in
*=*)
# echo "Exporting ..."
export "${arg}"
;;
*)
# echo "Saving for later ..."
# If not recognized, save it for later procesing ...
set -- "$@" "$arg"
;;
esac
done
## proof configuration selection
export DAV_PROOF_CONFIG_ID="age-verification-bc-person-credential"
## global
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-dav}"
export LOG_LEVEL=${LOG_LEVEL:-"DEBUG"}
# controller-db
export MONGODB_HOST="controller-db"
export MONGODB_PORT="27017"
export MONGODB_NAME="davcontroller"
export DAV_CONTROLLER_DB_USER="davcontrolleruser"
export DAV_CONTROLLER_DB_USER_PWD="davcontrollerpass"
# controller
export CONTROLLER_SERVICE_PORT=${CONTROLLER_SERVICE_PORT:-5000}
export CONTROLLER_URL="${CONTROLLER_URL:-http://controller:5000}"
export CONTROLLER_WEB_HOOK_URL=${CONTROLLER_WEB_HOOK_URL:-${CONTROLLER_URL}/webhooks}
if [ ! -z "${CONTROLLER_API_KEY}" ]; then
CONTROLLER_WEB_HOOK_URL="${CONTROLLER_WEB_HOOK_URL}#${CONTROLLER_API_KEY}"
fi
export ST_ACAPY_ADMIN_API_KEY_NAME="x-api-key"
# The redirect url can be a web link or the name of a template
export CONTROLLER_CAMERA_REDIRECT_URL="wallet_howto"
# The number of time in seconds a proof request will be valid for
export CONTROLLER_PRESENTATION_EXPIRE_TIME=20
#controller app settings
export SET_NON_REVOKED="True" # both work
export USE_OOB_PRESENT_PROOF="False" #BC wallet kinda supports true.
export USE_OOB_LOCAL_DID_SERVICE="False" #bc wallet does not support true
# agent
export AGENT_HOST=${AGENT_HOST:-aca-py}
export AGENT_NAME="DAV Agent"
export AGENT_HTTP_PORT=${AGENT_HTTP_PORT:-8030}
export AGENT_ADMIN_PORT=${AGENT_ADMIN_PORT:-"8077"}
export AGENT_ADMIN_URL=${AGENT_ADMIN_URL:-http://$AGENT_HOST:$AGENT_ADMIN_PORT}
export AGENT_ENDPOINT=${AGENT_ENDPOINT:-http://$AGENT_HOST:$AGENT_HTTP_PORT}
export AGENT_ADMIN_API_KEY=${AGENT_ADMIN_API_KEY}
export AGENT_ADMIN_MODE="admin-insecure-mode"
if [ ! -z "${AGENT_ADMIN_API_KEY}" ]; then
AGENT_ADMIN_MODE="admin-api-key ${AGENT_ADMIN_API_KEY}"
fi
export AGENT_WALLET_SEED=${AGENT_WALLET_SEED}
export MT_ACAPY_WALLET_ID=${MT_ACAPY_WALLET_ID}
export MT_ACAPY_WALLET_KEY=${MT_ACAPY_WALLET_KEY}
# wallet-db
export WALLET_TYPE="postgres_storage"
export WALLET_ENCRYPTION_KEY="key"
export POSTGRESQL_WALLET_HOST="wallet-db"
export POSTGRESQL_WALLET_PORT="5432"
export POSTGRESQL_WALLET_DATABASE="wallet_db"
export POSTGRESQL_WALLET_USER="walletuser"
export POSTGRESQL_WALLET_PASSWORD="walletpassword"
}
getStartupParams() {
CONTAINERS=""
ARGS="--force-recreate"
for arg in $@; do
case "$arg" in
*=*)
# Skip it
;;
-*)
ARGS+=" $arg"
;;
*)
CONTAINERS+=" $arg"
;;
esac
done
if [ -z "$CONTAINERS" ]; then
CONTAINERS="$DEFAULT_CONTAINERS"
fi
echo ${ARGS} ${CONTAINERS}
}
deleteVolumes() {
_projectName=${COMPOSE_PROJECT_NAME:-dav}
echo "Stopping and removing any running containers ..."
docker compose -f docker-compose.yaml -f docker-compose-ngrok.yaml down -v
_pattern="^${_projectName}_\|^docker_"
_volumes=$(docker volume ls -q | grep ${_pattern})
if [ ! -z "${_volumes}" ]; then
echo "Removing project volumes ..."
echo ${_volumes} | xargs docker volume rm
else
echo "No project volumes exist."
fi
}
toLower() {
echo $(echo ${@} | tr '[:upper:]' '[:lower:]')
}
# starts ngrok proxies for controller and, when in single-tenant mode, for the agent
function startNgrokContainers() {
# start ngrok container first so we can grab the tunnel URLs
echo "Starting ngrok container..."
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-dav}" docker compose -f docker-compose-ngrok.yaml up -d --force-recreate
}
# fetches and sets the ngrok endpoints for controlelr and agent for the current session
function setNgrokEndpoints() {
echoInfo "Determining ngrok url for controller service..."
getNgrokUrl http://${DOCKERHOST}:4046/api/tunnels controller-ngrok.json NGROK_CONTROLLER_URL controller-ngrok
export CONTROLLER_URL=${NGROK_CONTROLLER_URL}
echoSuccess "The controller url is: ${NGROK_CONTROLLER_URL}"
echoInfo "Determining ngrok url for agent service..."
getNgrokUrl http://${DOCKERHOST}:4046/api/tunnels agent-ngrok.json NGROK_AGENT_URL aca-py-ngrok
export AGENT_ENDPOINT=${NGROK_AGENT_URL}
echoSuccess "The agent url is: ${NGROK_AGENT_URL}"
}
function getNgrokUrl() {
_url=$1
_output_file=$2
_target_variable=$3
_tunnel_name=$4
function extractUrl() {
docker run --rm curlimages/curl -L -s $_url > $_output_file
NGROK_URL=$(docker run --rm -i ghcr.io/jqlang/jq:1.7rc1 < $_output_file --raw-output '.tunnels | map(select(.name=="'${_tunnel_name}'")) | .[0] | .public_url')
if [ -z "${NGROK_URL}" ] || [ "null" = "${NGROK_URL}" ]; then
return 1
else
return 0
fi
}
local startTime=${SECONDS}
local timeout=${TIMEOUT:-60}
while ! extractUrl; do
printf "."
local duration=$(($SECONDS - $startTime))
if (( ${duration} >= ${timeout} )); then
echoError "It was not possible to establish a connection with ngrok, please check ${_output_file} and the ngrok container logs for errors."
rtnCd=1
break
fi
sleep 1
done
rm $_output_file
# assign value to target variable
printf -v "$_target_variable" "%s" "$NGROK_URL"
}
initializeUserPrompts() {
######
# If .env-dev exists, use it to populate .env
# Otherwise, create an empty .env file
if [ -f ".env-dev" ] ; then
cat .env-dev > .env
echo "" >> .env
else
touch .env
fi
######
echo "AGENT_WALLET_SEED=$(generateSeed dav)" >> .env
startNgrokContainers
setNgrokEndpoints
}
# =================================================================================================================
pushd ${SCRIPT_HOME} >/dev/null
COMMAND=$(toLower ${1})
shift || COMMAND=usage
case "${COMMAND}" in
start|up)
_startupParams=$(getStartupParams $@)
if [[ ! -f ".env" ]]; then
# first/clean run, prompt user selections
initializeUserPrompts
echoWarning "User preferences were saved in docker/.env for future use"
else
# ngrok was already chosen, refresh containers/endpoints
echoInfo "Refreshing ngrok containers..."
startNgrokContainers
setNgrokEndpoints
fi
configureEnvironment $@
docker compose up -d ${_startupParams} ${DEFAULT_CONTAINERS} ${ACAPY_CONTAINERS} ${PROD_CONTAINERS}
docker compose logs -f
;;
logs)
configureEnvironment $@
docker compose logs -f
;;
stop)
configureEnvironment
docker compose stop
docker compose -f docker-compose-ngrok.yaml stop
;;
rm|down)
# delete previously saved settings
if [ -f ".env" ] ; then
rm ".env"
fi
configureEnvironment
deleteVolumes
;;
build)
_startupParams=$(getStartupParams $@)
configureEnvironment $@
buildImages
;;
*)
usage
;;
esac
popd >/dev/null