From f2dac36dd1a30be6399053e6c4708112d18efa2c Mon Sep 17 00:00:00 2001 From: Elijah DeLee Date: Tue, 8 Jun 2021 15:52:53 -0400 Subject: [PATCH] Remove the cred too if not passed NOW....should be idempotent and remove the the cred if it is not passed --- ...register_default_execution_environments.py | 54 +++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/awx/main/management/commands/register_default_execution_environments.py b/awx/main/management/commands/register_default_execution_environments.py index 4005df877c..fdc9cca7ed 100644 --- a/awx/main/management/commands/register_default_execution_environments.py +++ b/awx/main/management/commands/register_default_execution_environments.py @@ -68,23 +68,28 @@ class Command(BaseCommand): sys.stderr.write("No registry credential type found") sys.exit(1) - registry_cred, cred_created = Credential.objects.get_or_create( - name="Default Execution Environment Registry Credential", managed_by_tower=True, credential_type=registry_cred_type[0] - ) - - if cred_created: - print("'Default Execution Environment Credential' registered.") - inputs = { "host": options.get("registry_url"), "password": options.get("registry_password"), "username": options.get("registry_username"), "verify_ssl": options.get("verify_ssl"), } + registry_cred, cred_created = Credential.objects.get_or_create( + name="Default Execution Environment Registry Credential", + managed_by_tower=True, + credential_type=registry_cred_type[0], + defaults={'inputs': inputs}, + ) + + if cred_created: + changed = True + print("'Default Execution Environment Credential' registered.") + for key, value in inputs.items(): - if registry_cred.get_input(key) != value: + if not registry_cred.inputs.get(key) or registry_cred.get_input(key) != value: registry_cred.inputs[key] = value changed = True + if changed: registry_cred.save() print("'Default Execution Environment Credential' updated.") @@ -95,27 +100,34 @@ class Command(BaseCommand): if ee_created: changed = True print(f"'{ee['name']}' Default Execution Environment registered.") - elif _this_ee.image != ee["image"] or (registry_cred and _this_ee.credential_id != registry_cred.id): - _this_ee.image = ee["image"] - _this_ee.credential = registry_cred + else: + if _this_ee.image != ee["image"]: + _this_ee.image = ee["image"] + changed = True + if _this_ee.credential != registry_cred: + _this_ee.credential = registry_cred + changed = True + if changed: _this_ee.save() - changed = True print(f"'{ee['name']}' Default Execution Environment updated.") # Create the control plane execution environment that is used for project updates and system jobs - control_plane_ee_image = settings.CONTROL_PLANE_EXECUTION_ENVIRONMENT - _cp_ee, cp_created = ExecutionEnvironment.objects.get_or_create( - name="Control Plane Execution Environment", defaults={'image': control_plane_ee_image, 'managed_by_tower': True, 'credential': registry_cred} + ee = settings.CONTROL_PLANE_EXECUTION_ENVIRONMENT + _this_ee, cp_created = ExecutionEnvironment.objects.get_or_create( + name="Control Plane Execution Environment", defaults={'image': ee, 'managed_by_tower': True, 'credential': registry_cred} ) if cp_created: changed = True print("Control Plane Execution Environment registered.") - elif _cp_ee.image != control_plane_ee_image or (registry_cred and _cp_ee.credential_id != registry_cred.id): - _cp_ee.image = control_plane_ee_image - _cp_ee.credential = registry_cred - _cp_ee.save() - changed = True - print("Control Plane Execution Environment updated.") + else: + if _this_ee.image != ee: + _this_ee.image = ee + changed = True + if _this_ee.credential != registry_cred: + _this_ee.credential = registry_cred + changed = True + if changed: + _this_ee.save() if changed: print("(changed: True)")