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
scriptManageSvc := service.NewScriptService(h.Repo)
scriptManageHandlers := handlers.NewScriptHandlersGroup(scriptManageSvc, cmdr)
scriptManageHandlers := handlers.NewScriptHandlersGroup(scriptManageSvc, cmdr,
os.Getenv("WHEREAMI"))
agents := handlers.NewAgentsGroup(h, coll)
auth := handlers.AuthGroup{Handlers: h}
+3 -3
View File
@@ -1657,7 +1657,7 @@ const docTemplate = `{
"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": [
"application/json"
],
@@ -1677,7 +1677,7 @@ const docTemplate = `{
"required": true
},
{
"description": "Agent token and optional stdin",
"description": "Agent ID and optional stdin",
"name": "body",
"in": "body",
"required": true,
@@ -1690,7 +1690,7 @@ const docTemplate = `{
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/internal_handlers.JobResult"
"$ref": "#/definitions/internal_handlers.AddJobOut"
}
},
"400": {
+3 -3
View File
@@ -1646,7 +1646,7 @@
"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": [
"application/json"
],
@@ -1666,7 +1666,7 @@
"required": true
},
{
"description": "Agent token and optional stdin",
"description": "Agent ID and optional stdin",
"name": "body",
"in": "body",
"required": true,
@@ -1679,7 +1679,7 @@
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/internal_handlers.JobResult"
"$ref": "#/definitions/internal_handlers.AddJobOut"
}
},
"400": {
+3 -3
View File
@@ -1576,14 +1576,14 @@ paths:
consumes:
- application/json
description: Loads a script from storage, resolves interpreter command, and
executes on the specified agent
submits it to the agent
parameters:
- description: Script ID
in: path
name: id
required: true
type: integer
- description: Agent token and optional stdin
- description: Agent ID and optional stdin
in: body
name: body
required: true
@@ -1595,7 +1595,7 @@ paths:
"201":
description: Created
schema:
$ref: '#/definitions/internal_handlers.JobResult'
$ref: '#/definitions/internal_handlers.AddJobOut'
"400":
description: Bad Request
schema:
+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} JobResult
// @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, JobResult{
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,
})
}