mirror of
https://github.com/ZwareBear/awx.git
synced 2026-04-29 03:11:48 -05:00
refactor of create_unified_job in connection with new copy endpoint development
This commit is contained in:
@@ -633,14 +633,6 @@ class Job(UnifiedJob, JobOptions, SurveyJobMixin, JobNotificationMixin):
|
||||
content = super(Job, self)._result_stdout_raw(*args, **kwargs)
|
||||
return self._survey_search_and_replace(content)
|
||||
|
||||
def copy(self):
|
||||
presets = {}
|
||||
for kw in JobTemplate._get_unified_job_field_names():
|
||||
presets[kw] = getattr(self, kw)
|
||||
if not self.job_template:
|
||||
self.job_template = JobTemplate(name='temporary')
|
||||
return self.job_template.create_unified_job(**presets)
|
||||
|
||||
# Job Credential required
|
||||
@property
|
||||
def can_start(self):
|
||||
|
||||
@@ -102,9 +102,6 @@ class SurveyJobTemplateMixin(models.Model):
|
||||
Combine extra_vars with variable precedence order:
|
||||
JT extra_vars -> JT survey defaults -> runtime extra_vars
|
||||
'''
|
||||
if 'launch_type' in kwargs and kwargs['launch_type'] == 'relaunch':
|
||||
return kwargs
|
||||
|
||||
# Job Template extra_vars
|
||||
extra_vars = self.extra_vars_dict
|
||||
|
||||
|
||||
@@ -30,7 +30,10 @@ from djcelery.models import TaskMeta
|
||||
# AWX
|
||||
from awx.main.models.base import * # noqa
|
||||
from awx.main.models.schedules import Schedule
|
||||
from awx.main.utils import decrypt_field, _inventory_updates
|
||||
from awx.main.utils import (
|
||||
decrypt_field, _inventory_updates,
|
||||
copy_model_by_class, copy_m2m_relationships
|
||||
)
|
||||
from awx.main.redact import UriCleaner, REPLACE_STR
|
||||
from awx.main.consumers import emit_channel_notification
|
||||
from awx.main.fields import JSONField
|
||||
@@ -304,46 +307,13 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio
|
||||
Create a new unified job based on this unified job template.
|
||||
'''
|
||||
unified_job_class = self._get_unified_job_class()
|
||||
fields = self._get_unified_job_field_names()
|
||||
unified_job = copy_model_by_class(self, unified_job_class, fields, kwargs)
|
||||
|
||||
# Set the unified job template back-link on the job
|
||||
parent_field_name = unified_job_class._get_parent_field_name()
|
||||
kwargs.pop('%s_id' % parent_field_name, None)
|
||||
create_kwargs = {}
|
||||
m2m_fields = {}
|
||||
if self.pk:
|
||||
create_kwargs[parent_field_name] = self
|
||||
for field_name in self._get_unified_job_field_names():
|
||||
# Foreign keys can be specified as field_name or field_name_id.
|
||||
id_field_name = '%s_id' % field_name
|
||||
if hasattr(self, id_field_name):
|
||||
if field_name in kwargs:
|
||||
value = kwargs[field_name]
|
||||
elif id_field_name in kwargs:
|
||||
value = kwargs[id_field_name]
|
||||
else:
|
||||
value = getattr(self, id_field_name)
|
||||
if hasattr(value, 'id'):
|
||||
value = value.id
|
||||
create_kwargs[id_field_name] = value
|
||||
elif field_name in kwargs:
|
||||
if field_name == 'extra_vars' and isinstance(kwargs[field_name], dict):
|
||||
create_kwargs[field_name] = json.dumps(kwargs['extra_vars'])
|
||||
# We can't get a hold of django.db.models.fields.related.ManyRelatedManager to compare
|
||||
# so this is the next best thing.
|
||||
elif kwargs[field_name].__class__.__name__ is 'ManyRelatedManager':
|
||||
m2m_fields[field_name] = kwargs[field_name]
|
||||
else:
|
||||
create_kwargs[field_name] = kwargs[field_name]
|
||||
elif hasattr(self, field_name):
|
||||
field_obj = self._meta.get_field_by_name(field_name)[0]
|
||||
# Many to Many can be specified as field_name
|
||||
if isinstance(field_obj, models.ManyToManyField):
|
||||
m2m_fields[field_name] = getattr(self, field_name)
|
||||
else:
|
||||
create_kwargs[field_name] = getattr(self, field_name)
|
||||
if hasattr(self, '_update_unified_job_kwargs'):
|
||||
new_kwargs = self._update_unified_job_kwargs(**create_kwargs)
|
||||
else:
|
||||
new_kwargs = create_kwargs
|
||||
unified_job = unified_job_class(**new_kwargs)
|
||||
setattr(unified_job, parent_field_name, self)
|
||||
|
||||
# For JobTemplate-based jobs with surveys, add passwords to list for perma-redaction
|
||||
if hasattr(self, 'survey_spec') and getattr(self, 'survey_enabled', False):
|
||||
password_list = self.survey_password_variables()
|
||||
@@ -351,10 +321,10 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio
|
||||
for password in password_list:
|
||||
hide_password_dict[password] = REPLACE_STR
|
||||
unified_job.survey_passwords = hide_password_dict
|
||||
|
||||
unified_job.save()
|
||||
for field_name, src_field_value in m2m_fields.iteritems():
|
||||
dest_field = getattr(unified_job, field_name)
|
||||
dest_field.add(*list(src_field_value.all().values_list('id', flat=True)))
|
||||
# Labels coppied here
|
||||
copy_m2m_relationships(self, unified_job, fields, kwargs=kwargs)
|
||||
return unified_job
|
||||
|
||||
@classmethod
|
||||
@@ -363,33 +333,18 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio
|
||||
|
||||
def copy_unified_jt(self):
|
||||
'''
|
||||
Returns saved object, including related fields.
|
||||
Create a copy of this unified job template.
|
||||
'''
|
||||
unified_jt_class = self.__class__
|
||||
create_kwargs = {}
|
||||
m2m_fields = {}
|
||||
for field_name in self._get_unified_jt_copy_names():
|
||||
# Foreign keys can be specified as field_name or field_name_id.
|
||||
id_field_name = '%s_id' % field_name
|
||||
if hasattr(self, id_field_name):
|
||||
value = getattr(self, id_field_name)
|
||||
if hasattr(value, 'id'):
|
||||
value = value.id
|
||||
create_kwargs[id_field_name] = value
|
||||
elif hasattr(self, field_name):
|
||||
field_obj = self._meta.get_field_by_name(field_name)[0]
|
||||
# Many to Many can be specified as field_name
|
||||
if isinstance(field_obj, models.ManyToManyField):
|
||||
m2m_fields[field_name] = getattr(self, field_name)
|
||||
else:
|
||||
create_kwargs[field_name] = getattr(self, field_name)
|
||||
fields = self._get_unified_jt_copy_names()
|
||||
unified_jt = copy_model_by_class(self, unified_jt_class, fields, {})
|
||||
|
||||
time_now = datetime.now()
|
||||
create_kwargs['name'] = create_kwargs['name'] + ' @ ' + time_now.strftime('%H:%M:%S %p')
|
||||
unified_jt = unified_jt_class(**create_kwargs)
|
||||
unified_jt.name = unified_jt.name + ' @ ' + time_now.strftime('%H:%M:%S %p')
|
||||
|
||||
unified_jt.save()
|
||||
for field_name, src_field_value in m2m_fields.iteritems():
|
||||
dest_field = getattr(unified_jt, field_name)
|
||||
dest_field.add(*list(src_field_value.all().values_list('id', flat=True)))
|
||||
copy_m2m_relationships(self, unified_jt, fields)
|
||||
return unified_jt
|
||||
|
||||
|
||||
@@ -711,32 +666,20 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique
|
||||
|
||||
def copy_unified_job(self):
|
||||
'''
|
||||
Create a copy of this unified job.
|
||||
Returns saved object, including related fields.
|
||||
Create a copy of this unified job for the purpose of relaunch
|
||||
'''
|
||||
unified_job_class = self.__class__
|
||||
unified_jt_class = self._get_unified_job_template_class()
|
||||
create_kwargs = {}
|
||||
m2m_fields = {}
|
||||
for field_name in unified_jt_class._get_unified_job_field_names():
|
||||
# Foreign keys can be specified as field_name or field_name_id.
|
||||
id_field_name = '%s_id' % field_name
|
||||
if hasattr(self, id_field_name):
|
||||
value = getattr(self, id_field_name)
|
||||
if hasattr(value, 'id'):
|
||||
value = value.id
|
||||
create_kwargs[id_field_name] = value
|
||||
elif hasattr(self, field_name):
|
||||
field_obj = self._meta.get_field_by_name(field_name)[0]
|
||||
# Many to Many can be specified as field_name
|
||||
if isinstance(field_obj, models.ManyToManyField):
|
||||
m2m_fields[field_name] = getattr(self, field_name)
|
||||
else:
|
||||
create_kwargs[field_name] = getattr(self, field_name)
|
||||
unified_job = unified_job_class(**create_kwargs)
|
||||
parent_field_name = unified_job_class._get_parent_field_name()
|
||||
|
||||
fields = unified_jt_class._get_unified_job_field_names() + [parent_field_name]
|
||||
unified_job = copy_model_by_class(self, unified_job_class, fields, {})
|
||||
unified_job.job_type = 'relaunch'
|
||||
unified_job.save()
|
||||
for field_name, src_field_value in m2m_fields.iteritems():
|
||||
dest_field = getattr(unified_job, field_name)
|
||||
dest_field.add(*list(src_field_value.all().values_list('id', flat=True)))
|
||||
|
||||
# Labels coppied here
|
||||
copy_m2m_relationships(self, unified_job, fields)
|
||||
return unified_job
|
||||
|
||||
def result_stdout_raw_handle(self, attempt=0):
|
||||
|
||||
@@ -198,7 +198,7 @@ class WorkflowJobTemplateNode(WorkflowNodeBase):
|
||||
if not user.can_access(item.__class__, 'use', item):
|
||||
continue
|
||||
if field_name in ['unified_job_template']:
|
||||
if not user.can_access(item.__class__, 'start', item):
|
||||
if not user.can_access(item.__class__, 'start', item, validate_license=False):
|
||||
continue
|
||||
create_kwargs[field_name] = item
|
||||
create_kwargs['workflow_job_template'] = workflow_job_template
|
||||
@@ -323,7 +323,6 @@ class WorkflowJobOptions(BaseModel):
|
||||
self._inherit_node_relationships(old_node_list, node_links)
|
||||
|
||||
def create_relaunch_workflow_job(self):
|
||||
self.launch_type = 'relaunch'
|
||||
new_workflow_job = self.copy_unified_job()
|
||||
new_workflow_job.copy_nodes_from_original(original=self)
|
||||
return new_workflow_job
|
||||
@@ -436,7 +435,7 @@ class WorkflowJobTemplate(UnifiedJobTemplate, WorkflowJobOptions, SurveyJobTempl
|
||||
return new_wfjt
|
||||
|
||||
|
||||
# Stub in place because of old migraitons, can remove if migraitons are squashed
|
||||
# Stub in place because of old migrations, can remove if migrations are squashed
|
||||
class WorkflowJobInheritNodesMixin(object):
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user