From b6bda2fe154d067084a99733f42663252ff9b399 Mon Sep 17 00:00:00 2001 From: Dan Radez Date: Mon, 19 Sep 2016 15:06:43 -0400 Subject: Handling file loads and tmp dirs differently Change-Id: I602279b30b035cfc667e4ee9b83905a638440abb Signed-off-by: Dan Radez --- ci/deploy.sh | 3 ++- lib/parse-functions.sh | 2 +- lib/python/apex/common/utils.py | 2 +- lib/python/apex/deploy_settings.py | 2 +- lib/python/apex/inventory.py | 2 +- lib/python/apex/network_environment.py | 2 +- lib/python/apex/network_settings.py | 2 +- lib/python/apex_python_utils.py | 11 +++++++++-- lib/undercloud-functions.sh | 4 ++-- lib/virtual-setup-functions.sh | 4 ++-- tests/test_apex_deploy_settings.py | 16 ++++++++++------ tests/test_apex_python_utils_py.py | 5 +++++ 12 files changed, 36 insertions(+), 19 deletions(-) diff --git a/ci/deploy.sh b/ci/deploy.sh index 0a2eec2e..b3a518fc 100755 --- a/ci/deploy.sh +++ b/ci/deploy.sh @@ -36,6 +36,7 @@ declare -A deploy_options_array declare -a performance_options declare -A NET_MAP +APEX_TMP_DIR=$(python3 -c "import tempfile; print(tempfile.mkdtemp())") SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error) DEPLOY_OPTIONS="" CONFIG=${CONFIG:-'/var/opt/opnfv'} @@ -185,7 +186,7 @@ parse_cmdline() { echo -e "${red}ERROR: You should not specify an inventory file with virtual deployments${reset}" exit 1 else - INVENTORY_FILE='/tmp/inventory-virt.yaml' + INVENTORY_FILE="$APEX_TMP_DIR/inventory-virt.yaml" fi elif [[ -z "$INVENTORY_FILE" ]]; then echo -e "${red}ERROR: You must specify an inventory file for baremetal deployments! Exiting...${reset}" diff --git a/lib/parse-functions.sh b/lib/parse-functions.sh index 40cdb826..ef388f44 100755 --- a/lib/parse-functions.sh +++ b/lib/parse-functions.sh @@ -65,7 +65,7 @@ parse_network_settings() { done fi - if output=$(python3 -B $LIB/python/apex_python_utils.py parse-net-settings -s $NETSETS $net_isolation_arg -e $CONFIG/network-environment.yaml $parse_ext); then + if output=$(python3 -B $LIB/python/apex_python_utils.py parse-net-settings -s $NETSETS $net_isolation_arg -td $APEX_TMP_DIR -e $CONFIG/network-environment.yaml $parse_ext); then echo -e "${blue}${output}${reset}" eval "$output" else diff --git a/lib/python/apex/common/utils.py b/lib/python/apex/common/utils.py index fe34096d..d623638c 100644 --- a/lib/python/apex/common/utils.py +++ b/lib/python/apex/common/utils.py @@ -19,5 +19,5 @@ def str2bool(var): def parse_yaml(yaml_file): with open(yaml_file) as f: - parsed_dict = yaml.load(f) + parsed_dict = yaml.safe_load(f) return parsed_dict diff --git a/lib/python/apex/deploy_settings.py b/lib/python/apex/deploy_settings.py index b70efdac..c27eecf9 100644 --- a/lib/python/apex/deploy_settings.py +++ b/lib/python/apex/deploy_settings.py @@ -40,7 +40,7 @@ class DeploySettings(dict): init_dict = {} if type(filename) is str: with open(filename, 'r') as deploy_settings_file: - init_dict = yaml.load(deploy_settings_file) + init_dict = yaml.safe_load(deploy_settings_file) else: # assume input is a dict to build from init_dict = filename diff --git a/lib/python/apex/inventory.py b/lib/python/apex/inventory.py index f4a33b28..aa219680 100644 --- a/lib/python/apex/inventory.py +++ b/lib/python/apex/inventory.py @@ -24,7 +24,7 @@ class Inventory(dict): init_dict = {} if type(source) is str: with open(source, 'r') as network_settings_file: - yaml_dict = yaml.load(network_settings_file) + yaml_dict = yaml.safe_load(network_settings_file) # collapse node identifiers from the structure init_dict['nodes'] = list(map(lambda n: n[1], yaml_dict['nodes'].items())) diff --git a/lib/python/apex/network_environment.py b/lib/python/apex/network_environment.py index 175f408f..15fe873f 100644 --- a/lib/python/apex/network_environment.py +++ b/lib/python/apex/network_environment.py @@ -59,7 +59,7 @@ class NetworkEnvironment(dict): init_dict = {} if type(filename) is str: with open(filename, 'r') as net_env_fh: - init_dict = yaml.load(net_env_fh) + init_dict = yaml.safe_load(net_env_fh) super().__init__(init_dict) try: diff --git a/lib/python/apex/network_settings.py b/lib/python/apex/network_settings.py index ca91b8cf..8e39afd6 100644 --- a/lib/python/apex/network_settings.py +++ b/lib/python/apex/network_settings.py @@ -42,7 +42,7 @@ class NetworkSettings(dict): init_dict = {} if type(filename) is str: with open(filename, 'r') as network_settings_file: - init_dict = yaml.load(network_settings_file) + init_dict = yaml.safe_load(network_settings_file) else: # assume input is a dict to build from init_dict = filename diff --git a/lib/python/apex_python_utils.py b/lib/python/apex_python_utils.py index ebc49dc5..9d6110bb 100755 --- a/lib/python/apex_python_utils.py +++ b/lib/python/apex_python_utils.py @@ -43,7 +43,9 @@ def parse_net_settings(args): net_env = NetworkEnvironment(settings, args.net_env_file, args.compute_pre_config, args.controller_pre_config) - dump_yaml(dict(net_env), '/tmp/network-environment.yaml') + target = args.target_dir.split('/') + target.append('network-environment.yaml') + dump_yaml(dict(net_env), '/'.join(target)) settings.dump_bash() @@ -108,7 +110,7 @@ def build_nic_template(args): netsets = NetworkSettings(args.net_settings_file, args.network_isolation) - env = Environment(loader=FileSystemLoader(template_dir)) + env = Environment(loader=FileSystemLoader(template_dir), autoescape=True) template = env.get_template(template) # gather vlan values into a dict @@ -148,6 +150,11 @@ def get_parser(): default="network-environment.yaml", dest='net_env_file', help='path to network environment file') + net_settings.add_argument('-td', '--target-dir', + default="/tmp", + dest='target_dir', + help='directory to write the' + 'network-environment.yaml file') net_settings.add_argument('--compute-pre-config', default=False, action='store_true', diff --git a/lib/undercloud-functions.sh b/lib/undercloud-functions.sh index d2ba6bca..ccf39c02 100755 --- a/lib/undercloud-functions.sh +++ b/lib/undercloud-functions.sh @@ -122,8 +122,8 @@ function configure_undercloud { echo "Copying configuration files to Undercloud" if [[ "$net_isolation_enabled" == "TRUE" ]]; then echo -e "${blue}Network Environment set for Deployment: ${reset}" - cat /tmp/network-environment.yaml - scp ${SSH_OPTIONS[@]} /tmp/network-environment.yaml "stack@$UNDERCLOUD": + cat $APEX_TMP_DIR/network-environment.yaml + scp ${SSH_OPTIONS[@]} $APEX_TMP_DIR/network-environment.yaml "stack@$UNDERCLOUD": # check for ODL L3/ONOS if [ "${deploy_options_array['sdn_l3']}" == 'True' ]; then diff --git a/lib/virtual-setup-functions.sh b/lib/virtual-setup-functions.sh index 61dc6799..116d19b6 100755 --- a/lib/virtual-setup-functions.sh +++ b/lib/virtual-setup-functions.sh @@ -23,7 +23,7 @@ function setup_virtual_baremetal { ramsize=$(($2*1024)) fi #start by generating the opening yaml for the inventory-virt.yaml file - cat > /tmp/inventory-virt.yaml << EOF + cat > $APEX_TMP_DIR/inventory-virt.yaml << EOF nodes: EOF @@ -64,7 +64,7 @@ EOF #virsh vol-list default | grep baremetal${i} 2>&1> /dev/null || virsh vol-create-as default baremetal${i}.qcow2 41G --format qcow2 mac=$(virsh domiflist baremetal${i} | grep admin_network | awk '{ print $5 }') - cat >> /tmp/inventory-virt.yaml << EOF + cat >> $APEX_TMP_DIR/inventory-virt.yaml << EOF node${i}: mac_address: "$mac" ipmi_ip: 192.168.122.1 diff --git a/tests/test_apex_deploy_settings.py b/tests/test_apex_deploy_settings.py index 1e26b287..2af187b2 100644 --- a/tests/test_apex_deploy_settings.py +++ b/tests/test_apex_deploy_settings.py @@ -7,8 +7,9 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import io # https://docs.python.org/3/library/io.html +import io +import tempfile from apex.deploy_settings import DeploySettings from apex.deploy_settings import DeploySettingsException @@ -84,11 +85,14 @@ class TestIpUtils(object): def test__validate_settings(self): for c in test_deploy_content: - f = open('/tmp/apex_deploy_test_file', 'w') - f.write(c) - f.close() - assert_raises(DeploySettingsException, - DeploySettings, '/tmp/apex_deploy_test_file') + try: + f = tempfile.NamedTemporaryFile(mode='w') + f.write(c) + f.flush() + assert_raises(DeploySettingsException, + DeploySettings, f.name) + finally: + f.close() def test_dump_bash(self): # the performance file has the most use of the function diff --git a/tests/test_apex_python_utils_py.py b/tests/test_apex_python_utils_py.py index 237c5589..28180f06 100644 --- a/tests/test_apex_python_utils_py.py +++ b/tests/test_apex_python_utils_py.py @@ -7,7 +7,9 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import shutil import sys +import tempfile from test_apex_ip_utils import get_default_gateway_linux from apex_python_utils import main @@ -57,11 +59,14 @@ class TestCommonUtils(object): assert_equal(main(), None) def test_parse_net_settings(self): + tmp_dir = tempfile.mkdtemp() args = self.parser.parse_args(['parse-net-settings', '-s', net_sets, '--flat', + '-td', tmp_dir, '-e', net_env]) assert_equal(parse_net_settings(args), None) + shutil.rmtree(tmp_dir, ignore_errors=True) def test_parse_deploy_settings(self): args = self.parser.parse_args(['parse-deploy-settings', -- cgit 1.2.3-korg