From a444ebf92bd8cb6c2fead62a0257ffe7a42ae275 Mon Sep 17 00:00:00 2001 From: patrick borowy Date: Tue, 9 Apr 2024 11:24:04 +0200 Subject: [PATCH] env forward variables (twitter auth trough managed spotters) --- src/app.py | 4 +--- src/orchestrate_spotters.py | 39 +++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/app.py b/src/app.py index f52ddfe..0996745 100644 --- a/src/app.py +++ b/src/app.py @@ -40,7 +40,7 @@ def handle_signal(app, loop, signame): if CLOSE_CONTAINER_ID: app.on_startup.append(orchestrator_update_step_one) -if os.getenv("AUTOMATIC_UPDATE", True) and not CLOSE_CONTAINER_ID: +if os.getenv("AUTOMATIC_UPDATE", True) != "false" and not CLOSE_CONTAINER_ID: app.on_startup.append(start_update_task) logging.info("Will pull images and update out-of-date containers") else: @@ -51,8 +51,6 @@ def handle_signal(app, loop, signame): logging.info(f"Will close {FINAL_CLOSE_CONTAINER_ID}") app.on_startup.append(close_temporary_container) -# AUTOMATIC UPDATE END - # SPOTTERS ORCHESTRATION SPOTTERS_AMOUNT = os.getenv("SPOTTERS_AMOUNT", 0) logging.info(f"running {SPOTTERS_AMOUNT} spotters") diff --git a/src/orchestrate_spotters.py b/src/orchestrate_spotters.py index 59b45cc..74e1c6c 100644 --- a/src/orchestrate_spotters.py +++ b/src/orchestrate_spotters.py @@ -18,6 +18,23 @@ async def get_self_container(client): return containers[0] +def get_env_forward(): + """ + Some env variable are to be forwarded from the orchestrator to the different + spotters. This allows twitter auth to be passed trough for example. + """ + forward_variable_key_list: list[str] = [ + "SCWEET_EMAIL", + "SCWEET_USERNAME", + "SCWEET_PASSWORD" + ] + variables: list[str] = [] + for key in forward_variable_key_list: + var = os.getenv(key, 'null') + variables.append(f"{key}={var}") + return variables + + image_prefix = "exordelabs" # Prefix to convert module names to image names async def reconcile_containers(desired_state): """ @@ -63,6 +80,10 @@ async def reconcile_containers(desired_state): logging.info( f"Starting new container for image {prefixed_image}..." ) + env_forwarding = get_env_forward() + env_forwarding.append( + f"ORCHESTRATOR_NAME={self_container_details['Name'][1:]}" + ) container = await client.containers.create_or_replace( config={ "Image": prefixed_image, @@ -70,9 +91,7 @@ async def reconcile_containers(desired_state): "network.exorde.orchestrate": "spotter", "network.exorde.monitor": "true" }, - "Env": [ - f"ORCHESTRATOR_NAME={self_container_details['Name'][1:]}" - ], + "Env": env_forwarding, "HostConfig": { "NetworkMode": "exorde-network" } @@ -84,7 +103,9 @@ async def reconcile_containers(desired_state): # Stop and remove extra containers extra_containers = current_containers[desired_count:] for container in extra_containers: - logging.info(f"Stopping and removing container {container.id} for image {prefixed_image}...") + logging.info( + f"Stopping and removing container {container.id} for image {prefixed_image}..." + ) await container.stop() await container.delete() @@ -96,7 +117,9 @@ async def get_desired_state() -> dict[str, int]: ponderation = await get_ponderation() weights = ponderation.weights amount_of_containers = int(os.getenv("SPOTTERS_AMOUNT", 0)) - logging.info(f"Amount of containers to manage : {amount_of_containers}") + logging.info( + f"Amount of containers to manage : {amount_of_containers}" + ) # Calculate the total weight total_weight = sum(weights.values()) @@ -105,7 +128,9 @@ async def get_desired_state() -> dict[str, int]: module_containers_intended = { module: (weight / total_weight) * amount_of_containers for module, weight in weights.items() } - logging.info(f"module_containers_intended : {module_containers_intended}") + logging.info( + f"module_containers_intended : {module_containers_intended}" + ) # Adjust for rounding issues to ensure the sum of allocated containers matches the amount_of_containers exactly # This can be done by distributing rounding errors @@ -130,6 +155,7 @@ async def get_desired_state() -> dict[str, int]: } return adjusted_module_containers + async def delete_all_managed_containers(__app__): """In order to sanitize the state, we delete every managed `spotter`""" logging.info("Running spotter orchestration shutdown") @@ -151,6 +177,7 @@ async def delete_all_managed_containers(__app__): logging.error(f"Failed to delete container: {container.id}, Error: {e}") await client.close() + async def orchestration_task(app): refresh_time = int(os.getenv("SPOTTERS_TIME_WINDOW", "3600")) # Refresh time in seconds last_refresh = datetime.datetime.now() # Track the last refresh time