|
| 1 | +""" |
| 2 | +FoundationDB cluster bootstrap tasks. |
| 3 | +Initializes the cluster configuration (runs only on bootstrap node). |
| 4 | +""" |
| 5 | + |
| 6 | +import json |
| 7 | + |
| 8 | +from pyinfra import host |
| 9 | +from pyinfra.operations import python, server |
| 10 | + |
| 11 | + |
| 12 | +def bootstrap_cluster(): |
| 13 | + """ |
| 14 | + Initialize the FoundationDB cluster if not already configured. |
| 15 | + """ |
| 16 | + |
| 17 | + # Note: this command will take a while to execute because it has to timeout trying to get the |
| 18 | + # status, I have yet to find a quick way to do this check. |
| 19 | + check_status = server.shell( |
| 20 | + name="Check cluster status", |
| 21 | + commands="fdbcli --no-status --exec 'status json' || true", |
| 22 | + ) |
| 23 | + |
| 24 | + status_json = json.loads(check_status.stdout) |
| 25 | + status_client = status_json.get("client", {}) |
| 26 | + |
| 27 | + # Note this is, so far, the best way I can tell to determine if FDB cluster is bootstrapped, |
| 28 | + # despite that for production I'd recommend any configure new commands are executed by hand. |
| 29 | + if ( |
| 30 | + status_client.get("coordinators", {}).get("quorum_reachable") is True |
| 31 | + and status_client.get("database_status", {}).get("available") is False |
| 32 | + ): |
| 33 | + redundancy_mode = host.data.fdb_redundancy_mode |
| 34 | + storage_engine = host.data.fdb_storage_engine |
| 35 | + server.shell( |
| 36 | + name="Configure cluster", |
| 37 | + commands=f"fdbcli --no-status --exec 'configure new {redundancy_mode} {storage_engine}'", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +# Use python.call to execute the bootstrap function at runtime |
| 42 | +# This ensures proper state checking during deployment |
| 43 | +python.call( |
| 44 | + name="Bootstrap FoundationDB cluster", |
| 45 | + function=bootstrap_cluster, |
| 46 | +) |
0 commit comments