add option to set owner when creating file/folder
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mathieu Broillet 2023-06-21 09:33:31 +02:00
parent 30cbc33217
commit 423bc70634
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32
2 changed files with 9 additions and 4 deletions

View File

@ -155,11 +155,11 @@ def run_steps(lxc: LXC):
case "script": case "script":
lxc_utils.run_script_step_parser(lxc, step) lxc_utils.run_script_step_parser(lxc, step)
case "file_create": 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": case "file_copy":
lxc.pve.copy_file_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"]) lxc.pve.copy_file_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"])
case "folder": 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": case "folder_copy":
lxc.pve.copy_folder_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"]) lxc.pve.copy_folder_to_lxc(lxc, get_path(lxc, step["path"]), step["destination"])
case "command": case "command":

View File

@ -104,7 +104,7 @@ class LinuxMachine():
def list_dir(self, directory: str or Path): def list_dir(self, directory: str or Path):
pass 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""" """Create file"""
if isinstance(file, Path): if isinstance(file, Path):
file = str(file.as_posix()) file = str(file.as_posix())
@ -112,8 +112,11 @@ class LinuxMachine():
self.run_command(f"touch {file}", return_status_code=True) self.run_command(f"touch {file}", return_status_code=True)
if permission != 644: if permission != 644:
self.run_command(f"chmod {permission} {file}", return_status_code=True) 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""" """Create directory"""
if isinstance(directory, Path): if isinstance(directory, Path):
directory = str(directory.as_posix()) 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) self.run_command(f"mkdir -p {directory}", return_status_code=True, use_ssh=use_ssh)
if permission != 755: if permission != 755:
self.run_command(f"chmod -R {permission} {directory}", return_status_code=True, use_ssh=use_ssh) 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): def delete_file(self, file: str or Path, use_ssh: bool = True):
"""Delete file""" """Delete file"""