Browse Source

* WireGuard servers should now automatically start if last state was running before shutdown.

pull/5/head
Per-Arne 5 years ago
parent
commit
c28636d5aa
  1. 27
      wg_dashboard_backend/main.py

27
wg_dashboard_backend/main.py

@ -1,12 +1,14 @@
import logging import logging
import os import os
import typing
from sqlalchemy_utils import database_exists from sqlalchemy_utils import database_exists
from starlette.middleware.base import BaseHTTPMiddleware from starlette.middleware.base import BaseHTTPMiddleware
import middleware import middleware
from database import engine, SessionLocal from database import engine, SessionLocal
from routers.v1 import user, server, peer, wg from routers.v1 import user, server, peer, wg
import script.wireguard
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
if not logger.hasHandlers(): if not logger.hasHandlers():
@ -27,10 +29,13 @@ import models
app = FastAPI() app = FastAPI()
app.add_middleware(BaseHTTPMiddleware, dispatch=middleware.db_session_middleware) app.add_middleware(BaseHTTPMiddleware, dispatch=middleware.db_session_middleware)
_db: Session = SessionLocal()
# Ensure database existence
if not database_exists(engine.url): if not database_exists(engine.url):
models.Base.metadata.create_all(engine) models.Base.metadata.create_all(engine)
# Create default user # Create default user
_db: Session = SessionLocal()
_db.add(models.User( _db.add(models.User(
username=os.getenv("ADMIN_USERNAME", "admin"), username=os.getenv("ADMIN_USERNAME", "admin"),
password=middleware.get_password_hash(os.getenv("ADMIN_PASSWORD", "admin")), password=middleware.get_password_hash(os.getenv("ADMIN_PASSWORD", "admin")),
@ -38,8 +43,18 @@ if not database_exists(engine.url):
role="admin", role="admin",
email="" email=""
)) ))
_db.commit() _db.commit()
_db.close()
servers: typing.List[models.WGServer] = _db.query(models.WGServer).all()
for s in servers:
try:
last_state = s.is_running
if script.wireguard.is_installed() and last_state and not script.wireguard.is_running(s):
script.wireguard.start_interface(s)
except Exception as e:
print(e)
_db.close()
app.include_router( app.include_router(
@ -86,13 +101,11 @@ def root():
app.mount("/", StaticFiles(directory=pkg_resources.resource_filename(__name__, 'build')), name="static") app.mount("/", StaticFiles(directory=pkg_resources.resource_filename(__name__, 'build')), name="static")
@app.on_event("startup") @app.on_event("startup")
async def startup(): async def startup():
pass pass
@app.on_event("shutdown") @app.on_event("shutdown")
async def shutdown(): async def shutdown():
pass pass

Loading…
Cancel
Save