mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 01:17:37 -02:30
Tweaks to register_default_execution_environments command
- The credential will always be updated. I dont think we want to decrypt the password here for comparison - Preserve newlines in help text
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
# Copyright (c) 2015 Ansible, Inc.
|
# Copyright (c) 2015 Ansible, Inc.
|
||||||
# All Rights Reserved
|
# All Rights Reserved
|
||||||
import sys
|
import sys
|
||||||
|
from distutils.util import strtobool
|
||||||
|
from argparse import RawTextHelpFormatter
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@@ -16,6 +18,12 @@ class Command(BaseCommand):
|
|||||||
Note that settings.DEFAULT_EXECUTION_ENVIRONMENTS is and ordered list, the first in the list will be used for project updates and system jobs.
|
Note that settings.DEFAULT_EXECUTION_ENVIRONMENTS is and ordered list, the first in the list will be used for project updates and system jobs.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Preserves newlines in the help text
|
||||||
|
def create_parser(self, *args, **kwargs):
|
||||||
|
parser = super(Command, self).create_parser(*args, **kwargs)
|
||||||
|
parser.formatter_class = RawTextHelpFormatter
|
||||||
|
return parser
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--registry-url",
|
"--registry-url",
|
||||||
@@ -37,7 +45,7 @@ class Command(BaseCommand):
|
|||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--verify-ssl",
|
"--verify-ssl",
|
||||||
type=bool,
|
type=lambda x: bool(strtobool(str(x))),
|
||||||
default=True,
|
default=True,
|
||||||
help="Verify SSL when authenticating with the container registry",
|
help="Verify SSL when authenticating with the container registry",
|
||||||
)
|
)
|
||||||
@@ -50,33 +58,36 @@ class Command(BaseCommand):
|
|||||||
if not options.get("registry_password"):
|
if not options.get("registry_password"):
|
||||||
sys.stderr.write("Registry password must be provided when providing registry username\n")
|
sys.stderr.write("Registry password must be provided when providing registry username\n")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not options.get("registry_url"):
|
if not options.get("registry_url"):
|
||||||
sys.stderr.write("Registry url must be provided when providing registry username\n")
|
sys.stderr.write("Registry url must be provided when providing registry username\n")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
registry_cred_inputs = {
|
|
||||||
|
registry_cred_type = CredentialType.objects.filter(kind="registry")
|
||||||
|
if not registry_cred_type.exists():
|
||||||
|
sys.stderr.write("No registry credential type found")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
registry_cred, created = Credential.objects.get_or_create(
|
||||||
|
name="Default Execution Environment Registry Credential", managed_by_tower=True, credential_type=registry_cred_type[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
if created:
|
||||||
|
print("Default Execution Environment Credential registered.")
|
||||||
|
|
||||||
|
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_type = CredentialType.objects.filter(kind="registry")
|
|
||||||
if not registry_cred_type.exists():
|
registry_cred.inputs = inputs
|
||||||
sys.stderr.write("No registry credential type found")
|
registry_cred.save()
|
||||||
sys.exit(1)
|
changed = True
|
||||||
registry_cred, created = Credential.objects.get_or_create(
|
|
||||||
name="Default Execution Environment Registry Credential",
|
if not created:
|
||||||
managed_by_tower=True,
|
print('Updated Default Execution Environment Credential')
|
||||||
credential_type=registry_cred_type[0],
|
|
||||||
defaults={"inputs": registry_cred_inputs},
|
|
||||||
)
|
|
||||||
if created:
|
|
||||||
changed = True
|
|
||||||
print("Default Execution Environment Credential registered.")
|
|
||||||
elif registry_cred.inputs != registry_cred_inputs:
|
|
||||||
registry_cred.inputs = registry_cred_inputs
|
|
||||||
registry_cred.save()
|
|
||||||
changed = True
|
|
||||||
print("Default Execution Environment Credential updated.")
|
|
||||||
|
|
||||||
for ee in reversed(settings.DEFAULT_EXECUTION_ENVIRONMENTS):
|
for ee in reversed(settings.DEFAULT_EXECUTION_ENVIRONMENTS):
|
||||||
_, created = ExecutionEnvironment.objects.update_or_create(
|
_, created = ExecutionEnvironment.objects.update_or_create(
|
||||||
|
|||||||
Reference in New Issue
Block a user