lint: reformat python code with black
This commit is contained in:
parent
331beb01b4
commit
a406a7974b
88 changed files with 2579 additions and 1608 deletions
|
@ -16,20 +16,32 @@ BridgeResourceRow = Row[Tuple[AbstractResource, BridgeConf, CloudAccount]]
|
|||
|
||||
|
||||
def active_bridges_by_provider(provider: CloudProvider) -> Sequence[BridgeResourceRow]:
|
||||
stmt = select(Bridge, BridgeConf, CloudAccount).join_from(Bridge, BridgeConf).join_from(Bridge, CloudAccount).where(
|
||||
CloudAccount.provider == provider,
|
||||
Bridge.destroyed.is_(None),
|
||||
stmt = (
|
||||
select(Bridge, BridgeConf, CloudAccount)
|
||||
.join_from(Bridge, BridgeConf)
|
||||
.join_from(Bridge, CloudAccount)
|
||||
.where(
|
||||
CloudAccount.provider == provider,
|
||||
Bridge.destroyed.is_(None),
|
||||
)
|
||||
)
|
||||
bridges: Sequence[BridgeResourceRow] = db.session.execute(stmt).all()
|
||||
return bridges
|
||||
|
||||
|
||||
def recently_destroyed_bridges_by_provider(provider: CloudProvider) -> Sequence[BridgeResourceRow]:
|
||||
def recently_destroyed_bridges_by_provider(
|
||||
provider: CloudProvider,
|
||||
) -> Sequence[BridgeResourceRow]:
|
||||
cutoff = datetime.now(tz=timezone.utc) - timedelta(hours=72)
|
||||
stmt = select(Bridge, BridgeConf, CloudAccount).join_from(Bridge, BridgeConf).join_from(Bridge, CloudAccount).where(
|
||||
CloudAccount.provider == provider,
|
||||
Bridge.destroyed.is_not(None),
|
||||
Bridge.destroyed >= cutoff,
|
||||
stmt = (
|
||||
select(Bridge, BridgeConf, CloudAccount)
|
||||
.join_from(Bridge, BridgeConf)
|
||||
.join_from(Bridge, CloudAccount)
|
||||
.where(
|
||||
CloudAccount.provider == provider,
|
||||
Bridge.destroyed.is_not(None),
|
||||
Bridge.destroyed >= cutoff,
|
||||
)
|
||||
)
|
||||
bridges: Sequence[BridgeResourceRow] = db.session.execute(stmt).all()
|
||||
return bridges
|
||||
|
@ -60,35 +72,38 @@ class BridgeAutomation(TerraformAutomation):
|
|||
self.template,
|
||||
active_resources=active_bridges_by_provider(self.provider),
|
||||
destroyed_resources=recently_destroyed_bridges_by_provider(self.provider),
|
||||
global_namespace=app.config['GLOBAL_NAMESPACE'],
|
||||
terraform_modules_path=os.path.join(*list(os.path.split(app.root_path))[:-1], 'terraform-modules'),
|
||||
global_namespace=app.config["GLOBAL_NAMESPACE"],
|
||||
terraform_modules_path=os.path.join(
|
||||
*list(os.path.split(app.root_path))[:-1], "terraform-modules"
|
||||
),
|
||||
backend_config=f"""backend "http" {{
|
||||
lock_address = "{app.config['TFSTATE_BACKEND']}/{self.short_name}"
|
||||
unlock_address = "{app.config['TFSTATE_BACKEND']}/{self.short_name}"
|
||||
address = "{app.config['TFSTATE_BACKEND']}/{self.short_name}"
|
||||
}}""",
|
||||
**{
|
||||
k: app.config[k.upper()]
|
||||
for k in self.template_parameters
|
||||
}
|
||||
**{k: app.config[k.upper()] for k in self.template_parameters},
|
||||
)
|
||||
|
||||
def tf_posthook(self, *, prehook_result: Any = None) -> None:
|
||||
outputs = self.tf_output()
|
||||
for output in outputs:
|
||||
if output.startswith('bridge_hashed_fingerprint_'):
|
||||
parts = outputs[output]['value'].split(" ")
|
||||
if output.startswith("bridge_hashed_fingerprint_"):
|
||||
parts = outputs[output]["value"].split(" ")
|
||||
if len(parts) < 2:
|
||||
continue
|
||||
bridge = Bridge.query.filter(Bridge.id == output[len('bridge_hashed_fingerprint_'):]).first()
|
||||
bridge = Bridge.query.filter(
|
||||
Bridge.id == output[len("bridge_hashed_fingerprint_") :]
|
||||
).first()
|
||||
bridge.nickname = parts[0]
|
||||
bridge.hashed_fingerprint = parts[1]
|
||||
bridge.terraform_updated = datetime.now(tz=timezone.utc)
|
||||
if output.startswith('bridge_bridgeline_'):
|
||||
parts = outputs[output]['value'].split(" ")
|
||||
if output.startswith("bridge_bridgeline_"):
|
||||
parts = outputs[output]["value"].split(" ")
|
||||
if len(parts) < 4:
|
||||
continue
|
||||
bridge = Bridge.query.filter(Bridge.id == output[len('bridge_bridgeline_'):]).first()
|
||||
bridge = Bridge.query.filter(
|
||||
Bridge.id == output[len("bridge_bridgeline_") :]
|
||||
).first()
|
||||
del parts[3]
|
||||
bridge.bridgeline = " ".join(parts)
|
||||
bridge.terraform_updated = datetime.now(tz=timezone.utc)
|
||||
|
|
|
@ -7,10 +7,7 @@ class BridgeGandiAutomation(BridgeAutomation):
|
|||
description = "Deploy Tor bridges on GandiCloud VPS"
|
||||
provider = CloudProvider.GANDI
|
||||
|
||||
template_parameters = [
|
||||
"ssh_public_key_path",
|
||||
"ssh_private_key_path"
|
||||
]
|
||||
template_parameters = ["ssh_public_key_path", "ssh_private_key_path"]
|
||||
|
||||
template = """
|
||||
terraform {
|
||||
|
|
|
@ -7,10 +7,7 @@ class BridgeHcloudAutomation(BridgeAutomation):
|
|||
description = "Deploy Tor bridges on Hetzner Cloud"
|
||||
provider = CloudProvider.HCLOUD
|
||||
|
||||
template_parameters = [
|
||||
"ssh_private_key_path",
|
||||
"ssh_public_key_path"
|
||||
]
|
||||
template_parameters = ["ssh_private_key_path", "ssh_public_key_path"]
|
||||
|
||||
template = """
|
||||
terraform {
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -7,10 +7,7 @@ class BridgeOvhAutomation(BridgeAutomation):
|
|||
description = "Deploy Tor bridges on OVH Public Cloud"
|
||||
provider = CloudProvider.OVH
|
||||
|
||||
template_parameters = [
|
||||
"ssh_public_key_path",
|
||||
"ssh_private_key_path"
|
||||
]
|
||||
template_parameters = ["ssh_public_key_path", "ssh_private_key_path"]
|
||||
|
||||
template = """
|
||||
terraform {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue