feat!(backend): unify script run and ad-hoc job run
This commit is contained in:
@@ -13,12 +13,13 @@ import (
|
||||
)
|
||||
|
||||
type ScriptHandlers struct {
|
||||
svc *service.ScriptService
|
||||
tracker *commander.ConnTracker
|
||||
svc *service.ScriptService
|
||||
tracker *commander.ConnTracker
|
||||
whereami string
|
||||
}
|
||||
|
||||
func NewScriptHandlers(svc *service.ScriptService, tracker *commander.ConnTracker) ScriptHandlers {
|
||||
return ScriptHandlers{svc: svc, tracker: tracker}
|
||||
func NewScriptHandlers(svc *service.ScriptService, tracker *commander.ConnTracker, whereami string) ScriptHandlers {
|
||||
return ScriptHandlers{svc: svc, tracker: tracker, whereami: whereami}
|
||||
}
|
||||
|
||||
type RunScriptIn struct {
|
||||
@@ -28,73 +29,52 @@ type RunScriptIn struct {
|
||||
Stdin *string `json:"stdin"`
|
||||
}
|
||||
|
||||
type RunScriptOut struct {
|
||||
ID int64 `json:"id"`
|
||||
Command []string `json:"command"`
|
||||
Stdin *string `json:"stdin"`
|
||||
Stdout string `json:"stdout"`
|
||||
Stderr string `json:"stderr"`
|
||||
Status int32 `json:"status"`
|
||||
}
|
||||
|
||||
// RunScript executes a script on a target agent.
|
||||
// RunScript submits a script as a job and returns a wait_url for the result.
|
||||
// @Summary Run a script on an agent
|
||||
// @Description Resolves interpreter argv[] and sends the full command to the agent
|
||||
// @Tags scripts
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body RunScriptIn true "Script request"
|
||||
// @Success 201 {object} RunScriptOut
|
||||
// @Success 201 {object} AddJobOut
|
||||
// @Security Bearer
|
||||
// @Router /scripts/run [post]
|
||||
func (h *ScriptHandlers) RunScript(c *gin.Context) {
|
||||
err := func() error {
|
||||
var in RunScriptIn
|
||||
if err := c.Bind(&in); err != nil {
|
||||
return err
|
||||
}
|
||||
var in RunScriptIn
|
||||
if err := c.Bind(&in); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
command, err := h.svc.ResolveCommand(
|
||||
c.Request.Context(),
|
||||
in.InterpreterID,
|
||||
in.ScriptText,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
agent, ok := h.tracker.GetAgent(in.AgentID)
|
||||
if !ok {
|
||||
c.Status(http.StatusNotFound)
|
||||
c.Error(fmt.Errorf("agent not found"))
|
||||
return
|
||||
}
|
||||
|
||||
agent, ok := h.tracker.GetAgent(in.AgentID)
|
||||
if !ok {
|
||||
c.Status(http.StatusNotFound)
|
||||
return fmt.Errorf("agent not found")
|
||||
}
|
||||
|
||||
jid, err := agent.AddJob(models.JobForInsert{
|
||||
Command: command,
|
||||
Stdin: in.Stdin,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
job, err := agent.WaitJob(jid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, RunScriptOut{
|
||||
ID: job.ID,
|
||||
Command: job.Command,
|
||||
Stdin: job.Stdin,
|
||||
Stdout: job.Stdout,
|
||||
Stderr: job.Stderr,
|
||||
Status: job.Status,
|
||||
})
|
||||
return nil
|
||||
}()
|
||||
command, err := h.svc.ResolveCommand(c.Request.Context(), in.InterpreterID, in.ScriptText)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
jid, err := agent.AddJob(models.JobForInsert{
|
||||
Command: command,
|
||||
Stdin: in.Stdin,
|
||||
})
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
waitURL := fmt.Sprintf("%s/api/v1/jobs/%d/wait", h.whereami, jid)
|
||||
|
||||
c.JSON(http.StatusCreated, AddJobOut{
|
||||
ID: jid,
|
||||
Command: command,
|
||||
WaitURL: waitURL,
|
||||
})
|
||||
}
|
||||
|
||||
// ListInterpreters returns all registered script interpreters.
|
||||
|
||||
Reference in New Issue
Block a user