Ajout detail usage commandes import/export acl
This commit is contained in:
68
vm/export_acl.py
Executable file
68
vm/export_acl.py
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
def extract_acls(vmid_min, vmid_max, output_file, remove_acl=False, append=False):
|
||||
print("Fetching ACLs...")
|
||||
|
||||
if not append:
|
||||
with open(output_file, "w") as f:
|
||||
f.write("")
|
||||
|
||||
result = subprocess.run(
|
||||
["pveum", "acl", "list", "--output-format=json"],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
acls = json.loads(result.stdout)
|
||||
|
||||
def process_acl(acl_entry):
|
||||
path = acl_entry["path"]
|
||||
role = acl_entry["roleid"]
|
||||
user = acl_entry["ugid"]
|
||||
|
||||
if "/vms/" in path:
|
||||
vmid = int(path.split("/vms/")[1])
|
||||
if vmid_min <= vmid <= vmid_max:
|
||||
print(f"Processing VMID: {vmid} (Path: {path}, Role: {role}, User: {user})")
|
||||
|
||||
with open(output_file, "a") as f:
|
||||
f.write(f"{path} {user} {role}\n")
|
||||
|
||||
if (remove_acl):
|
||||
subprocess.run(["pveum", "acl", "delete", path, "--users", users, "--roles", roles])
|
||||
|
||||
for acl_entry in acls:
|
||||
process_acl(acl_entry)
|
||||
|
||||
print(f"ACLs extracted and saved to {output_file}")
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 4:
|
||||
print(f"Usage: {sys.argv[0]} <vmid_min> <vmid_max> <output_file> [--remove] [--append]")
|
||||
print("""
|
||||
Arguments:
|
||||
vmid_min - Minimum VMID to extract ACLs from
|
||||
vmid_max - Maximum VMID to extract ACLs from
|
||||
output_file - File to save the extracted ACLs to
|
||||
|
||||
Options (order doesn't matter but must be after the arguments):
|
||||
--remove - Remove the ACLs after extracting them
|
||||
--append - Append to the output file instead of overwriting it""")
|
||||
sys.exit(1)
|
||||
|
||||
vmid_min = int(sys.argv[1])
|
||||
vmid_max = int(sys.argv[2])
|
||||
output_file = sys.argv[3]
|
||||
remove_acl = "--remove" in sys.argv
|
||||
append = "--append" in sys.argv
|
||||
|
||||
extract_acls(vmid_min, vmid_max, output_file, remove_acl, append)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user