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
10 changes: 8 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ const docTemplate = `{
},
{
"type": "boolean",
"description": "Remove lab directory after destroy",
"description": "Remove containerlab lab artifacts during destroy phase",
"name": "cleanup",
"in": "query"
},
Expand Down Expand Up @@ -634,10 +634,16 @@ const docTemplate = `{
},
{
"type": "boolean",
"description": "Remove lab directory after destroy",
"description": "Remove containerlab lab artifacts after destroy",
"name": "cleanup",
"in": "query"
},
{
"type": "boolean",
"description": "Purge topology parent directory for managed lab paths (~/.clab or shared labs dir)",
"name": "purgeLabDir",
"in": "query"
},
{
"type": "boolean",
"description": "Attempt graceful shutdown",
Expand Down
10 changes: 8 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@
},
{
"type": "boolean",
"description": "Remove lab directory after destroy",
"description": "Remove containerlab lab artifacts during destroy phase",
"name": "cleanup",
"in": "query"
},
Expand Down Expand Up @@ -630,10 +630,16 @@
},
{
"type": "boolean",
"description": "Remove lab directory after destroy",
"description": "Remove containerlab lab artifacts after destroy",
"name": "cleanup",
"in": "query"
},
{
"type": "boolean",
"description": "Purge topology parent directory for managed lab paths (~/.clab or shared labs dir)",
"name": "purgeLabDir",
"in": "query"
},
{
"type": "boolean",
"description": "Attempt graceful shutdown",
Expand Down
9 changes: 7 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,15 @@ paths:
name: labName
required: true
type: string
- description: Remove lab directory after destroy
- description: Remove containerlab lab artifacts after destroy
in: query
name: cleanup
type: boolean
- description: Purge topology parent directory for managed lab paths (~/.clab
or shared labs dir)
in: query
name: purgeLabDir
type: boolean
- description: Attempt graceful shutdown
in: query
name: graceful
Expand Down Expand Up @@ -1157,7 +1162,7 @@ paths:
name: labName
required: true
type: string
- description: Remove lab directory after destroy
- description: Remove containerlab lab artifacts during destroy phase
in: query
name: cleanup
type: boolean
Expand Down
28 changes: 20 additions & 8 deletions internal/api/lab_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ func DeployLabArchiveHandler(c *gin.Context) {
// @Security BearerAuth
// @Produce json
// @Param labName path string true "Name of the lab to destroy"
// @Param cleanup query boolean false "Remove lab directory after destroy"
// @Param cleanup query boolean false "Remove containerlab lab artifacts after destroy"
// @Param purgeLabDir query boolean false "Purge topology parent directory for managed lab paths (~/.clab or shared labs dir)"
// @Param graceful query boolean false "Attempt graceful shutdown"
// @Param keepMgmtNet query boolean false "Keep the management network"
// @Param nodeFilter query string false "Destroy only specific nodes"
Expand All @@ -468,6 +469,7 @@ func DestroyLabHandler(c *gin.Context) {
}

cleanup := c.Query("cleanup") == "true"
purgeLabDir := c.Query("purgeLabDir") == "true"
graceful := c.Query("graceful") == "true"
keepMgmtNet := c.Query("keepMgmtNet") == "true"
nodeFilter := c.Query("nodeFilter")
Expand All @@ -488,6 +490,16 @@ func DestroyLabHandler(c *gin.Context) {
return
}

// For purgeLabDir checks, derive the managed base directory from the lab owner.
// This allows superusers to purge labs owned by other users while still enforcing
// path safety constraints (managed ~/.clab or shared labs directory only).
purgeBaseUser := username
if purgeLabDir {
if info, exists, err := getLabInfo(ctx, username, labName); err == nil && exists && info != nil && info.Owner != "" {
purgeBaseUser = info.Owner
}
}

var nodeFilterSlice []string
if nodeFilter != "" {
nodeFilterSlice = strings.Split(nodeFilter, ",")
Expand All @@ -498,12 +510,12 @@ func DestroyLabHandler(c *gin.Context) {
TopoPath: originalTopoPath,
Username: username,
Graceful: graceful,
Cleanup: false, // We handle cleanup separately
Cleanup: cleanup, // Keep containerlab cleanup behavior for all topology locations.
KeepMgmtNet: keepMgmtNet,
NodeFilter: nodeFilterSlice,
}

log.Infof("DestroyLab user '%s': Destroying lab '%s' (cleanup=%t)...", username, labName, cleanup)
log.Infof("DestroyLab user '%s': Destroying lab '%s' (cleanup=%t, purgeLabDir=%t)...", username, labName, cleanup, purgeLabDir)
if err := svc.Destroy(ctx, destroyOpts); err != nil {
log.Errorf("DestroyLab failed for user '%s', lab '%s': %v", username, labName, err)
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: fmt.Sprintf("Failed to destroy lab '%s': %s", labName, err.Error())})
Expand All @@ -512,16 +524,16 @@ func DestroyLabHandler(c *gin.Context) {

log.Infof("Lab '%s' destroyed successfully for user '%s'.", labName, username)

// Cleanup directory if requested
if cleanup && originalTopoPath != "" && !strings.HasPrefix(originalTopoPath, "http") {
// Purge topology parent directory if requested.
if purgeLabDir && originalTopoPath != "" && !strings.HasPrefix(originalTopoPath, "http") {
targetDir := filepath.Dir(originalTopoPath)

sharedDir := os.Getenv("CLAB_SHARED_LABS_DIR")
expectedBase := ""
if sharedDir != "" {
expectedBase = filepath.Join(sharedDir, "users", username)
expectedBase = filepath.Join(sharedDir, "users", purgeBaseUser)
} else {
usr, lookupErr := user.Lookup(username)
usr, lookupErr := user.Lookup(purgeBaseUser)
if lookupErr == nil {
expectedBase = filepath.Join(usr.HomeDir, ".clab")
}
Expand All @@ -548,7 +560,7 @@ func DestroyLabHandler(c *gin.Context) {
// @Security BearerAuth
// @Produce json
// @Param labName path string true "Name of the lab to redeploy"
// @Param cleanup query boolean false "Remove lab directory after destroy"
// @Param cleanup query boolean false "Remove containerlab lab artifacts during destroy phase"
// @Param graceful query boolean false "Attempt graceful shutdown"
// @Param keepMgmtNet query boolean false "Keep the management network"
// @Param maxWorkers query int false "Limit concurrent workers"
Expand Down
Loading