Remove the cred too if not passed

NOW....should be idempotent and remove the the cred if it is not passed
This commit is contained in:
Elijah DeLee
2021-06-08 15:52:53 -04:00
parent e2b0a4f7a7
commit f2dac36dd1

View File

@@ -68,23 +68,28 @@ class Command(BaseCommand):
sys.stderr.write("No registry credential type found") sys.stderr.write("No registry credential type found")
sys.exit(1) 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 = { inputs = {
"host": options.get("registry_url"), "host": options.get("registry_url"),
"password": options.get("registry_password"), "password": options.get("registry_password"),
"username": options.get("registry_username"), "username": options.get("registry_username"),
"verify_ssl": options.get("verify_ssl"), "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(): 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 registry_cred.inputs[key] = value
changed = True changed = True
if changed: if changed:
registry_cred.save() registry_cred.save()
print("'Default Execution Environment Credential' updated.") print("'Default Execution Environment Credential' updated.")
@@ -95,27 +100,34 @@ class Command(BaseCommand):
if ee_created: if ee_created:
changed = True changed = True
print(f"'{ee['name']}' Default Execution Environment registered.") 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): else:
_this_ee.image = ee["image"] if _this_ee.image != ee["image"]:
_this_ee.credential = registry_cred _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() _this_ee.save()
changed = True
print(f"'{ee['name']}' Default Execution Environment updated.") print(f"'{ee['name']}' Default Execution Environment updated.")
# Create the control plane execution environment that is used for project updates and system jobs # Create the control plane execution environment that is used for project updates and system jobs
control_plane_ee_image = settings.CONTROL_PLANE_EXECUTION_ENVIRONMENT ee = settings.CONTROL_PLANE_EXECUTION_ENVIRONMENT
_cp_ee, cp_created = ExecutionEnvironment.objects.get_or_create( _this_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} name="Control Plane Execution Environment", defaults={'image': ee, 'managed_by_tower': True, 'credential': registry_cred}
) )
if cp_created: if cp_created:
changed = True changed = True
print("Control Plane Execution Environment registered.") 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): else:
_cp_ee.image = control_plane_ee_image if _this_ee.image != ee:
_cp_ee.credential = registry_cred _this_ee.image = ee
_cp_ee.save() changed = True
changed = True if _this_ee.credential != registry_cred:
print("Control Plane Execution Environment updated.") _this_ee.credential = registry_cred
changed = True
if changed:
_this_ee.save()
if changed: if changed:
print("(changed: True)") print("(changed: True)")