Skip to content

Commit

Permalink
env forward variables (twitter auth trough managed spotters)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick borowy committed Apr 9, 2024
1 parent b6c69a8 commit a444ebf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand Down
39 changes: 33 additions & 6 deletions src/orchestrate_spotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -63,16 +80,18 @@ 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,
"Labels": {
"network.exorde.orchestrate": "spotter",
"network.exorde.monitor": "true"
},
"Env": [
f"ORCHESTRATOR_NAME={self_container_details['Name'][1:]}"
],
"Env": env_forwarding,
"HostConfig": {
"NetworkMode": "exorde-network"
}
Expand All @@ -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()

Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit a444ebf

Please sign in to comment.