-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.sh
executable file
·374 lines (332 loc) · 13.2 KB
/
start.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
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
#!/bin/bash
# Script to start all Frequency Developer Gateway services on the Frequency Paseo Testnet
# Load external functions
. ./bash_functions.sh
SKIP_CHAIN_SETUP=false
###################################################################################
# show_help
#
# Description: Display usage instructions for the script
#
###################################################################################
function show_help() {
echo "Usage: ./start.sh [options]"
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -n, --name Specify the project name"
echo " -s, --skip-setup Skip running chain scenario setup (provider, capacity, etc)"
}
###################################################################################
# parse_arguments
#
# Description: Parse command-line arguments
#
###################################################################################
function parse_arguments() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) show_help; return 0 ;;
-n|--name) BASE_NAME="$2"; shift ;;
-s|--skip-setup) SKIP_CHAIN_SETUP=true ;;
*) echo "Unknown parameter passed: $1"; show_help; return 1 ;;
esac
shift
done
}
###################################################################################
# setup_environment
#
# Description: Set up environment variables and directories
#
###################################################################################
function setup_environment() {
if [ ! -d "${BASE_DIR}" ]; then
mkdir -p "${BASE_DIR}"
fi
ENV_FILE=${BASE_DIR}/.env.${BASE_NAME}
COMPOSE_PROJECT_NAME=${BASE_NAME}
if [[ -n $ENV_FILE ]]; then
${OUTPUT} "Using environment file: $ENV_FILE"
fi
return 0
}
###################################################################################
# check_dependencies
#
# Description: Ensure Docker and Docker Compose are installed
#
###################################################################################
function check_dependencies() {
if ! command -v docker &> /dev/null || ! command -v docker compose &> /dev/null; then
${OUTPUT} "Docker and Docker Compose are required but not installed. Please install them and try again.\n"
exit 1
fi
}
###################################################################################
# handle_env_file
#
# Description: Manage existing environment files
#
###################################################################################
function handle_env_file() {
if [ -f ${ENV_FILE} ]; then
echo -e "Found saved environment from a previous run:\n"
redacted_content=$(redact_sensitive_values "${ENV_FILE}")
echo "${redacted_content}"
if yesno "Do you want to re-use the saved parameters" Y; then
${OUTPUT} "Loading environment values from file..."
else
${OUTPUT} "Removing previous saved environment..."
rm ${ENV_FILE}
# If the file fails to delete, exit the script
if [ -f ${ENV_FILE} ]; then
${OUTPUT} "Failed to remove previous saved environment. Exiting..."
exit 1
fi
fi
fi
return 0
}
###################################################################################
# prompt_for_configuration
#
# Description: Interactive prompts to gather necessary configuration
#
###################################################################################
function prompt_for_configuration() {
${OUTPUT} << EOI
Creating project environment file:
${ENV_FILE}
EOI
# Setup some variables for easy port management
STARTING_PORT=3010
for i in {0..10}; do
eval SERVICE_PORT_${i}=$(( STARTING_PORT + i ))
export_save_variable SERVICE_PORT_${i} $(( STARTING_PORT + i ))
done
export_save_variable COMPOSE_PROJECT_NAME "${COMPOSE_PROJECT_NAME}"
# Ask the user if they want to use published containers or build from source
if yesno "Do you want to use the published Gateway Services containers" Y; then
export_save_variable USE_PUBLISHED true
echo
read -p "Enter a tag to use to pull the Gateway Docker images [latest]: " tag
export_save_variable DOCKER_TAG "${tag:-latest}"
COMPOSE_FILES="docker-compose-published.yaml"
${OUTPUT} << EOI
Using Gateway Services published containers from Docker Hub with tag: ${DOCKER_TAG}
EOI
else
export_save_variable USE_PUBLISHED false
COMPOSE_FILES="docker-compose.yaml docker-compose-local-selective.yaml"
${OUTPUT} << EOI
Using Gateway Services development containers built from local source...
EOI
fi
# Ask the user if they want to start on testnet or local
if yesno "Do you want to start on Frequency Paseo Testnet" N; then
TESTNET_ENV=true
PROFILES="${PROFILES}"
else
TESTNET_ENV=false
COMPOSE_FILES="${COMPOSE_FILES} docker-compose.local-frequency.yaml"
fi
export_save_variable TESTNET_ENV "${TESTNET_ENV}"
# Ask the user which services they want to start
${OUTPUT} << EOI
Select the services you want to start.
If you only want to start selected services, enter 'n' to exclude the service.
Hit <ENTER> to accept the default value or enter new value and then hit <ENTER>
EOI
if yesno "Start the account service" Y; then
PROFILES="${PROFILES} account"
fi
if yesno "Start the graph service" Y; then
PROFILES="${PROFILES} graph"
fi
if yesno "Start the content-publishing service" Y; then
PROFILES="${PROFILES} content_publishing"
fi
if yesno "Start the content-watcher service" Y; then
PROFILES="${PROFILES} content_watcher"
fi
if [ "${TESTNET_ENV}" != true ]; then
PROFILES="${PROFILES} local-node"
fi
${OUTPUT} << EOI
Selected services to start:
${PROFILES}
EOI
if [[ ${TESTNET_ENV} = true ]]; then
${OUTPUT} << EOI
Setting defaults for testnet...
Hit <ENTER> to accept the default value, or,
enter new value and then hit <ENTER>"
EOI
DEFAULT_TESTNET_ENV="testnet"
DEFAULT_FREQUENCY_API_WS_URL="wss://0.rpc.testnet.amplica.io"
DEFAULT_SIWF_NODE_RPC_URL="https://0.rpc.testnet.amplica.io"
DEFAULT_PROVIDER_ID="INPUT REQUIRED"
DEFAULT_PROVIDER_ACCOUNT_SEED_PHRASE="INPUT REQUIRED"
else
echo -e "\nStarting on local..."
DEFAULT_TESTNET_ENV="local"
DEFAULT_FREQUENCY_API_WS_URL="ws://frequency:9944"
DEFAULT_SIWF_NODE_RPC_URL="http://localhost:9944"
DEFAULT_PROVIDER_ID="1"
DEFAULT_PROVIDER_ACCOUNT_SEED_PHRASE="//Alice"
fi
DEFAULT_IPFS_VOLUME="/data/ipfs"
DEFAULT_IPFS_ENDPOINT="http://ipfs:5001"
DEFAULT_IPFS_GATEWAY_URL='https://ipfs.io/ipfs/[CID]'
DEFAULT_IPFS_BASIC_AUTH_USER=""
DEFAULT_IPFS_BASIC_AUTH_SECRET=""
DEFAULT_IPFS_UA_GATEWAY_URL="http://localhost:8080"
DEFAULT_CONTENT_DB_VOLUME="content_db"
ask_and_save FREQUENCY_API_WS_URL "Enter the Frequency API Websocket URL" "$DEFAULT_FREQUENCY_API_WS_URL"
ask_and_save SIWF_NODE_RPC_URL "Enter the SIWF Node RPC URL" "$DEFAULT_SIWF_NODE_RPC_URL"
box_text_attention -w ${BOX_WIDTH} << EOI
A Provider is required to start the services.
If you need to become a provider, visit
https://provider.frequency.xyz/ to get a Provider ID.
EOI
ask_and_save PROVIDER_ID "Enter Provider ID" "$DEFAULT_PROVIDER_ID"
ask_and_save PROVIDER_ACCOUNT_SEED_PHRASE "Enter Provider Seed Phrase" "$DEFAULT_PROVIDER_ACCOUNT_SEED_PHRASE" true
${OUTPUT} "Suggestion: Change to an IPFS Pinning Service for better persistence and availability."
if yesno "===> Given this suggestion, would you like to change the IPFS settings?" N; then
ask_and_save IPFS_VOLUME "Enter the IPFS volume" "$DEFAULT_IPFS_VOLUME"
ask_and_save IPFS_ENDPOINT "Enter the IPFS Endpoint" "$DEFAULT_IPFS_ENDPOINT"
ask_and_save IPFS_GATEWAY_URL "Enter the IPFS Gateway URL" "$DEFAULT_IPFS_GATEWAY_URL"
ask_and_save IPFS_BASIC_AUTH_USER "Enter the IPFS Basic Auth User" "$DEFAULT_IPFS_BASIC_AUTH_USER" true
ask_and_save IPFS_BASIC_AUTH_SECRET "Enter the IPFS Basic Auth Secret" "$DEFAULT_IPFS_BASIC_AUTH_SECRET" true
ask_and_save IPFS_UA_GATEWAY_URL "Enter the browser-resolveable IPFS Gateway URL" "$DEFAULT_IPFS_UA_GATEWAY_URL"
else
# Add the IPFS settings to the .env-saved file so defaults work with local testing
cat >> ${ENV_FILE} << EOI
IPFS_VOLUME="${DEFAULT_IPFS_VOLUME}"
IPFS_ENDPOINT="${DEFAULT_IPFS_ENDPOINT}"
IPFS_GATEWAY_URL="${DEFAULT_IPFS_GATEWAY_URL}"
IPFS_BASIC_AUTH_USER="${DEFAULT_IPFS_BASIC_AUTH_USER}"
IPFS_BASIC_AUTH_SECRET="${DEFAULT_IPFS_BASIC_AUTH_SECRET}"
IPFS_UA_GATEWAY_URL="${DEFAULT_IPFS_UA_GATEWAY_URL}"
EOI
fi
export_save_variable PROFILES "${PROFILES}"
export_save_variable COMPOSE_FILES "${COMPOSE_FILES}"
}
###################################################################################
# local_setup_tasks
#
# Description: Perform tasks after starting services, such as setting up environments
#
###################################################################################
function local_setup_tasks() {
if [ "${SKIP_CHAIN_SETUP}" != true ] && [ "${TESTNET_ENV}" != true ]; then
# Wait 1 minute for Frequency node to be ready
health_attempts=0
while (( health_attempts < 60 )) && ! is_frequency_ready; do
(( health_attempts += 1 ))
echo "Waiting for Frequency node to respond..."
sleep 1
done
if is_frequency_ready; then
# Run npm run local:init
echo "Running local setup script to provision Provider with capacity, etc..."
npm run setup:common:chain
else
echo "Timed out waiting for Frequency node to be ready" >&2
fi
fi
}
###################################################################################
# start_services
#
# Description: Start Docker Compose services based on selected profiles
#
###################################################################################
function start_services() {
set -a
source ${ENV_FILE}
set +a
if [[ ${TESTNET_ENV} = false ]]; then
# Start specific services in detached mode
echo -e "\nStarting local frequency services..."
docker compose up -d frequency
local_setup_tasks
fi
# Start all services in detached mode
echo -e "\nStarting selected services..."
COMPOSE_CMD=$( prefix_postfix_values "${COMPOSE_FILES}" "-f ")
PROFILE_CMD=$( prefix_postfix_values "${PROFILES}" "--profile ")
docker compose ${COMPOSE_CMD} ${PROFILE_CMD} up -d
}
###################################################################################
# display_services_info
#
# Description: Display information about the running services
#
###################################################################################
function display_services_info() {
SERVICES_STR="\
The selected services are running.
You can access the Gateway at the following local addresses:
"
if [[ ${PROFILES} =~ account ]]; then
SERVICES_STR="${SERVICES_STR}
* account-service:
- API: http://localhost:${SERVICE_PORT_3}
- Queue management: http://localhost:${SERVICE_PORT_3}/queues
- Swagger UI: http://localhost:${SERVICE_PORT_3}/docs/swagger
- Mock Webhook: http://mock-webhook-logger:${ACCOUNT_WEBHOOK_PORT:-3001}/webhooks/account-service
(View log messages in docker)
"
fi
if [[ ${PROFILES} =~ content_publishing ]]; then
SERVICES_STR="${SERVICES_STR}
* content-publishing-service:
- API: http://localhost:${SERVICE_PORT_0}
- Queue management: http://localhost:${SERVICE_PORT_0}/queues
- Swagger UI: http://localhost:${SERVICE_PORT_0}/docs/swagger
"
fi
if [[ ${PROFILES} =~ content_watcher ]]; then
SERVICES_STR="${SERVICES_STR}
* content-watcher-service:
- API: http://localhost:${SERVICE_PORT_1}
- Queue management: http://localhost:${SERVICE_PORT_1}/queues
- Swagger UI: http://localhost:${SERVICE_PORT_1}/docs/swagger
"
fi
if [[ ${PROFILES} =~ graph ]]; then
SERVICES_STR="${SERVICES_STR}
* graph-service:
- API: http://localhost:${SERVICE_PORT_2}
- Queue management: http://localhost:${SERVICE_PORT_2}/queues
- Swagger UI: http://localhost:${SERVICE_PORT_2}/docs/swagger
"
fi
box_text_attention -w 0 "${SERVICES_STR}"
}
###################################################################################
# main
#
# Description: Main function to execute the script logic
#
###################################################################################
function main() {
# Call the check_pcre_grep function to initialize PCRE_GREP and OUTPUT
check_pcre_grep
parse_arguments "$@"
setup_environment
check_dependencies
handle_env_file
if [ ! -f "${ENV_FILE}" ]; then
prompt_for_configuration
fi
start_services
display_services_info
exit 0
}
# Call main function if the script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi