/
main.go
274 lines (232 loc) · 7.51 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"time"
"github.com/andlabs/ui"
"github.com/rackerlabs/libcarina"
"github.com/samalba/dockerclient"
)
var (
w ui.Window
carinaClient *libcarina.ClusterClient
loggedInFlag bool
)
// VERSION is just a string that will show up in the windowbar
const VERSION string = "0.2.0"
func gui() {
//Define endpoint
apiEndpointLabel := ui.NewLabel("API Endpoint:")
apiEndpointTextField := ui.NewTextField()
if len(os.Getenv("CARINA_API_ENDPOINT")) > 0 {
apiEndpointTextField.SetText(os.Getenv("CARINA_API_ENDPOINT"))
} else {
apiEndpointTextField.SetText(libcarina.BetaEndpoint)
}
//Define credentials area
usernameLabel := ui.NewLabel("Username:")
usernameTextField := ui.NewTextField()
if len(os.Getenv("CARINA_USERNAME")) > 0 {
usernameTextField.SetText(os.Getenv("CARINA_USERNAME"))
}
apiKeyLabel := ui.NewLabel("API Key:")
apiKeyTextField := ui.NewPasswordField()
if len(os.Getenv("CARINA_APIKEY")) > 0 {
apiKeyTextField.SetText(os.Getenv("CARINA_APIKEY"))
}
connectBtn := ui.NewButton("Connect")
// layout the login controls on a grid
loginGrid := ui.NewGrid()
loginGrid.Add(apiEndpointLabel, nil, ui.East, true, ui.LeftTop, false, ui.Center, 1, 1)
loginGrid.Add(apiEndpointTextField, apiEndpointLabel, ui.South, true, ui.Fill, false, ui.Center, 1, 1)
loginGrid.Add(usernameLabel, apiEndpointLabel, ui.East, true, ui.LeftTop, false, ui.Center, 1, 1)
loginGrid.Add(usernameTextField, usernameLabel, ui.South, true, ui.Fill, false, ui.Center, 1, 1)
loginGrid.Add(apiKeyLabel, usernameLabel, ui.East, true, ui.LeftTop, false, ui.Center, 1, 1)
loginGrid.Add(apiKeyTextField, apiKeyLabel, ui.South, true, ui.Fill, false, ui.Center, 1, 1)
loginGrid.Add(connectBtn, nil, ui.East, true, ui.LeftTop, false, ui.Center, 1, 1)
loginGrid.SetPadded(true)
//div grp1
divGrp1 := ui.NewGroup("", ui.Space())
divGrp1.SetMargined(true)
// Define the table that lists all running clusters
var c libcarina.Cluster
clusterListTable := ui.NewTable(reflect.TypeOf(c))
// Create control buttons
newBtn := ui.NewButton("New")
growBtn := ui.NewButton("Grow")
rebuildBtn := ui.NewButton("Rebuild")
credentialsBtn := ui.NewButton("Credentials")
deleteBtn := ui.NewButton("Delete")
buttonStack := ui.NewVerticalStack(newBtn, growBtn, rebuildBtn, credentialsBtn, deleteBtn)
//div grp2
divGrp2 := ui.NewGroup("", ui.Space())
divGrp2.SetMargined(true)
//Show containers on the cluster
containerListLabel := ui.NewLabel("Containers")
var cont dockerclient.Container
containerListTable := ui.NewTable(reflect.TypeOf(cont))
mainGrid := ui.NewGrid()
mainGrid.Add(loginGrid, nil, ui.East, true, ui.Fill, false, ui.Center, 12, 1)
mainGrid.Add(divGrp1, loginGrid, ui.South, true, ui.Fill, false, ui.Center, 12, 1)
mainGrid.Add(clusterListTable, divGrp1, ui.South, true, ui.Fill, false, ui.Center, 9, 1)
mainGrid.Add(buttonStack, clusterListTable, ui.East, true, ui.Fill, false, ui.Center, 3, 1)
mainGrid.Add(divGrp2, clusterListTable, ui.South, true, ui.Fill, false, ui.Center, 12, 1)
mainGrid.Add(containerListLabel, divGrp2, ui.South, true, ui.Fill, false, ui.Center, 12, 1)
mainGrid.Add(containerListTable, containerListLabel, ui.South, true, ui.Fill, false, ui.Center, 12, 1)
mainGrid.SetPadded(true)
connectBtn.OnClicked(func() {
connect(apiEndpointTextField.Text(), usernameTextField.Text(), apiKeyTextField.Text())
go monitorClusterList(clusterListTable)
})
clusterListTable.OnSelected(func() {
c, found := getSelectedCluster(clusterListTable)
if found {
if c.Status == "active" {
containers := getContainers(c.ClusterName)
containerListTable.Lock()
d := containerListTable.Data().(*[]dockerclient.Container)
*d = containers
containerListTable.Unlock()
txt := fmt.Sprintf("%d containers running on %s cluster", len(containers), c.ClusterName)
containerListLabel.SetText(txt)
}
}
})
newBtn.OnClicked(func() {
if loggedInFlag {
newCluster()
}
})
deleteBtn.OnClicked(func() {
c, found := getSelectedCluster(clusterListTable)
if found {
carinaClient.Delete(c.ClusterName)
fmt.Println("Deleting", c.ClusterName)
}
})
rebuildBtn.OnClicked(func() {
c, found := getSelectedCluster(clusterListTable)
if found {
fmt.Println("Rebuiding", c.ClusterName)
carinaClient.Rebuild(c.ClusterName)
}
})
credentialsBtn.OnClicked(func() {
c, found := getSelectedCluster(clusterListTable)
if found {
fmt.Println("Getting credentials for", c.ClusterName)
carinaClient.GetCredentials(c.ClusterName)
}
})
growBtn.OnClicked(func() {
c, found := getSelectedCluster(clusterListTable)
if found {
fmt.Println("Growing", c.ClusterName)
}
})
//Main stack of the interfaces
w = ui.NewWindow("Carina by Rackspace GUI Client ("+VERSION+")", 620, 300, mainGrid)
w.SetMargined(true)
w.OnClosing(func() bool {
ui.Stop()
return true
})
w.Show()
}
// Set up global connection to the cluster
func connect(endpoint, username, apiKey string) {
// Connect to Carina
var err error
carinaClient, err = libcarina.NewClusterClient(endpoint, username, apiKey)
loggedInFlag = true
if err != nil {
log.Fatal("Cannot create cluster client: ", err)
}
}
// monitor the carina client
func monitorClusterList(t ui.Table) {
for {
clusters, _ := carinaClient.List()
t.Lock()
d := t.Data().(*[]libcarina.Cluster)
*d = clusters
t.Unlock()
time.Sleep(5 * time.Second)
}
}
func getSelectedCluster(table ui.Table) (libcarina.Cluster, bool) {
var out libcarina.Cluster
found := false
c := table.Selected()
table.Lock()
d := table.Data().(*[]libcarina.Cluster)
newC := *d
table.Unlock()
if c > -1 {
out = newC[c]
found = true
}
return out, found
}
func newCluster() {
clusterNameLabel := ui.NewLabel("Cluster Name:")
clusterNameTextField := ui.NewTextField()
clusterNodeCountLabel := ui.NewLabel("Number of Nodes:")
clusterNodeCountTextField := ui.NewTextField()
clusterNodeCountTextField.SetText("1")
autoscaleLabel := ui.NewLabel("Autoscale:")
autoscaleCheckbox := ui.NewCheckbox("")
newClusterBtn := ui.NewButton("Create Cluster")
cancelBtn := ui.NewButton("Cancel")
newClusterGrid := ui.NewSimpleGrid(2,
clusterNameLabel, clusterNameTextField,
clusterNodeCountLabel, clusterNodeCountTextField,
autoscaleLabel, autoscaleCheckbox,
newClusterBtn, cancelBtn)
newClusterGrid.SetPadded(true)
newClusterGrp := ui.NewGroup("", newClusterGrid)
newClusterGrp.SetMargined(true)
newWin := ui.NewWindow("New Cluster", 400, 300, newClusterGrp)
newWin.SetMargined(true)
newWin.Show()
newClusterBtn.OnClicked(func() {
var c libcarina.Cluster
c.ClusterName = clusterNameTextField.Text()
n, _ := strconv.Atoi(clusterNodeCountTextField.Text())
c.Nodes = libcarina.Number(n)
c.AutoScale = autoscaleCheckbox.Checked()
carinaClient.Create(c)
time.Sleep(250 * time.Millisecond)
newWin.Close()
})
cancelBtn.OnClicked(func() {
newWin.Close()
})
newWin.OnClosing(func() bool {
newWin.Close()
return true
})
}
//Lists all containers running on a cluster
func getContainers(clusterName string) []dockerclient.Container {
host, tlsConfig, _ := carinaClient.GetDockerConfig(clusterName)
// Setup the docker host
docker, err := dockerclient.NewDockerClient(host, tlsConfig)
containers, err := docker.ListContainers(false, false, "")
if err != nil {
log.Fatal(err)
}
return containers
}
func main() {
// This runs the code that displays our GUI.
// All code that interfaces with package ui (except event handlers) must be run from within a ui.Do() call.
go ui.Do(gui)
err := ui.Go()
if err != nil {
log.Print(err)
}
}