Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
89 lines (69 loc) · 3.06 KB
/
Copy pathsetup.py
File metadata and controls
89 lines (69 loc) · 3.06 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
"""One-time bootstrap.
1. Read domains from config.yaml.
2. Generate N unique inboxes per domain (default 5).
3. Insert into SQLite and initialize warmup_state.
4. (Optional) report Amazon SES domain-verification status, if an `aws:`
block is present in config.yaml and boto3 is installed.
5. Print a summary.
Re-runnable: existing inboxes are preserved (INSERT OR IGNORE), so this is
safe to re-run after editing config.yaml — it only adds new rows.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
import yaml
import database
import fleet
def load_config() -> dict:
path = Path(__file__).resolve().parent / "config.yaml"
with open(path, encoding="utf-8") as f:
return yaml.safe_load(f)
def _verify_ses_identities(domains: list[str], region: str) -> dict[str, str]:
"""Optional Amazon SES helper. Returns {domain: status_string}.
Only meaningful if you send through SES and have verified these domains as
SES identities. Degrades gracefully when boto3 or AWS creds are absent.
"""
try:
import boto3
except ImportError:
return {d: "boto3 not installed (optional)" for d in domains}
if not (os.environ.get("AWS_ACCESS_KEY_ID") and os.environ.get("AWS_SECRET_ACCESS_KEY")):
return {d: "AWS credentials not set (optional)" for d in domains}
try:
client = boto3.client("ses", region_name=region)
resp = client.get_identity_verification_attributes(Identities=domains)
attrs = resp.get("VerificationAttributes", {})
return {d: attrs.get(d, {}).get("VerificationStatus", "NotFound") for d in domains}
except Exception as exc: # noqa: BLE001
return {d: f"SES error: {exc}" for d in domains}
def run() -> None:
config = load_config()
database.init_db()
per_domain = int(config.get("warmup", {}).get("inboxes_per_domain", 5))
domain_names = [d["name"] for d in config["domains"]]
print(f"\nBootstrapping {len(domain_names)} domain(s), {per_domain} inbox(es) each...\n")
added = fleet.ensure_fleet(domain_names, per_domain)
for domain in domain_names:
n = added.get(domain, 0)
total = len(database.inboxes_for_domain(domain))
tag = f"[+{n}]" if n else "[skip]"
print(f" {tag} {domain} -> {total} inbox(es)")
# Optional SES identity check — only if you configured an `aws:` block.
aws_cfg = config.get("aws")
if aws_cfg and aws_cfg.get("region"):
statuses = _verify_ses_identities(domain_names, aws_cfg["region"])
print("\n=== Amazon SES verification (optional) ===")
for d, status in statuses.items():
flag = "OK " if status == "Success" else "!! "
print(f" {flag}{d}: {status}")
print("\n=== Inboxes ===")
for inbox in database.all_inboxes():
print(f" {inbox['domain']:<24} {inbox['email']}")
print("\nSetup complete. Next: python main.py\n")
if __name__ == "__main__":
try:
run()
except Exception as exc: # noqa: BLE001
print(f"Setup failed: {exc}", file=sys.stderr)
sys.exit(1)