handleRunJob handles POST requests to run a job
(w http.ResponseWriter, r *http.Request)
| 1742 | |
| 1743 | // handleRunJob handles POST requests to run a job |
| 1744 | func handleRunJob(w http.ResponseWriter, r *http.Request) { |
| 1745 | if r.Method != "POST" { |
| 1746 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 1747 | return |
| 1748 | } |
| 1749 | |
| 1750 | var request struct { |
| 1751 | Command string `json:"command"` |
| 1752 | CrontabFilename string `json:"crontab_filename"` |
| 1753 | Key string `json:"key"` |
| 1754 | WithMonitoring bool `json:"with_monitoring"` |
| 1755 | } |
| 1756 | |
| 1757 | if err := json.NewDecoder(r.Body).Decode(&request); err != nil { |
| 1758 | http.Error(w, "Invalid request body", http.StatusBadRequest) |
| 1759 | return |
| 1760 | } |
| 1761 | |
| 1762 | if request.Command == "" { |
| 1763 | http.Error(w, "Command parameter is required", http.StatusBadRequest) |
| 1764 | return |
| 1765 | } |
| 1766 | |
| 1767 | shell := "/bin/sh" // Default shell |
| 1768 | var monitorCode string // For monitoring |
| 1769 | |
| 1770 | // If crontab filename and key are provided, use them to find the specific job |
| 1771 | if request.CrontabFilename != "" && request.Key != "" { |
| 1772 | crontab, err := lib.GetCrontab(request.CrontabFilename) |
| 1773 | if err != nil { |
| 1774 | http.Error(w, "Crontab not found", http.StatusForbidden) |
| 1775 | return |
| 1776 | } else { |
| 1777 | // Use the shell from this crontab |
| 1778 | if crontab.Shell != "" { |
| 1779 | shell = crontab.Shell |
| 1780 | } |
| 1781 | |
| 1782 | // Find the specific job by key |
| 1783 | var foundLine *lib.Line |
| 1784 | for _, line := range crontab.Lines { |
| 1785 | if line.IsJob && line.Key(crontab.CanonicalName()) == request.Key { |
| 1786 | foundLine = line |
| 1787 | break |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | if foundLine != nil { |
| 1792 | // Validate that the command matches what's in the crontab |
| 1793 | if foundLine.CommandToRun != request.Command && isSafeModeEnabled { |
| 1794 | http.Error(w, "Command does not match the job in the crontab", http.StatusForbidden) |
| 1795 | return |
| 1796 | } |
| 1797 | // Get monitor code if monitoring is requested |
| 1798 | if request.WithMonitoring && foundLine.Code != "" { |
| 1799 | monitorCode = foundLine.Code |
| 1800 | } |
| 1801 | } else if isSafeModeEnabled { |
nothing calls this directly
no test coverage detected