/
server.go
335 lines (305 loc) · 10.4 KB
/
server.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package server
import (
"fmt"
"net/http"
"strconv"
"sync"
"time"
log "github.com/Sirupsen/logrus"
negroni "github.com/codegangsta/negroni"
"github.com/gorilla/mux"
config "github.com/codedellemc/scaleio-framework/scaleio-scheduler/config"
common "github.com/codedellemc/scaleio-framework/scaleio-scheduler/scheduler/common"
kvstore "github.com/codedellemc/scaleio-framework/scaleio-scheduler/scheduler/kvstore"
types "github.com/codedellemc/scaleio-framework/scaleio-scheduler/types"
)
const (
rootKey = "scaleio-framework"
)
//RestServer representation for a REST API server
type RestServer struct {
Config *config.Config
Store *kvstore.KvStore
Server *negroni.Negroni
State *types.ScaleIOFramework
Index int
sync.Mutex
}
//NewRestServer generates a new REST API server
func NewRestServer(cfg *config.Config, store *kvstore.KvStore) *RestServer {
preconfig := cfg.PrimaryMdmAddress != "" && cfg.SecondaryMdmAddress != "" &&
cfg.TieBreakerMdmAddress != ""
scaleio := &types.ScaleIOFramework{
SchedulerAddress: fmt.Sprintf("http://%s:%d", cfg.RestAddress, cfg.RestPort),
LogLevel: cfg.LogLevel,
Debug: cfg.Debug,
Experimental: cfg.Experimental,
KeyValue: make(map[string]string),
ScaleIO: &types.ScaleIOConfig{
Configured: store.GetConfigured(),
ClusterID: cfg.ClusterID,
ClusterName: cfg.ClusterName,
LbGateway: cfg.LbGateway,
ProtectionDomain: cfg.ProtectionDomain,
StoragePool: cfg.StoragePool,
AdminPassword: cfg.AdminPassword,
APIVersion: cfg.APIVersion,
FakeUsedData: 0,
CapacityData: 0,
UsedData: 0,
AtLeastOneImperative: false,
KeyValue: make(map[string]string),
Preconfig: types.ScaleIOPreConfig{
PreConfigEnabled: preconfig,
PrimaryMdmAddress: cfg.PrimaryMdmAddress,
SecondaryMdmAddress: cfg.SecondaryMdmAddress,
TieBreakerMdmAddress: cfg.TieBreakerMdmAddress,
GatewayAddress: cfg.GatewayAddress,
},
Ubuntu14: types.Ubuntu14Packages{
Mdm: cfg.DebMdm,
Sds: cfg.DebSds,
Sdc: cfg.DebSdc,
Lia: cfg.DebLia,
Gw: cfg.DebGw,
},
Rhel7: types.Rhel7Packages{
Mdm: cfg.RpmMdm,
Sds: cfg.RpmSds,
Sdc: cfg.RpmSdc,
Lia: cfg.RpmLia,
Gw: cfg.RpmGw,
},
},
Rexray: types.RexrayConfig{
Branch: cfg.RexrayBranch,
Version: cfg.RexrayVersion,
},
Isolator: types.IsolatorConfig{
Binary: cfg.IsolatorBinary,
},
}
restServer := &RestServer{
Config: cfg,
Store: store,
State: scaleio,
Index: 1,
}
mux := mux.NewRouter()
mux.HandleFunc("/scaleio-executor", func(w http.ResponseWriter, r *http.Request) {
downloadExecutor(w, r, restServer)
}).Methods("GET")
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
getVersion(w, r, restServer)
}).Methods("GET")
mux.HandleFunc("/api/state", func(w http.ResponseWriter, r *http.Request) {
getState(w, r, restServer)
}).Methods("GET")
mux.HandleFunc("/api/state", func(w http.ResponseWriter, r *http.Request) {
setState(w, r, restServer)
}).Methods("POST")
mux.HandleFunc("/api/node/state", func(w http.ResponseWriter, r *http.Request) {
setNodeState(w, r, restServer)
}).Methods("POST")
mux.HandleFunc("/api/node/device", func(w http.ResponseWriter, r *http.Request) {
setNodeDevices(w, r, restServer)
}).Methods("POST")
mux.HandleFunc("/api/node/ping", func(w http.ResponseWriter, r *http.Request) {
setNodePing(w, r, restServer)
}).Methods("POST")
mux.HandleFunc("/api/fake", func(w http.ResponseWriter, r *http.Request) {
setFakeData(w, r, restServer)
}).Methods("POST")
mux.HandleFunc("/ui", func(w http.ResponseWriter, r *http.Request) {
displayState(w, r, restServer)
}).Methods("GET")
//TODO delete this below when a real UI is embedded
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
displayState(w, r, restServer)
}).Methods("GET")
server := negroni.Classic()
server.UseHandler(mux)
//Run is a blocking call for Negroni... so go routine it
go func() {
server.Run(cfg.RestAddress + ":" + strconv.Itoa(cfg.RestPort))
}()
restServer.Server = server
//MonitorForState watch for state changes
go func() {
err := restServer.MonitorForState()
if err != nil {
log.Errorln("MonitorForState:", err)
}
}()
return restServer
}
func cloneState(src *types.ScaleIOFramework) *types.ScaleIOFramework {
dst := &types.ScaleIOFramework{}
dst.Debug = src.Debug
dst.Experimental = src.Experimental
dst.LogLevel = src.LogLevel
dst.Rexray.Branch = src.Rexray.Branch
dst.Rexray.Version = src.Rexray.Version
dst.SchedulerAddress = src.SchedulerAddress
dst.Isolator.Binary = src.Isolator.Binary
dst.KeyValue = make(map[string]string)
for key, val := range src.KeyValue {
dst.KeyValue[key] = val
}
dst.ScaleIO = &types.ScaleIOConfig{}
dst.ScaleIO.AdminPassword = src.ScaleIO.AdminPassword
dst.ScaleIO.APIVersion = src.ScaleIO.APIVersion
dst.ScaleIO.ClusterID = src.ScaleIO.ClusterID
dst.ScaleIO.ClusterName = src.ScaleIO.ClusterName
dst.ScaleIO.Configured = src.ScaleIO.Configured
dst.ScaleIO.LbGateway = src.ScaleIO.LbGateway
dst.ScaleIO.Preconfig.GatewayAddress = src.ScaleIO.Preconfig.GatewayAddress
dst.ScaleIO.Preconfig.PreConfigEnabled = src.ScaleIO.Preconfig.PreConfigEnabled
dst.ScaleIO.Preconfig.PrimaryMdmAddress = src.ScaleIO.Preconfig.PrimaryMdmAddress
dst.ScaleIO.Preconfig.SecondaryMdmAddress = src.ScaleIO.Preconfig.SecondaryMdmAddress
dst.ScaleIO.Preconfig.TieBreakerMdmAddress = src.ScaleIO.Preconfig.TieBreakerMdmAddress
dst.ScaleIO.ProtectionDomain = src.ScaleIO.ProtectionDomain
dst.ScaleIO.FakeUsedData = src.ScaleIO.FakeUsedData
dst.ScaleIO.CapacityData = src.ScaleIO.CapacityData
dst.ScaleIO.UsedData = src.ScaleIO.UsedData
dst.ScaleIO.AtLeastOneImperative = src.ScaleIO.AtLeastOneImperative
dst.ScaleIO.Rhel7.Gw = src.ScaleIO.Rhel7.Gw
dst.ScaleIO.Rhel7.Lia = src.ScaleIO.Rhel7.Lia
dst.ScaleIO.Rhel7.Mdm = src.ScaleIO.Rhel7.Mdm
dst.ScaleIO.Rhel7.Sdc = src.ScaleIO.Rhel7.Sdc
dst.ScaleIO.Rhel7.Sds = src.ScaleIO.Rhel7.Sds
dst.ScaleIO.StoragePool = src.ScaleIO.StoragePool
dst.ScaleIO.Ubuntu14.Gw = src.ScaleIO.Ubuntu14.Gw
dst.ScaleIO.Ubuntu14.Lia = src.ScaleIO.Ubuntu14.Lia
dst.ScaleIO.Ubuntu14.Mdm = src.ScaleIO.Ubuntu14.Mdm
dst.ScaleIO.Ubuntu14.Sdc = src.ScaleIO.Ubuntu14.Sdc
dst.ScaleIO.Ubuntu14.Sds = src.ScaleIO.Ubuntu14.Sds
dst.ScaleIO.KeyValue = make(map[string]string)
dst.ScaleIO.Nodes = make([]*types.ScaleIONode, 0)
for key, val := range src.ScaleIO.KeyValue {
dst.ScaleIO.KeyValue[key] = val
}
for _, node := range src.ScaleIO.Nodes {
dstNode := &types.ScaleIONode{
AgentID: node.AgentID,
TaskID: node.TaskID,
ExecutorID: node.ExecutorID,
OfferID: node.OfferID,
IPAddress: node.IPAddress,
Hostname: node.Hostname,
Persona: node.Persona,
State: node.State,
LastContact: node.LastContact,
Imperative: node.Imperative,
Advertised: node.Advertised,
KeyValue: make(map[string]string),
ProvidesDomains: make(map[string]*types.ProtectionDomain),
ConsumesDomains: make(map[string]*types.ProtectionDomain),
}
for key, val := range node.KeyValue {
dstNode.KeyValue[key] = val
}
for keyDomain, pDomain := range node.ProvidesDomains {
dstPDomain := &types.ProtectionDomain{
Name: pDomain.Name,
KeyValue: make(map[string]string),
Pools: make(map[string]*types.StoragePool),
}
for key, val := range pDomain.KeyValue {
dstPDomain.KeyValue[key] = val
}
for keyPool, pPool := range pDomain.Pools {
dstPool := &types.StoragePool{
Name: pPool.Name,
Devices: make([]string, 0),
KeyValue: make(map[string]string),
}
for _, device := range pPool.Devices {
dstPool.Devices = append(dstPool.Devices, device)
}
for key, val := range pPool.KeyValue {
dstPool.KeyValue[key] = val
}
dstPDomain.Pools[keyPool] = dstPool
}
dstNode.ProvidesDomains[keyDomain] = dstPDomain
}
for keyDomain, cDomain := range node.ConsumesDomains {
dstCDomain := &types.ProtectionDomain{
Name: cDomain.Name,
KeyValue: make(map[string]string),
Pools: make(map[string]*types.StoragePool),
}
for key, val := range cDomain.KeyValue {
dstCDomain.KeyValue[key] = val
}
for keyPool, pPool := range cDomain.Pools {
dstPool := &types.StoragePool{
Name: pPool.Name,
Devices: make([]string, 0),
KeyValue: make(map[string]string),
}
for _, device := range pPool.Devices {
dstPool.Devices = append(dstPool.Devices, device)
}
for key, val := range pPool.KeyValue {
dstPool.KeyValue[key] = val
}
dstCDomain.Pools[keyPool] = dstPool
}
dstNode.ConsumesDomains[keyDomain] = dstCDomain
}
dst.ScaleIO.Nodes = append(dst.ScaleIO.Nodes, dstNode)
}
return dst
}
//MonitorForState monitors for changes in state
func (s *RestServer) MonitorForState() error {
cnt := uint64(1)
var err error
for {
time.Sleep(time.Duration(common.PollStatusInSeconds) * time.Second)
//must make a copy of the state because these operations can take a long time
s.Lock()
copyState := cloneState(s.State)
s.Unlock()
if common.SyncRunState(copyState, types.StateAddResourcesToScaleIO, true) {
log.Debugln("Calling addResourcesToScaleIO()...")
err := s.addResourcesToScaleIO(copyState)
if err != nil {
log.Errorln("addResourcesToScaleIO err:", err)
}
s.updateNodeState(types.StateInstallRexRay)
}
//to add more else if { SyncRunState(otherState) }
//if in AWS, check for full and expand if needed
if !copyState.ScaleIO.AtLeastOneImperative && (cnt%uint64(s.Config.CheckFull)) == 0 &&
len(s.Config.AccessKey) > 0 && len(s.Config.SecretKey) > 0 {
log.Debugln("Calling checkForFull()...")
pairDomainPool, errCheck := s.checkForFull(copyState)
if errCheck != nil {
log.Errorln("checkForFull err:", errCheck)
} else if len(pairDomainPool.Pools) == 0 {
log.Infoln("There are no StoragePools that need expanding.")
} else {
errExpand := s.expandPools(pairDomainPool)
if errExpand != nil {
log.Errorln("expandPools err:", errCheck)
}
}
}
//if in AWS, check for full and expand if needed
cnt = cnt + 1
}
return err
}
func (s *RestServer) updateNodeState(state int) {
s.Lock()
for i := 0; i < len(s.State.ScaleIO.Nodes); i++ {
if s.State.ScaleIO.Nodes[i].State > state { //only update state if less than current
continue
}
s.State.ScaleIO.Nodes[i].State = state
}
s.Unlock()
}