From 423bc70634346c6433ecd910c9b1f34f3adac8f4 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Wed, 21 Jun 2023 09:33:31 +0200 Subject: [PATCH] add option to set owner when creating file/folder --- src/lxc/creation_utils.py | 4 ++-- src/utils/machine.py | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lxc/creation_utils.py b/src/lxc/creation_utils.py index 73f2f41..a7b5e92 100644 --- a/src/lxc/creation_utils.py +++ b/src/lxc/creation_utils.py @@ -155,11 +155,11 @@ def run_steps(lxc: LXC): case "script": lxc_utils.run_script_step_parser(lxc, step) case "file_create": - lxc.create_file(step["path"], step.get("permission", 644)) + lxc.create_file(step["path"], step.get("permission", 644), step.get("owner", "root")) case "file_copy": lxc.pve.copy_file_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"]) case "folder": - lxc.create_directory(step["path"], step.get("permission", 755)) + lxc.create_directory(step["path"], step.get("permission", 755), step.get("owner", "root")) case "folder_copy": lxc.pve.copy_folder_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"]) case "command": diff --git a/src/utils/machine.py b/src/utils/machine.py index 415f951..5a97885 100644 --- a/src/utils/machine.py +++ b/src/utils/machine.py @@ -104,7 +104,7 @@ class LinuxMachine(): def list_dir(self, directory: str or Path): pass - def create_file(self, file: str or Path, permission: int = 644): + def create_file(self, file: str or Path, permission: int = 644, owner: str = "root"): """Create file""" if isinstance(file, Path): file = str(file.as_posix()) @@ -112,8 +112,11 @@ class LinuxMachine(): self.run_command(f"touch {file}", return_status_code=True) if permission != 644: self.run_command(f"chmod {permission} {file}", return_status_code=True) + if owner != "root": + self.run_command(f"chown {owner} {file}", return_status_code=True) - def create_directory(self, directory: str or Path, permission: int = 755, use_ssh: bool = True): + def create_directory(self, directory: str or Path, permission: int = 755, owner: str = "root", + use_ssh: bool = True): """Create directory""" if isinstance(directory, Path): directory = str(directory.as_posix()) @@ -121,6 +124,8 @@ class LinuxMachine(): self.run_command(f"mkdir -p {directory}", return_status_code=True, use_ssh=use_ssh) if permission != 755: self.run_command(f"chmod -R {permission} {directory}", return_status_code=True, use_ssh=use_ssh) + if owner != "root": + self.run_command(f"chown -R {owner} {directory}", return_status_code=True) def delete_file(self, file: str or Path, use_ssh: bool = True): """Delete file"""