-
Notifications
You must be signed in to change notification settings - Fork 4
/
exec.go
129 lines (119 loc) · 3.75 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/robert-nix/ansihtml"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/lib/exec"
"projectforge.dev/projectforge/app/lib/websocket"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/views/vexec"
)
func ExecList(rc *fasthttp.RequestCtx) {
controller.Act("exec.list", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
ps.Title = "Processes"
ps.Data = as.Services.Exec.Execs
return controller.Render(rc, as, &vexec.List{Execs: as.Services.Exec.Execs}, ps, "exec")
})
}
func ExecForm(rc *fasthttp.RequestCtx) {
controller.Act("exec.form", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
x := &exec.Exec{}
ps.Title = "New Process"
ps.Data = x
return controller.Render(rc, as, &vexec.Form{Exec: x}, ps, "exec", "New Process")
})
}
func ExecNew(rc *fasthttp.RequestCtx) {
controller.Act("exec.new", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
frm, err := cutil.ParseForm(rc)
if err != nil {
return "", err
}
cmd := strings.TrimSpace(frm.GetStringOpt("cmd"))
if cmd == "" {
return "", errors.New("must provide non-empty [cmd]")
}
key := strings.TrimSpace(frm.GetStringOpt("key"))
if key == "" {
key, _ = util.StringSplit(cmd, ' ', true)
}
path := strings.TrimSpace(frm.GetStringOpt("path"))
if path == "" {
path = "."
}
env := util.StringSplitAndTrim(strings.TrimSpace(frm.GetStringOpt("env")), ",")
x, err := as.Services.Exec.NewExec(key, cmd, path, env...)
if err != nil {
return "", err
}
w := func(key string, b []byte) error {
m := util.ValueMap{"msg": string(b), "html": string(ansihtml.ConvertToHTML(b))}
msg := &websocket.Message{Channel: key, Cmd: "output", Param: util.ToJSONBytes(m, true)}
return as.Services.Socket.WriteChannel(msg, ps.Logger)
}
err = x.Start(ps.Context, ps.Logger, w)
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "started process", x.WebPath(), rc, ps)
})
}
func ExecDetail(rc *fasthttp.RequestCtx) {
controller.Act("exec.detail", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
ex, err := getExecRC(rc, as)
if err != nil {
return "", err
}
ps.Title = ex.String()
ps.Data = ex
return controller.Render(rc, as, &vexec.Detail{Exec: ex}, ps, "exec", ex.String())
})
}
func ExecSocket(rc *fasthttp.RequestCtx) {
controller.Act("exec.socket", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
ex, err := getExecRC(rc, as)
if err != nil {
return "", err
}
err = as.Services.Socket.Upgrade(rc, ex.String(), ps.Profile, ps.Logger)
if err != nil {
ps.Logger.Warn("unable to upgrade connection to websocket")
return "", err
}
return "", nil
})
}
func ExecKill(rc *fasthttp.RequestCtx) {
controller.Act("exec.kill", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
proc, err := getExecRC(rc, as)
if err != nil {
return "", err
}
err = proc.Kill()
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, fmt.Sprintf("Killed process [%s]", proc.String()), "/admin/exec", rc, ps)
})
}
func getExecRC(rc *fasthttp.RequestCtx, as *app.State) (*exec.Exec, error) {
key, err := cutil.RCRequiredString(rc, "key", false)
if err != nil {
return nil, err
}
idx, err := cutil.RCRequiredInt(rc, "idx")
if err != nil {
return nil, err
}
proc := as.Services.Exec.Execs.Get(key, idx)
if proc == nil {
return nil, errors.Errorf("no process found with key [%s] and index [%d]", key, idx)
}
return proc, nil
}