-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
186 lines (144 loc) · 5.99 KB
/
setup.py
File metadata and controls
186 lines (144 loc) · 5.99 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
"""
Quick Start Script - Sets up the environment and runs a demo
"""
import subprocess
import sys
import os
import time
from pathlib import Path
def print_header(text):
"""Print formatted header"""
print("\n" + "="*70)
print(f" {text}")
print("="*70 + "\n")
def run_command(cmd, description, check=True):
"""Run a command and print status"""
print(f"→ {description}...")
try:
result = subprocess.run(cmd, shell=True, check=check, capture_output=True, text=True)
print(f"✓ {description} complete")
return True
except subprocess.CalledProcessError as e:
print(f"✗ {description} failed: {e}")
if e.stderr:
print(f" Error: {e.stderr}")
return False
def check_prerequisites():
"""Check if required tools are installed"""
print_header("Checking Prerequisites")
# Check Python version
if sys.version_info < (3, 9):
print("✗ Python 3.9+ is required")
return False
print(f"✓ Python {sys.version_info.major}.{sys.version_info.minor} detected")
# Check Docker
result = subprocess.run("docker --version", shell=True, capture_output=True)
if result.returncode == 0:
print("✓ Docker is installed")
else:
print("⚠ Docker not found - will run in local mode")
return True
def setup_environment():
"""Setup Python environment and install dependencies"""
print_header("Setting Up Environment")
# Create virtual environment
if not Path("venv").exists():
run_command(
f"{sys.executable} -m venv venv",
"Creating virtual environment"
)
# Activate venv and install dependencies
if sys.platform == "win32":
pip_cmd = "venv\\Scripts\\pip"
python_cmd = "venv\\Scripts\\python"
else:
pip_cmd = "venv/bin/pip"
python_cmd = "venv/bin/python"
run_command(
f"{pip_cmd} install --upgrade pip",
"Upgrading pip"
)
run_command(
f"{pip_cmd} install -r requirements.txt",
"Installing dependencies"
)
# Generate gRPC code
run_command(
f"{python_cmd} -m grpc_tools.protoc -I./proto --python_out=. --grpc_python_out=. proto/logs.proto",
"Generating gRPC code from proto files"
)
return True
def create_test_directories():
"""Create necessary directories"""
print_header("Creating Directories")
directories = ["logs", "test_logs"]
for d in directories:
Path(d).mkdir(exist_ok=True)
print(f"✓ Created directory: {d}")
def generate_test_logs():
"""Generate sample test logs"""
print_header("Generating Test Logs")
if sys.platform == "win32":
python_cmd = "venv\\Scripts\\python"
else:
python_cmd = "venv/bin/python"
# Create a simple test log file
test_log_file = "test_logs/test.log"
with open(test_log_file, "w") as f:
f.write("Mar 6 10:00:00 server sshd[1234]: Accepted password for user from 192.168.1.100 port 45231 ssh2\n")
f.write("Mar 6 10:00:05 server sshd[1235]: Failed password for root from 10.0.0.50 port 45232 ssh2\n")
print(f"✓ Created test log file: {test_log_file}")
def print_instructions():
"""Print usage instructions"""
print_header("Setup Complete!")
print("The Distributed Log-Analyzer has been set up successfully!\n")
print("📋 Next Steps:\n")
print("Option 1: Docker Deployment (Recommended)")
print("-" * 50)
print(" docker-compose -f docker/docker-compose.yml up -d")
print(" docker-compose -f docker/docker-compose.yml logs -f server\n")
print("Option 2: Local Development")
print("-" * 50)
print(" 1. Start PostgreSQL and Redis (or use Docker for these)")
print(" 2. Setup database:")
print(" psql -U postgres -f scripts/setup_database.sql\n")
if sys.platform == "win32":
print(" 3. Start server:")
print(" venv\\Scripts\\python -m server.grpc_server\n")
print(" 4. Start client (in another terminal):")
print(" venv\\Scripts\\python -m client.log_agent --server localhost:50051 --client-id client-001 --log-files test_logs/test.log\n")
else:
print(" 3. Start server:")
print(" venv/bin/python -m server.grpc_server\n")
print(" 4. Start client (in another terminal):")
print(" venv/bin/python -m client.log_agent --server localhost:50051 --client-id client-001 --log-files test_logs/test.log\n")
print("📚 Documentation:")
print("-" * 50)
print(" README.md - Quick start and overview")
print(" ARCHITECTURE.md - Detailed system architecture\n")
print("🧪 Testing:")
print("-" * 50)
if sys.platform == "win32":
print(" venv\\Scripts\\pytest tests/ -v\n")
else:
print(" venv/bin/pytest tests/ -v\n")
def main():
"""Main setup function"""
print("\n")
print("╔══════════════════════════════════════════════════════════════════╗")
print("║ ║")
print("║ DISTRIBUTED LOG-ANALYZER - QUICK SETUP ║")
print("║ ║")
print("╚══════════════════════════════════════════════════════════════════╝")
if not check_prerequisites():
print("\n✗ Prerequisites check failed. Please install required tools.\n")
return 1
if not setup_environment():
print("\n✗ Environment setup failed.\n")
return 1
create_test_directories()
generate_test_logs()
print_instructions()
return 0
if __name__ == "__main__":
sys.exit(main())