File size: 3,780 Bytes
021e065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# ==============================================================================
# Senti AI — Debian VM Lightweight Setup Script
# ==============================================================================
# Run this script on the target Compute Engine Debian VM.
# ==============================================================================

set -e # exit on error

echo "====== [SENTI VM SETUP] ======"

# 1. Update Packages
echo "--> 1. Updating packages..."
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y git python3 python3-pip python3-venv nginx curl gcc g++ pkg-config build-essential tesseract-ocr

# 2. Install Rust (for cargo/senti_calc maturin compilation)
if ! command -v cargo &> /dev/null; then
    echo "--> 2. Installing Rust..."
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
    source "$HOME/.cargo/env"
fi

# 3. Setup Node.js & NPM (for frontend build)
if ! command -v node &> /dev/null; then
    echo "--> 3. Installing Node.js..."
    curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
    sudo apt-get install -y nodejs
fi

# 4. Build Frontend
echo "--> 4. Building React Frontend..."
cd /app/frontend || cd ~/senti_ai/frontend || cd "$(pwd)/frontend"
npm ci
npm run build

# 5. Configure Nginx
echo "--> 5. Configuring Nginx..."
FRONTEND_DIST_PATH=$(pwd)/dist
sudo tee /etc/nginx/sites-available/senti <<EOF
server {
    listen 80;
    server_name _;

    # Frontend Static Files
    location / {
        root $FRONTEND_DIST_PATH;
        index index.html;
        try_files \$uri \$uri/ /index.html;
    }

    # API Proxy to Uvicorn Gateway
    location /api/ {
        proxy_pass http://127.0.0.1:8000/api/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host \$host;
        proxy_cache_bypass \$http_upgrade;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
    }
    
    # Websocket support
    location /ws/ {
        proxy_pass http://127.0.0.1:8000/ws/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host \$host;
    }
}
EOF

sudo ln -sf /etc/nginx/sites-available/senti /etc/nginx/sites-enabled/default
sudo systemctl restart nginx

# 6. Build Backend
echo "--> 6. Building Python Backend Virtual Environment..."
cd ..
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r senti/requirements.txt

# Compile Rust engine
echo "--> 7. Building Rust engine extensions..."
cd senti/senti_calc
pip install maturin
maturin build --release
pip install .
cd ../..

# 7. Create Systemd Service for Gateway
echo "--> 8. Creating systemd service for Senti Gateway..."
PROJECT_DIR=$(pwd)
sudo tee /etc/systemd/system/senti-gateway.service <<EOF
[Unit]
Description=Senti AI API Gateway Daemon
After=network.target

[Service]
User=$USER
WorkingDirectory=$PROJECT_DIR/senti
ExecStart=$PROJECT_DIR/venv/bin/uvicorn backend.api.main:app --host 127.0.0.1 --port 8000 --workers 1
Restart=always
Environment=SENTI_ENV=production
Environment=USE_LM=false
Environment=DATABASE_URL=sqlite:///senti_db.sqlite

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable senti-gateway
sudo systemctl start senti-gateway

# 8. Verify status
echo "--> 9. Checking status of services..."
sudo systemctl status nginx --no-pager
sudo systemctl status senti-gateway --no-pager

echo "====== [VM SETUP AND DEPLOYMENT COMPLETED] ======"
echo "Access Senti AI on http://<VM-External-IP>"