wip
This commit is contained in:
parent
6dd38036e7
commit
097b3dbf09
34 changed files with 1719 additions and 520 deletions
101
run.py
101
run.py
|
@ -3,6 +3,13 @@ import sys
|
|||
import importlib.util
|
||||
from flask import Flask, render_template
|
||||
from app import create_app
|
||||
from app.core.extensions import db
|
||||
from app.core.models import Server, Subnet, App, Port
|
||||
from app.core.auth import User # Import User from auth module
|
||||
from datetime import datetime
|
||||
import random
|
||||
import string
|
||||
import json
|
||||
|
||||
# Add the current directory to Python path
|
||||
current_dir = os.path.abspath(os.path.dirname(__file__))
|
||||
|
@ -60,12 +67,96 @@ def register_routes(app):
|
|||
except ImportError as e:
|
||||
print(f"Could not import IPAM blueprint: {e}")
|
||||
|
||||
# Create a development application instance
|
||||
print("Starting Flask app with SQLite database...")
|
||||
app = create_app('development')
|
||||
|
||||
@app.shell_context_processor
|
||||
def make_shell_context():
|
||||
return {
|
||||
'db': db,
|
||||
'User': User,
|
||||
'Server': Server,
|
||||
'Subnet': Subnet,
|
||||
'App': App,
|
||||
'Port': Port
|
||||
}
|
||||
|
||||
def init_db():
|
||||
"""Initialize database tables"""
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
def create_admin_user():
|
||||
"""Create an admin user if no users exist"""
|
||||
with app.app_context():
|
||||
if User.query.count() == 0:
|
||||
admin = User(
|
||||
username='admin',
|
||||
email='admin@example.com',
|
||||
is_admin=True
|
||||
)
|
||||
admin.set_password('admin')
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
print("Created admin user: admin@example.com (password: admin)")
|
||||
|
||||
# Update seed_data to use consistent structures
|
||||
def seed_data():
|
||||
"""Add some sample data to the database"""
|
||||
with app.app_context():
|
||||
# Only seed if the database is empty
|
||||
if Subnet.query.count() == 0:
|
||||
# Create sample subnets
|
||||
subnet1 = Subnet(cidr='192.168.1.0/24', location='Office', active_hosts=json.dumps([]))
|
||||
subnet2 = Subnet(cidr='10.0.0.0/24', location='Datacenter', active_hosts=json.dumps([]))
|
||||
|
||||
db.session.add_all([subnet1, subnet2])
|
||||
db.session.commit()
|
||||
|
||||
# Create sample servers
|
||||
server1 = Server(hostname='web-server', ip_address='192.168.1.10', subnet=subnet1)
|
||||
server2 = Server(hostname='db-server', ip_address='192.168.1.11', subnet=subnet1)
|
||||
server3 = Server(hostname='app-server', ip_address='10.0.0.5', subnet=subnet2)
|
||||
|
||||
db.session.add_all([server1, server2, server3])
|
||||
db.session.commit()
|
||||
|
||||
# Create sample apps
|
||||
app1 = App(name='Website', server=server1, documentation='# Company Website\nRunning on Nginx/PHP')
|
||||
app2 = App(name='PostgreSQL', server=server2, documentation='# Database Server\nPostgreSQL 15')
|
||||
app3 = App(name='API Service', server=server3, documentation='# REST API\nNode.js service')
|
||||
|
||||
db.session.add_all([app1, app2, app3])
|
||||
db.session.commit()
|
||||
|
||||
# Create sample ports
|
||||
port1 = Port(app=app1, port_number=80, protocol='TCP', description='HTTP')
|
||||
port2 = Port(app=app1, port_number=443, protocol='TCP', description='HTTPS')
|
||||
port3 = Port(app=app2, port_number=5432, protocol='TCP', description='PostgreSQL')
|
||||
port4 = Port(app=app3, port_number=3000, protocol='TCP', description='Node.js API')
|
||||
|
||||
db.session.add_all([port1, port2, port3, port4])
|
||||
db.session.commit()
|
||||
|
||||
print("Sample data has been added to the database")
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Initialize database if needed
|
||||
if not os.path.exists('app.db') and 'sqlite' in app.config['SQLALCHEMY_DATABASE_URI']:
|
||||
print("Database not found, initializing...")
|
||||
try:
|
||||
init_db()
|
||||
create_admin_user()
|
||||
# Uncomment to add sample data
|
||||
# seed_data()
|
||||
except Exception as e:
|
||||
print(f"Error initializing database: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# Run the application
|
||||
try:
|
||||
print("Starting Flask app with SQLite database...")
|
||||
app = create_app('development')
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
app.run(debug=True, port=5000)
|
||||
except Exception as e:
|
||||
print(f"Error starting Flask app: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
Loading…
Add table
Add a link
Reference in a new issue