post-create.sh 4.52 KB
Newer Older
1
2
3
#!/bin/bash
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
4
5
6

set -eu

7
8
9
# Use WORKSPACE_DIR if set (should be set by Dockerfile.local_dev)
# Otherwise, use /workspace as fallback
WORKSPACE_DIR="${WORKSPACE_DIR:-/workspace}"
10

11
12
13
# Ensure we're not running as root
if [ "$(id -u)" -eq 0 ]; then
    echo "❌ ERROR: This script should not be run as root!"
14
    echo "The script should run as the 'dynamo' user, not root."
15
16
17
18
19
    echo "Current user: $(whoami) (UID: $(id -u))"
    exit 1
fi

# Verify we're running as the expected user
20
21
if [ "$(whoami)" != "dynamo" ]; then
    echo "⚠️  WARNING: Expected to run as 'dynamo' user, but running as '$(whoami)'"
22
23
24
25
    echo "This might cause permission issues."
fi

echo "Running post-create script as user: $(whoami) (UID: $(id -u))"
26

27
trap 'echo "❌ ERROR: Command failed at line $LINENO: $BASH_COMMAND"; echo "⚠️ This was unexpected and setup was not completed. Can try to resolve yourself and then manually run the rest of the commands in this file or file a bug."' ERR
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
retry() {
    # retries for connectivity issues in installs
    local retries=3
    local count=0
    until "$@"; do
        exit_code=$?
        wait_time=$((2 ** count))
        echo "Command failed with exit code $exit_code. Retrying in $wait_time seconds..."
        sleep $wait_time
        count=$((count + 1))
        if [ $count -ge $retries ]; then
            echo "Command failed after $retries attempts."
            return $exit_code
        fi
    done
    return 0
}
46

47
48
49
50
51
52
53
show_and_run() {
    # Run commands with debug output shown
    set -x
    "$@"
    { set +x; } 2>/dev/null
}

54
set -x
55

56
# Pre-commit hooks
57
cd $WORKSPACE_DIR && pre-commit install && retry pre-commit install-hooks
58
pre-commit run --all-files || true # don't fail the build if pre-commit hooks fail
59

60
61
62
# Use CARGO_TARGET_DIR from environment (should be set by Dockerfile.local_dev)
# Fallback to $WORKSPACE_DIR/target if not set
export CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-$WORKSPACE_DIR/target}
63
mkdir -p $CARGO_TARGET_DIR
64

65
66
67
uv pip uninstall --yes ai-dynamo ai-dynamo-runtime 2>/dev/null || true

# Build project, with `dev` profile it will be saved at $CARGO_TARGET_DIR/debug
68
cargo build --locked --profile dev --features dynamo-llm/block-manager
69

70
# install the python bindings
71
72
73
74
75
76
77
78
79
80
81

# Install maturin if not already installed.
# TODO: Uncomment for SGLANG. Right now, the CI team is making a refactor
#       to the Dockerfile, so this is a temporary fix.
# if ! command -v maturin &> /dev/null; then
#     echo "Installing maturin..."
#     retry uv pip install maturin[patchelf]
# else
#     echo "maturin is already installed"
# fi

82
# install ai-dynamo-runtime
83
(cd $WORKSPACE_DIR/lib/bindings/python && retry maturin develop)
84

85
# install ai-dynamo
86
cd $WORKSPACE_DIR && retry env DYNAMO_BIN_PATH=$CARGO_TARGET_DIR/debug uv pip install -e .
87

88
89
{ set +x; } 2>/dev/null

90
91
92
echo -e "\n" >> ~/.bashrc
echo "# === This section is generated by the post-create.sh script ===" >> ~/.bashrc

93
if ! grep -q "export GPG_TTY=" ~/.bashrc; then
94
    show_and_run echo 'export GPG_TTY=$(tty)' >> ~/.bashrc
95
fi
96
97
98
99

# Unset empty tokens/variables to avoid issues with authentication and SSH
if ! grep -q "# Unset empty tokens" ~/.bashrc; then
    echo -e "\n# Unset empty tokens and environment variables" >> ~/.bashrc
100
101
102
    echo '[ -z "${HF_TOKEN:-}" ] && unset HF_TOKEN' >> ~/.bashrc
    echo '[ -z "${GITHUB_TOKEN:-}" ] && unset GITHUB_TOKEN' >> ~/.bashrc
    echo '[ -z "${SSH_AUTH_SOCK:-}" ] && unset SSH_AUTH_SOCK' >> ~/.bashrc
103
104
105
fi

# Check SSH agent forwarding status
106
if [ -n "${SSH_AUTH_SOCK:-}" ]; then
107
    if ssh-add -l > /dev/null 2>&1; then
108
109
        echo "SSH agent forwarding is working - found $(ssh-add -l | wc -l) key(s):"
        ssh-add -l
110
    else
111
        echo "⚠️ SSH_AUTH_SOCK is set but ssh-add failed - agent may not be accessible"
112
113
114
115
116
    fi
else
    echo "⚠️ SSH agent forwarding not configured - SSH_AUTH_SOCK is not set"
fi

117
show_and_run $WORKSPACE_DIR/deploy/sanity_check.py
118

119
120
121
122
123
124
125
126
127
128
129
cat <<EOF

✅ SUCCESS: Built cargo project, installed Python bindings, configured pre-commit hooks

Example commands:
  cargo build --locked --profile dev              # Build Rust project in $CARGO_TARGET_DIR
  cd lib/bindings/python && maturin develop --uv  # Update Python bindings (if you changed them)
  cargo fmt && cargo clippy                       # Format and lint code before committing
  cargo doc --no-deps                             # Generate documentation
  uv pip install -e .                             # Install various Python packages Dynamo depends on
EOF