Handling file loads and tmp dirs differently 99/21499/6
authorDan Radez <dradez@redhat.com>
Mon, 19 Sep 2016 19:06:43 +0000 (15:06 -0400)
committerDan Radez <dradez@redhat.com>
Tue, 20 Sep 2016 18:24:17 +0000 (14:24 -0400)
Change-Id: I602279b30b035cfc667e4ee9b83905a638440abb
Signed-off-by: Dan Radez <dradez@redhat.com>
12 files changed:
ci/deploy.sh
lib/parse-functions.sh
lib/python/apex/common/utils.py
lib/python/apex/deploy_settings.py
lib/python/apex/inventory.py
lib/python/apex/network_environment.py
lib/python/apex/network_settings.py
lib/python/apex_python_utils.py
lib/undercloud-functions.sh
lib/virtual-setup-functions.sh
tests/test_apex_deploy_settings.py
tests/test_apex_python_utils_py.py

index 0a2eec2..b3a518f 100755 (executable)
@@ -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}"
index 40cdb82..ef388f4 100755 (executable)
@@ -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
index fe34096..d623638 100644 (file)
@@ -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
index b70efda..c27eecf 100644 (file)
@@ -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
index f4a33b2..aa21968 100644 (file)
@@ -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()))
index 175f408..15fe873 100644 (file)
@@ -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:
index ca91b8c..8e39afd 100644 (file)
@@ -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
index ebc49dc..9d6110b 100755 (executable)
@@ -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',
index d2ba6bc..ccf39c0 100755 (executable)
@@ -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
index 61dc679..116d19b 100755 (executable)
@@ -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
index 1e26b28..2af187b 100644 (file)
@@ -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
index 237c558..28180f0 100644 (file)
@@ -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',