-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.sh
864 lines (722 loc) · 23.8 KB
/
start.sh
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
#!/bin/bash
# Exit on any error
set -e
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to install system packages
install_system_packages() {
echo "Updating system packages..."
sudo apt update
sudo apt upgrade -y
echo "Installing essential packages..."
sudo apt-get install -y \
curl \
git \
python3 \
python3-pip \
python3-venv \
software-properties-common \
jq
}
# Function to install Node.js
install_nodejs() {
if ! command_exists node; then
echo "Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
fi
echo "Node.js version: $(node --version)"
echo "npm version: $(npm --version)"
}
# Function to install OVN
install_ovn() {
echo "Installing OVN..."
sudo apt-get update
sudo apt-get install -y openvswitch-switch openvswitch-common ovn-central ovn-host
# Create necessary directories
sudo mkdir -p /var/run/ovn
sudo mkdir -p /etc/ovn
sudo chown -R root:root /var/run/ovn
sudo chown -R root:root /etc/ovn
# Start OpenVSwitch service first
echo "Starting OpenVSwitch service..."
if systemctl is-active --quiet openvswitch-switch; then
echo "OpenVSwitch service is already running"
else
sudo systemctl start openvswitch-switch
sudo systemctl enable openvswitch-switch
fi
# Start OVN central service (this starts ovsdb-server)
echo "Starting OVN central service..."
if systemctl is-active --quiet ovn-central; then
echo "OVN central is already running"
else
sudo systemctl start ovn-central
sudo systemctl enable ovn-central
fi
# Wait for ovsdb-server to be ready
echo "Waiting for ovsdb-server..."
for i in {1..30}; do
if sudo ovsdb-client list-dbs > /dev/null 2>&1; then
echo "ovsdb-server is ready!"
break
fi
echo "Waiting for ovsdb-server... ($i/30)"
sleep 1
done
# Initialize OVN databases
echo "Initializing OVN databases..."
sudo ovs-vsctl set open_vswitch . external_ids:ovn-remote=unix:/var/run/ovn/ovnsb_db.sock
sudo ovs-vsctl set open_vswitch . external_ids:ovn-encap-type=geneve
sudo ovs-vsctl set open_vswitch . external_ids:ovn-encap-ip=127.0.0.1
# Initialize the OVN databases if they don't exist
if [ ! -f "/etc/ovn/ovnnb_db.db" ]; then
echo "Initializing OVN North database..."
sudo ovn-nbctl init
fi
if [ ! -f "/etc/ovn/ovnsb_db.db" ]; then
echo "Initializing OVN South database..."
sudo ovn-sbctl init
fi
# Start remaining OVN services
echo "Starting OVN services..."
if systemctl is-active --quiet ovn-northd; then
echo "OVN Northd is already running"
else
sudo systemctl start ovn-northd
sudo systemctl enable ovn-northd
fi
if systemctl is-active --quiet ovn-controller; then
echo "OVN Controller is already running"
else
sudo systemctl start ovn-controller
sudo systemctl enable ovn-controller
fi
# Wait for services to be fully up and databases to be ready
echo "Waiting for OVN services and databases to initialize..."
for i in {1..30}; do
echo "Waiting... ($i/30)"
if sudo ovn-nbctl show > /dev/null 2>&1; then
echo "OVN database is now accessible!"
break
fi
sleep 1
done
# Verify database connection
echo "Verifying OVN database connection..."
if ! sudo ovn-nbctl show > /dev/null 2>&1; then
echo "Error: Unable to connect to OVN database. Retrying with explicit socket path..."
export OVN_NB_DB=unix:/var/run/ovn/ovnnb_db.sock
if ! sudo ovn-nbctl show > /dev/null 2>&1; then
echo "Error: Still unable to connect to OVN database."
echo "Debugging information:"
echo "1. Checking OVN socket file..."
ls -l /var/run/ovn/
echo "2. Checking OVN service status..."
systemctl status ovn-northd
systemctl status ovn-central
echo "3. Checking OVS database status..."
sudo ovsdb-client list-dbs
return 1
fi
fi
}
# Function to check OVN services
check_ovn_services() {
echo "Checking OVN services..."
# Check OpenVSwitch
if systemctl is-active --quiet openvswitch-switch; then
echo " openvswitch-switch is running"
else
echo " openvswitch-switch is not running"
echo "Attempting to start openvswitch-switch..."
sudo systemctl start openvswitch-switch || echo "Failed to start openvswitch-switch"
fi
# Check OVN services
if systemctl is-active --quiet ovn-northd; then
echo " ovn-northd is running"
else
echo " ovn-northd is not running"
echo "Attempting to start ovn-northd..."
sudo systemctl start ovn-northd || echo "Failed to start ovn-northd"
fi
if systemctl is-active --quiet ovn-controller; then
echo " ovn-controller is running"
else
echo " ovn-controller is not running"
echo "Attempting to start ovn-controller..."
sudo systemctl start ovn-controller || echo "Failed to start ovn-controller"
fi
}
# Function to create basic OVN network configuration
setup_ovn_network() {
echo "Setting up basic OVN network configuration..."
# Clean up existing network configuration
echo "Cleaning up existing network configuration..."
# Get list of existing switches and delete them
echo "Checking existing logical switches..."
# First try direct command output
switches=$(sudo ovn-nbctl ls-list | awk '{if(NR>1)print $1}')
if [ ! -z "$switches" ]; then
echo "Found existing switches, removing them..."
echo "$switches" | while read switch_uuid; do
if [ ! -z "$switch_uuid" ]; then
echo "Removing logical switch with UUID: $switch_uuid"
sudo ovn-nbctl ls-del "$switch_uuid"
fi
done
else
echo "No existing switches found."
fi
# Get list of existing routers and delete them
echo "Checking existing logical routers..."
# First try direct command output
routers=$(sudo ovn-nbctl lr-list | awk '{if(NR>1)print $1}')
if [ ! -z "$routers" ]; then
echo "Found existing routers, removing them..."
echo "$routers" | while read router_uuid; do
if [ ! -z "$router_uuid" ]; then
echo "Removing logical router with UUID: $router_uuid"
sudo ovn-nbctl lr-del "$router_uuid"
fi
done
else
echo "No existing routers found."
fi
# Create logical switches
echo "Creating logical switches..."
sudo ovn-nbctl ls-add demo-switch1
sudo ovn-nbctl ls-add demo-switch2
# Create logical router
echo "Creating logical router..."
sudo ovn-nbctl lr-add demo-router
# Create logical ports on switches
echo "Creating logical ports..."
# Ports on switch1
sudo ovn-nbctl lsp-add demo-switch1 demo-switch1-port1
sudo ovn-nbctl lsp-set-addresses demo-switch1-port1 "02:ac:10:ff:01:01 172.16.1.11"
sudo ovn-nbctl lsp-add demo-switch1 demo-switch1-router-port
sudo ovn-nbctl lsp-set-type demo-switch1-router-port router
sudo ovn-nbctl lsp-set-addresses demo-switch1-router-port router
# Ports on switch2
sudo ovn-nbctl lsp-add demo-switch2 demo-switch2-port1
sudo ovn-nbctl lsp-set-addresses demo-switch2-port1 "02:ac:10:ff:02:01 172.16.2.11"
sudo ovn-nbctl lsp-add demo-switch2 demo-switch2-router-port
sudo ovn-nbctl lsp-set-type demo-switch2-router-port router
sudo ovn-nbctl lsp-set-addresses demo-switch2-router-port router
# Connect router ports
echo "Connecting router ports..."
sudo ovn-nbctl lrp-add demo-router demo-router-port1 02:ac:10:ff:01:02 172.16.1.1/24
sudo ovn-nbctl lrp-add demo-router demo-router-port2 02:ac:10:ff:02:02 172.16.2.1/24
# Set up port bindings
sudo ovn-nbctl lsp-set-options demo-switch1-router-port router-port=demo-router-port1
sudo ovn-nbctl lsp-set-options demo-switch2-router-port router-port=demo-router-port2
echo "Basic OVN network configuration completed!"
}
# Function to verify OVN configuration
verify_ovn_config() {
echo "Verifying OVN configuration..."
echo -e "\nLogical Switches:"
sudo ovn-nbctl ls-list
echo -e "\nLogical Router:"
sudo ovn-nbctl lr-list
echo -e "\nLogical Switch Ports:"
sudo ovn-nbctl lsp-list demo-switch1
sudo ovn-nbctl lsp-list demo-switch2
}
# Function to set up Python environment
setup_python_env() {
echo "Setting up Python environment..."
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Create backend directory structure
mkdir -p backend/utils
# Create validators.py
cat > backend/utils/validators.py << 'EOL'
def validate_switch_data(data):
"""Validate logical switch data."""
required_fields = ['name']
if not isinstance(data, dict):
return False, "Data must be a dictionary"
for field in required_fields:
if field not in data:
return False, f"Missing required field: {field}"
if not isinstance(data[field], str):
return False, f"Field {field} must be a string"
if not data[field].strip():
return False, f"Field {field} cannot be empty"
return True, None
def validate_router_data(data):
"""Validate logical router data."""
required_fields = ['name']
if not isinstance(data, dict):
return False, "Data must be a dictionary"
for field in required_fields:
if field not in data:
return False, f"Missing required field: {field}"
if not isinstance(data[field], str):
return False, f"Field {field} must be a string"
if not data[field].strip():
return False, f"Field {field} cannot be empty"
return True, None
def validate_port_data(data):
"""Validate port data."""
required_fields = ['name', 'type']
if not isinstance(data, dict):
return False, "Data must be a dictionary"
for field in required_fields:
if field not in data:
return False, f"Missing required field: {field}"
if not isinstance(data[field], str):
return False, f"Field {field} must be a string"
if not data[field].strip():
return False, f"Field {field} cannot be empty"
return True, None
EOL
# Start the Flask backend server
echo "Starting Flask backend server..."
nohup python3 backend/app.py --host 0.0.0.0 --port 5000 > backend.log 2>&1 &
echo "Backend server started and listening on all interfaces (port 5000)"
}
# Function to set up frontend
setup_frontend() {
echo "Setting up frontend..."
cd frontend
# Create public directory if it doesn't exist
mkdir -p public
# Create index.html
cat > public/index.html << 'EOL'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="OVN Web Manager - A modern interface for Open Virtual Network"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>OVN Web Manager</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
EOL
# Create manifest.json
cat > public/manifest.json << 'EOL'
{
"short_name": "OVN Manager",
"name": "OVN Web Manager",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
EOL
# Create a simple favicon (1x1 transparent pixel in base64)
echo 'AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' | base64 -d > public/favicon.ico
# Install dependencies
echo "Installing frontend dependencies..."
npm install @mui/material @mui/icons-material @emotion/react @emotion/styled react-router-dom
npm install
# Create directories
mkdir -p src/components
mkdir -p src/pages
# Create index.js if it doesn't exist
if [ ! -f src/index.js ]; then
cat > src/index.js << 'EOL'
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import { ThemeProvider, createTheme } from '@mui/material';
import CssBaseline from '@mui/material/CssBaseline';
import App from './App';
const theme = createTheme({
palette: {
mode: 'light',
},
});
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<ThemeProvider theme={theme}>
<CssBaseline />
<App />
</ThemeProvider>
</BrowserRouter>
</React.StrictMode>
);
EOL
fi
# Create App.js if it doesn't exist
if [ ! -f src/App.js ]; then
cat > src/App.js << 'EOL'
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Layout from './components/Layout';
import Dashboard from './pages/Dashboard';
import LogicalSwitches from './pages/LogicalSwitches';
import LogicalRouters from './pages/LogicalRouters';
import LoadBalancers from './pages/LoadBalancers';
import ACLs from './pages/ACLs';
import Settings from './pages/Settings';
function App() {
return (
<Layout>
<Routes>
<Route path="/" element={<Dashboard />} />
<Route path="/logical-switches" element={<LogicalSwitches />} />
<Route path="/logical-routers" element={<LogicalRouters />} />
<Route path="/load-balancers" element={<LoadBalancers />} />
<Route path="/acls" element={<ACLs />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Layout>
);
}
export default App;
EOL
fi
# Create Dashboard.js if it doesn't exist
if [ ! -f src/pages/Dashboard.js ]; then
cat > src/pages/Dashboard.js << 'EOL'
import React from 'react';
import { Box, Typography, Grid, Paper } from '@mui/material';
function Dashboard() {
return (
<Box sx={{ flexGrow: 1, p: 3 }}>
<Typography variant="h4" gutterBottom>
Dashboard
</Typography>
<Grid container spacing={3}>
<Grid item xs={12} md={6} lg={4}>
<Paper sx={{ p: 2 }}>
<Typography variant="h6">Network Overview</Typography>
<Typography variant="body1">
Monitor your OVN network components and their status.
</Typography>
</Paper>
</Grid>
</Grid>
</Box>
);
}
export default Dashboard;
EOL
fi
# Create placeholder pages for other routes
for page in "LogicalSwitches" "LogicalRouters" "LoadBalancers" "ACLs" "Settings"; do
if [ ! -f "src/pages/${page}.js" ]; then
cat > "src/pages/${page}.js" << EOL
import React from 'react';
import { Box, Typography } from '@mui/material';
function ${page}() {
return (
<Box sx={{ flexGrow: 1, p: 3 }}>
<Typography variant="h4" gutterBottom>
${page}
</Typography>
<Typography variant="body1">
This page is under construction.
</Typography>
</Box>
);
}
export default ${page};
EOL
fi
done
# Install additional dependencies
npm install @mui/material @mui/icons-material @emotion/react @emotion/styled react-router-dom
# Install dependencies and build
echo "Installing frontend dependencies..."
npm install
# Install serve locally
npm install serve --save-dev
echo "Creating production build..."
npm run build
# Kill any existing serve processes
pkill -f "serve -s build" || true
# Start the frontend server using local serve
echo "Starting frontend server..."
nohup ./node_modules/.bin/serve -s build --listen 0.0.0.0:3000 --cors > frontend.log 2>&1 &
# Wait a moment for the server to start
sleep 2
# Display network access information
echo ""
echo "Application started successfully!"
echo "----------------------------------------"
echo "Available network interfaces:"
ip -4 addr show | grep inet | awk '{print $2}' | cut -d/ -f1 | while read ip; do
if [ "$ip" != "127.0.0.1" ]; then
echo "http://$ip:3000 (Frontend)"
echo "http://$ip:5000 (Backend API)"
fi
done
echo "----------------------------------------"
echo "The application is listening on all network interfaces"
echo "Use any of the above URLs based on your network configuration"
cd ..
}
# Function to cleanup processes
cleanup() {
echo "Stopping application..."
kill $BACKEND_PID 2>/dev/null || true
kill $FRONTEND_PID 2>/dev/null || true
exit 0
}
# Main installation process
main() {
echo "Starting OVN Web Manager installation..."
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo "Please do not run this script as root"
exit 1
fi
# Check Ubuntu version
if ! grep -q "Ubuntu 22.04" /etc/os-release; then
echo "This script requires Ubuntu 22.04"
exit 1
fi
# Install all required packages
install_system_packages
install_nodejs
install_ovn
# Set up application
setup_python_env
setup_frontend
# Check services
check_ovn_services
# Set up OVN network
echo "Setting up OVN network..."
setup_ovn_network
verify_ovn_config
# Create GitHub workflows directory
mkdir -p .github/workflows
# Create frontend workflow
cat > .github/workflows/frontend.yml << 'EOL'
name: Frontend CI
on:
push:
branches: [ main ]
paths:
- 'frontend/**'
- '.github/workflows/frontend.yml'
pull_request:
branches: [ main ]
paths:
- 'frontend/**'
- '.github/workflows/frontend.yml'
jobs:
build:
runs-on: ubuntu-22.04
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint || true
- name: Build
run: npm run build
- name: Run tests
run: npm test || true
EOL
# Create backend workflow
cat > .github/workflows/backend.yml << 'EOL'
name: Backend CI
on:
push:
branches: [ main ]
paths:
- 'backend/**'
- 'requirements.txt'
- '.github/workflows/backend.yml'
pull_request:
branches: [ main ]
paths:
- 'backend/**'
- 'requirements.txt'
- '.github/workflows/backend.yml'
jobs:
test:
runs-on: ubuntu-22.04
services:
ovn:
image: ovn/ovn-controller-vtep
ports:
- 6641:6641
- 6642:6642
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install -r requirements.txt
- name: Run linter
run: |
flake8 backend --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 backend --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Run tests
run: |
pytest backend/tests/ || true
EOL
# Create deployment workflow
cat > .github/workflows/deploy.yml << 'EOL'
name: Deploy
on:
push:
branches: [ main ]
tags:
- 'v*'
jobs:
build-and-push:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Frontend
uses: docker/build-push-action@v4
with:
context: frontend
push: true
tags: |
ghcr.io/${{ github.repository }}/frontend:latest
ghcr.io/${{ github.repository }}/frontend:${{ github.sha }}
- name: Build and push Backend
uses: docker/build-push-action@v4
with:
context: .
file: backend/Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository }}/backend:latest
ghcr.io/${{ github.repository }}/backend:${{ github.sha }}
deploy:
needs: build-and-push
runs-on: ubuntu-22.04
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Deploy to production
run: |
echo "Deploying version ${{ github.ref_name }} to production"
# Add your deployment commands here
EOL
# Create Dockerfiles
# Frontend Dockerfile
cat > frontend/Dockerfile << 'EOL'
FROM node:18-alpine as builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
EOL
# Backend Dockerfile
cat > backend/Dockerfile << 'EOL'
FROM python:3.10-slim
WORKDIR /app
RUN apt-get update && \
apt-get install -y --no-install-recommends \
openvswitch-switch \
openvswitch-common \
ovn-central \
ovn-host && \
rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY backend/ ./backend/
EXPOSE 5000
CMD ["python", "backend/app.py", "--host", "0.0.0.0", "--port", "5000"]
EOL
# Create nginx configuration for frontend
cat > frontend/nginx.conf << 'EOL'
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://backend:5000;
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;
}
}
EOL
echo "Installation completed successfully!"
# Ask user if they want to start the application
read -p "Do you want to start the application now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Start backend
echo "Starting backend..."
cd backend
python3 app.py &
BACKEND_PID=$!
# Start frontend development server
echo "Starting frontend..."
cd ../frontend
npm start &
FRONTEND_PID=$!
# Setup cleanup on script termination
trap cleanup SIGINT SIGTERM
# Wait for processes
wait
else
echo "You can start the application later by running:"
echo "cd backend && python3 app.py &"
echo "cd frontend && npm start &"
fi
}
# Run main function
main