-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathrunners.go
178 lines (131 loc) · 3.55 KB
/
runners.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package api
import (
"bufio"
"bytes"
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/util"
log "github.com/sirupsen/logrus"
"net/http"
"github.com/gorilla/context"
)
func getAllRunners(w http.ResponseWriter, r *http.Request) {
runners, err := helpers.Store(r).GetAllRunners(false, false)
if err != nil {
panic(err)
}
var result = make([]db.Runner, 0)
for _, runner := range runners {
result = append(result, runner)
}
helpers.WriteJSON(w, http.StatusOK, result)
}
type runnerWithToken struct {
db.Runner
Token string `json:"token"`
PrivateKey string `json:"private_key"`
}
func addGlobalRunner(w http.ResponseWriter, r *http.Request) {
var runner db.Runner
if !helpers.Bind(w, r, &runner) {
return
}
runner.ProjectID = nil
var privateKey []byte
if runner.PublicKey == nil {
var b bytes.Buffer
privateKeyFile := bufio.NewWriter(&b)
publicKey, err := util.GeneratePrivateKey(privateKeyFile)
if err != nil {
helpers.WriteError(w, err)
return
}
err = privateKeyFile.Flush()
if err != nil {
helpers.WriteError(w, err)
return
}
privateKey = b.Bytes()
runner.PublicKey = &publicKey
}
newRunner, err := helpers.Store(r).CreateRunner(runner)
if err != nil {
log.Warn("Runner is not created: " + err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
helpers.WriteJSON(w, http.StatusCreated, runnerWithToken{
Runner: newRunner,
Token: newRunner.Token,
PrivateKey: string(privateKey),
})
}
func globalRunnerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
runnerID, err := helpers.GetIntParam("runner_id", w, r)
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "runner_id required",
})
return
}
store := helpers.Store(r)
runner, err := store.GetGlobalRunner(runnerID)
if err != nil {
helpers.WriteJSON(w, http.StatusNotFound, map[string]string{
"error": "Runner not found",
})
return
}
context.Set(r, "runner", &runner)
next.ServeHTTP(w, r)
})
}
func getGlobalRunner(w http.ResponseWriter, r *http.Request) {
runner := context.Get(r, "runner").(*db.Runner)
helpers.WriteJSON(w, http.StatusOK, runner)
}
func updateGlobalRunner(w http.ResponseWriter, r *http.Request) {
oldRunner := context.Get(r, "runner").(*db.Runner)
var runner db.Runner
if !helpers.Bind(w, r, &runner) {
return
}
store := helpers.Store(r)
runner.ID = oldRunner.ID
runner.ProjectID = nil
err := store.UpdateRunner(runner)
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}
func deleteGlobalRunner(w http.ResponseWriter, r *http.Request) {
runner := context.Get(r, "runner").(*db.Runner)
store := helpers.Store(r)
err := store.DeleteGlobalRunner(runner.ID)
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}
func setGlobalRunnerActive(w http.ResponseWriter, r *http.Request) {
runner := context.Get(r, "runner").(*db.Runner)
store := helpers.Store(r)
var body struct {
Active bool `json:"active"`
}
if !helpers.Bind(w, r, &body) {
helpers.WriteErrorStatus(w, "Invalid request body", http.StatusBadRequest)
return
}
runner.Active = body.Active
err := store.UpdateRunner(*runner)
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}