Merge "Add exclude for permissions in kolla config.json file"

This commit is contained in:
Zuul 2024-08-21 17:18:55 +00:00 committed by Gerrit Code Review
commit 8f6bc332bf
3 changed files with 24 additions and 3 deletions

View File

@ -65,6 +65,9 @@ The `kolla_set_configs`_ script understands the following attributes:
Must be passed in the numeric octal form.
* **recurse**: whether to apply the change recursively over the target
directory. Boolean, defaults to ``false``.
* **exclude**: array of names of the directories or files to be excluded when
``recurse`` is set to ``true``. Supports Python regular expressions.
Defaults to empty array.
Here is an example configuration file:
@ -85,7 +88,8 @@ Here is an example configuration file:
{
"path": "/var/log/kolla/trove",
"owner": "trove:trove",
"recurse": true
"recurse": true,
"exclude": ["/var/log/^snapshot.*"]
}
]
}

View File

@ -19,6 +19,7 @@ import json
import logging
import os
import pwd
import re
import shutil
import stat
import sys
@ -334,6 +335,7 @@ def handle_permissions(config):
owner = permission.get('owner')
recurse = permission.get('recurse', False)
perm = permission.get('perm')
exclude = permission.get('exclude', [])
desired_user, desired_group = user_group(owner)
uid = pwd.getpwnam(desired_user).pw_uid
@ -373,14 +375,24 @@ def handle_permissions(config):
LOG.exception('Failed to set permission of %s to %s',
path, perm)
def handle_exclusion(root, path_suffix):
full_path = os.path.join(root, path_suffix)
LOG.debug("Checking for exclusion: %s" % full_path)
if exclude:
for exclude_ in exclude:
if not re.search(exclude_, full_path):
set_perms(full_path, uid, gid, perm)
else:
set_perms(full_path, uid, gid, perm)
for dest in glob.glob(path):
set_perms(dest, uid, gid, perm)
if recurse and os.path.isdir(dest):
for root, dirs, files in os.walk(dest):
for dir_ in dirs:
set_perms(os.path.join(root, dir_), uid, gid, perm)
handle_exclusion(root, dir_)
for file_ in files:
set_perms(os.path.join(root, file_), uid, gid, perm)
handle_exclusion(root, file_)
def execute_config_strategy(config):

View File

@ -0,0 +1,5 @@
---
features:
- |
Adds option to exclude files and directories when setting permissions
defined in kolla config.json using regular expression.