parent
9b8ac493b1
commit
082de33b5d
7 changed files with 194 additions and 143 deletions
|
@ -1,16 +1,22 @@
|
|||
import datetime
|
||||
from collections import defaultdict
|
||||
import datetime
|
||||
import math
|
||||
import string
|
||||
import random
|
||||
|
||||
from sqlalchemy import text
|
||||
from tldextract import tldextract
|
||||
|
||||
from app import app
|
||||
from app.extensions import db
|
||||
from app.models.base import Group
|
||||
from app.models.mirrors import Origin, Proxy
|
||||
from app.terraform import BaseAutomation
|
||||
from app.models.mirrors import Proxy
|
||||
from app.terraform.terraform import TerraformAutomation
|
||||
|
||||
|
||||
class ProxyAutomation(BaseAutomation):
|
||||
class ProxyAutomation(TerraformAutomation):
|
||||
subgroup_max = math.inf
|
||||
|
||||
def get_subgroups(self):
|
||||
conn = db.engine.connect()
|
||||
result = conn.execute(text("""
|
||||
|
@ -26,26 +32,38 @@ class ProxyAutomation(BaseAutomation):
|
|||
return subgroups
|
||||
|
||||
def create_missing_proxies(self):
|
||||
origins = Origin.query.filter(Origin.destroyed == None).all()
|
||||
for origin in origins:
|
||||
cloudfront_proxies = [
|
||||
x for x in origin.proxies
|
||||
if x.provider == self.provider and x.deprecated is None and x.destroyed is None
|
||||
]
|
||||
if not cloudfront_proxies:
|
||||
proxy = Proxy()
|
||||
proxy.origin_id = origin.id
|
||||
proxy.provider = self.provider
|
||||
proxy.added = datetime.datetime.utcnow()
|
||||
proxy.updated = datetime.datetime.utcnow()
|
||||
db.session.add(proxy)
|
||||
db.session.commit()
|
||||
groups = Group.query.all()
|
||||
subgroups = self.get_subgroups()
|
||||
for group in groups:
|
||||
subgroup = 0
|
||||
for origin in group.origins:
|
||||
while True:
|
||||
if subgroups[group.id][subgroup] >= self.subgroup_max:
|
||||
subgroup += 1
|
||||
else:
|
||||
break
|
||||
proxies = [
|
||||
x for x in origin.proxies
|
||||
if x.provider == self.provider and x.deprecated is None and x.destroyed is None
|
||||
]
|
||||
if not proxies:
|
||||
subgroups[group.id][subgroup] += 1
|
||||
proxy = Proxy()
|
||||
proxy.origin_id = origin.id
|
||||
proxy.provider = self.provider
|
||||
proxy.psg = subgroup
|
||||
proxy.slug = tldextract.extract(origin.domain_name).domain[:5] + ''.join(
|
||||
random.choices(string.ascii_lowercase, k=12))
|
||||
proxy.added = datetime.datetime.utcnow()
|
||||
proxy.updated = datetime.datetime.utcnow()
|
||||
db.session.add(proxy)
|
||||
db.session.commit()
|
||||
|
||||
def deprecate_orphaned_proxies(self):
|
||||
proxies = Proxy.query.filter(
|
||||
Proxy.deprecated == None,
|
||||
Proxy.destroyed == None,
|
||||
Proxy.provider == self.provider,
|
||||
Proxy.deprecated == None
|
||||
Proxy.provider == self.provider
|
||||
).all()
|
||||
for proxy in proxies:
|
||||
if proxy.origin.destroyed is not None:
|
||||
|
@ -64,13 +82,16 @@ class ProxyAutomation(BaseAutomation):
|
|||
proxy.updated = datetime.datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
def pre_housekeeping(self):
|
||||
def tf_prehook(self):
|
||||
self.create_missing_proxies()
|
||||
self.deprecate_orphaned_proxies()
|
||||
self.destroy_expired_proxies()
|
||||
|
||||
def generate_terraform(self):
|
||||
self.write_terraform_config(
|
||||
def tf_posthook(self):
|
||||
self.import_state(self.tf_show())
|
||||
|
||||
def tf_generate(self):
|
||||
self.tf_write(
|
||||
self.template,
|
||||
groups=Group.query.all(),
|
||||
proxies=Proxy.query.filter(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue