Skip to content

Commit d10b2bf

Browse files
committed
fix: [website] poetry and main
1 parent 3b64c77 commit d10b2bf

File tree

4 files changed

+3536
-241
lines changed

4 files changed

+3536
-241
lines changed

website/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ Use all MISP modules through a dedicated website without requiring a MISP instan
1111
The MISP Modules website uses [Poetry](https://python-poetry.org/) for dependency management. It is recommended to install dependencies in a virtual environment managed by Poetry.
1212

1313
### Prerequisites
14-
- Python 3.8 or higher
14+
- Python 3.10 or higher
1515
- Poetry
16-
- `misp-modules` installed in the parent directory (`../`)
1716

1817
### Steps
1918
1. **Clone the Repository**:
@@ -35,13 +34,10 @@ The MISP Modules website uses [Poetry](https://python-poetry.org/) for dependenc
3534

3635
4. **Initialize the Database**:
3736
```bash
38-
poetry run db-init
37+
poetry run mmw db init
3938
```
4039
This creates the database (`misp-module.sqlite`), initializes modules, and sets up the admin password (generated in development if not set).
4140

42-
5. **Install `misp-modules`**:
43-
Ensure `misp-modules` is installed in the parent directory (`../misp-modules`). Follow the main repository’s instructions for setup.
44-
4541
## Configuration
4642

4743
Configuration is managed via a `.env` file in `website/`. Copy the example and edit as needed:
@@ -83,7 +79,7 @@ FLASK_APP=main
8379
Run both `misp-modules` and the website in development mode with debug enabled:
8480

8581
```bash
86-
poetry run dev-site
82+
poetry run mmw dev
8783
```
8884

8985
- If `ADMIN_PASSWORD` is unset in `.env`, a random 20-character password is generated and printed.
@@ -104,10 +100,10 @@ If `ADMIN_PASSWORD` is set in `.env`, the admin user is active. Access the login
104100
Manage the database with the following commands:
105101

106102
```bash
107-
poetry run db-init # Initialize database and modules
108-
poetry run db-migrate # Generate a new migration
109-
poetry run db-upgrade # Apply migrations
110-
poetry run db-downgrade # Revert the latest migration
103+
poetry run mmw db init # Initialize database and modules
104+
poetry run mmw db migrate # Generate a new migration
105+
poetry run mmw db upgrade # Apply migrations
106+
poetry run mmw db downgrade # Revert the latest migration
111107
```
112108

113109
## Systemd Services

website/main.py

Lines changed: 96 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import time
66

77
from dotenv import load_dotenv
8-
98
from app import create_app, db
109

1110
# Load environment variables from .env
@@ -16,91 +15,109 @@
1615
app = create_app()
1716

1817

19-
# CLI commands using argparse
18+
def run_dev():
19+
"""Run misp-modules + Flask dev server"""
20+
# Only the parent process starts misp-modules
21+
if os.getenv("WERKZEUG_RUN_MAIN") != "true":
22+
print("Starting misp-modules...")
23+
24+
modules_env = os.environ.copy()
25+
modules_env.pop("VIRTUAL_ENV", None)
26+
27+
misp_proc = subprocess.Popen(
28+
["poetry", "run", "misp-modules", "-l", "127.0.0.1"],
29+
cwd="..",
30+
env=modules_env,
31+
)
32+
time.sleep(2)
33+
34+
from app.utils import IS_DEVELOPMENT
35+
36+
IS_DEVELOPMENT = True
37+
38+
try:
39+
print("Starting website in debug mode...")
40+
app.run(
41+
host=app.config["FLASK_URL"],
42+
port=app.config["FLASK_PORT"],
43+
debug=IS_DEVELOPMENT,
44+
)
45+
finally:
46+
if os.getenv("WERKZEUG_RUN_MAIN") != "true":
47+
misp_proc.terminate()
48+
49+
50+
def db_init():
51+
"""Initialize the database"""
52+
from app.utils import gen_admin_password
53+
from app.utils.init_modules import create_modules_db
54+
55+
modules_env = os.environ.copy()
56+
modules_env.pop("VIRTUAL_ENV", None)
57+
58+
misp_proc = subprocess.Popen(
59+
["poetry", "run", "misp-modules", "-l", "127.0.0.1"],
60+
cwd="..",
61+
env=modules_env,
62+
)
63+
time.sleep(5)
64+
65+
with app.app_context():
66+
db.create_all()
67+
create_modules_db()
68+
gen_admin_password()
69+
print("Database initialized.")
70+
71+
misp_proc.terminate()
72+
73+
74+
def db_migrate():
75+
subprocess.run(["flask", "db", "migrate"])
76+
77+
78+
def db_upgrade():
79+
subprocess.run(["flask", "db", "upgrade"])
80+
81+
82+
def db_downgrade():
83+
subprocess.run(["flask", "db", "downgrade"])
84+
85+
2086
def main():
2187
parser = argparse.ArgumentParser(description="MISP Modules Website CLI")
22-
parser.add_argument(
23-
"--dev",
24-
action="store_true",
25-
help="Run misp-modules and the website in debug mode",
26-
)
27-
parser.add_argument(
28-
"--db-init", action="store_true", help="Initialize the database"
29-
)
30-
parser.add_argument(
31-
"--db-migrate", action="store_true", help="Generate a new database migration"
32-
)
33-
parser.add_argument(
34-
"--db-upgrade", action="store_true", help="Apply database migrations"
35-
)
36-
parser.add_argument(
37-
"--db-downgrade",
38-
action="store_true",
39-
help="Revert the latest database migration",
40-
)
88+
subparsers = parser.add_subparsers(dest="command", required=True)
89+
90+
# --- dev ---
91+
subparsers.add_parser("dev", help="Run misp-modules + website in debug mode")
92+
93+
# --- db group ---
94+
db_parser = subparsers.add_parser("db", help="Database operations")
95+
db_sub = db_parser.add_subparsers(dest="action", required=True)
96+
97+
db_sub.add_parser("init", help="Initialize the database")
98+
db_sub.add_parser("migrate", help="Generate a new migration")
99+
db_sub.add_parser("upgrade", help="Apply migrations")
100+
db_sub.add_parser("downgrade", help="Revert the last migration")
101+
41102
args = parser.parse_args()
42103

43-
# Set FLASK_APP for flask db commands
104+
# ensure flask commands know where to find the app
44105
os.environ["FLASK_APP"] = "app:create_app()"
45106

46-
if args.dev:
47-
# Import utils to set development mode
48-
from app.utils import utils as utils_module
107+
if args.command == "dev":
108+
run_dev()
49109

50-
# Only the FIRST process (the parent) should launch misp‑modules
51-
if os.getenv("WERKZEUG_RUN_MAIN") != "true":
52-
print("Starting misp-modules...")
53-
54-
modules_env = os.environ.copy()
55-
modules_env.pop("VIRTUAL_ENV", None)
56-
57-
misp_proc = subprocess.Popen(
58-
["poetry", "run", "misp-modules", "-l", "127.0.0.1"],
59-
cwd="..",
60-
env=modules_env,
61-
)
62-
time.sleep(5)
63-
64-
# Import utils after app creation to avoid circular imports
65-
from app.utils import IS_DEVELOPMENT, gen_admin_password
66-
from app.utils.init_modules import create_modules_db
67-
68-
IS_DEVELOPMENT = True # Set global variable in utils.py
69-
70-
try:
71-
print("Starting website in debug mode...")
72-
app.run(
73-
host=app.config["FLASK_URL"],
74-
port=app.config["FLASK_PORT"],
75-
debug=IS_DEVELOPMENT,
76-
)
77-
finally:
78-
# Only parent created misp_proc
79-
if os.getenv("WERKZEUG_RUN_MAIN") != "true":
80-
misp_proc.terminate()
81-
82-
elif args.db_init:
83-
# Use the already-created app instance
84-
from app.utils import gen_admin_password
85-
from app.utils.init_modules import create_modules_db
86-
87-
with app.app_context():
88-
db.create_all()
89-
create_modules_db()
90-
gen_admin_password()
91-
print("Database initialized.")
92-
elif args.db_migrate:
93-
# Run flask db migrate without creating app
94-
subprocess.run(["flask", "db", "migrate"])
95-
elif args.db_upgrade:
96-
# Run flask db upgrade without creating app
97-
subprocess.run(["flask", "db", "upgrade"])
98-
elif args.db_downgrade:
99-
# Run flask db downgrade without creating app
100-
subprocess.run(["flask", "db", "downgrade"])
101-
else:
102-
parser.print_help()
110+
elif args.command == "db":
111+
match args.action:
112+
case "init":
113+
db_init()
114+
case "migrate":
115+
db_migrate()
116+
case "upgrade":
117+
db_upgrade()
118+
case "downgrade":
119+
db_downgrade()
103120

104121

105122
if __name__ == "__main__":
106-
main()
123+
main()

0 commit comments

Comments
 (0)