-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample-setup.sh
More file actions
executable file
Β·189 lines (163 loc) Β· 5.86 KB
/
example-setup.sh
File metadata and controls
executable file
Β·189 lines (163 loc) Β· 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
set -e
##############################################################################
# Brev Setup Script - Best Practices Example
##############################################################################
# This demonstrates all conventions used in the setup-scripts collection:
# - Battle-tested user detection (works with ubuntu/shadeform/nvidia users)
# - Idempotency (safe to re-run)
# - Permission fixes (when running as root)
# - Clear output with progress indicators
# - Verification at the end
# - Under 150 lines
#
# This example sets up a Python development environment with a simple web app
##############################################################################
# Detect Brev user (handles ubuntu, nvidia, shadeform, etc.)
detect_brev_user() {
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then
echo "$SUDO_USER"
return
fi
# Check for Brev-specific markers
for user_home in /home/*; do
username=$(basename "$user_home")
[ "$username" = "launchpad" ] && continue
if ls "$user_home"/.lifecycle-script-ls-*.log 2>/dev/null | grep -q . || \
[ -f "$user_home/.verb-setup.log" ] || \
{ [ -L "$user_home/.cache" ] && [ "$(readlink "$user_home/.cache")" = "/ephemeral/cache" ]; }; then
echo "$username"
return
fi
done
# Fallback to common users
[ -d "/home/nvidia" ] && echo "nvidia" && return
[ -d "/home/ubuntu" ] && echo "ubuntu" && return
echo "ubuntu"
}
# Set USER and HOME if running as root
if [ "$(id -u)" -eq 0 ] || [ "${USER:-}" = "root" ]; then
DETECTED_USER=$(detect_brev_user)
export USER="$DETECTED_USER"
export HOME="/home/$DETECTED_USER"
fi
echo "π Example Setup Script - Python Web App"
echo "User: $USER | Home: $HOME"
##############################################################################
# Install System Dependencies
##############################################################################
echo "Installing system dependencies..."
sudo apt-get update -qq
sudo apt-get install -y -qq python3-pip python3-venv curl
##############################################################################
# Install Python Environment (Example: virtualenv)
##############################################################################
# Create project directory
PROJECT_DIR="$HOME/my-web-app"
mkdir -p "$PROJECT_DIR"
# Create virtual environment if it doesn't exist
if [ ! -d "$PROJECT_DIR/venv" ]; then
echo "Creating Python virtual environment..."
cd "$PROJECT_DIR"
python3 -m venv venv
else
echo "Virtual environment already exists, skipping..."
fi
# Activate and install dependencies
cd "$PROJECT_DIR"
source venv/bin/activate
echo "Installing Python packages..."
pip install --upgrade pip
pip install flask gunicorn requests
##############################################################################
# Create Example Application
##############################################################################
# Create a simple Flask app if it doesn't exist
if [ ! -f "$PROJECT_DIR/app.py" ]; then
echo "Creating example Flask app..."
cat > "$PROJECT_DIR/app.py" << 'EOF'
from flask import Flask, jsonify
import os
app = Flask(__name__)
@app.route('/')
def hello():
return jsonify({
"message": "Hello from Brev!",
"user": os.getenv("USER", "unknown"),
"status": "running"
})
@app.route('/health')
def health():
return jsonify({"status": "healthy"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
EOF
fi
# Create requirements.txt for reproducibility
cat > "$PROJECT_DIR/requirements.txt" << 'EOF'
flask==3.0.0
gunicorn==21.2.0
requests==2.31.0
EOF
# Create .env.example
if [ ! -f "$PROJECT_DIR/.env.example" ]; then
cat > "$PROJECT_DIR/.env.example" << 'EOF'
# Example environment variables
PORT=5000
DEBUG=false
# Add your API keys here
EOF
fi
# Create start script
cat > "$PROJECT_DIR/start.sh" << 'EOF'
#!/bin/bash
cd ~/my-web-app
source venv/bin/activate
gunicorn --bind 0.0.0.0:5000 --workers 2 app:app
EOF
chmod +x "$PROJECT_DIR/start.sh"
##############################################################################
# Fix Permissions (if running as root)
##############################################################################
if [ "$(id -u)" -eq 0 ]; then
echo "Fixing permissions..."
chown -R $USER:$USER "$PROJECT_DIR"
fi
##############################################################################
# Verification
##############################################################################
echo ""
echo "Verifying installation..."
cd "$PROJECT_DIR"
source venv/bin/activate
python3 -c "import flask; print(f'β Flask {flask.__version__}')"
python3 -c "import gunicorn; print('β Gunicorn installed')"
echo ""
echo "β
Setup complete!"
echo ""
echo "Project location: $PROJECT_DIR"
echo ""
echo "Quick start:"
echo " cd $PROJECT_DIR"
echo " source venv/bin/activate"
echo " python app.py # Development server"
echo " ./start.sh # Production with Gunicorn"
echo ""
echo "β οΈ To access from outside Brev, open port: 5000/tcp"
echo ""
echo "Test the app:"
echo " curl http://localhost:5000"
echo " curl http://localhost:5000/health"
echo ""
##############################################################################
# Key Conventions Demonstrated:
##############################################################################
# β
User detection - Works on all providers
# β
Idempotency - Check before creating/installing
# β
Permission fixes - chown when running as root
# β
Simple and focused - Does one thing well
# β
Port information - Clear about what to open
# β
Verification - Test that it worked
# β
Quick start - Show users how to use it
# β
Under 150 lines - Easy to understand
##############################################################################