Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
feat: multiple agents
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmulloy committed Aug 31, 2023
1 parent 36077df commit cfb877a
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ def print_diff(a, b, a_title, b_title):
lines_a = a.splitlines(keepends=True)
lines_b = b.splitlines(keepends=True)


print(''.join(difflib.unified_diff(lines_a, lines_b, fromfile=a_title, tofile=b_title)))



def update_image_in_elastic_profile(host, token, profile_id, new_pod_config, apply=False):
"""
Upload the new_pod_config to the specified profile_id
Expand All @@ -61,7 +59,7 @@ def update_image_in_elastic_profile(host, token, profile_id, new_pod_config, app
pod_configuration_value = pod_configuration['value']

logging.info("Diff of old and new pod configuration")
print_diff(pod_configuration_value, new_pod_config, 'existing_pod_configuration', 'new_pod_configuration')
print_diff(pod_configuration_value, new_pod_config, f"existing_pod_configuration {profile_id}", f"new_pod_configuration {profile_id}")

if apply:
# Now modify the original list since it is copy by value
Expand All @@ -75,30 +73,31 @@ def update_image_in_elastic_profile(host, token, profile_id, new_pod_config, app
@click.command()
@click.option('--host', help='gocd hostname without protocol eg gocd.tools.edx.org', required=True)
@click.option('--token', help='gocd auth token', required=True)
@click.option('--agent-image', help='image to change the tag on eg: myregistry/myapp', required=True)
#@click.option('--agent-image', help='image to change the tag on eg: myregistry/myapp', required=True)
@click.option('--agent-tag', help='new tag of the image to replace', required=True)
@click.option('--profile-id', help='new tag of the image to replace', required=True)
@click.option('--profile-id', help='new tag of the image to replace', required=True, multiple=True)
@click.option('--templates-dir', help='Directory holding Jinja2 template files for pod configuration', required=True)
@click.option('--apply', is_flag=True, default=False,
help='Must be present to actually apply chagnes, otherwise we will only print a diff and exit', required=True)
def update_gocd_legacy_agents(token, host, agent_image, agent_tag, profile_id, templates_dir, apply):
def configure_gocd_agents(token, host, agent_tag, profile_id, templates_dir, apply):
"""
For the specified profile get the pod yaml and replace the tag of any images that
match with the new tag for a given profile
For the specified profile update the pod yaml with a rendered jina2 templat
"""
j2_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(templates_dir)
)
j2_template = j2_environment.get_template(f"{profile_id}.yaml.j2")
#print(j2_template.render(image=image, tag=tag))
new_pod_config = j2_template.render(agent_image=agent_image, agent_tag=agent_tag)
try:
logging.info(f"Updating agent profile {profile_id} with agent image {agent_image}:{agent_tag}")
update_image_in_elastic_profile(host, token, profile_id, new_pod_config, apply)
except Exception as err: # pylint: disable=broad-except
traceback.print_exc()
click.secho('{}'.format(err), fg='red')
sys.exit(1)
profile_ids = profile_id
for profile_id in profile_ids:
j2_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(templates_dir)
)
j2_template = j2_environment.get_template(f"{profile_id}.yaml.j2")
#print(j2_template.render(image=image, tag=tag))
new_pod_config = j2_template.render(agent_tag=agent_tag)
try:
logging.info(f"Updating agent profile {profile_id} with agent image tag {agent_tag}")
update_image_in_elastic_profile(host, token, profile_id, new_pod_config, apply)
except Exception as err: # pylint: disable=broad-except
traceback.print_exc()
click.secho('{}'.format(err), fg='red')
sys.exit(1)

if __name__ == "__main__":
update_gocd_legacy_agents() # pylint: disable=no-value-for-parameter
configure_gocd_agents() # pylint: disable=no-value-for-parameter

0 comments on commit cfb877a

Please sign in to comment.