/
state.go
146 lines (116 loc) · 3.78 KB
/
state.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
package server
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
log "github.com/Sirupsen/logrus"
types "github.com/codedellemc/scaleio-framework/scaleio-scheduler/types"
)
func displayState(w http.ResponseWriter, r *http.Request, server *RestServer) {
response := "<html><head><title>Output</title><meta http-equiv=\"refresh\" content=\"2\" /></head><body>"
server.Lock()
for _, node := range server.State.ScaleIO.Nodes {
response += node.ExecutorID
response += " = "
switch node.State {
case types.StateUnknown:
response += "Installing Prerequisite Packages"
case types.StateCleanPrereqsReboot:
response += "Sync on Prerequisite Install"
case types.StatePrerequisitesInstalled:
response += "Installing ScaleIO Packages"
case types.StateBasePackagedInstalled:
response += "Creating ScaleIO Cluster"
case types.StateInitializeCluster:
response += "Initializing ScaleIO"
case types.StateAddResourcesToScaleIO:
response += "Adding resources to ScaleIO cluster"
case types.StateInstallRexRay:
response += "Installing REX-Ray"
case types.StateCleanInstallReboot:
response += "Sync Before for Reboot"
case types.StateSystemReboot:
response += "System is Rebooting"
case types.StateFinishInstall:
response += "ScaleIO Running"
case types.StateFatalInstall:
response += "Installation Failed"
}
response += "<br />"
}
capacityData := server.State.ScaleIO.CapacityData
usedData := server.State.ScaleIO.UsedData
fakeUsedData := server.State.ScaleIO.FakeUsedData
usedSpacePercent := int(float64(usedData+fakeUsedData) / float64(capacityData) * 100)
if capacityData == 0 {
usedSpacePercent = 0
}
if len(server.Config.AccessKey) > 0 && len(server.Config.SecretKey) > 0 {
response += "<br /><br /><br />"
response += "Capacity (KB): "
response += strconv.Itoa(capacityData)
response += "<br />"
response += "Fake Used Data (KB): "
response += strconv.Itoa(fakeUsedData)
response += "<br /><br />"
response += "Percent Used: "
response += strconv.Itoa(usedSpacePercent)
response += "<br />"
response += "Threshold Percent: "
response += strconv.Itoa(server.Config.UsedThreshold)
response += "<br />"
}
server.Unlock()
response += "</body></html>"
//log.Debugln("response:", string(response))
fmt.Fprintf(w, string(response))
}
func setState(w http.ResponseWriter, r *http.Request, server *RestServer) {
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
http.Error(w, "Unable to read the HTTP Body stream", http.StatusBadRequest)
return
}
if err := r.Body.Close(); err != nil {
log.Warnln("Unable to close the HTTP Body stream:", err)
}
state := &types.UpdateCluster{
Acknowledged: false,
KeyValue: make(map[string]string),
}
if err := json.Unmarshal(body, &state); err != nil {
http.Error(w, "Unable to marshall the response", http.StatusBadRequest)
return
}
//update the object
server.Lock()
server.State.ScaleIO.Configured = true
server.Unlock()
//update the store
err = server.Store.SetConfigured()
if err != nil {
http.Error(w, "Failed to update the Cluster configured bit", http.StatusBadRequest)
return
}
//acknowledged the state change
state.Acknowledged = true
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(state); err != nil {
http.Error(w, "Unable to marshall the response", http.StatusBadRequest)
}
}
func getState(w http.ResponseWriter, r *http.Request, server *RestServer) {
server.Lock()
response, err := json.MarshalIndent(server.State, "", " ")
server.Unlock()
if err != nil {
http.Error(w, "Unable to marshall the response", http.StatusBadRequest)
return
}
log.Debugln("response:", string(response))
fmt.Fprintf(w, string(response))
}