Close HOT template files after read

Previously get_base_nest_hot_dict() used by tacker-conductor
opens and reads HOT template files but does not close them.
As a result, tacker-conductor will reach the limit of open
file descriptor while it is running long time.

This patch fixes to close HOT template files after read
them.

This patch makes some clean-ups too.
* use existing method to make path construction and make it easy to
  understand.
* remove _get_base_hot_dict() because it was replaced to
  get_base_nest_hot_dict() and is not used anywhere now.
  And it is considerd that it will not be used any more.
* remove meaningless array to tupple translation.
* same bug fix and clean-ups are applied in the same file.

Closes-Bug: #1913978

Change-Id: I6ffdadc196cf2ddcec3625c6bd99b5133e37af4b
This commit is contained in:
Itsuro Oda 2021-02-01 13:39:13 +09:00
parent a190526e47
commit 54aca142b0
1 changed files with 21 additions and 47 deletions

View File

@ -14,7 +14,6 @@
# under the License.
import copy
import io
import os
import yaml
@ -64,10 +63,8 @@ def _get_vim(context, vim_connection_info):
def _get_vnfd_dict(context, vnfd_id, flavour_id):
vnf_package_id = _get_vnf_package_id(context, vnfd_id)
vnf_package_base_path = cfg.CONF.vnf_package.vnf_package_csar_path
vnf_package_csar_path = vnf_package_base_path + '/' + vnf_package_id
vnfd_dict = _get_flavour_based_vnfd(vnf_package_csar_path, flavour_id)
vnfd_dict = _get_flavour_based_vnfd(
_get_vnf_package_path(context, vnfd_id), flavour_id)
# Remove requirements from substitution mapping
vnfd_dict.get('topology_template').get(
@ -356,7 +353,7 @@ def _create_grant_request(vnfd_dict, package_uuid):
return vnf_sw_image
def _get_image_path(artifact_image_path, package_uuid):
vnf_package_path = cfg.CONF.vnf_package.vnf_package_csar_path
vnf_package_path = CONF.vnf_package.vnf_package_csar_path
artifact_image_path = os.path.join(
vnf_package_path, package_uuid,
artifact_image_path.split('../')[-1])
@ -412,14 +409,14 @@ def _make_final_vnf_dict(vnfd_dict, id, name, param_values, vnf_dict=None):
def _get_flavour_based_vnfd(csar_path, flavour_id):
ext = [".yaml", ".yml"]
ext = (".yaml", ".yml")
file_path_and_data = {}
imp_list = []
for item in os.listdir(csar_path):
src_path = os.path.join(csar_path, item)
if os.path.isdir(src_path):
for file in os.listdir(src_path):
if file.endswith(tuple(ext)):
if file.endswith(ext):
source_file_path = os.path.join(src_path, file)
with open(source_file_path) as file_obj:
data = yaml.safe_load(file_obj)
@ -436,8 +433,9 @@ def _get_flavour_based_vnfd(csar_path, flavour_id):
return data
elif src_path.endswith(tuple(ext)):
file_data = yaml.safe_load(io.open(src_path))
elif src_path.endswith(ext):
with open(src_path) as file_obj:
file_data = yaml.safe_load(file_obj)
substitution_map = file_data.get(
'topology_template', {}).get('substitution_mappings', {})
if substitution_map.get(
@ -1040,53 +1038,29 @@ def _convert_desired_capacity(inst_level_id, vnfd_dict, vdu):
def _get_vnf_package_path(context, vnfd_id):
vnf_package_id = _get_vnf_package_id(context, vnfd_id)
vnf_package_base_path = cfg.CONF.vnf_package.vnf_package_csar_path
vnf_package_path = vnf_package_base_path + '/' + vnf_package_id
return vnf_package_path
def _get_base_hot_dict(context, vnfd_id):
vnf_package_id = _get_vnf_package_id(context, vnfd_id)
vnf_package_base_path = cfg.CONF.vnf_package.vnf_package_csar_path
vnf_package_csar_path = vnf_package_base_path + '/' + vnf_package_id
base_hot_dir = 'BaseHOT'
ext = [".yaml", ".yml"]
base_hot_path = vnf_package_csar_path + '/' + base_hot_dir
base_hot_dict = None
if os.path.exists(base_hot_path):
for file in os.listdir(base_hot_path):
if file.endswith(tuple(ext)):
source_file_path = os.path.join(base_hot_path, file)
base_hot_dict = yaml.safe_load(open(source_file_path))
LOG.debug("Loaded base hot: %s", base_hot_dict)
return base_hot_dict
return os.path.join(CONF.vnf_package.vnf_package_csar_path,
_get_vnf_package_id(context, vnfd_id))
def get_base_nest_hot_dict(context, flavour_id, vnfd_id):
vnf_package_id = _get_vnf_package_id(context, vnfd_id)
vnf_package_base_path = cfg.CONF.vnf_package.vnf_package_csar_path
vnf_package_csar_path = vnf_package_base_path + '/' + vnf_package_id
base_hot_dir = 'BaseHOT'
ext = [".yaml", ".yml"]
base_hot_path = os.path.join(_get_vnf_package_path(context, vnfd_id),
'BaseHOT', flavour_id)
nested_hot_path = os.path.join(base_hot_path, 'nested')
ext = (".yaml", ".yml")
base_hot_path = vnf_package_csar_path + '/' + \
base_hot_dir + '/' + flavour_id
base_hot_dict = None
nested_hot_path = base_hot_path + '/nested'
nested_hot_dict = {}
if os.path.exists(base_hot_path):
for file in os.listdir(base_hot_path):
if file.endswith(tuple(ext)):
source_file_path = os.path.join(base_hot_path, file)
base_hot_dict = yaml.safe_load(open(source_file_path))
if file.endswith(ext):
with open(os.path.join(base_hot_path, file)) as file_obj:
base_hot_dict = yaml.safe_load(file_obj)
if os.path.exists(nested_hot_path):
for file in os.listdir(nested_hot_path):
if file.endswith(tuple(ext)):
source_file_path = os.path.join(nested_hot_path, file)
nested_hot = yaml.safe_load(open(source_file_path))
nested_hot_dict[file] = nested_hot
if file.endswith(ext):
with open(os.path.join(nested_hot_path, file)) as file_obj:
nested_hot = yaml.safe_load(file_obj)
nested_hot_dict[file] = nested_hot
LOG.debug("Loaded base hot: %s", base_hot_dict)
LOG.debug("Loaded nested_hot_dict: %s", nested_hot_dict)
return base_hot_dict, nested_hot_dict