-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_script.py
More file actions
272 lines (220 loc) · 6.8 KB
/
setup_script.py
File metadata and controls
272 lines (220 loc) · 6.8 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
#!/usr/bin/env python3
"""
Setup script for Secure Chatbot with Palo Alto Networks AI Runtime Security
"""
import subprocess
import sys
import os
from pathlib import Path
def run_command(command, description):
"""Run a command and handle errors"""
print(f"\n🔧 {description}...")
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(f"✅ {description} completed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"❌ {description} failed: {e}")
print(f"Error output: {e.stderr}")
return False
def check_python_version():
"""Check if Python version is compatible"""
print("🐍 Checking Python version...")
if sys.version_info < (3, 9):
print(f"❌ Python 3.9+ required, but you have {sys.version}")
return False
print(f"✅ Python {sys.version.split()[0]} is compatible")
return True
def create_virtual_environment():
"""Create and activate virtual environment"""
venv_path = Path("venv")
if venv_path.exists():
print("📁 Virtual environment already exists")
return True
return run_command(f"{sys.executable} -m venv venv", "Creating virtual environment")
def get_activation_command():
"""Get the virtual environment activation command based on OS"""
if os.name == 'nt': # Windows
return "venv\\Scripts\\activate"
else: # Unix/Linux/macOS
return "source venv/bin/activate"
def setup_palo_alto_repository():
"""Configure pip for Palo Alto Networks repository"""
commands = [
'python -m pip config set global.extra-index-url "https://art.code.pan.run/artifactory/api/pypi/aisec-api-pypi/simple"',
'python -m pip install --upgrade pip'
]
for command in commands:
if not run_command(command, f"Running: {command}"):
return False
return True
def install_requirements():
"""Install Python requirements"""
if not run_command("pip install -r requirements.txt", "Installing requirements"):
return False
return True
def create_env_file():
"""Create .env file from template if it doesn't exist"""
env_file = Path(".env")
env_example = Path(".env.example")
if env_file.exists():
print("📄 .env file already exists")
return True
if env_example.exists():
try:
env_file.write_text(env_example.read_text())
print("✅ Created .env file from template")
print("⚠️ Please edit .env file with your actual API keys and configuration")
return True
except Exception as e:
print(f"❌ Failed to create .env file: {e}")
return False
else:
print("⚠️ .env.example not found, please create .env manually")
return True
def create_gitignore():
"""Create or update .gitignore file"""
gitignore_content = """# Secure Chatbot - Git Ignore File
# Environment variables
.env
.env.local
.env.production
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Virtual environments
venv/
env/
ENV/
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Logs
*.log
logs/
# Configuration files with secrets
config.py
secrets.json
# Test coverage
.coverage
htmlcov/
.pytest_cache/
# Documentation
docs/_build/
"""
gitignore_file = Path(".gitignore")
try:
gitignore_file.write_text(gitignore_content)
print("✅ Created/updated .gitignore file")
return True
except Exception as e:
print(f"❌ Failed to create .gitignore: {e}")
return False
def test_installation():
"""Test if the installation was successful"""
test_script = """
import sys
try:
import requests
print("✅ requests imported successfully")
import openai
print("✅ openai imported successfully")
try:
import aisecurity
print("✅ aisecurity imported successfully")
except ImportError:
print("⚠️ aisecurity not available - you may need to configure Palo Alto repository access")
import tkinter
print("✅ tkinter imported successfully")
from dotenv import load_dotenv
print("✅ python-dotenv imported successfully")
print("\\n🎉 All core dependencies are available!")
except ImportError as e:
print(f"❌ Import error: {e}")
sys.exit(1)
"""
return run_command(f'python -c "{test_script}"', "Testing installation")
def print_next_steps():
"""Print next steps for the user"""
activation_cmd = get_activation_command()
print("""
🎉 Setup completed successfully!
📋 Next Steps:
1. Activate your virtual environment:
{}
2. Edit your .env file with your actual API keys:
- Get Palo Alto Networks API key from Strata Cloud Manager
- Get Azure OpenAI credentials from Azure Portal
3. Run the chatbots:
python chatbot_sdk.py # For Python SDK version
python chatbot_api.py # For Direct API version
📚 Documentation:
- README.md for detailed setup instructions
- .env.example for configuration examples
🔐 Security Notes:
- Never commit .env file to version control
- Keep your API keys secure
- Use different credentials for different environments
🆘 Need Help?
- Check the troubleshooting section in README.md
- Review Palo Alto Networks documentation
- Ensure your firewall allows HTTPS connections
""".format(activation_cmd))
def main():
"""Main setup function"""
print("🚀 Setting up Secure Chatbot with Palo Alto Networks AI Runtime Security")
print("=" * 70)
# Check Python version
if not check_python_version():
sys.exit(1)
# Create virtual environment
if not create_virtual_environment():
sys.exit(1)
print(f"\n⚠️ Please activate your virtual environment and run this script again:")
print(f" {get_activation_command()}")
print(f" python setup.py")
# Check if we're in a virtual environment
if sys.prefix == sys.base_prefix:
print("\n⚠️ Virtual environment not activated. Please activate it first.")
return
print("\n🔧 Continuing setup in virtual environment...")
# Setup Palo Alto repository
if not setup_palo_alto_repository():
print("⚠️ Palo Alto repository setup failed. You may need to install aisecurity manually.")
# Install requirements
if not install_requirements():
sys.exit(1)
# Create configuration files
create_env_file()
create_gitignore()
# Test installation
if not test_installation():
sys.exit(1)
# Print next steps
print_next_steps()
if __name__ == "__main__":
main()