lint: reformat python code with black
This commit is contained in:
parent
331beb01b4
commit
a406a7974b
88 changed files with 2579 additions and 1608 deletions
|
@ -25,10 +25,17 @@ def active_bridges_in_account(account: CloudAccount) -> List[Bridge]:
|
|||
return bridges
|
||||
|
||||
|
||||
def create_bridges_in_account(bridgeconf: BridgeConf, account: CloudAccount, count: int) -> int:
|
||||
def create_bridges_in_account(
|
||||
bridgeconf: BridgeConf, account: CloudAccount, count: int
|
||||
) -> int:
|
||||
created = 0
|
||||
while created < count and len(active_bridges_in_account(account)) < account.max_instances:
|
||||
logging.debug("Creating bridge for configuration %s in account %s", bridgeconf.id, account)
|
||||
while (
|
||||
created < count
|
||||
and len(active_bridges_in_account(account)) < account.max_instances
|
||||
):
|
||||
logging.debug(
|
||||
"Creating bridge for configuration %s in account %s", bridgeconf.id, account
|
||||
)
|
||||
bridge = Bridge()
|
||||
bridge.pool_id = bridgeconf.pool.id
|
||||
bridge.conf_id = bridgeconf.id
|
||||
|
@ -45,16 +52,18 @@ def create_bridges_by_cost(bridgeconf: BridgeConf, count: int) -> int:
|
|||
"""
|
||||
Creates bridge resources for the given bridge configuration using the cheapest available provider.
|
||||
"""
|
||||
logging.debug("Creating %s bridges by cost for configuration %s", count, bridgeconf.id)
|
||||
logging.debug(
|
||||
"Creating %s bridges by cost for configuration %s", count, bridgeconf.id
|
||||
)
|
||||
created = 0
|
||||
for provider in BRIDGE_PROVIDERS:
|
||||
if created >= count:
|
||||
break
|
||||
logging.info("Creating bridges in %s accounts", provider.description)
|
||||
for account in CloudAccount.query.filter(
|
||||
CloudAccount.destroyed.is_(None),
|
||||
CloudAccount.enabled.is_(True),
|
||||
CloudAccount.provider == provider,
|
||||
CloudAccount.destroyed.is_(None),
|
||||
CloudAccount.enabled.is_(True),
|
||||
CloudAccount.provider == provider,
|
||||
).all():
|
||||
logging.info("Creating bridges in %s", account)
|
||||
created += create_bridges_in_account(bridgeconf, account, count - created)
|
||||
|
@ -78,7 +87,9 @@ def create_bridges_by_random(bridgeconf: BridgeConf, count: int) -> int:
|
|||
"""
|
||||
Creates bridge resources for the given bridge configuration using random providers.
|
||||
"""
|
||||
logging.debug("Creating %s bridges by random for configuration %s", count, bridgeconf.id)
|
||||
logging.debug(
|
||||
"Creating %s bridges by random for configuration %s", count, bridgeconf.id
|
||||
)
|
||||
created = 0
|
||||
while candidate_accounts := _accounts_with_room():
|
||||
# Not security-critical random number generation
|
||||
|
@ -97,16 +108,24 @@ def create_bridges(bridgeconf: BridgeConf, count: int) -> int:
|
|||
return create_bridges_by_random(bridgeconf, count)
|
||||
|
||||
|
||||
def deprecate_bridges(bridgeconf: BridgeConf, count: int, reason: str = "redundant") -> int:
|
||||
logging.debug("Deprecating %s bridges (%s) for configuration %s", count, reason, bridgeconf.id)
|
||||
def deprecate_bridges(
|
||||
bridgeconf: BridgeConf, count: int, reason: str = "redundant"
|
||||
) -> int:
|
||||
logging.debug(
|
||||
"Deprecating %s bridges (%s) for configuration %s", count, reason, bridgeconf.id
|
||||
)
|
||||
deprecated = 0
|
||||
active_conf_bridges = iter(Bridge.query.filter(
|
||||
Bridge.conf_id == bridgeconf.id,
|
||||
Bridge.deprecated.is_(None),
|
||||
Bridge.destroyed.is_(None),
|
||||
).all())
|
||||
active_conf_bridges = iter(
|
||||
Bridge.query.filter(
|
||||
Bridge.conf_id == bridgeconf.id,
|
||||
Bridge.deprecated.is_(None),
|
||||
Bridge.destroyed.is_(None),
|
||||
).all()
|
||||
)
|
||||
while deprecated < count:
|
||||
logging.debug("Deprecating bridge %s for configuration %s", deprecated + 1, bridgeconf.id)
|
||||
logging.debug(
|
||||
"Deprecating bridge %s for configuration %s", deprecated + 1, bridgeconf.id
|
||||
)
|
||||
bridge = next(active_conf_bridges)
|
||||
logging.debug("Bridge %r", bridge)
|
||||
bridge.deprecate(reason=reason)
|
||||
|
@ -129,7 +148,9 @@ class BridgeMetaAutomation(BaseAutomation):
|
|||
for bridge in deprecated_bridges:
|
||||
if bridge.deprecated is None:
|
||||
continue # Possible due to SQLAlchemy lazy loading
|
||||
cutoff = datetime.now(tz=timezone.utc) - timedelta(hours=bridge.conf.expiry_hours)
|
||||
cutoff = datetime.now(tz=timezone.utc) - timedelta(
|
||||
hours=bridge.conf.expiry_hours
|
||||
)
|
||||
if bridge.deprecated < cutoff:
|
||||
logging.debug("Destroying expired bridge")
|
||||
bridge.destroy()
|
||||
|
@ -146,7 +167,9 @@ class BridgeMetaAutomation(BaseAutomation):
|
|||
activate_bridgeconfs = BridgeConf.query.filter(
|
||||
BridgeConf.destroyed.is_(None),
|
||||
).all()
|
||||
logging.debug("Found %s active bridge configurations", len(activate_bridgeconfs))
|
||||
logging.debug(
|
||||
"Found %s active bridge configurations", len(activate_bridgeconfs)
|
||||
)
|
||||
for bridgeconf in activate_bridgeconfs:
|
||||
active_conf_bridges = Bridge.query.filter(
|
||||
Bridge.conf_id == bridgeconf.id,
|
||||
|
@ -157,16 +180,18 @@ class BridgeMetaAutomation(BaseAutomation):
|
|||
Bridge.conf_id == bridgeconf.id,
|
||||
Bridge.destroyed.is_(None),
|
||||
).all()
|
||||
logging.debug("Generating new bridges for %s (active: %s, total: %s, target: %s, max: %s)",
|
||||
bridgeconf.id,
|
||||
len(active_conf_bridges),
|
||||
len(total_conf_bridges),
|
||||
bridgeconf.target_number,
|
||||
bridgeconf.max_number
|
||||
)
|
||||
logging.debug(
|
||||
"Generating new bridges for %s (active: %s, total: %s, target: %s, max: %s)",
|
||||
bridgeconf.id,
|
||||
len(active_conf_bridges),
|
||||
len(total_conf_bridges),
|
||||
bridgeconf.target_number,
|
||||
bridgeconf.max_number,
|
||||
)
|
||||
missing = min(
|
||||
bridgeconf.target_number - len(active_conf_bridges),
|
||||
bridgeconf.max_number - len(total_conf_bridges))
|
||||
bridgeconf.max_number - len(total_conf_bridges),
|
||||
)
|
||||
if missing > 0:
|
||||
create_bridges(bridgeconf, missing)
|
||||
elif missing < 0:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue