Apply proper mask on libvirt image files

PROD-28442

Change-Id: Ia70e8568c2eba57b685ad640c91e9a7935e7e866
diff --git a/_modules/virtng.py b/_modules/virtng.py
index 933d590..ae906d5 100644
--- a/_modules/virtng.py
+++ b/_modules/virtng.py
@@ -591,6 +591,9 @@
         if not diskp[0][disk_name].get('image', None):
             diskp[0][disk_name]['image'] = image
 
+    mask = kwargs.get('file_mask', os.umask(0))
+    os.umask(mask)
+
     # Create multiple disks, empty or from specified images.
     for disk in diskp:
         log.debug("Creating disk for VM [ {0} ]: {1}".format(name, disk))
@@ -631,6 +634,8 @@
                 img_dir = os.path.dirname(img_dest)
                 if not os.path.isdir(img_dir):
                     os.makedirs(img_dir)
+                    dir_mode = (0o0777 ^ mask) & 0o0777
+                    os.chmod(img_dir, dir_mode)
 
                 if 'image' in args:
                     if dry_run:
@@ -639,14 +644,11 @@
                     sfn = __salt__['cp.cache_file'](args['image'], saltenv)
                     try:
                         salt.utils.files.copyfile(sfn, img_dest)
-                        mask = os.umask(0)
-                        os.umask(mask)
-                        # Apply umask and remove exec bit
-
                         # Resizing image TCP cloud
                         cmd = 'qemu-img resize ' + img_dest  + ' ' +  str(disk_size) + 'M'
                         subprocess.call(cmd, shell=True)
 
+                        # Apply umask and remove exec bit
                         mode = (0o0777 ^ mask) & 0o0666
                         os.chmod(img_dest, mode)
 
@@ -664,14 +666,11 @@
                 else:
                     # Create empty disk
                     try:
-                        mask = os.umask(0)
-                        os.umask(mask)
-                        # Apply umask and remove exec bit
-
                         # Create empty image
                         cmd = 'qemu-img create -f ' + disk_type + ' '  + img_dest  + ' ' +  str(disk_size) + 'M'
                         subprocess.call(cmd, shell=True)
 
+                        # Apply umask and remove exec bit
                         mode = (0o0777 ^ mask) & 0o0666
                         os.chmod(img_dest, mode)