post-create.sh 3.76 KB
Newer Older
1
#!/bin/bash
2
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
# 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
show_and_run() {
    # Run commands with debug output shown
    set -x
    "$@"
51
    local exit_code=$?
52
    { set +x; } 2>/dev/null
53
    return $exit_code
54
55
}

56
set -x
57

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

62
63
64
# 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}
65
mkdir -p $CARGO_TARGET_DIR
66

67
# Note: Build steps moved to after sanity check - see instructions at the end
68

69
70
{ set +x; } 2>/dev/null

71
72
73
echo -e "\n" >> ~/.bashrc
echo "# === This section is generated by the post-create.sh script ===" >> ~/.bashrc

74
if ! grep -q "export GPG_TTY=" ~/.bashrc; then
75
    show_and_run echo 'export GPG_TTY=$(tty)' >> ~/.bashrc
76
fi
77
78
79
80

# 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
81
82
83
    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
84
85
86
fi

# Check SSH agent forwarding status
87
if [ -n "${SSH_AUTH_SOCK:-}" ]; then
88
    if ssh-add -l > /dev/null 2>&1; then
89
90
        echo "SSH agent forwarding is working - found $(ssh-add -l | wc -l) key(s):"
        ssh-add -l
91
    else
92
        echo "⚠️ SSH_AUTH_SOCK is set but ssh-add failed - agent may not be accessible"
93
94
95
96
97
98
99
    fi
else
    echo "⚠️ SSH agent forwarding not configured - SSH_AUTH_SOCK is not set"
fi

cat <<EOF

100
101
102
103
========================================
✅ Pre-commit hooks configured

Now build the project:
104
105
106
  cargo build --features dynamo-llm/block-manager
  cd lib/bindings/python && maturin develop --uv && cd $WORKSPACE_DIR
  uv pip install --no-deps -e $WORKSPACE_DIR
107

108
109
110
Optional:
  uv pip install -e lib/gpu_memory_service  # GPU memory manager with C++ extension
  cd lib/bindings/kvbm && maturin develop --uv  # For KVBM support
111

112
113
Verify with:
  $WORKSPACE_DIR/deploy/sanity_check.py
114
========================================
115
EOF