-
Notifications
You must be signed in to change notification settings - Fork 38
441 lines (381 loc) · 17.5 KB
/
manual-deploy-obscuro-gateway.yml
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Deploys TEN Gateway on Azure for Testnet
# Builds the TEN Gateway image, pushes the image to dockerhub and starts the TEN Gateway on Azure VM
# This action requires the following environment variables to be set:
# - DOCKER_BUILD_TAG_GATEWAY
# - AZURE_DEPLOY_GROUP_GATEWAY
# - L2_RPC_URL_VALIDATOR
# - GATEWAY_RATE_LIMIT_USER_COMPUTE_TIME
# - GATEWAY_RATE_LIMIT_WINDOW
# - GATEWAY_MAX_CONCURRENT_REQUESTS_PER_USER
# If we are deploying to a non primary instance all those variables should be prefixed with the instance name
# example: DEXYNTH_DOCKER_BUILD_TAG_GATEWAY
name: '[M] Deploy Ten Gateway Backend'
run-name: '[M] Deploy Ten Gateway Backend ( ${{ github.event.inputs.testnet_type }} )'
on:
workflow_dispatch:
inputs:
testnet_type:
description: "Environment"
required: true
default: "dev-testnet"
type: choice
options:
- "dev-testnet"
- "uat-testnet"
- "sepolia-testnet"
instance_type:
description: "Instance"
required: true
default: "primary"
type: choice
options:
- "primary"
- "DEXYNTH"
recreate_vm:
description: "Delete and recreate VM"
required: false
default: "false"
type: choice
options:
- "false"
- "true"
jobs:
validate-inputs:
runs-on: ubuntu-latest
steps:
- name: "Check if deployment is allowed"
run: |
if [[ "${{ github.event.inputs.instance_type }}" == "DEXYNTH" && "${{ github.event.inputs.testnet_type }}" != "sepolia-testnet" ]]; then
echo "Error: Dexynth can only be deployed to sepolia-testnet."
exit 1
fi
build-and-deploy:
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs.testnet_type }}
steps:
- name: "Set up environment variables"
id: setup_env
run: |
INSTANCE_SUFFIX=""
INSTANCE_PREFIX=""
if [[ "${{ github.event.inputs.instance_type }}" != "primary" ]]; then
INSTANCE_SUFFIX="-${{ github.event.inputs.instance_type }}"
INSTANCE_PREFIX="${{ github.event.inputs.instance_type }}_"
fi
echo "INSTANCE_SUFFIX=$INSTANCE_SUFFIX" >> $GITHUB_ENV
echo "INSTANCE_PREFIX=$INSTANCE_PREFIX" >> $GITHUB_ENV
# Set infrastructure variables
PUBLIC_IP="${{ github.event.inputs.testnet_type }}-OG-static${INSTANCE_SUFFIX,,}"
DNS_NAME="obscurogateway-${{ github.event.inputs.testnet_type }}${INSTANCE_SUFFIX,,}"
VM_NAME="${{ github.event.inputs.testnet_type }}-OG${INSTANCE_SUFFIX}"
DEPLOY_GROUP="ObscuroGateway-${{ github.event.inputs.testnet_type }}${INSTANCE_SUFFIX}"
VNET_NAME="ObscuroGateway-${{ github.event.inputs.testnet_type }}-01VNET${INSTANCE_SUFFIX}"
SUBNET_NAME="ObscuroGateway-${{ github.event.inputs.testnet_type }}-01Subnet${INSTANCE_SUFFIX}"
echo "PUBLIC_IP=$PUBLIC_IP" >> $GITHUB_ENV
echo "DNS_NAME=$DNS_NAME" >> $GITHUB_ENV
echo "VM_NAME=$VM_NAME" >> $GITHUB_ENV
echo "DEPLOY_GROUP=$DEPLOY_GROUP" >> $GITHUB_ENV
echo "VNET_NAME=$VNET_NAME" >> $GITHUB_ENV
echo "SUBNET_NAME=$SUBNET_NAME" >> $GITHUB_ENV
# Set instance-specific variables
declare -a VAR_NAMES=(
"DOCKER_BUILD_TAG_GATEWAY"
"AZURE_DEPLOY_GROUP_GATEWAY"
"L2_RPC_URL_VALIDATOR"
"GATEWAY_RATE_LIMIT_USER_COMPUTE_TIME"
"GATEWAY_RATE_LIMIT_WINDOW"
"GATEWAY_MAX_CONCURRENT_REQUESTS_PER_USER"
"GATEWAY_KEY_EXCHANGE_URL"
"GATEWAY_TLS_DOMAIN"
)
for VAR_NAME in "${VAR_NAMES[@]}"; do
FULL_VAR_NAME="${INSTANCE_PREFIX}${VAR_NAME}"
VAR_VALUE=$(jq -r --arg key "$FULL_VAR_NAME" '.[$key] // empty' <<< '${{ toJson(vars) }}')
if [[ -n "$VAR_VALUE" ]]; then
echo "${VAR_NAME}=${VAR_VALUE}" >> $GITHUB_ENV
else
echo "Warning: ${FULL_VAR_NAME} not found in vars" >&2
fi
done
- name: "Print environment variables"
run: |
echo "INSTANCE_SUFFIX: $INSTANCE_SUFFIX"
echo "INSTANCE_PREFIX: $INSTANCE_PREFIX"
echo "PUBLIC_IP: $PUBLIC_IP"
echo "DNS_NAME: $DNS_NAME"
echo "VM_NAME: $VM_NAME"
echo "DEPLOY_GROUP: $DEPLOY_GROUP"
echo "VNET_NAME: $VNET_NAME"
echo "SUBNET_NAME: $SUBNET_NAME"
echo "DOCKER_BUILD_TAG_GATEWAY: $DOCKER_BUILD_TAG_GATEWAY"
echo "AZURE_DEPLOY_GROUP_GATEWAY: $AZURE_DEPLOY_GROUP_GATEWAY"
echo "L2_RPC_URL_VALIDATOR: $L2_RPC_URL_VALIDATOR"
echo "GATEWAY_RATE_LIMIT_USER_COMPUTE_TIME: $GATEWAY_RATE_LIMIT_USER_COMPUTE_TIME"
echo "GATEWAY_RATE_LIMIT_WINDOW: $GATEWAY_RATE_LIMIT_WINDOW"
echo "GATEWAY_MAX_CONCURRENT_REQUESTS_PER_USER: $GATEWAY_MAX_CONCURRENT_REQUESTS_PER_USER"
echo "GATEWAY_KEY_EXCHANGE_URL: $GATEWAY_KEY_EXCHANGE_URL"
echo "GATEWAY_TLS_DOMAIN: $GATEWAY_TLS_DOMAIN"
- name: "Print GitHub variables"
run: |
echo "GitHub Variables = ${{ toJSON(vars) }}"
- uses: actions/checkout@v4
- name: "Extract branch name"
shell: bash
run: |
echo "Branch Name: ${GITHUB_REF_NAME}"
echo "BRANCH_NAME=${GITHUB_REF_NAME}" >> $GITHUB_ENV
- name: "Set up Docker"
uses: docker/setup-buildx-action@v1
- name: "Login to Azure docker registry"
uses: azure/docker-login@v1
with:
login-server: testnetobscuronet.azurecr.io
username: testnetobscuronet
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: "Login via Azure CLI"
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Build and Push Docker Image
run: |
DOCKER_BUILDKIT=1 docker build --build-arg TESTNET_TYPE=${{ github.event.inputs.testnet_type }} -t ${{ env.DOCKER_BUILD_TAG_GATEWAY }} -f ./tools/walletextension/enclave.Dockerfile .
docker push ${{ env.DOCKER_BUILD_TAG_GATEWAY }}
# If recreate_vm = true, delete VMs and their dependencies
- name: "Delete deployed VMs"
if: ${{ github.event.inputs.recreate_vm == 'true' }}
uses: azure/CLI@v1
with:
inlineScript: |
$(az resource list --tag ${{ env.AZURE_DEPLOY_GROUP_GATEWAY }}=true --query '[]."id"' -o tsv | xargs -n1 az resource delete --verbose -g Testnet --ids) || true
- name: "Delete VMs dependencies"
if: ${{ github.event.inputs.recreate_vm == 'true' }}
uses: azure/CLI@v1
with:
inlineScript: |
$(az resource list --tag ${{ env.AZURE_DEPLOY_GROUP_GATEWAY }}=true --query '[]."id"' -o tsv | xargs -n1 az resource delete --verbose -g Testnet --ids) || true
# If recreate_vm = false, check if VM exists
- name: "Check if VM exists"
if: ${{ github.event.inputs.recreate_vm == 'false' }}
id: check_vm
shell: bash
run: |
if ! az vm show -g Testnet -n "${{ env.VM_NAME }}" &> /dev/null; then
echo "vm_exists=false" >> $GITHUB_ENV
else
echo "vm_exists=true" >> $GITHUB_ENV
fi
- name: "Ensure VM Static Public IP and DNS if needed"
if: ${{ github.event.inputs.recreate_vm == 'true' || env.vm_exists == 'false' }}
uses: azure/CLI@v1
with:
inlineScript: |
az network public-ip show -g Testnet -n "${{ env.PUBLIC_IP }}" || az network public-ip create -g Testnet -n "${{ env.PUBLIC_IP }}" --allocation-method Static --sku Standard
existing_dns_name=$(az network public-ip show -g Testnet -n "${{ env.PUBLIC_IP }}" --query dnsSettings.domainNameLabel -o tsv)
if [ -z "$existing_dns_name" ]; then
az network public-ip update -g Testnet -n "${{ env.PUBLIC_IP }}" --dns-name "${{ env.DNS_NAME }}"
fi
- name: "Create VM if it doesn't exist (recreate_vm=false)"
if: ${{ github.event.inputs.recreate_vm == 'false' && env.vm_exists == 'false' }}
uses: azure/CLI@v1
with:
inlineScript: |
az vm create -g Testnet -n "${{ env.VM_NAME }}" \
--admin-username obscurouser --admin-password "${{ secrets.OBSCURO_NODE_VM_PWD }}" \
--public-ip-address "${{ env.PUBLIC_IP }}" \
--tags deploygroup="${{ env.DEPLOY_GROUP }}" ${{ env.AZURE_DEPLOY_GROUP_GATEWAY }}=true \
--vnet-name "${{ env.VNET_NAME }}" --subnet "${{ env.SUBNET_NAME }}" \
--size Standard_DC2s_v3 --storage-sku StandardSSD_LRS --image ObscuroConfUbuntu \
--authentication-type password
az vm open-port -g Testnet -n "${{ env.VM_NAME }}" --port 80,81,443
# Allow time for VM initialization
sleep 30
- name: "Create VM if recreate_vm = true"
if: ${{ github.event.inputs.recreate_vm == 'true' }}
uses: azure/CLI@v1
with:
inlineScript: |
az vm create -g Testnet -n "${{ env.VM_NAME }}" \
--admin-username obscurouser --admin-password "${{ secrets.OBSCURO_NODE_VM_PWD }}" \
--public-ip-address "${{ env.PUBLIC_IP }}" \
--tags deploygroup="${{ env.DEPLOY_GROUP }}" ${{ env.AZURE_DEPLOY_GROUP_GATEWAY }}=true \
--vnet-name "${{ env.VNET_NAME }}" --subnet "${{ env.SUBNET_NAME }}" \
--size Standard_DC2s_v3 --storage-sku StandardSSD_LRS --image ObscuroConfUbuntu \
--authentication-type password
az vm open-port -g Testnet -n "${{ env.VM_NAME }}" --port 80,81,443
# Allow time for VM initialization
sleep 30
- name: "Start TEN Gateway on Azure"
uses: azure/CLI@v1
with:
inlineScript: |
az vm run-command invoke -g Testnet -n "${{ env.VM_NAME }}" \
--command-id RunShellScript \
--scripts '
set -e
mkdir -p /home/obscuro
# Wait for dpkg lock to be released
while sudo fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
echo "Waiting for dpkg lock to be released..."
sleep 1
done
# Proceed with package installations
sudo apt-get update
sudo apt-get install -y gcc
sudo snap refresh && sudo snap install --channel=1.18 go --classic
# Wait again before running get-docker.sh
while sudo fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
echo "Waiting for dpkg lock to be released before installing Docker..."
sleep 1
done
curl -fsSL https://get.docker.com -o get-docker.sh && sh ./get-docker.sh
rm -rf /home/obscuro/go-obscuro
git clone --depth 1 -b "${{ env.BRANCH_NAME }}" https://github.com/ten-protocol/go-ten.git /home/obscuro/go-obscuro
if ! docker network inspect node_network >/dev/null 2>&1; then
docker network create --driver bridge node_network
fi
cd /home/obscuro/go-obscuro/
# Promtail Integration Start
mkdir -p /home/obscuro/metrics
cat <<EOF > /home/obscuro/metrics/promtail-config.yaml
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: "${{ vars.LOKI_URI }}"
batchwait: 3s
batchsize: 1048576
tls_config:
insecure_skip_verify: true
basic_auth:
username: "${{ secrets.LOKI_USER }}"
password: "${{ secrets.LOKI_PASSWORD }}"
scrape_configs:
- job_name: flog_scrape
docker_sd_configs:
- host: unix:///var/run/docker.sock
refresh_interval: 5s
relabel_configs:
- source_labels: ["__meta_docker_container_name"]
regex: "/(.*)"
target_label: "container"
- source_labels: ["__meta_docker_container_log_stream"]
target_label: "logstream"
- source_labels: ["__meta_docker_container_label_logging_jobname"]
target_label: "job"
- replacement: "${{ env.VM_NAME }}"
target_label: "node_name"
EOF
docker stop promtail || true
docker rm promtail || true
docker run -d --name promtail \
--network node_network \
-e HOSTNAME="${{ env.VM_NAME }}" \
-v /var/log:/var/log \
-v /home/obscuro/metrics:/etc/promtail \
-v /var/lib/docker/containers:/var/lib/docker/containers:ro \
-v /var/run/docker.sock:/var/run/docker.sock \
grafana/promtail:latest \
-config.file=/etc/promtail/promtail-config.yaml -config.expand-env=true
cat <<EOF > /home/obscuro/metrics/prometheus.yaml
global:
scrape_interval: 15s
evaluation_interval: 15s
remote_write:
- url: "${{ vars.PROMETHEUS_URI }}"
tls_config:
insecure_skip_verify: true
basic_auth:
username: "${{ secrets.LOKI_USER }}"
password: "${{ secrets.LOKI_PASSWORD }}"
scrape_configs:
# Node metrics
- job_name: node-${{ env.VM_NAME }}
scrape_interval: 5s
static_configs:
- targets:
- node_exporter:9100
relabel_configs:
- source_labels: [job]
target_label: "node"
replacement: node-${{ env.VM_NAME }}
# Container metrics
- job_name: container-${{ env.VM_NAME }}
scrape_interval: 5s
static_configs:
- targets:
- cadvisor:8080
relabel_configs:
- source_labels: [job]
target_label: "node"
replacement: container-${{ env.VM_NAME }}
EOF
docker stop prometheus || true
docker rm prometheus || true
docker volume create prometheus-data || true
docker run -d --name prometheus \
--network node_network \
-p 9090:9090 \
-v /home/obscuro/metrics/prometheus.yaml:/etc/prometheus/prometheus.yml \
-v prometheus-data:/prometheus \
prom/prometheus:latest \
--config.file=/etc/prometheus/prometheus.yml
docker stop node_exporter || true
docker rm node_exporter || true
docker run -d --name node_exporter \
--network node_network \
-p 9100:9100 \
--pid="host" \
-v /:/host:ro \
quay.io/prometheus/node-exporter:latest \
--path.rootfs=/host
docker stop cadvisor || true
docker rm cadvisor || true
docker run -d --name cadvisor \
--network node_network \
-p 8080:8080 \
--privileged \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
-v /dev/disk/:/dev/disk:ro \
gcr.io/cadvisor/cadvisor:latest
# Promtail Integration End
docker volume create "TENGateway-${{ github.event.inputs.testnet_type }}-data" || true
# Stop and remove existing container if it exists
docker stop "${{ env.VM_NAME }}" || true
docker rm "${{ env.VM_NAME }}" || true
docker pull ${{ env.DOCKER_BUILD_TAG_GATEWAY }}
# Start Ten Gateway Container
docker run -d -p 80:80 -p 81:81 -p 443:443 --name "${{ env.VM_NAME }}" \
--device /dev/sgx_enclave --device /dev/sgx_provision \
-v "TENGateway-${{ github.event.inputs.testnet_type }}-data:/data" \
-e OBSCURO_GATEWAY_VERSION="${{ github.run_number }}-${{ github.sha }}" \
-e OE_SIMULATION=0 \
"${{ env.DOCKER_BUILD_TAG_GATEWAY }}" \
ego run /home/ten/go-ten/tools/walletextension/main/main \
-host=0.0.0.0 -port=443 -portWS=443 -nodeHost="${{ env.L2_RPC_URL_VALIDATOR }}" -verbose=true \
-logPath=sys_out -dbType=cosmosDB -dbConnectionURL="${{ secrets.COSMOS_DB_CONNECTION_STRING }}" \
-rateLimitUserComputeTime="${{ env.GATEWAY_RATE_LIMIT_USER_COMPUTE_TIME }}" \
-rateLimitWindow="${{ env.GATEWAY_RATE_LIMIT_WINDOW }}" \
-maxConcurrentRequestsPerUser="${{ env.GATEWAY_MAX_CONCURRENT_REQUESTS_PER_USER }}" \
-keyExchangeURL="${{ env.GATEWAY_KEY_EXCHANGE_URL }}" \
-insideEnclave=true \
-enableTLS=true \
-tlsDomain="${{ env.GATEWAY_TLS_DOMAIN }}"
docker exec "${{ env.VM_NAME }}" sh -c "
echo \"Checking volume mount...\";
df -h | grep /data;
echo \"Directory listing:\";
ls -la /data;
echo \"Current working directory:\";
pwd;
echo \"Directory permissions:\";
ls -la /;
echo \"Process status:\";
ps aux;
"
'