-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_env.sh
executable file
·240 lines (220 loc) · 7.08 KB
/
setup_env.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
#!/bin/bash
# This script uses flags to create a `.env` file for docker compose
# each optional argument adds a new component to the `.env` file
# Check if no arguments or --help is passed
if [[ $# -eq 0 ]] || [[ "$1" == "--help" ]]; then
echo "Usage: ./setup_env.sh [flags]"
echo "Flag Groups:"
echo " --all : Fund accounts and enable all components."
echo " --develop : Fund accounts and enable anvil, data, and ports. Suitable for local development work."
echo " --remote-service-bots : Runs service bots on a remote chain."
echo "Flags:"
echo " --anvil : Spin up an Anvil node, deploy Hyperdrive to it, and serve artifacts on an nginx server."
echo " --blocktime : Sets the anvil node to run in blocktime mode."
echo " --testnet : Uses the testnet hyperdrive image with restricted mint access."
echo " --frontend : Build the frontend container."
echo " --postgres : Runs a postgres db container for storing data."
echo " --data : Runs the data framework, querying the chain and writing to postgres."
echo " --service-bot : Runs checkpoint bot and invariance check bots on the chain."
echo " --random-bot : Runs random bots on the chain."
echo " --ports : Expose docker images to your machine, as specified in env/env.ports."
echo " --fund-accounts : Fund accounts from /accounts/balances.json."
echo " --rate-bot : Yield source will have a dynamic variable rate."
exit 0
fi
# Parse all of the arguments
## Initialize variables
ANVIL=false
BLOCKTIME=false
TESTNET=false
FRONTEND=false
POSTGRES=false
DATA=false
PORTS=false
SERVICE_BOT=false
RANDOM_BOT=false
RATE_BOT=false
FUND_ACCOUNTS=false
## Loop through the arguments
while [[ $# -gt 0 ]]; do
case $1 in
# Flag groups
--all)
ANVIL=true
# Don't run blocktime here, we want chain to be in fast mode
# Don't run testnet image here since random bots need to be able to mint
FRONTEND=true
POSTGRES=true
DATA=true
PORTS=true
SERVICE_BOT=true
RANDOM_BOT=true
RATE_BOT=true
FUND_ACCOUNTS=true
;;
--develop)
ANVIL=true
POSTGRES=true
DATA=true
PORTS=true
FUND_ACCOUNTS=true
;;
--remote-service-bots)
PORTS=true
SERVICE_BOT=true
;;
# Flags
--anvil)
ANVIL=true
;;
--blocktime)
BLOCKTIME=true
;;
--testnet)
TESTNET=true
;;
--frontend)
FRONTEND=true
;;
--data)
DATA=true
;;
--postgres)
POSTGRES=true
;;
--ports)
PORTS=true
;;
--service-bot)
SERVICE_BOT=true
;;
--rate-bot)
RATE_BOT=true
;;
--fund-accounts)
FUND_ACCOUNTS=true
;;
*)
echo "Unknown flag: $1"
;;
esac
shift
done
# Make a new .env file
rm -f .env
touch .env
echo "# Environment for Docker compose" >>.env
# Set up the COMPOSE_FILE environment variable. To ensure that we don't
# experience issues with missing "image" or "build" fields, we always include
# all of the services in the compose context. We determine which layers are
# turned on using docker compose profiles.
anvil_compose="docker-compose.anvil.yaml"
blocktime_compose="docker-compose.blocktime.yaml"
testnet_compose="docker-compose.testnet.yaml"
frontend_compose="docker-compose.frontend.yaml"
postgres_compose="docker-compose.postgres.yaml"
data_compose="docker-compose.data.yaml"
service_bot_compose="docker-compose.agent0-bots.yaml"
ports_compose="docker-compose.ports.yaml"
# We default to using profiles to control which services are turned on.
full_compose_files="COMPOSE_FILE=$anvil_compose:$frontend_compose:$postgres_compose:$data_compose:$service_bot_compose:"
# We only add controls here if these compose files update existing services.
if $BLOCKTIME; then
full_compose_files+="$blocktime_compose:"
fi
if $TESTNET; then
full_compose_files+="$testnet_compose:"
fi
if $PORTS; then
full_compose_files+="$ports_compose:"
fi
# Check if ":" is at the end of the string
if [[ $full_compose_files == *":" ]]; then
# Remove ":" from the end of the string
full_compose_files=${full_compose_files%:}
fi
# Append the COMPOSE_FILE environment variable to the .env file
echo $full_compose_files >>.env
# Set up the COMPOSE_PROFILES environment variable. This toggles which layers
# should be started.
anvil_profile="anvil"
blocktime_profile="blocktime"
frontend_profile="frontend"
postgres_profile="postgres"
data_profile="data"
service_bot_profile="service-bot"
random_bot_profile="random-bot"
rate_bot_profile="rate-bot"
fund_accounts_profile="fund-accounts"
full_compose_profiles="COMPOSE_PROFILES="
if $ANVIL; then
full_compose_profiles+="$anvil_profile,"
fi
if $BLOCKTIME; then
full_compose_profiles+="$blocktime_profile,"
fi
if $FRONTEND; then
full_compose_profiles+="$frontend_profile,"
fi
if $POSTGRES; then
full_compose_profiles+="$postgres_profile,"
fi
if $DATA; then
full_compose_profiles+="$data_profile,"
fi
if $SERVICE_BOT; then
full_compose_profiles+="$service_bot_profile,"
fi
if $RANDOM_BOT; then
full_compose_profiles+="$random_bot_profile,"
fi
if $RATE_BOT; then
full_compose_profiles+="$rate_bot_profile,"
fi
if $FUND_ACCOUNTS; then
full_compose_profiles+="$fund_accounts_profile,"
fi
# Check if "," is at the end of the string
if [[ $full_compose_profiles == *"," ]]; then
# Remove "," from the end of the string
full_compose_profiles=${full_compose_profiles%,}
fi
# Append the COMPOSE_PROFILES environment variable to the .env file
echo $full_compose_profiles >>.env
# cat env.common and env.ports to the .env file
echo "" >>.env
cat env/env.common >> .env
# optionally cat env.anvil to .env file if --anvil
if $ANVIL; then
echo "" >>.env
cat env/env.anvil >>.env
fi
echo "" >>.env
cat env/env.ports >>.env
# cat env.images to the .env file. We have to manage conflicts between different
# anvil images here, so we do it manually.
echo "" >>.env
cat env/env.images >>.env
source env/env.images
# optionally add an env.time to .env file if --blocktime
if $BLOCKTIME; then
echo "" >>.env
cat env/env.time >>.env
fi
# optionally add an env.frontend to .env file if --frontend
if $FRONTEND; then
echo "" >>.env
cat env/env.frontend >>.env
fi
# optionally add an env.frontend to .env file if --postgres or --data
# POSTGRES uses these flags to launch postgres.
# All agent0 images uses these flags to connect
if $POSTGRES || $DATA || $SERVICE_BOT || $RANDOM_BOT || $RATE_BOT; then
echo "" >>.env
cat env/env.postgres >> .env
fi
if $SERVICE_BOT; then
echo "" >>.env
cat env/env.bots >> .env
fi
echo "Environment filed created at .env"