add bconsole on backup management dashboard with limited commands
This commit is contained in:
@@ -116,3 +116,29 @@ func (h *Handler) CreateJob(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusCreated, job)
|
||||
}
|
||||
|
||||
// ExecuteBconsoleCommand executes a bconsole command
|
||||
func (h *Handler) ExecuteBconsoleCommand(c *gin.Context) {
|
||||
var req struct {
|
||||
Command string `json:"command" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "command is required"})
|
||||
return
|
||||
}
|
||||
|
||||
output, err := h.service.ExecuteBconsoleCommand(c.Request.Context(), req.Command)
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to execute bconsole command", "error", err, "command", req.Command)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "failed to execute command",
|
||||
"output": output,
|
||||
"details": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"output": output,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -451,6 +451,42 @@ func (s *Service) getClientNameFromJob(ctx context.Context, jobID int) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// ExecuteBconsoleCommand executes a bconsole command and returns the output
|
||||
func (s *Service) ExecuteBconsoleCommand(ctx context.Context, command string) (string, error) {
|
||||
// Sanitize command
|
||||
command = strings.TrimSpace(command)
|
||||
if command == "" {
|
||||
return "", fmt.Errorf("command cannot be empty")
|
||||
}
|
||||
|
||||
// Remove any existing quit commands from user input
|
||||
command = strings.TrimSuffix(strings.ToLower(command), "quit")
|
||||
command = strings.TrimSpace(command)
|
||||
|
||||
// Ensure command ends with quit
|
||||
commandWithQuit := command + "\nquit"
|
||||
|
||||
// Use printf instead of echo -e for better compatibility
|
||||
// Escape single quotes in command
|
||||
escapedCommand := strings.ReplaceAll(commandWithQuit, "'", "'\"'\"'")
|
||||
|
||||
// Execute bconsole command using printf to avoid echo -e issues
|
||||
cmd := exec.CommandContext(ctx, "sh", "-c", fmt.Sprintf("printf '%%s\\n' '%s' | bconsole", escapedCommand))
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
// bconsole may return non-zero exit code even on success, so check output
|
||||
outputStr := string(output)
|
||||
if len(outputStr) > 0 {
|
||||
// If there's output, return it even if there's an error
|
||||
return outputStr, nil
|
||||
}
|
||||
return outputStr, fmt.Errorf("bconsole error: %w", err)
|
||||
}
|
||||
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
// upsertJob inserts or updates a job in the database
|
||||
func (s *Service) upsertJob(ctx context.Context, job Job) error {
|
||||
query := `
|
||||
|
||||
Reference in New Issue
Block a user