/
main.go
176 lines (151 loc) · 4.18 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
package main
import (
"fmt"
"log"
"os"
"reflect"
"strings"
"time"
"github.com/andlabs/ui"
"github.com/joho/godotenv"
"github.com/samalba/dockerclient"
"github.com/toqueteos/webbrowser"
)
// Container shows metadata about containers running on the cluster
type Container struct {
Image string
Name string
}
var (
w ui.Window
docker *dockerclient.DockerClient
)
func gui() {
connect()
var c Container
//Stack for the control
l := ui.NewLabel("Image to start")
imageName := ui.NewTextField()
imageName.SetText("ipython/scipystack")
startBtn := ui.NewButton("Launch")
controlStack := ui.NewVerticalStack(l, imageName, startBtn)
controlGrp := ui.NewGroup("Launch Image", controlStack)
controlGrp.SetMargined(true)
// Table of running containers
table := ui.NewTable(reflect.TypeOf(c))
openBrowserBtn := ui.NewButton("Open in browser")
killBtn := ui.NewButton("Kill")
manageRunningContainerGrp := ui.NewHorizontalStack(openBrowserBtn, killBtn)
containerControlGrp := ui.NewVerticalStack(table, manageRunningContainerGrp)
containerListGrp := ui.NewGroup("Running containers", containerControlGrp)
containerListGrp.SetMargined(true)
//Container info area
selectedContainerInfo := ui.NewTextField()
// Now make a new 2 column stack
topStack := ui.NewHorizontalStack(controlGrp, containerListGrp)
topStack.SetStretchy(0)
topStack.SetStretchy(1)
mainStack := ui.NewVerticalStack(topStack, selectedContainerInfo)
mainStack.SetStretchy(0)
mainStack.SetStretchy(1)
startBtn.OnClicked(func() {
go Start(imageName.Text())
})
table.OnSelected(func() {
c := table.Selected()
table.Lock()
d := table.Data().(*[]Container)
//this makes a shallow copy of the structure so that we can access elements per
// http://giantmachines.tumblr.com/post/51007535999/golang-struct-shallow-copy
newC := *d
table.Unlock()
if c > -1 {
fmt.Println("Getting info for container ", newC[c].Name)
selectedContainerInfo.SetText(Info(newC[c].Name))
}
})
openBrowserBtn.OnClicked(func() {
c := table.Selected()
table.Lock()
d := table.Data().(*[]Container)
//this makes a shallow copy of the structure so that we can access elements per
// http://giantmachines.tumblr.com/post/51007535999/golang-struct-shallow-copy
newC := *d
table.Unlock()
url := fmt.Sprintf("%s.%s", newC[c].Name, os.Getenv("DOMAIN_NAME"))
webbrowser.Open(url)
})
killBtn.OnClicked(func() {
c := table.Selected()
table.Lock()
d := table.Data().(*[]Container)
//this makes a shallow copy of the structure so that we can access elements per
// http://giantmachines.tumblr.com/post/51007535999/golang-struct-shallow-copy
newC := *d
table.Unlock()
go Kill(newC[c].Name)
})
w = ui.NewWindow("Manage Containers on RCS", 600, 450, mainStack)
w.SetMargined(true)
w.OnClosing(func() bool {
ui.Stop()
return true
})
go updateTable(table)
w.Show()
}
func updateTable(table ui.Table) {
for {
running := ps()
table.Lock()
d := table.Data().(*[]Container)
*d = running
table.Unlock()
time.Sleep(1 * time.Second)
}
}
func connect() {
// Load the environment variables we need
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Set up connection to swarm
tlsConfig, err := GetTLSConfig(os.Getenv("SWARM_CREDS_DIR"))
if err != nil {
log.Fatal("Could not find TLS certificate.")
}
// Setup the docker host
docker, err = dockerclient.NewDockerClient(os.Getenv("DOCKER_HOST"), tlsConfig)
if err != nil {
log.Fatal("Error initializing docker: ", err)
}
log.Println("Swarm connection inialized", docker)
}
func ps() []Container {
var out []Container
// Get only running containers
containers, err := docker.ListContainers(false, false, "")
if err != nil {
log.Fatal(err)
}
for _, c := range containers {
//log.Println(c.Id)
//container, _ := docker.InspectContainer(c.Id)
cNew := Container{
Image: c.Image,
Name: strings.Split(c.Names[0], "/")[2],
}
out = append(out, cNew)
}
return out
}
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)
}
}