lint: reformat python code with black
This commit is contained in:
parent
331beb01b4
commit
a406a7974b
88 changed files with 2579 additions and 1608 deletions
|
@ -18,12 +18,16 @@ from app.terraform.proxy.azure_cdn import ProxyAzureCdnAutomation
|
|||
from app.terraform.proxy.cloudfront import ProxyCloudfrontAutomation
|
||||
from app.terraform.proxy.fastly import ProxyFastlyAutomation
|
||||
|
||||
PROXY_PROVIDERS: Dict[str, Type[ProxyAutomation]] = {p.provider: p for p in [ # type: ignore[attr-defined]
|
||||
# In order of preference
|
||||
ProxyCloudfrontAutomation,
|
||||
ProxyFastlyAutomation,
|
||||
ProxyAzureCdnAutomation
|
||||
] if p.enabled} # type: ignore[attr-defined]
|
||||
PROXY_PROVIDERS: Dict[str, Type[ProxyAutomation]] = {
|
||||
p.provider: p # type: ignore[attr-defined]
|
||||
for p in [
|
||||
# In order of preference
|
||||
ProxyCloudfrontAutomation,
|
||||
ProxyFastlyAutomation,
|
||||
ProxyAzureCdnAutomation,
|
||||
]
|
||||
if p.enabled # type: ignore[attr-defined]
|
||||
}
|
||||
|
||||
SubgroupCount = OrderedDictT[str, OrderedDictT[int, OrderedDictT[int, int]]]
|
||||
|
||||
|
@ -61,8 +65,9 @@ def random_slug(origin_domain_name: str) -> str:
|
|||
"exampasdfghjkl"
|
||||
"""
|
||||
# The random slug doesn't need to be cryptographically secure, hence the use of `# nosec`
|
||||
return tldextract.extract(origin_domain_name).domain[:5] + ''.join(
|
||||
random.choices(string.ascii_lowercase, k=12)) # nosec
|
||||
return tldextract.extract(origin_domain_name).domain[:5] + "".join(
|
||||
random.choices(string.ascii_lowercase, k=12) # nosec: B311
|
||||
)
|
||||
|
||||
|
||||
def calculate_subgroup_count(proxies: Optional[List[Proxy]] = None) -> SubgroupCount:
|
||||
|
@ -95,8 +100,13 @@ def calculate_subgroup_count(proxies: Optional[List[Proxy]] = None) -> SubgroupC
|
|||
return subgroup_count
|
||||
|
||||
|
||||
def next_subgroup(subgroup_count: SubgroupCount, provider: str, group_id: int, max_subgroup_count: int,
|
||||
max_subgroup_members: int) -> Optional[int]:
|
||||
def next_subgroup(
|
||||
subgroup_count: SubgroupCount,
|
||||
provider: str,
|
||||
group_id: int,
|
||||
max_subgroup_count: int,
|
||||
max_subgroup_members: int,
|
||||
) -> Optional[int]:
|
||||
"""
|
||||
Find the first available subgroup with less than the specified maximum count in the specified provider and group.
|
||||
If the last subgroup in the group is full, return the next subgroup number as long as it doesn't exceed
|
||||
|
@ -137,27 +147,36 @@ def auto_deprecate_proxies() -> None:
|
|||
- The "max_age_reached" reason means the proxy has been in use for longer than the maximum allowed period.
|
||||
The maximum age cutoff is randomly set to a time between 24 and 48 hours.
|
||||
"""
|
||||
origin_destroyed_proxies = (db.session.query(Proxy)
|
||||
.join(Origin, Proxy.origin_id == Origin.id)
|
||||
.filter(Proxy.destroyed.is_(None),
|
||||
Proxy.deprecated.is_(None),
|
||||
Origin.destroyed.is_not(None))
|
||||
.all())
|
||||
origin_destroyed_proxies = (
|
||||
db.session.query(Proxy)
|
||||
.join(Origin, Proxy.origin_id == Origin.id)
|
||||
.filter(
|
||||
Proxy.destroyed.is_(None),
|
||||
Proxy.deprecated.is_(None),
|
||||
Origin.destroyed.is_not(None),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
logging.debug("Origin destroyed: %s", origin_destroyed_proxies)
|
||||
for proxy in origin_destroyed_proxies:
|
||||
proxy.deprecate(reason="origin_destroyed")
|
||||
max_age_proxies = (db.session.query(Proxy)
|
||||
.join(Origin, Proxy.origin_id == Origin.id)
|
||||
.filter(Proxy.destroyed.is_(None),
|
||||
Proxy.deprecated.is_(None),
|
||||
Proxy.pool_id != -1, # do not rotate hotspare proxies
|
||||
Origin.assets,
|
||||
Origin.auto_rotation)
|
||||
.all())
|
||||
max_age_proxies = (
|
||||
db.session.query(Proxy)
|
||||
.join(Origin, Proxy.origin_id == Origin.id)
|
||||
.filter(
|
||||
Proxy.destroyed.is_(None),
|
||||
Proxy.deprecated.is_(None),
|
||||
Proxy.pool_id != -1, # do not rotate hotspare proxies
|
||||
Origin.assets,
|
||||
Origin.auto_rotation,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
logging.debug("Max age: %s", max_age_proxies)
|
||||
for proxy in max_age_proxies:
|
||||
max_age_cutoff = datetime.now(timezone.utc) - timedelta(
|
||||
days=1, seconds=86400 * random.random()) # nosec: B311
|
||||
days=1, seconds=86400 * random.random() # nosec: B311
|
||||
)
|
||||
if proxy.added < max_age_cutoff:
|
||||
proxy.deprecate(reason="max_age_reached")
|
||||
|
||||
|
@ -171,8 +190,7 @@ def destroy_expired_proxies() -> None:
|
|||
"""
|
||||
expiry_cutoff = datetime.now(timezone.utc) - timedelta(days=4)
|
||||
proxies = Proxy.query.filter(
|
||||
Proxy.destroyed.is_(None),
|
||||
Proxy.deprecated < expiry_cutoff
|
||||
Proxy.destroyed.is_(None), Proxy.deprecated < expiry_cutoff
|
||||
).all()
|
||||
for proxy in proxies:
|
||||
logging.debug("Destroying expired proxy")
|
||||
|
@ -244,12 +262,17 @@ class ProxyMetaAutomation(BaseAutomation):
|
|||
if origin.destroyed is not None:
|
||||
continue
|
||||
proxies = [
|
||||
x for x in origin.proxies
|
||||
if x.pool_id == pool.id and x.deprecated is None and x.destroyed is None
|
||||
x
|
||||
for x in origin.proxies
|
||||
if x.pool_id == pool.id
|
||||
and x.deprecated is None
|
||||
and x.destroyed is None
|
||||
]
|
||||
logging.debug("Proxies for group %s: %s", group.group_name, proxies)
|
||||
if not proxies:
|
||||
logging.debug("Creating new proxy for %s in pool %s", origin, pool)
|
||||
logging.debug(
|
||||
"Creating new proxy for %s in pool %s", origin, pool
|
||||
)
|
||||
if not promote_hot_spare_proxy(pool.id, origin):
|
||||
# No "hot spare" available
|
||||
self.create_proxy(pool.id, origin)
|
||||
|
@ -270,8 +293,13 @@ class ProxyMetaAutomation(BaseAutomation):
|
|||
"""
|
||||
for provider in PROXY_PROVIDERS.values():
|
||||
logging.debug("Looking at provider %s", provider.provider)
|
||||
subgroup = next_subgroup(self.subgroup_count, provider.provider, origin.group_id,
|
||||
provider.subgroup_members_max, provider.subgroup_count_max)
|
||||
subgroup = next_subgroup(
|
||||
self.subgroup_count,
|
||||
provider.provider,
|
||||
origin.group_id,
|
||||
provider.subgroup_members_max,
|
||||
provider.subgroup_count_max,
|
||||
)
|
||||
if subgroup is None:
|
||||
continue # Exceeded maximum number of subgroups and last subgroup is full
|
||||
self.increment_subgroup(provider.provider, origin.group_id, subgroup)
|
||||
|
@ -317,9 +345,7 @@ class ProxyMetaAutomation(BaseAutomation):
|
|||
If an origin is not destroyed and lacks active proxies (not deprecated and not destroyed),
|
||||
a new 'hot spare' proxy for this origin is created in the reserve pool (with pool_id = -1).
|
||||
"""
|
||||
origins = Origin.query.filter(
|
||||
Origin.destroyed.is_(None)
|
||||
).all()
|
||||
origins = Origin.query.filter(Origin.destroyed.is_(None)).all()
|
||||
for origin in origins:
|
||||
if origin.countries:
|
||||
risk_levels = origin.risk_level.items()
|
||||
|
@ -328,7 +354,10 @@ class ProxyMetaAutomation(BaseAutomation):
|
|||
if highest_risk_level < 4:
|
||||
for proxy in origin.proxies:
|
||||
if proxy.destroyed is None and proxy.pool_id == -1:
|
||||
logging.debug("Destroying hot spare proxy for origin %s (low risk)", origin)
|
||||
logging.debug(
|
||||
"Destroying hot spare proxy for origin %s (low risk)",
|
||||
origin,
|
||||
)
|
||||
proxy.destroy()
|
||||
continue
|
||||
if origin.destroyed is not None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue