From 7eb8fdd0f161c2ee3d88e194a5972b9b66ed0964 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 19 Jun 2017 12:05:26 -0400 Subject: [PATCH] Change use of subprocess lib for calculating system capacity Suggested by Ryan Petrello as a part of the PR feedback for the isolated instance setup work. --- awx/lib/management_modules/tower_capacity.py | 7 +++++-- awx/main/utils/common.py | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/awx/lib/management_modules/tower_capacity.py b/awx/lib/management_modules/tower_capacity.py index bd8dcab80e..2c3785fe1a 100644 --- a/awx/lib/management_modules/tower_capacity.py +++ b/awx/lib/management_modules/tower_capacity.py @@ -25,8 +25,11 @@ def main(): argument_spec = dict() ) # Duplicated with awx.main.utils.common.get_system_task_capacity - proc = subprocess.Popen(['free', '-m'], stdout=subprocess.PIPE) - out,err = proc.communicate() + try: + out = subprocess.check_output(['free', '-m']) + except subprocess.CalledProcessError as e: + module.fail_json(msg=str(e)) + return total_mem_value = out.split()[7] if int(total_mem_value) <= 2048: cap = 50 diff --git a/awx/main/utils/common.py b/awx/main/utils/common.py index b78f5b4ba5..d8c9e11f20 100644 --- a/awx/main/utils/common.py +++ b/awx/main/utils/common.py @@ -543,8 +543,11 @@ def get_system_task_capacity(): from django.conf import settings if hasattr(settings, 'SYSTEM_TASK_CAPACITY'): return settings.SYSTEM_TASK_CAPACITY - proc = subprocess.Popen(['free', '-m'], stdout=subprocess.PIPE) - out,err = proc.communicate() + try: + out = subprocess.check_output(['free', '-m']) + except subprocess.CalledProcessError as e: + logger.error('Problem obtaining capacity from system, error:\n{}'.format(str(e))) + return 0 total_mem_value = out.split()[7] if int(total_mem_value) <= 2048: return 50