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
427if [ -f " .env" ]; then
528 # Load variables from .env
@@ -57,6 +80,9 @@ GATEWAY_SERVING_ADDR=0.0.0.0:9204
5780GUEST_AGENT_ADDR=127.0.0.1:9206
5881WG_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+
6086EOF
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
112139MY_URL=$MY_URL
113140BOOTNODE_URL=$BOOTNODE_URL
114141SUBNET_INDEX=$SUBNET_INDEX
142+ APP_LAUNCH_TOKEN=$APP_LAUNCH_TOKEN
115143EOF
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
128174rm " $COMPOSE_TMP "
0 commit comments