Skip to content

Commit bd14fd0

Browse files
committed
update phala cloud prelaunch script
- Enforce setup password even if DSTACK_ROOT_PASSWORD is not set. - Use user-config introduced in dstack v0.5.x for gateway domain setup. - Update SSH public keys setup to be compatible with dstack v0.5.x read-only filesystem layout.
1 parent 681f00c commit bd14fd0

1 file changed

Lines changed: 41 additions & 14 deletions

File tree

phala-cloud-prelaunch-script/prelaunch.sh

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
echo "----------------------------------------------"
3-
echo "Running Phala Cloud Pre-Launch Script v0.0.8"
3+
echo "Running Phala Cloud Pre-Launch Script v0.0.10"
44
echo "----------------------------------------------"
55
set -e
66

@@ -136,22 +136,39 @@ fi
136136
perform_cleanup
137137

138138
#
139-
# Set root password if DSTACK_ROOT_PASSWORD is set.
139+
# Set root password.
140140
#
141-
if [[ -n "$DSTACK_ROOT_PASSWORD" ]]; then
142-
echo "$DSTACK_ROOT_PASSWORD" | passwd --stdin root 2>/dev/null || echo -e "$DSTACK_ROOT_PASSWORD\n$DSTACK_ROOT_PASSWORD" | passwd root
143-
unset $DSTACK_ROOT_PASSWORD
144-
echo "Root password set"
141+
if [ -n "$DSTACK_ROOT_PASSWORD" ]; then
142+
echo "$DSTACK_ROOT_PASSWORD" | passwd --stdin root 2>/dev/null \
143+
|| printf '%s\n%s\n' "$DSTACK_ROOT_PASSWORD" "$DSTACK_ROOT_PASSWORD" | passwd root
144+
unset DSTACK_ROOT_PASSWORD
145+
echo "Root password set/updated from DSTACK_ROOT_PASSWORD"
146+
147+
elif [ -z "$(grep '^root:' /etc/shadow 2>/dev/null | cut -d: -f2)" ]; then
148+
DSTACK_ROOT_PASSWORD=$(
149+
dd if=/dev/urandom bs=32 count=1 2>/dev/null \
150+
| sha256sum \
151+
| awk '{print $1}' \
152+
| cut -c1-32
153+
)
154+
echo "$DSTACK_ROOT_PASSWORD" | passwd --stdin root 2>/dev/null \
155+
|| printf '%s\n%s\n' "$DSTACK_ROOT_PASSWORD" "$DSTACK_ROOT_PASSWORD" | passwd root
156+
unset DSTACK_ROOT_PASSWORD
157+
echo "Root password set (random auto-init)"
158+
159+
else
160+
echo "Root password already set; no changes."
145161
fi
162+
146163
if [[ -n "$DSTACK_ROOT_PUBLIC_KEY" ]]; then
147-
mkdir -p /root/.ssh
148-
echo "$DSTACK_ROOT_PUBLIC_KEY" > /root/.ssh/authorized_keys
164+
mkdir -p /home/root/.ssh
165+
echo "$DSTACK_ROOT_PUBLIC_KEY" > /home/root/.ssh/authorized_keys
149166
unset $DSTACK_ROOT_PUBLIC_KEY
150167
echo "Root public key set"
151168
fi
152169
if [[ -n "$DSTACK_AUTHORIZED_KEYS" ]]; then
153-
mkdir -p /root/.ssh
154-
echo "$DSTACK_AUTHORIZED_KEYS" > /root/.ssh/authorized_keys
170+
mkdir -p /home/root/.ssh
171+
echo "$DSTACK_AUTHORIZED_KEYS" > /home/root/.ssh/authorized_keys
155172
unset $DSTACK_AUTHORIZED_KEYS
156173
echo "Root authorized_keys set"
157174
fi
@@ -162,10 +179,20 @@ if [[ -S /var/run/dstack.sock ]]; then
162179
elif [[ -S /var/run/tappd.sock ]]; then
163180
export DSTACK_APP_ID=$(curl -s --unix-socket /var/run/tappd.sock http://dstack/prpc/Tappd.Info | jq -j .app_id)
164181
fi
165-
# Check if app-compose.json has default_gateway_domain field and DSTACK_GATEWAY_DOMAIN is not set
166-
# If true, set DSTACK_GATEWAY_DOMAIN from app-compose.json
167-
if [[ $(jq 'has("default_gateway_domain")' app-compose.json) == "true" && -z "$DSTACK_GATEWAY_DOMAIN" ]]; then
168-
export DSTACK_GATEWAY_DOMAIN=$(jq -j '.default_gateway_domain' app-compose.json)
182+
# Check if DSTACK_GATEWAY_DOMAIN is not set, try to get it from user_config or app-compose.json
183+
# Priority: user_config > app-compose.json
184+
if [[ -z "$DSTACK_GATEWAY_DOMAIN" ]]; then
185+
# First try to get from /dstack/user_config if it exists and is valid JSON
186+
if [[ -f /dstack/user_config ]] && jq empty /dstack/user_config 2>/dev/null; then
187+
if [[ $(jq 'has("default_gateway_domain")' /dstack/user_config 2>/dev/null) == "true" ]]; then
188+
export DSTACK_GATEWAY_DOMAIN=$(jq -j '.default_gateway_domain' /dstack/user_config)
189+
fi
190+
fi
191+
192+
# If still not set, try to get from app-compose.json
193+
if [[ -z "$DSTACK_GATEWAY_DOMAIN" ]] && [[ $(jq 'has("default_gateway_domain")' app-compose.json) == "true" ]]; then
194+
export DSTACK_GATEWAY_DOMAIN=$(jq -j '.default_gateway_domain' app-compose.json)
195+
fi
169196
fi
170197
if [[ -n "$DSTACK_GATEWAY_DOMAIN" ]]; then
171198
export DSTACK_APP_DOMAIN=$DSTACK_APP_ID"."$DSTACK_GATEWAY_DOMAIN

0 commit comments

Comments
 (0)