forked from go-squads/saga-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
301 lines (250 loc) · 8.77 KB
/
scheduler.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/pborman/uuid"
log "github.com/sirupsen/logrus"
)
type scheduler struct {
Router *mux.Router
DB PostgresQL
client client
metricsDB metricsDB
}
type PostgresQL interface {
Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
NamedExec(query string, arg interface{}) (sql.Result, error)
Exec(query string, args ...interface{}) (sql.Result, error)
}
type client interface {
executeOperationRequest(req *http.Request) (*operation, error)
}
type agentClient struct{}
func (a agentClient) executeOperationRequest(req *http.Request) (*operation, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
response, err := client.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var op *operation
err = json.Unmarshal(body, &op)
if err != nil {
return nil, err
}
return op, nil
}
func (s *scheduler) initialize(user, password, dbname, host, port string) error {
connectionString := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s", user, password, dbname, host, port)
var err error
s.DB, err = sqlx.Connect("postgres", connectionString)
if err != nil {
return err
}
s.Router = mux.NewRouter()
s.Router.HandleFunc("/api/v1/lxc", s.createNewLxcHandler).Methods("POST")
s.Router.HandleFunc("/api/v1/lxc", s.getContainerHandler).Methods("GET")
s.Router.HandleFunc("/api/v1/lxc", s.updateLxcStatusByIDHandler).Methods("PUT")
s.Router.HandleFunc("/api/v1/lxc", s.deleteLxcHandler).Methods("DELETE")
s.Router.HandleFunc("/api/v1/lxd/{lxdName}/lxc", s.getLxcListByLxdNameHandler).Methods("GET")
s.Router.HandleFunc("/api/v1/lxc-services", s.createNewLxcServiceHandler).Methods("POST")
s.Router.HandleFunc("/api/v1/lxc-services/{lxdName}", s.getLxcServicesListHandler).Methods("GET")
s.Router.HandleFunc("/api/v1/lxc-services", s.updateLxcServicesStatusByIDHandler).Methods("PUT")
s.Router.HandleFunc("/api/v1/lxc-services/lxc/{lxcId}", s.getLxcServiceListByLxcIDHandler).Methods("GET")
s.client = agentClient{}
s.metricsDB = prometheusMetricsDB{}
return nil
}
func (s *scheduler) run(port string) {
log.Fatal(http.ListenAndServe(port, s.Router))
}
func (s *scheduler) getContainerHandler(w http.ResponseWriter, r *http.Request) {
type resp struct {
ID string `json:"id" db:"id"`
LxdID string `json:"lxd_id" db:"lxd_id"`
LXDName string `json:"lxd_name" db:"lxd_name"`
LXCName string `json:"lxc_name" db:"lxc_name"`
Image string `json:"image" db:"image"`
Status string `json:"status" db:"status"`
}
var result []resp
rows, err := s.DB.Queryx(`SELECT c.id as "id", c.name as "lxc_name", d.id as "lxd_id", d.name as "lxd_name", c.alias as "image", c.status as "status" FROM lxc c JOIN lxd d ON c.lxd_id = d.id`)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
for rows.Next() {
var temp resp
err = rows.StructScan(&temp)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
}
result = append(result, temp)
}
respondWithJSON(w, http.StatusOK, result)
}
// BUG IF LXC ALREADY EXIST
func (s *scheduler) createNewLxcHandler(w http.ResponseWriter, r *http.Request) {
log.Info("-- Got new create lxc request --")
newLxc := lxc{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&newLxc); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
exist := newLxc.checkIfLxcExist(s.DB)
if !exist {
lxdInstance, err := s.metricsDB.getLowestLoadLxdInstance(newLxc.WeightScore, newLxc.WeightValue, s.DB)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
err = lxdInstance.getLxdByIP(s.DB)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
newLxc.ID = uuid.New()
newLxc.Status = "creating"
newLxc.LxdID = lxdInstance.ID
err = newLxc.insertLxc(s.DB)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusOK, newLxc)
} else {
errStr := fmt.Sprintf("Lxc with name %s already exists", newLxc.Name)
respondWithError(w, http.StatusBadRequest, errStr)
}
}
func (s *scheduler) deleteLxcHandler(w http.ResponseWriter, r *http.Request) {
log.Info("-- Got delete lxc request --")
lxcDeleteData := lxc{}
if err := json.NewDecoder(r.Body).Decode(&lxcDeleteData); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := lxcDeleteData.deleteLxc(s.DB); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"message": "delete lxc success"})
}
func (s *scheduler) getLxcListByLxdNameHandler(w http.ResponseWriter, r *http.Request) {
log.Info("-- Got get lxc by lxd name request --")
vars := mux.Vars(r)
lxdName := vars["lxdName"]
lxdSearch := lxd{Name: lxdName}
if err := lxdSearch.getLxdIDByName(s.DB); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
lxcSearch := lxc{}
lxcList, err := lxcSearch.getLxcListByLxdID(s.DB, lxdSearch.ID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, lxcList)
}
func (s *scheduler) updateLxcStatusByIDHandler(w http.ResponseWriter, r *http.Request) {
log.Info("-- Got update lxc status by id request --")
lxcUpdateData := lxc{}
if err := json.NewDecoder(r.Body).Decode(&lxcUpdateData); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := lxcUpdateData.updateStatusByID(s.DB); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"message": "success updating lxc state"})
}
func (s *scheduler) createNewLxcServiceHandler(w http.ResponseWriter, r *http.Request) {
log.Info("-- Got new create lxc services request --")
newLxcService := lxcService{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&newLxcService); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
log.Infof("LXC ID %s", newLxcService.LxcID)
lxc := lxc{ID: newLxcService.LxcID}
lxd := lxd{ID: newLxcService.LxdID}
lxcName := lxc.getLxcNameByID(s.DB)
lxdAddress, lxdName := lxd.getLxdNameAndAddressByID(s.DB)
newLxcService.LxcName = lxcName
newLxcService.LxdName = lxdName
newLxcService.LxdAddress = lxdAddress
exist := newLxcService.checkIfLxcServiceExist(s.DB)
if !exist {
newLxcService.ID = uuid.New()
if err := newLxcService.insertLxcService(s.DB); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusOK, newLxcService)
} else {
errMsg := fmt.Sprintf("Lxc Service: %s for lxc: %s on %s:%s already exists", newLxcService.Service, newLxcService.LxcName, newLxcService.LxdPort, newLxcService.LxcPort)
respondWithError(w, http.StatusBadRequest, errMsg)
}
}
func (s *scheduler) getLxcServicesListHandler(w http.ResponseWriter, r *http.Request) {
log.Info("==> Received Request for Lxc Services List <==")
vars := mux.Vars(r)
lxdName := vars["lxdName"]
lxcServicesList, err := getLxcServicesListByLxdName(s.DB, lxdName)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, lxcServicesList)
}
func (s *scheduler) updateLxcServicesStatusByIDHandler(w http.ResponseWriter, r *http.Request) {
l := lxcService{}
if err := json.NewDecoder(r.Body).Decode(&l); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
if err := l.updateStatusByID(s.DB); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"message": "success updating lxc service status"})
}
func (s *scheduler) getLxcServiceListByLxcIDHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Get lxc services list by lxc id request")
vars := mux.Vars(r)
lxcId := vars["lxcId"]
l := lxcService{LxcID: lxcId}
lxcServicesList, err := l.getLxcServicesListByLxcID(s.DB)
if err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusOK, lxcServicesList)
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(code)
w.Write(response)
}