Adding prevent_instance_group_fallback

This commit is contained in:
John Westcott IV
2022-08-23 14:13:06 -04:00
parent 87c65c9997
commit 0983bd8dc0
9 changed files with 308 additions and 269 deletions

View File

@@ -1680,6 +1680,7 @@ class InventorySerializer(LabelsListMixin, BaseSerializerWithVariables):
'total_inventory_sources', 'total_inventory_sources',
'inventory_sources_with_failures', 'inventory_sources_with_failures',
'pending_deletion', 'pending_deletion',
'prevent_instance_group_fallback',
) )
def get_related(self, obj): def get_related(self, obj):
@@ -2937,6 +2938,7 @@ class JobTemplateSerializer(JobTemplateMixin, UnifiedJobTemplateSerializer, JobO
'job_slice_count', 'job_slice_count',
'webhook_service', 'webhook_service',
'webhook_credential', 'webhook_credential',
'prevent_instance_group_fallback',
) )
read_only_fields = ('*', 'custom_virtualenv') read_only_fields = ('*', 'custom_virtualenv')

View File

@@ -175,6 +175,13 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin, RelatedJobsMixin):
related_name='inventory_labels', related_name='inventory_labels',
help_text=_('Labels associated with this inventory.'), help_text=_('Labels associated with this inventory.'),
) )
prevent_instance_group_fallback = models.BooleanField(
default=False,
help_text=(
"If enabled, the job template will prevent adding any inventory or organization "
"instance groups to the list of preferred instances groups to run on."
),
)
def get_absolute_url(self, request=None): def get_absolute_url(self, request=None):
return reverse('api:inventory_detail', kwargs={'pk': self.pk}, request=request) return reverse('api:inventory_detail', kwargs={'pk': self.pk}, request=request)

View File

@@ -274,6 +274,13 @@ class JobTemplate(UnifiedJobTemplate, JobOptions, SurveyJobTemplateMixin, Resour
'admin_role', 'admin_role',
], ],
) )
prevent_instance_group_fallback = models.BooleanField(
default=False,
help_text=(
"If enabled, the job template will prevent adding any inventory or organization "
"instance groups to the list of preferred instances groups to run on."
),
)
@classmethod @classmethod
def _get_unified_job_class(cls): def _get_unified_job_class(cls):
@@ -797,19 +804,14 @@ class Job(UnifiedJob, JobOptions, SurveyJobMixin, JobNotificationMixin, TaskMana
def preferred_instance_groups(self): def preferred_instance_groups(self):
# If the user specified instance groups those will be handled by the unified_job.create_unified_job # If the user specified instance groups those will be handled by the unified_job.create_unified_job
# This function handles only the defaults for a template w/o user specification # This function handles only the defaults for a template w/o user specification
if self.organization is not None: selected_groups = []
organization_groups = [x for x in self.organization.instance_groups.all()] for obj_type in ['job_template', 'inventory', 'organization']:
else: if getattr(self, obj_type) is not None:
organization_groups = [] for instance_group in getattr(self, obj_type).instance_groups.all():
if self.inventory is not None: selected_groups.append(instance_group)
inventory_groups = [x for x in self.inventory.instance_groups.all()] if getattr(getattr(self, obj_type), 'prevent_instance_group_fallback', False):
else: logger.error("Breaking in preferred instance group at {}".format(obj_type))
inventory_groups = [] break
if self.job_template is not None:
template_groups = [x for x in self.job_template.instance_groups.all()]
else:
template_groups = []
selected_groups = template_groups + inventory_groups + organization_groups
if not selected_groups: if not selected_groups:
return self.global_instance_groups return self.global_instance_groups
return selected_groups return selected_groups

View File

@@ -191,6 +191,7 @@ const getInventoryHelpTextStrings = () => ({
sourcePath: t`The inventory file sourcePath: t`The inventory file
to be synced by this source. You can select from to be synced by this source. You can select from
the dropdown or enter a file within the input.`, the dropdown or enter a file within the input.`,
preventInstanceGroupFallback: t`If enabled, the job template will prevent adding any inventory or organization instance groups to the list of preferred instances groups to run on.`,
}); });
export default getInventoryHelpTextStrings; export default getInventoryHelpTextStrings;

View File

@@ -5,7 +5,10 @@ import { func, shape } from 'prop-types';
import { Form, FormGroup } from '@patternfly/react-core'; import { Form, FormGroup } from '@patternfly/react-core';
import { VariablesField } from 'components/CodeEditor'; import { VariablesField } from 'components/CodeEditor';
import Popover from 'components/Popover'; import Popover from 'components/Popover';
import FormField, { FormSubmitError } from 'components/FormField'; import FormField, {
CheckboxField,
FormSubmitError,
} from 'components/FormField';
import FormActionGroup from 'components/FormActionGroup'; import FormActionGroup from 'components/FormActionGroup';
import { required } from 'util/validators'; import { required } from 'util/validators';
import LabelSelect from 'components/LabelSelect'; import LabelSelect from 'components/LabelSelect';
@@ -71,6 +74,12 @@ function InventoryFormFields({ inventory }) {
}} }}
fieldName="instanceGroups" fieldName="instanceGroups"
/> />
<CheckboxField
id="option-prevent-instance-group-fallback"
name="prevent_instance_group_fallback"
label={t`Prevent Instance Group Fallback`}
tooltip={helpText.preventInstanceGroupFallback}
/>
<FormFullWidthLayout> <FormFullWidthLayout>
<FormGroup <FormGroup
label={t`Labels`} label={t`Labels`}
@@ -112,6 +121,8 @@ function InventoryForm({
null, null,
instanceGroups: instanceGroups || [], instanceGroups: instanceGroups || [],
labels: inventory?.summary_fields?.labels?.results || [], labels: inventory?.summary_fields?.labels?.results || [],
prevent_instance_group_fallback:
inventory.prevent_instance_group_fallback || false,
}; };
return ( return (
<Formik <Formik

View File

@@ -91,5 +91,6 @@
"has_inventory_sources": false, "has_inventory_sources": false,
"total_inventory_sources": 0, "total_inventory_sources": 0,
"inventory_sources_with_failures": 0, "inventory_sources_with_failures": 0,
"pending_deletion": false "pending_deletion": false,
"prevent_instance_group_fallback": false
} }

