forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses.go
More file actions
148 lines (124 loc) · 3.36 KB
/
processes.go
File metadata and controls
148 lines (124 loc) · 3.36 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package controllers
import (
"net/http"
"sort"
"strconv"
"github.com/convox/rack/api/httperr"
"github.com/convox/rack/api/models"
"github.com/convox/rack/api/structs"
"github.com/convox/rack/provider"
"github.com/gorilla/mux"
"golang.org/x/net/websocket"
)
// ProcessExecAttached runs an attached command in an existing process
func ProcessExecAttached(ws *websocket.Conn) *httperr.Error {
vars := mux.Vars(ws.Request())
header := ws.Request().Header
app := vars["app"]
_, err := models.Provider().AppGet(app)
if err != nil {
if provider.ErrorNotFound(err) {
return httperr.New(404, err)
}
return httperr.Server(err)
}
pid := vars["pid"]
command := header.Get("Command")
height, _ := strconv.Atoi(header.Get("Height"))
width, _ := strconv.Atoi(header.Get("Width"))
err = models.Provider().ProcessExec(app, pid, command, ws, structs.ProcessExecOptions{
Height: height,
Width: width,
})
if provider.ErrorNotFound(err) {
return httperr.New(404, err)
}
if err != nil {
return httperr.Server(err)
}
return nil
}
// ProcessGet returns a process for an app
func ProcessGet(rw http.ResponseWriter, r *http.Request) *httperr.Error {
app := mux.Vars(r)["app"]
process := mux.Vars(r)["process"]
ps, err := models.Provider().ProcessGet(app, process)
if provider.ErrorNotFound(err) {
return httperr.NotFound(err)
}
if err != nil {
return httperr.Server(err)
}
return RenderJson(rw, ps)
}
// ProcessList returns a list of processes for an app
func ProcessList(rw http.ResponseWriter, r *http.Request) *httperr.Error {
app := mux.Vars(r)["app"]
ps, err := models.Provider().ProcessList(app)
if provider.ErrorNotFound(err) {
return httperr.NotFound(err)
}
if err != nil {
return httperr.Server(err)
}
sort.Sort(ps)
return RenderJson(rw, ps)
}
// ProcessRunAttached runs an attached command in an new process
func ProcessRunAttached(ws *websocket.Conn) *httperr.Error {
vars := mux.Vars(ws.Request())
header := ws.Request().Header
app := vars["app"]
process := vars["process"]
command := header.Get("Command")
release := header.Get("Release")
height, _ := strconv.Atoi(header.Get("Height"))
width, _ := strconv.Atoi(header.Get("Width"))
_, err := models.Provider().ProcessRun(app, process, structs.ProcessRunOptions{
Command: command,
Height: height,
Width: width,
Release: release,
Stream: ws,
})
if provider.ErrorNotFound(err) {
return httperr.New(404, err)
}
if err != nil {
return httperr.Server(err)
}
return nil
}
// ProcessRunDetached runs a process in the background
func ProcessRunDetached(rw http.ResponseWriter, r *http.Request) *httperr.Error {
vars := mux.Vars(r)
app := vars["app"]
process := vars["process"]
command := GetForm(r, "command")
release := GetForm(r, "release")
_, err := models.Provider().ProcessRun(app, process, structs.ProcessRunOptions{
Command: command,
Release: release,
})
if provider.ErrorNotFound(err) {
return httperr.New(404, err)
}
if err != nil {
return httperr.Server(err)
}
return RenderSuccess(rw)
}
// ProcessStop stops a Process
func ProcessStop(rw http.ResponseWriter, r *http.Request) *httperr.Error {
vars := mux.Vars(r)
app := vars["app"]
process := vars["process"]
err := models.Provider().ProcessStop(app, process)
if provider.ErrorNotFound(err) {
return httperr.New(404, err)
}
if err != nil {
return httperr.Server(err)
}
return RenderSuccess(rw)
}