86
86
from rest_framework .exceptions import PermissionDenied
87
87
from django .utils .translation import gettext_lazy as _
88
88
89
+ # Django flags
90
+ from flags .state import flag_enabled
91
+
89
92
logger = logging .getLogger ('awx.main.tasks.jobs' )
90
93
91
94
@@ -439,20 +442,17 @@ def final_run_hook(self, instance, status, private_data_dir):
439
442
Hook for any steps to run after job/task is marked as complete.
440
443
"""
441
444
instance .log_lifecycle ("finalize_run" )
442
- artifact_dir = os .path .join (private_data_dir , 'artifacts' , str (self .instance .id ))
443
- collections_info = os .path .join (artifact_dir , 'collections.json' )
444
- ansible_version_file = os .path .join (artifact_dir , 'ansible_version.txt' )
445
-
446
- if os .path .exists (collections_info ):
447
- with open (collections_info ) as ee_json_info :
448
- ee_collections_info = json .loads (ee_json_info .read ())
449
- instance .installed_collections = ee_collections_info
450
- instance .save (update_fields = ['installed_collections' ])
451
- if os .path .exists (ansible_version_file ):
452
- with open (ansible_version_file ) as ee_ansible_info :
453
- ansible_version_info = ee_ansible_info .readline ()
454
- instance .ansible_version = ansible_version_info
455
- instance .save (update_fields = ['ansible_version' ])
445
+ if flag_enabled ("FEATURE_INDIRECT_NODE_COUNTING_ENABLED" ):
446
+ artifact_dir = os .path .join (private_data_dir , 'artifacts' , str (self .instance .id ))
447
+ data_file_path = os .path .join (artifact_dir , 'ansible_data.json' )
448
+
449
+ if os .path .exists (data_file_path ):
450
+ with open (data_file_path ) as f :
451
+ collected_data = json .loads (f .read ())
452
+
453
+ instance .installed_collections = collected_data ['installed_collections' ]
454
+ instance .ansible_version = collected_data ['ansible_version' ]
455
+ instance .save (update_fields = ['installed_collections' , 'ansible_version' ])
456
456
457
457
# Run task manager appropriately for speculative dependencies
458
458
if instance .unifiedjob_blocked_jobs .exists ():
@@ -927,11 +927,16 @@ def build_env(self, job, private_data_dir, private_data_files=None):
927
927
if authorize :
928
928
env ['ANSIBLE_NET_AUTH_PASS' ] = network_cred .get_input ('authorize_password' , default = '' )
929
929
930
- path_vars = (
930
+ path_vars = [
931
931
('ANSIBLE_COLLECTIONS_PATHS' , 'collections_paths' , 'requirements_collections' , '~/.ansible/collections:/usr/share/ansible/collections' ),
932
932
('ANSIBLE_ROLES_PATH' , 'roles_path' , 'requirements_roles' , '~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles' ),
933
933
('ANSIBLE_COLLECTIONS_PATH' , 'collections_path' , 'requirements_collections' , '~/.ansible/collections:/usr/share/ansible/collections' ),
934
- )
934
+ ]
935
+
936
+ if flag_enabled ("FEATURE_INDIRECT_NODE_COUNTING_ENABLED" ):
937
+ path_vars .append (
938
+ ('ANSIBLE_CALLBACK_PLUGINS' , 'callback_plugins' , 'plugins_path' , '~/.ansible/plugins:/plugins/callback:/usr/share/ansible/plugins/callback' ),
939
+ )
935
940
936
941
config_values = read_ansible_config (os .path .join (private_data_dir , 'project' ), list (map (lambda x : x [1 ], path_vars )))
937
942
@@ -948,6 +953,11 @@ def build_env(self, job, private_data_dir, private_data_files=None):
948
953
paths = [os .path .join (CONTAINER_ROOT , folder )] + paths
949
954
env [env_key ] = os .pathsep .join (paths )
950
955
956
+ if flag_enabled ("FEATURE_INDIRECT_NODE_COUNTING_ENABLED" ):
957
+ env ['ANSIBLE_CALLBACKS_ENABLED' ] = 'indirect_instance_count'
958
+ if 'callbacks_enabled' in config_values :
959
+ env ['ANSIBLE_CALLBACKS_ENABLED' ] += ':' + config_values ['callbacks_enabled' ]
960
+
951
961
return env
952
962
953
963
def build_args (self , job , private_data_dir , passwords ):
@@ -1388,6 +1398,17 @@ def make_local_copy(project, job_private_data_dir):
1388
1398
shutil .copytree (cache_subpath , dest_subpath , symlinks = True )
1389
1399
logger .debug ('{0} {1} prepared {2} from cache' .format (type (project ).__name__ , project .pk , dest_subpath ))
1390
1400
1401
+ if flag_enabled ("FEATURE_INDIRECT_NODE_COUNTING_ENABLED" ):
1402
+ # copy the special callback (not stdout type) plugin to get list of collections
1403
+ pdd_plugins_path = os .path .join (job_private_data_dir , 'plugins_path' )
1404
+ if not os .path .exists (pdd_plugins_path ):
1405
+ os .mkdir (pdd_plugins_path )
1406
+ from awx .playbooks import library
1407
+
1408
+ plugin_file_source = os .path .join (library .__path__ ._path [0 ], 'indirect_instance_count.py' )
1409
+ plugin_file_dest = os .path .join (pdd_plugins_path , 'indirect_instance_count.py' )
1410
+ shutil .copyfile (plugin_file_source , plugin_file_dest )
1411
+
1391
1412
def post_run_hook (self , instance , status ):
1392
1413
super (RunProjectUpdate , self ).post_run_hook (instance , status )
1393
1414
# To avoid hangs, very important to release lock even if errors happen here
0 commit comments