-
Notifications
You must be signed in to change notification settings - Fork 4
/
exec.go
120 lines (109 loc) · 3.61 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
// Package clib - Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
"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/util"
"projectforge.dev/projectforge/views/vexec"
)
const execIcon = "file"
func ExecList(w http.ResponseWriter, r *http.Request) {
controller.Act("exec.list", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
ps.SetTitleAndData("Processes", as.Services.Exec.Execs)
return controller.Render(r, as, &vexec.List{Execs: as.Services.Exec.Execs}, ps, "exec")
})
}
func ExecForm(w http.ResponseWriter, r *http.Request) {
controller.Act("exec.form", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
x := &exec.Exec{}
ps.SetTitleAndData("New Process", x)
ps.DefaultNavIcon = execIcon
return controller.Render(r, as, &vexec.Form{Exec: x}, ps, "exec", "New Process")
})
}
func ExecNew(w http.ResponseWriter, r *http.Request) {
controller.Act("exec.new", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
frm, err := cutil.ParseForm(r, ps.RequestBody)
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 := as.Services.Exec.NewExec(key, cmd, path, env...)
err = x.Start(as.Services.Socket.Terminal(x.String(), ps.Logger))
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "started process", x.WebPath(), ps)
})
}
func ExecDetail(w http.ResponseWriter, r *http.Request) {
controller.Act("exec.detail", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
ex, err := getExecPath(r, as)
if err != nil {
return "", err
}
ps.SetTitleAndData(ex.String(), ex)
ps.DefaultNavIcon = execIcon
return controller.Render(r, as, &vexec.Detail{Exec: ex}, ps, "exec", ex.String())
})
}
func ExecSocket(w http.ResponseWriter, r *http.Request) {
controller.Act("exec.socket", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
ex, err := getExecPath(r, as)
if err != nil {
return "", err
}
id, err := as.Services.Socket.Upgrade(ps.Context, w, r, ex.String(), ps.Profile, nil, ps.Logger)
if err != nil {
ps.Logger.Warn("unable to upgrade connection to websocket")
return "", err
}
return "", as.Services.Socket.ReadLoop(ps.Context, id, ps.Logger)
})
}
func ExecKill(w http.ResponseWriter, r *http.Request) {
controller.Act("exec.kill", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
proc, err := getExecPath(r, 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", ps)
})
}
func getExecPath(r *http.Request, as *app.State) (*exec.Exec, error) {
key, err := cutil.PathString(r, "key", false)
if err != nil {
return nil, err
}
idx, err := cutil.PathInt(r, "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
}