2 Commits

Author SHA1 Message Date
zero@thinky b88245e7d9 feat(backend/jobs): add agent_id parameter
ci-agent / build (push) Failing after 5m27s
2026-04-05 04:17:55 +03:00
zero@thinky fd01eecfcc feat!(backend): unify script run and ad-hoc job run 2026-04-05 04:17:43 +03:00
5 changed files with 29 additions and 24 deletions
+1 -2
View File
@@ -105,8 +105,7 @@ 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 submits it to the agent", "description": "Loads a script from storage, resolves interpreter command, and executes on the specified agent",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@@ -1677,7 +1677,7 @@ const docTemplate = `{
"required": true "required": true
}, },
{ {
"description": "Agent ID and optional stdin", "description": "Agent token 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.AddJobOut" "$ref": "#/definitions/internal_handlers.JobResult"
} }
}, },
"400": { "400": {
+3 -3
View File
@@ -1646,7 +1646,7 @@
"Bearer": [] "Bearer": []
} }
], ],
"description": "Loads a script from storage, resolves interpreter command, and submits it to the agent", "description": "Loads a script from storage, resolves interpreter command, and executes on the specified agent",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@@ -1666,7 +1666,7 @@
"required": true "required": true
}, },
{ {
"description": "Agent ID and optional stdin", "description": "Agent token 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.AddJobOut" "$ref": "#/definitions/internal_handlers.JobResult"
} }
}, },
"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
submits it to the agent executes on the specified 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 ID and optional stdin - description: Agent token 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.AddJobOut' $ref: '#/definitions/internal_handlers.JobResult'
"400": "400":
description: Bad Request description: Bad Request
schema: schema:
+17 -11
View File
@@ -18,12 +18,11 @@ import (
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, whereami string) *ScriptHandlersGroup { func NewScriptHandlersGroup(svc *service.ScriptService, cmder *commander.Commander) *ScriptHandlersGroup {
return &ScriptHandlersGroup{svc: svc, cmder: cmder, whereami: whereami} return &ScriptHandlersGroup{svc: svc, cmder: cmder}
} }
// GetTree returns the script directory tree. // GetTree returns the script directory tree.
@@ -193,13 +192,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 submits it to the agent // @Description Loads a script from storage, resolves interpreter command, and executes on the specified 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 ID and optional stdin" // @Param body body RunStoredScriptIn true "Agent token and optional stdin"
// @Success 201 {object} AddJobOut // @Success 201 {object} JobResult
// @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
@@ -249,12 +248,19 @@ func (sh *ScriptHandlersGroup) RunScriptByID(c *gin.Context) {
return return
} }
waitURL := fmt.Sprintf("%s/api/v1/jobs/%d/wait", sh.whereami, jid) job, err := agent.WaitJob(jid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("job execution failed: %v", err)})
return
}
c.JSON(http.StatusCreated, AddJobOut{ c.JSON(http.StatusCreated, JobResult{
ID: jid, ID: job.ID,
Command: command, Command: job.Command,
WaitURL: waitURL, Stdin: job.Stdin,
Stdout: job.Stdout,
Stderr: job.Stderr,
Status: job.Status,
}) })
} }