2 Commits

Author SHA1 Message Date
zero@thinky b1e6775f1b feat(backend/jobs): add agent_id parameter
ci-agent / build (push) Failing after 5m40s
2026-04-05 04:46:50 +03:00
zero@thinky 8226429b5b feat!(backend): unify script run and ad-hoc job run 2026-04-05 04:46:50 +03:00
5 changed files with 24 additions and 29 deletions
+2 -1
View File
@@ -105,7 +105,8 @@ func main() {
// Initialize script management service and handlers // Initialize script management service and handlers
scriptManageSvc := service.NewScriptService(h.Repo) scriptManageSvc := service.NewScriptService(h.Repo)
scriptManageHandlers := handlers.NewScriptHandlersGroup(scriptManageSvc, cmdr) scriptManageHandlers := handlers.NewScriptHandlersGroup(scriptManageSvc, cmdr,
os.Getenv("WHEREAMI"))
agents := handlers.NewAgentsGroup(h, coll) agents := handlers.NewAgentsGroup(h, coll)
auth := handlers.AuthGroup{Handlers: h} auth := handlers.AuthGroup{Handlers: h}
+3 -3
View File
@@ -1657,7 +1657,7 @@ const docTemplate = `{
"Bearer": [] "Bearer": []
} }
], ],
"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",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@@ -1677,7 +1677,7 @@ const docTemplate = `{
"required": true "required": true
}, },
{ {
"description": "Agent token and optional stdin", "description": "Agent ID and optional stdin",
"name": "body", "name": "body",
"in": "body", "in": "body",
"required": true, "required": true,
@@ -1690,7 +1690,7 @@ const docTemplate = `{
"201": { "201": {
"description": "Created", "description": "Created",
"schema": { "schema": {
"$ref": "#/definitions/internal_handlers.JobResult" "$ref": "#/definitions/internal_handlers.AddJobOut"
} }
}, },
"400": { "400": {
+3 -3
View File
@@ -1646,7 +1646,7 @@
"Bearer": [] "Bearer": []
} }
], ],
"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",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@@ -1666,7 +1666,7 @@
"required": true "required": true
}, },
{ {
"description": "Agent token and optional stdin", "description": "Agent ID and optional stdin",
"name": "body", "name": "body",
"in": "body", "in": "body",
"required": true, "required": true,
@@ -1679,7 +1679,7 @@
"201": { "201": {
"description": "Created", "description": "Created",
"schema": { "schema": {
"$ref": "#/definitions/internal_handlers.JobResult" "$ref": "#/definitions/internal_handlers.AddJobOut"
} }
}, },
"400": { "400": {
+3 -3
View File
@@ -1576,14 +1576,14 @@ paths:
consumes: consumes:
- application/json - application/json
description: Loads a script from storage, resolves interpreter command, and description: Loads a script from storage, resolves interpreter command, and
executes on the specified agent submits it to the agent
parameters: parameters:
- description: Script ID - description: Script ID
in: path in: path
name: id name: id
required: true required: true
type: integer type: integer
- description: Agent token and optional stdin - description: Agent ID and optional stdin
in: body in: body
name: body name: body
required: true required: true
@@ -1595,7 +1595,7 @@ paths:
"201": "201":
description: Created description: Created
schema: schema:
$ref: '#/definitions/internal_handlers.JobResult' $ref: '#/definitions/internal_handlers.AddJobOut'
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
+13 -19
View File
@@ -16,13 +16,14 @@ import (
// ScriptHandlersGroup handles script management routes. // ScriptHandlersGroup handles script management routes.
type ScriptHandlersGroup struct { type ScriptHandlersGroup struct {
svc *service.ScriptService svc *service.ScriptService
cmder *commander.Commander cmder *commander.Commander
whereami string
} }
// NewScriptHandlersGroup creates a new ScriptHandlersGroup. // NewScriptHandlersGroup creates a new ScriptHandlersGroup.
func NewScriptHandlersGroup(svc *service.ScriptService, cmder *commander.Commander) *ScriptHandlersGroup { func NewScriptHandlersGroup(svc *service.ScriptService, cmder *commander.Commander, whereami string) *ScriptHandlersGroup {
return &ScriptHandlersGroup{svc: svc, cmder: cmder} return &ScriptHandlersGroup{svc: svc, cmder: cmder, whereami: whereami}
} }
// GetTree returns the script directory tree. // 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. // RunScriptByID executes a stored script on a target agent.
// @Summary Run script by ID // @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 // @Tags scripts
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param id path int true "Script ID" // @Param id path int true "Script ID"
// @Param body body RunStoredScriptIn true "Agent token and optional stdin" // @Param body body RunStoredScriptIn true "Agent ID and optional stdin"
// @Success 201 {object} JobResult // @Success 201 {object} AddJobOut
// @Failure 400 {object} map[string]string // @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string // @Failure 404 {object} map[string]string
// @Failure 500 {object} map[string]string // @Failure 500 {object} map[string]string
@@ -248,19 +249,12 @@ func (sh *ScriptHandlersGroup) RunScriptByID(c *gin.Context) {
return return
} }
job, err := agent.WaitJob(jid) waitURL := fmt.Sprintf("%s/api/v1/jobs/%d/wait", sh.whereami, jid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("job execution failed: %v", err)})
return
}
c.JSON(http.StatusCreated, JobResult{ c.JSON(http.StatusCreated, AddJobOut{
ID: job.ID, ID: jid,
Command: job.Command, Command: command,
Stdin: job.Stdin, WaitURL: waitURL,
Stdout: job.Stdout,
Stderr: job.Stderr,
Status: job.Status,
}) })
} }