-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-testnet.sh
More file actions
executable file
·323 lines (265 loc) · 9.16 KB
/
deploy-testnet.sh
File metadata and controls
executable file
·323 lines (265 loc) · 9.16 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/bash
# NUSA Chain Testnet Deployment Script
# This script automates the deployment of NUSA testnet to a VPS
set -e # Exit on error
echo "╔═══════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ 🚀 NUSA CHAIN TESTNET DEPLOYMENT SCRIPT 🚀 ║"
echo "║ ║"
echo "╚═══════════════════════════════════════════════════════════════════╝"
echo ""
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Configuration
VPS_IP=""
VPS_USER="root"
DOMAIN=""
DB_PASSWORD=""
# Functions
print_step() {
echo -e "${GREEN}[STEP]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_requirements() {
print_step "Checking requirements..."
if ! command -v cargo &> /dev/null; then
print_error "Rust/Cargo not found. Please install: https://rustup.rs"
exit 1
fi
if ! command -v git &> /dev/null; then
print_error "Git not found. Please install git."
exit 1
fi
if ! command -v ssh &> /dev/null; then
print_error "SSH not found. Please install openssh-client."
exit 1
fi
echo "✅ All requirements met!"
}
gather_info() {
print_step "Gathering deployment information..."
read -p "Enter VPS IP address: " VPS_IP
read -p "Enter VPS username (default: root): " VPS_USER
VPS_USER=${VPS_USER:-root}
read -p "Enter domain (optional, press enter to skip): " DOMAIN
read -sp "Enter PostgreSQL password: " DB_PASSWORD
echo ""
echo ""
echo "Configuration:"
echo " VPS IP: $VPS_IP"
echo " User: $VPS_USER"
echo " Domain: ${DOMAIN:-'Not configured'}"
echo ""
read -p "Is this correct? (y/n): " confirm
if [ "$confirm" != "y" ]; then
echo "Deployment cancelled."
exit 0
fi
}
build_project() {
print_step "Building NUSA Chain for testnet..."
cargo build --release
if [ $? -eq 0 ]; then
echo "✅ Build successful!"
else
print_error "Build failed!"
exit 1
fi
}
setup_vps() {
print_step "Setting up VPS..."
echo "Connecting to VPS and installing dependencies..."
ssh $VPS_USER@$VPS_IP << 'ENDSSH'
# Update system
echo "Updating system..."
apt update && apt upgrade -y
# Install dependencies
echo "Installing dependencies..."
apt install -y build-essential curl git postgresql nginx certbot \
python3-certbot-nginx ufw fail2ban
# Install Rust
if ! command -v cargo &> /dev/null; then
echo "Installing Rust..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
fi
# Setup firewall
echo "Configuring firewall..."
ufw allow 22/tcp # SSH
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
ufw allow 8545/tcp # RPC
ufw --force enable
# Create nusa user
if ! id -u nusa &>/dev/null; then
useradd -r -s /bin/bash -m nusa
fi
# Create directories
mkdir -p /opt/nusa-chain
mkdir -p /var/log/nusa-testnet
mkdir -p /var/backups/nusa-testnet
mkdir -p /etc/nusa
chown -R nusa:nusa /opt/nusa-chain
chown -R nusa:nusa /var/log/nusa-testnet
echo "✅ VPS setup complete!"
ENDSSH
}
setup_database() {
print_step "Setting up PostgreSQL..."
ssh $VPS_USER@$VPS_IP << ENDSSH
sudo -u postgres psql << EOF
DROP DATABASE IF EXISTS nusa_testnet;
CREATE DATABASE nusa_testnet;
DROP USER IF EXISTS nusa;
CREATE USER nusa WITH PASSWORD '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE nusa_testnet TO nusa;
ALTER DATABASE nusa_testnet OWNER TO nusa;
EOF
echo "✅ Database created!"
ENDSSH
}
deploy_binary() {
print_step "Deploying binary to VPS..."
scp target/release/nusa-node $VPS_USER@$VPS_IP:/opt/nusa-chain/
scp testnet-config.toml $VPS_USER@$VPS_IP:/etc/nusa/config.toml
ssh $VPS_USER@$VPS_IP "chown -R nusa:nusa /opt/nusa-chain /etc/nusa"
echo "✅ Binary deployed!"
}
setup_systemd() {
print_step "Setting up systemd service..."
ssh $VPS_USER@$VPS_IP << ENDSSH
cat > /etc/systemd/system/nusa-testnet.service << 'EOF'
[Unit]
Description=NUSA Testnet Node
After=network.target postgresql.service
[Service]
Type=simple
User=nusa
WorkingDirectory=/opt/nusa-chain
Environment="DATABASE_URL=postgres://nusa:$DB_PASSWORD@localhost/nusa_testnet"
Environment="RUST_LOG=info"
Environment="NETWORK_MODE=testnet"
Environment="CONFIG_FILE=/etc/nusa/config.toml"
ExecStart=/opt/nusa-chain/nusa-node
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nusa-testnet
systemctl start nusa-testnet
echo "✅ Systemd service created and started!"
ENDSSH
}
setup_nginx() {
if [ -z "$DOMAIN" ]; then
print_warning "Domain not configured. Skipping Nginx setup."
print_warning "You can access RPC at: http://$VPS_IP:8545"
return
fi
print_step "Setting up Nginx reverse proxy..."
ssh $VPS_USER@$VPS_IP << ENDSSH
cat > /etc/nginx/sites-available/nusa-testnet << 'EOF'
server {
listen 80;
server_name $DOMAIN;
location / {
proxy_pass http://localhost:8545;
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;
# CORS
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS" always;
add_header Access-Control-Allow-Headers "Content-Type" always;
}
}
EOF
ln -sf /etc/nginx/sites-available/nusa-testnet /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
echo "✅ Nginx configured!"
ENDSSH
# Setup SSL
print_step "Setting up SSL certificate..."
ssh $VPS_USER@$VPS_IP "certbot --nginx -d $DOMAIN --non-interactive --agree-tos --email admin@$DOMAIN || true"
}
check_deployment() {
print_step "Checking deployment status..."
sleep 5 # Wait for service to start
ssh $VPS_USER@$VPS_IP << 'ENDSSH'
echo "Service status:"
systemctl status nusa-testnet --no-pager | head -20
echo ""
echo "Recent logs:"
journalctl -u nusa-testnet -n 20 --no-pager
ENDSSH
}
print_summary() {
echo ""
echo "╔═══════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ ✅ TESTNET DEPLOYMENT COMPLETE! ✅ ║"
echo "║ ║"
echo "╚═══════════════════════════════════════════════════════════════════╝"
echo ""
echo "🌐 RPC Endpoint:"
if [ -n "$DOMAIN" ]; then
echo " https://$DOMAIN"
else
echo " http://$VPS_IP:8545"
fi
echo ""
echo "🔧 Management Commands:"
echo " Check status: ssh $VPS_USER@$VPS_IP 'systemctl status nusa-testnet'"
echo " View logs: ssh $VPS_USER@$VPS_IP 'journalctl -u nusa-testnet -f'"
echo " Restart: ssh $VPS_USER@$VPS_IP 'systemctl restart nusa-testnet'"
echo " Stop: ssh $VPS_USER@$VPS_IP 'systemctl stop nusa-testnet'"
echo ""
echo "📊 Test RPC:"
if [ -n "$DOMAIN" ]; then
echo " curl https://$DOMAIN/health"
else
echo " curl http://$VPS_IP:8545/health"
fi
echo ""
echo "📝 Next Steps:"
echo " 1. Setup faucet (see TESTNET_LAUNCH_GUIDE.md)"
echo " 2. Deploy block explorer"
echo " 3. Create status page"
echo " 4. Announce testnet launch"
echo " 5. Invite testers"
echo ""
echo "🐛 Bug Reports:"
echo " GitHub: https://github.com/alejandrozahran-cyber/zahran-2-chain/issues"
echo ""
echo "Good luck with your testnet! 🚀"
}
# Main execution
main() {
check_requirements
gather_info
build_project
setup_vps
setup_database
deploy_binary
setup_systemd
setup_nginx
check_deployment
print_summary
}
# Run main function
main