...
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.ourworld.tf/herocode/heroagent/pkg/heroscript/playbook"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Example of using the process manager handler through heroscript
|
||||
|
||||
// Create a new playbook
|
||||
pb := playbook.New()
|
||||
|
||||
// Start a simple process
|
||||
startAction := pb.NewAction("1", "start", "process", 0, playbook.ActionTypeUnknown)
|
||||
startAction.Params.Set("name", "example_process")
|
||||
startAction.Params.Set("command", "ping -c 60 localhost")
|
||||
startAction.Params.Set("log", "true")
|
||||
|
||||
// List all processes
|
||||
listAction := pb.NewAction("2", "list", "process", 0, playbook.ActionTypeUnknown)
|
||||
listAction.Params.Set("format", "table")
|
||||
|
||||
// Get status of a specific process
|
||||
statusAction := pb.NewAction("3", "status", "process", 0, playbook.ActionTypeUnknown)
|
||||
statusAction.Params.Set("name", "example_process")
|
||||
|
||||
// Get logs of a specific process
|
||||
logsAction := pb.NewAction("4", "logs", "process", 0, playbook.ActionTypeUnknown)
|
||||
logsAction.Params.Set("name", "example_process")
|
||||
logsAction.Params.Set("lines", "10")
|
||||
|
||||
// Stop a process
|
||||
stopAction := pb.NewAction("5", "stop", "process", 0, playbook.ActionTypeUnknown)
|
||||
stopAction.Params.Set("name", "example_process")
|
||||
|
||||
// Generate the heroscript
|
||||
script := pb.HeroScript(true)
|
||||
|
||||
// Print the script
|
||||
fmt.Println("=== Example HeroScript for Process Manager ===")
|
||||
fmt.Println(script)
|
||||
fmt.Println("============================================")
|
||||
fmt.Println("To use this script:")
|
||||
fmt.Println("1. Start the process manager handler server")
|
||||
fmt.Println("2. Connect to it using: telnet localhost 8025")
|
||||
fmt.Println("3. Authenticate with: !!auth 1234")
|
||||
fmt.Println("4. Copy and paste the above script")
|
||||
fmt.Println("5. Or use individual commands like: !!process.start name:myprocess command:\"sleep 60\"")
|
||||
}
|
@@ -0,0 +1,221 @@
|
||||
package processmanagerhandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.ourworld.tf/herocode/heroagent/pkg/heroscript/handlerfactory/core"
|
||||
"git.ourworld.tf/herocode/heroagent/pkg/processmanager"
|
||||
)
|
||||
|
||||
// ProcessManagerHandler handles process manager-related actions
|
||||
type ProcessManagerHandler struct {
|
||||
core.BaseHandler
|
||||
pm *processmanager.ProcessManager
|
||||
}
|
||||
|
||||
// NewProcessManagerHandler creates a new process manager handler
|
||||
func NewProcessManagerHandler() *ProcessManagerHandler {
|
||||
return &ProcessManagerHandler{
|
||||
BaseHandler: core.BaseHandler{
|
||||
ActorName: "process",
|
||||
},
|
||||
pm: processmanager.NewProcessManager(), // Empty string as secret was removed from ProcessManager
|
||||
}
|
||||
}
|
||||
|
||||
// Start handles the process.start action
|
||||
func (h *ProcessManagerHandler) Start(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
name := params.Get("name")
|
||||
if name == "" {
|
||||
return "Error: Process name is required"
|
||||
}
|
||||
|
||||
command := params.Get("command")
|
||||
if command == "" {
|
||||
return "Error: Command is required"
|
||||
}
|
||||
|
||||
logEnabled := params.GetBoolDefault("log", true)
|
||||
deadline := params.GetIntDefault("deadline", 0)
|
||||
cron := params.Get("cron")
|
||||
jobID := params.Get("job_id")
|
||||
|
||||
err = h.pm.StartProcess(name, command, logEnabled, deadline, cron, jobID)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error starting process: %v", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Process '%s' started successfully", name)
|
||||
}
|
||||
|
||||
// Stop handles the process.stop action
|
||||
func (h *ProcessManagerHandler) Stop(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
name := params.Get("name")
|
||||
if name == "" {
|
||||
return "Error: Process name is required"
|
||||
}
|
||||
|
||||
err = h.pm.StopProcess(name)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error stopping process: %v", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Process '%s' stopped successfully", name)
|
||||
}
|
||||
|
||||
// Restart handles the process.restart action
|
||||
func (h *ProcessManagerHandler) Restart(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
name := params.Get("name")
|
||||
if name == "" {
|
||||
return "Error: Process name is required"
|
||||
}
|
||||
|
||||
err = h.pm.RestartProcess(name)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error restarting process: %v", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Process '%s' restarted successfully", name)
|
||||
}
|
||||
|
||||
// Delete handles the process.delete action
|
||||
func (h *ProcessManagerHandler) Delete(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
name := params.Get("name")
|
||||
if name == "" {
|
||||
return "Error: Process name is required"
|
||||
}
|
||||
|
||||
err = h.pm.DeleteProcess(name)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error deleting process: %v", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Process '%s' deleted successfully", name)
|
||||
}
|
||||
|
||||
// List handles the process.list action
|
||||
func (h *ProcessManagerHandler) List(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
processes := h.pm.ListProcesses()
|
||||
if len(processes) == 0 {
|
||||
return "No processes found"
|
||||
}
|
||||
|
||||
format := params.Get("format")
|
||||
if format == "" {
|
||||
format = "json"
|
||||
}
|
||||
|
||||
output, err := processmanager.FormatProcessList(processes, format)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error formatting process list: %v", err)
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
// Status handles the process.status action
|
||||
func (h *ProcessManagerHandler) Status(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
name := params.Get("name")
|
||||
if name == "" {
|
||||
return "Error: Process name is required"
|
||||
}
|
||||
|
||||
procInfo, err := h.pm.GetProcessStatus(name)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error getting process status: %v", err)
|
||||
}
|
||||
|
||||
format := params.Get("format")
|
||||
if format == "" {
|
||||
format = "json"
|
||||
}
|
||||
|
||||
output, err := processmanager.FormatProcessInfo(procInfo, format)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error formatting process status: %v", err)
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
// Logs handles the process.logs action
|
||||
func (h *ProcessManagerHandler) Logs(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
name := params.Get("name")
|
||||
if name == "" {
|
||||
return "Error: Process name is required"
|
||||
}
|
||||
|
||||
lines := params.GetIntDefault("lines", 100)
|
||||
|
||||
logs, err := h.pm.GetProcessLogs(name, lines)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error getting process logs: %v", err)
|
||||
}
|
||||
|
||||
return logs
|
||||
}
|
||||
|
||||
// SetLogsPath handles the process.set_logs_path action
|
||||
func (h *ProcessManagerHandler) SetLogsPath(script string) string {
|
||||
params, err := h.ParseParams(script)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error parsing parameters: %v", err)
|
||||
}
|
||||
|
||||
path := params.Get("path")
|
||||
if path == "" {
|
||||
return "Error: Path is required"
|
||||
}
|
||||
|
||||
h.pm.SetLogsBasePath(path)
|
||||
return fmt.Sprintf("Process logs path set to '%s'", path)
|
||||
}
|
||||
|
||||
// Help handles the process.help action
|
||||
func (h *ProcessManagerHandler) Help(script string) string {
|
||||
return `Process Manager Handler Commands:
|
||||
process.start name:<name> command:<command> [log:true|false] [deadline:<seconds>] [cron:<cron_expr>] [job_id:<id>]
|
||||
process.stop name:<name>
|
||||
process.restart name:<name>
|
||||
process.delete name:<name>
|
||||
process.list [format:json|table|text]
|
||||
process.status name:<name> [format:json|table|text]
|
||||
process.logs name:<name> [lines:<count>]
|
||||
process.set_logs_path path:<path>
|
||||
process.help`
|
||||
}
|
Reference in New Issue
Block a user