feat: abstracting cloud providers
This commit is contained in:
parent
af36a545a1
commit
0a72aeed96
18 changed files with 629 additions and 181 deletions
|
@ -4,18 +4,40 @@ from typing import Tuple, List
|
|||
|
||||
from app import db
|
||||
from app.models.bridges import BridgeConf, Bridge
|
||||
from app.models.cloud import CloudProvider, CloudAccount
|
||||
from app.terraform import BaseAutomation
|
||||
from app.terraform.bridge.gandi import BridgeGandiAutomation
|
||||
from app.terraform.bridge.hcloud import BridgeHcloudAutomation
|
||||
from app.terraform.bridge.ovh import BridgeOvhAutomation
|
||||
|
||||
BRIDGE_PROVIDERS = {p.provider: p for p in [
|
||||
BRIDGE_PROVIDERS = [
|
||||
# In order of cost
|
||||
BridgeHcloudAutomation,
|
||||
BridgeGandiAutomation,
|
||||
BridgeOvhAutomation,
|
||||
# BridgeAWSAutomation, TODO: This module is broken right now
|
||||
] if p.enabled}
|
||||
CloudProvider.HCLOUD,
|
||||
CloudProvider.GANDI,
|
||||
CloudProvider.OVH,
|
||||
CloudProvider.AWS,
|
||||
]
|
||||
|
||||
|
||||
def active_bridges_in_account(account: CloudAccount) -> List[Bridge]:
|
||||
bridges: List[Bridge] = Bridge.query.filter(
|
||||
Bridge.cloud_account_id == account.id,
|
||||
Bridge.destroyed.is_(None),
|
||||
).all()
|
||||
return bridges
|
||||
|
||||
|
||||
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)
|
||||
bridge = Bridge()
|
||||
bridge.pool_id = bridgeconf.pool.id
|
||||
bridge.conf_id = bridgeconf.id
|
||||
bridge.cloud_account = account
|
||||
bridge.added = datetime.datetime.utcnow()
|
||||
bridge.updated = datetime.datetime.utcnow()
|
||||
logging.debug("Creating bridge %s", bridge)
|
||||
db.session.add(bridge)
|
||||
created += 1
|
||||
return created
|
||||
|
||||
|
||||
def create_bridges(bridgeconf: BridgeConf, count: int) -> int:
|
||||
|
@ -24,24 +46,17 @@ def create_bridges(bridgeconf: BridgeConf, count: int) -> int:
|
|||
"""
|
||||
logging.debug("Creating %s bridges for configuration %s", count, bridgeconf.id)
|
||||
created = 0
|
||||
# TODO: deal with the fact that I created a dictionary and then forgot it wasn't ordered
|
||||
while created < count:
|
||||
for provider in BRIDGE_PROVIDERS:
|
||||
if BRIDGE_PROVIDERS[provider].max_bridges > BRIDGE_PROVIDERS[provider].active_bridges_count():
|
||||
logging.debug("Creating bridge for configuration %s with provider %s", bridgeconf.id, provider)
|
||||
bridge = Bridge()
|
||||
bridge.pool_id = bridgeconf.pool.id
|
||||
bridge.conf_id = bridgeconf.id
|
||||
bridge.provider = provider
|
||||
bridge.added = datetime.datetime.utcnow()
|
||||
bridge.updated = datetime.datetime.utcnow()
|
||||
logging.debug("Creating bridge %s", bridge)
|
||||
db.session.add(bridge)
|
||||
created += 1
|
||||
break
|
||||
else:
|
||||
logging.debug("No provider has available quota to create missing bridge for configuration %s",
|
||||
bridgeconf.id)
|
||||
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,
|
||||
):
|
||||
logging.info("Creating bridges in %s", account)
|
||||
created += create_bridges_in_account(bridgeconf, account, count - created)
|
||||
logging.debug("Created %s bridges", created)
|
||||
return created
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue