feat!(backend): unify script run and ad-hoc job run

This commit is contained in:
2026-04-05 04:10:59 +03:00
parent add1242b97
commit 8226429b5b
7 changed files with 95 additions and 174 deletions
+13 -19
View File
@@ -16,13 +16,14 @@ import (
// ScriptHandlersGroup handles script management routes.
type ScriptHandlersGroup struct {
svc *service.ScriptService
cmder *commander.Commander
svc *service.ScriptService
cmder *commander.Commander
whereami string
}
// NewScriptHandlersGroup creates a new ScriptHandlersGroup.
func NewScriptHandlersGroup(svc *service.ScriptService, cmder *commander.Commander) *ScriptHandlersGroup {
return &ScriptHandlersGroup{svc: svc, cmder: cmder}
func NewScriptHandlersGroup(svc *service.ScriptService, cmder *commander.Commander, whereami string) *ScriptHandlersGroup {
return &ScriptHandlersGroup{svc: svc, cmder: cmder, whereami: whereami}
}
// GetTree returns the script directory tree.
@@ -192,13 +193,13 @@ func (sh *ScriptHandlersGroup) DeleteScript(c *gin.Context) {
// RunScriptByID executes a stored script on a target agent.
// @Summary Run script by ID
// @Description Loads a script from storage, resolves interpreter command, and executes on the specified agent
// @Description Loads a script from storage, resolves interpreter command, and submits it to the agent
// @Tags scripts
// @Accept json
// @Produce json
// @Param id path int true "Script ID"
// @Param body body RunStoredScriptIn true "Agent token and optional stdin"
// @Success 201 {object} RunScriptOut
// @Param body body RunStoredScriptIn true "Agent ID and optional stdin"
// @Success 201 {object} AddJobOut
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Failure 500 {object} map[string]string
@@ -248,19 +249,12 @@ func (sh *ScriptHandlersGroup) RunScriptByID(c *gin.Context) {
return
}
job, err := agent.WaitJob(jid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("job execution failed: %v", err)})
return
}
waitURL := fmt.Sprintf("%s/api/v1/jobs/%d/wait", sh.whereami, jid)
c.JSON(http.StatusCreated, RunScriptOut{
ID: job.ID,
Command: job.Command,
Stdin: job.Stdin,
Stdout: job.Stdout,
Stderr: job.Stderr,
Status: job.Status,
c.JSON(http.StatusCreated, AddJobOut{
ID: jid,
Command: command,
WaitURL: waitURL,
})
}