Skip to content

Commit ad2beec

Browse files
committed
gw: Add LAUNCH TOKEN
1 parent 0f4d347 commit ad2beec

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
generated/
66
node_modules/
77
/.cargo
8+
.venv

gateway/dstack-app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.app-compose.json
2+
/.prelaunch.sh
23
/.env
34
/.venv
45
/.app_env

gateway/dstack-app/deploy-to-vmm.sh

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
#!/bin/bash
22

3+
APP_COMPOSE_FILE=""
4+
5+
usage() {
6+
echo "Usage: $0 [-c <app compose file>]"
7+
echo " -c App compose file"
8+
}
9+
10+
while getopts "c:h" opt; do
11+
case $opt in
12+
c)
13+
APP_COMPOSE_FILE=$OPTARG
14+
;;
15+
h)
16+
usage
17+
exit 0
18+
;;
19+
\?)
20+
usage
21+
exit 1
22+
;;
23+
esac
24+
done
25+
326
# Check if .env exists
427
if [ -f ".env" ]; then
528
# Load variables from .env
@@ -57,6 +80,9 @@ GATEWAY_SERVING_ADDR=0.0.0.0:9204
5780
GUEST_AGENT_ADDR=127.0.0.1:9206
5881
WG_ADDR=0.0.0.0:9202
5982
83+
# The token used to launch the App
84+
APP_LAUNCH_TOKEN=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
85+
6086
EOF
6187
echo "Please edit the .env file and set the required variables, then run this script again."
6288
exit 1
@@ -72,6 +98,7 @@ required_env_vars=(
7298
"WG_ADDR"
7399
"GATEWAY_APP_ID"
74100
"MY_URL"
101+
"APP_LAUNCH_TOKEN"
75102
# "BOOTNODE_URL"
76103
)
77104

@@ -112,17 +139,36 @@ WG_ENDPOINT=$PUBLIC_IP:$WG_PORT
112139
MY_URL=$MY_URL
113140
BOOTNODE_URL=$BOOTNODE_URL
114141
SUBNET_INDEX=$SUBNET_INDEX
142+
APP_LAUNCH_TOKEN=$APP_LAUNCH_TOKEN
115143
EOF
116144

117-
$CLI compose \
118-
--docker-compose "$COMPOSE_TMP" \
119-
--name dstack-gateway \
120-
--kms \
121-
--env-file .app_env \
122-
--public-logs \
123-
--public-sysinfo \
124-
--no-instance-id \
125-
--output .app-compose.json
145+
if [ -n "$APP_COMPOSE_FILE" ]; then
146+
cp "$APP_COMPOSE_FILE" .app-compose.json
147+
else
148+
149+
EXPECTED_TOKEN_HASH=$(echo -n "$APP_LAUNCH_TOKEN" | sha256sum | cut -d' ' -f1)
150+
cat >.prelaunch.sh <<EOF
151+
ACTUAL_TOKEN_HASH=\$(echo -n "\$APP_LAUNCH_TOKEN" | sha256sum | cut -d' ' -f1)
152+
if [ "$EXPECTED_TOKEN_HASH" != "\$ACTUAL_TOKEN_HASH" ]; then
153+
echo "Error: Incorrect APP_LAUNCH_TOKEN, please make sure set the correct APP_LAUNCH_TOKEN in env"
154+
reboot
155+
exit 1
156+
else
157+
echo "APP_LAUNCH_TOKEN checked OK"
158+
fi
159+
EOF
160+
161+
$CLI compose \
162+
--docker-compose "$COMPOSE_TMP" \
163+
--name dstack-gateway \
164+
--kms \
165+
--env-file .app_env \
166+
--public-logs \
167+
--public-sysinfo \
168+
--no-instance-id \
169+
--prelaunch-script .prelaunch.sh \
170+
--output .app-compose.json
171+
fi
126172

127173
# Remove the temporary file as it is no longer needed
128174
rm "$COMPOSE_TMP"

vmm/src/vmm-cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def create_app_compose(self,
426426
"secure_time": True,
427427
}
428428
if prelaunch_script:
429-
app_compose["prelaunch_script"] = prelaunch_script
429+
app_compose["pre_launch_script"] = open(prelaunch_script, 'rb').read().decode('utf-8')
430430

431431
compose_file = json.dumps(app_compose, indent=4).encode('utf-8')
432432
compose_hash = hashlib.sha256(compose_file).hexdigest()

kms/dstack-app/venv.sh renamed to vmm/venv.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ if [ -f ".venv/bin/activate" ]; then
55
else
66
python3 -m venv .venv
77
source .venv/bin/activate
8-
pip install requests eth_keys cryptography
8+
pip install requests eth_keys cryptography "eth-hash[pycryptodome]"
9+
cp src/vmm-cli.py .venv/bin/
10+
ln -sf vmm-cli.py .venv/bin/vmm
911
fi

0 commit comments

Comments
 (0)