-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_local.sh
More file actions
executable file
·257 lines (211 loc) · 7.6 KB
/
setup_local.sh
File metadata and controls
executable file
·257 lines (211 loc) · 7.6 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/bash
# Local setup script for Biotact Production (without Docker)
set -e # Exit on error
echo "=========================================="
echo " Biotact Local Setup (No Docker) "
echo "=========================================="
echo ""
# Colors
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m'
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo -e "${RED}Please do not run this script as root!${NC}"
echo "We will ask for sudo when needed."
exit 1
fi
# Function to check command existence
command_exists() {
command -v "$1" >/dev/null 2>&1
}
echo -e "${GREEN}Step 1: Installing system dependencies${NC}"
echo "This will require sudo password..."
echo ""
# Update package list
sudo apt-get update
# Install PostgreSQL 16
echo -e "${YELLOW}Installing PostgreSQL 16...${NC}"
if ! command_exists psql; then
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y postgresql-16 postgresql-client-16 postgresql-contrib-16
else
echo -e "${GREEN}PostgreSQL already installed${NC}"
fi
# Install Redis
echo -e "${YELLOW}Installing Redis...${NC}"
if ! command_exists redis-server; then
sudo apt-get install -y redis-server
# Configure Redis
sudo sed -i 's/^# maxmemory <bytes>/maxmemory 256mb/' /etc/redis/redis.conf
sudo sed -i 's/^# maxmemory-policy noeviction/maxmemory-policy allkeys-lru/' /etc/redis/redis.conf
sudo systemctl enable redis-server
sudo systemctl restart redis-server
else
echo -e "${GREEN}Redis already installed${NC}"
fi
# Install Python 3.11
echo -e "${YELLOW}Checking Python 3.11...${NC}"
if ! command_exists python3.11; then
sudo apt-get install -y python3.11 python3.11-venv python3.11-dev python3-pip
else
echo -e "${GREEN}Python 3.11 already installed${NC}"
fi
# Install Node.js 20 for frontend
echo -e "${YELLOW}Installing Node.js 20...${NC}"
if ! command_exists node || [ $(node -v | cut -d'v' -f2 | cut -d'.' -f1) -lt 20 ]; then
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
else
echo -e "${GREEN}Node.js already installed${NC}"
fi
# Install nginx
echo -e "${YELLOW}Installing Nginx...${NC}"
if ! command_exists nginx; then
sudo apt-get install -y nginx
else
echo -e "${GREEN}Nginx already installed${NC}"
fi
# Install other dependencies
echo -e "${YELLOW}Installing build tools...${NC}"
sudo apt-get install -y build-essential libpq-dev libssl-dev libffi-dev git curl wget
echo ""
echo -e "${GREEN}Step 2: Setting up Python virtual environment${NC}"
# Create virtual environment
if [ ! -d "venv" ]; then
python3.11 -m venv venv
echo -e "${GREEN}Virtual environment created${NC}"
else
echo -e "${YELLOW}Virtual environment already exists${NC}"
fi
# Activate venv and install Python packages
source venv/bin/activate
pip install --upgrade pip setuptools wheel
echo -e "${YELLOW}Installing Python dependencies...${NC}"
pip install -r backend/requirements.txt
echo ""
echo -e "${GREEN}Step 3: Setting up PostgreSQL${NC}"
# Setup PostgreSQL database and user
sudo -u postgres psql << EOF
-- Create user if not exists
DO \$\$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_user WHERE usename = 'biotact_admin') THEN
CREATE USER biotact_admin WITH PASSWORD 'biotact_password';
END IF;
END
\$\$;
-- Create database if not exists
SELECT 'CREATE DATABASE biotact_production OWNER biotact_admin'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'biotact_production');
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE biotact_production TO biotact_admin;
-- Create extensions
\c biotact_production;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
EOF
echo -e "${GREEN}PostgreSQL setup complete${NC}"
echo ""
echo -e "${GREEN}Step 4: Initializing database schema${NC}"
# Run SQL migrations
PGPASSWORD=biotact_password psql -h localhost -U biotact_admin -d biotact_production -f infrastructure/postgres/init/001_create_extensions.sql
PGPASSWORD=biotact_password psql -h localhost -U biotact_admin -d biotact_production -f infrastructure/postgres/init/002_create_schema.sql
echo ""
echo -e "${GREEN}Step 5: Installing and configuring Qdrant${NC}"
# Create directory for Qdrant
mkdir -p ~/qdrant-storage
# Download and run Qdrant (if not already running)
if ! pgrep -x "qdrant" > /dev/null; then
echo -e "${YELLOW}Downloading Qdrant...${NC}"
# Download Qdrant binary
if [ ! -f ~/qdrant ]; then
cd ~
curl -L https://github.com/qdrant/qdrant/releases/download/v1.12.5/qdrant-x86_64-unknown-linux-gnu.tar.gz -o qdrant.tar.gz
tar -xzf qdrant.tar.gz
rm qdrant.tar.gz
cd -
fi
echo -e "${YELLOW}Note: Qdrant will need to be started separately${NC}"
else
echo -e "${GREEN}Qdrant already running${NC}"
fi
echo ""
echo -e "${GREEN}Step 6: Configuring Nginx${NC}"
# Copy nginx config
sudo cp infrastructure/nginx/nginx.conf /etc/nginx/sites-available/biotact
sudo ln -sf /etc/nginx/sites-available/biotact /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
echo ""
echo -e "${GREEN}Step 7: Creating systemd services${NC}"
# Create systemd service for backend
sudo tee /etc/systemd/system/biotact-backend.service > /dev/null << 'EOF'
[Unit]
Description=Biotact Backend API
After=network.target postgresql.service redis.service
[Service]
Type=simple
User=$USER
WorkingDirectory=/home/$USER/project/biotact-production
Environment="PATH=/home/$USER/project/biotact-production/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/home/$USER/project/biotact-production/venv/bin/gunicorn app.main:app \
--worker-class uvicorn.workers.UvicornWorker \
--workers 2 \
--bind 0.0.0.0:8000 \
--timeout 120 \
--access-logfile /home/$USER/project/biotact-production/logs/access.log \
--error-logfile /home/$USER/project/biotact-production/logs/error.log
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Replace $USER with actual username
sudo sed -i "s/\$USER/$USER/g" /etc/systemd/system/biotact-backend.service
# Create systemd service for Qdrant
sudo tee /etc/systemd/system/qdrant.service > /dev/null << 'EOF'
[Unit]
Description=Qdrant Vector Database
After=network.target
[Service]
Type=simple
User=$USER
WorkingDirectory=/home/$USER
ExecStart=/home/$USER/qdrant --storage-path /home/$USER/qdrant-storage
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Replace $USER with actual username
sudo sed -i "s/\$USER/$USER/g" /etc/systemd/system/qdrant.service
# Reload systemd
sudo systemctl daemon-reload
echo ""
echo "=========================================="
echo -e "${GREEN}✅ Local setup complete!${NC}"
echo "=========================================="
echo ""
echo "Next steps:"
echo " 1. Edit .env file with your API keys"
echo " 2. Start Qdrant: ~/qdrant --storage-path ~/qdrant-storage"
echo " 3. Start backend: source venv/bin/activate && python backend/app/main.py"
echo " 4. Or use systemd:"
echo " sudo systemctl start biotact-backend"
echo " sudo systemctl start qdrant"
echo ""
echo "Services will be available at:"
echo " - API: http://localhost:8000"
echo " - PostgreSQL: localhost:5432"
echo " - Redis: localhost:6379"
echo " - Qdrant: http://localhost:6333"
echo ""
echo "To enable services on boot:"
echo " sudo systemctl enable biotact-backend"
echo " sudo systemctl enable qdrant"