Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions p4tenant/src/p4tenant/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,13 @@ def remove(
print_warning(f"No VMs found matching '{username}' in restsrv01.yaml")

# 2. Show config file changes and ask about updating them
config_changes = manager.remove_tenant(username, dry_run=True)
config_changes = manager.remove_tenant(username, vm_names=selected_vms, dry_run=True)
if config_changes:
# Add admin inventories that will be modified
for inv_file in BASE_DIR.glob("inventory-*.yaml"):
config_changes.append((inv_file.name, f"Remove '{vm_name}' if present"))
# Show removal for each selected VM
for selected_vm in selected_vms:
config_changes.append((inv_file.name, f"Remove '{selected_vm}' if present"))

should_update_config = not skip_config
if config_changes and not skip_config:
Expand Down Expand Up @@ -833,12 +835,13 @@ def remove(
# Update configuration files
if should_update_config:
console.print("[dim]Removing tenant from configuration files...[/dim]")
manager.remove_tenant(username, dry_run=False)
manager.remove_tenant(username, vm_names=selected_vms, dry_run=False)

# Remove from admin inventories
modified_invs = remove_from_admin_inventories(vm_name)
for inv_path in modified_invs:
print_success(f"Removed from {inv_path.name}")
# Remove from admin inventories (for each selected VM)
for selected_vm in selected_vms:
modified_invs = remove_from_admin_inventories(selected_vm)
for inv_path in modified_invs:
print_success(f"Removed from {inv_path.name}")

print_success(f"Configuration files updated for '{username}'")
else:
Expand Down
107 changes: 63 additions & 44 deletions p4tenant/src/p4tenant/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,67 +245,86 @@ def add_vm(

return changes

def remove_tenant(self, username: str, dry_run: bool = False) -> list[tuple[str, str]]:
"""Remove a tenant from all configuration files.
def remove_tenant(
self,
username: str,
vm_names: list[str] | None = None,
dry_run: bool = False,
) -> list[tuple[str, str]]:
"""Remove a tenant or specific VMs from all configuration files.

Args:
username: Username to remove
vm_names: Optional list of specific VM names to remove. If None, removes all VMs for the user.
dry_run: If True, only return changes without applying

Returns:
List of (file_path, description) for changes made
"""
vm_name = get_vm_name(username)
changes = []

# 1. Remove from restart_users
all_data = load_yaml(GROUP_VARS_ALL)
if username in all_data.get("restart_users", []):
changes.append(
(
"group_vars/all.yaml",
f"Remove '{username}' from restart_users",
# Get all VMs for this user if specific VMs not provided
if vm_names is None:
vm_names = self.get_user_vms(username)
# Fallback to default VM name if no VMs found
if not vm_names:
vm_names = [get_vm_name(username, 1)]

# Determine if we're removing ALL user's VMs
all_user_vms = self.get_user_vms(username)
removing_all_vms = set(vm_names) == set(all_user_vms) if all_user_vms else True

# 1. Remove from restart_users only if removing ALL VMs
if removing_all_vms:
all_data = load_yaml(GROUP_VARS_ALL)
if username in all_data.get("restart_users", []):
changes.append(
(
"group_vars/all.yaml",
f"Remove '{username}' from restart_users",
)
)
)
if not dry_run:
self._remove_from_restart_users(username)
if not dry_run:
self._remove_from_restart_users(username)

# 2. Remove from restsrv01 vms
srv_data = load_yaml(HOST_VARS_RESTSRV01)
if vm_name in srv_data.get("vms", []):
changes.append(
(
"host_vars/restsrv01.yaml",
f"Remove '{vm_name}' from vms list",
# 2-4. For each VM, remove from vms list, delete host_vars, remove from inventory
for vm_name in vm_names:
# 2. Remove from restsrv01 vms
srv_data = load_yaml(HOST_VARS_RESTSRV01)
if vm_name in srv_data.get("vms", []):
changes.append(
(
"host_vars/restsrv01.yaml",
f"Remove '{vm_name}' from vms list",
)
)
)
if not dry_run:
self._remove_from_restsrv01_vms(vm_name)
if not dry_run:
self._remove_from_restsrv01_vms(vm_name)

# 3. Delete host_vars file
host_vars_path = get_host_vars_path(vm_name)
if host_vars_path.exists():
changes.append(
(
f"host_vars/{vm_name}.yaml",
"[DELETE] Remove host vars file",
# 3. Delete host_vars file
host_vars_path = get_host_vars_path(vm_name)
if host_vars_path.exists():
changes.append(
(
f"host_vars/{vm_name}.yaml",
"[DELETE] Remove host vars file",
)
)
)
if not dry_run:
self._delete_host_vars(vm_name)
if not dry_run:
self._delete_host_vars(vm_name)

# 4. Remove from inventory
inv_data = load_yaml(INVENTORY_FILE)
vms_hosts = inv_data.get("vms", {}).get("hosts", {})
if vm_name in vms_hosts:
changes.append(
(
"inventory.yaml",
f"Remove '{vm_name}' from vms.hosts",
# 4. Remove from inventory
inv_data = load_yaml(INVENTORY_FILE)
vms_hosts = inv_data.get("vms", {}).get("hosts", {})
if vm_name in vms_hosts:
changes.append(
(
"inventory.yaml",
f"Remove '{vm_name}' from vms.hosts",
)
)
)
if not dry_run:
self._remove_from_inventory(vm_name)
if not dry_run:
self._remove_from_inventory(vm_name)

return changes

Expand Down