Spaces:
Runtime error
Runtime error
Create infra_simulator.py
Browse files- infra_simulator.py +34 -0
infra_simulator.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# infra_simulator.py
|
| 2 |
+
import random
|
| 3 |
+
from typing import Dict, List, Optional
|
| 4 |
+
|
| 5 |
+
class InfraSimulator:
|
| 6 |
+
"""Simulates a dynamic infrastructure. Maintains per‑session state."""
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.reset()
|
| 9 |
+
|
| 10 |
+
def reset(self):
|
| 11 |
+
self.components = {
|
| 12 |
+
"switch-1": {"type": "switch", "status": "up", "connections": ["server-1", "server-2"]},
|
| 13 |
+
"server-1": {"type": "server", "status": "up", "connections": ["switch-1"], "services": ["db"]},
|
| 14 |
+
"server-2": {"type": "server", "status": "up", "connections": ["switch-1"], "services": ["web"]},
|
| 15 |
+
"service-db": {"type": "service", "status": "up", "runs_on": "server-1"},
|
| 16 |
+
"service-web": {"type": "service", "status": "up", "runs_on": "server-2"},
|
| 17 |
+
}
|
| 18 |
+
self.fault_mode: Optional[str] = None
|
| 19 |
+
|
| 20 |
+
def set_fault(self, fault: str):
|
| 21 |
+
self.reset() # start fresh
|
| 22 |
+
self.fault_mode = fault
|
| 23 |
+
if fault == "switch_down":
|
| 24 |
+
self.components["switch-1"]["status"] = "down"
|
| 25 |
+
elif fault == "server_overload":
|
| 26 |
+
self.components["server-1"]["status"] = "overloaded"
|
| 27 |
+
elif fault == "cascade":
|
| 28 |
+
self.components["switch-1"]["status"] = "down"
|
| 29 |
+
self.components["server-1"]["status"] = "down"
|
| 30 |
+
self.components["service-db"]["status"] = "down"
|
| 31 |
+
# etc.
|
| 32 |
+
|
| 33 |
+
def read_state(self) -> Dict:
|
| 34 |
+
return self.components
|