View File

@@ -30,6 +30,7 @@ const jtHelpTextStrings = () => ({
privilegeEscalation: t`If enabled, run this playbook as an administrator.`, privilegeEscalation: t`If enabled, run this playbook as an administrator.`,
enableWebhook: t`Enable webhook for this template.`, enableWebhook: t`Enable webhook for this template.`,
concurrentJobs: t`If enabled, simultaneous runs of this job template will be allowed.`, concurrentJobs: t`If enabled, simultaneous runs of this job template will be allowed.`,
preventInstanceGroupFallback: t`If enabled, the job template will prevent adding any inventory or organization instance groups to the list of preferred instances groups to run on.`,
enableFactStorage: t`If enabled, this will store gathered facts so they can be viewed at the host level. Facts are persisted and injected into the fact cache at runtime.`, enableFactStorage: t`If enabled, this will store gathered facts so they can be viewed at the host level. Facts are persisted and injected into the fact cache at runtime.`,
enabledOptions: ( enabledOptions: (
<> <>
@@ -38,6 +39,7 @@ const jtHelpTextStrings = () => ({
<p>{t`Privilege escalation: If enabled, run this playbook as an administrator.`}</p> <p>{t`Privilege escalation: If enabled, run this playbook as an administrator.`}</p>
<p>{t`Provisioning callbacks: Enables creation of a provisioning callback URL. Using the URL a host can contact Ansible AWX and request a configuration update using this job template.`}</p> <p>{t`Provisioning callbacks: Enables creation of a provisioning callback URL. Using the URL a host can contact Ansible AWX and request a configuration update using this job template.`}</p>
<p>{t`Webhooks: Enable webhook for this template.`}</p> <p>{t`Webhooks: Enable webhook for this template.`}</p>
<p>{t`Prevent Instance Group Fallback: If enabled, the job template will prevent adding any inventory or organization instance groups to the list of preferred instances groups to run on.`}</p>
</> </>
), ),
forks: ( forks: (

View File

@@ -597,6 +597,12 @@ function JobTemplateForm({
label={t`Enable Fact Storage`} label={t`Enable Fact Storage`}
tooltip={helpText.enableFactStorage} tooltip={helpText.enableFactStorage}
/> />
<CheckboxField
id="option-prevent-instance-group-fallback"
name="prevent_instance_group_fallback"
label={t`Prevent Instance Group Fallback`}
tooltip={helpText.preventInstanceGroupFallback}
/>
</FormCheckboxLayout> </FormCheckboxLayout>
</FormGroup> </FormGroup>
</FormFullWidthLayout> </FormFullWidthLayout>
@@ -731,6 +737,8 @@ const FormikApp = withFormik({
limit: template.limit || '', limit: template.limit || '',
name: template.name || '', name: template.name || '',
playbook: template.playbook || '', playbook: template.playbook || '',
prevent_instance_group_fallback:
template.prevent_instance_group_fallback || false,
project: summary_fields?.project || projectValues || null, project: summary_fields?.project || projectValues || null,
scm_branch: template.scm_branch || '', scm_branch: template.scm_branch || '',
skip_tags: template.skip_tags || '', skip_tags: template.skip_tags || '',

View File

@@ -103,22 +103,27 @@
}, },
"labels": { "labels": {
"count": 1, "count": 1,
"results": [{ "results": [
{
"id": 91, "id": 91,
"name": "L_91o2" "name": "L_91o2"
}] }
]
}, },
"survey": { "survey": {
"title": "", "title": "",
"description": "" "description": ""
}, },
"recent_jobs": [{ "recent_jobs": [
{
"id": 12, "id": 12,
"status": "successful", "status": "successful",
"finished": "2019-10-01T14:34:35.142483Z", "finished": "2019-10-01T14:34:35.142483Z",
"type": "job" "type": "job"
}], }
"credentials": [{ ],
"credentials": [
{
"id": 1, "id": 1,
"kind": "ssh", "kind": "ssh",
"name": "Credential 1" "name": "Credential 1"
@@ -132,7 +137,6 @@
"webhook_credential": { "webhook_credential": {
"id": "1", "id": "1",
"name": "Webhook Credential" "name": "Webhook Credential"
}, },
"execution_environment": { "execution_environment": {
"id": 1, "id": 1,
@@ -190,5 +194,6 @@
"webhook_credential": 1, "webhook_credential": 1,
"webhook_key": "asertdyuhjkhgfd234567kjgfds", "webhook_key": "asertdyuhjkhgfd234567kjgfds",
"webhook_service": "github", "webhook_service": "github",
"execution_environment": 1 "execution_environment": 1,
"prevent_instance_group_fallback": false
} }