-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (188 loc) · 5.19 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"syscall"
)
func main() {
var configurations []ImageConfiguration
currentUser, err := user.Current()
if err != nil {
log.Fatal(err)
}
ReadDockerConfiguration(currentUser.HomeDir+"/.docker-manager.yaml", &configurations)
var selectedConfiguration = -1
if len(os.Args) > 1 {
for idx := range configurations {
if configurations[idx].Name == os.Args[1] {
selectedConfiguration = idx
break
}
}
}
if selectedConfiguration < 0 {
printImageSelectionMenu(configurations)
selectedConfiguration = getUserChoice(1, len(configurations)) - 1
}
var selectedAction = -1
if len(os.Args) > 2 {
switch os.Args[2] {
case "start":
selectedAction = startContainerAction
case "connect":
selectedAction = connectToContainerAction
case "save":
selectedAction = saveContainerAction
case "stop":
selectedAction = stopContainerAction
case "show":
selectedAction = showContainerConfiguration
}
}
if selectedAction < 0 {
printActionSelectionMenu()
selectedAction = getUserChoice(1, 5) - 1
}
switch selectedAction {
case startContainerAction:
startContainer(&configurations[selectedConfiguration])
case connectToContainerAction:
connectToContainer(&configurations[selectedConfiguration])
case saveContainerAction:
saveContainer(&configurations[selectedConfiguration])
case stopContainerAction:
stopContainer(&configurations[selectedConfiguration])
case showContainerConfiguration:
configurations[selectedConfiguration].Print()
}
}
func printImageSelectionMenu(configurations []ImageConfiguration) {
fmt.Println("Please select an image:")
for idx := range configurations {
fmt.Print("\t")
fmt.Print(idx + 1)
fmt.Println(") " + configurations[idx].Name)
}
}
func printActionSelectionMenu() {
fmt.Println("Please select an action for this container:")
fmt.Println("\t1) Start")
fmt.Println("\t2) Connect")
fmt.Println("\t3) Save")
fmt.Println("\t4) Stop")
fmt.Println("\t5) Show")
}
func getUserChoice(min, max int) int {
var inputOk = false
var selection int
for !inputOk {
fmt.Print(">>> ")
fmt.Scanf("%d", &selection)
if selection >= min && selection <= max {
inputOk = true
}
}
return selection
}
const (
startContainerAction = iota
connectToContainerAction = iota
saveContainerAction = iota
stopContainerAction = iota
showContainerConfiguration = iota
)
func readContainerID(conf *ImageConfiguration) []byte {
var id []byte
if _, err := os.Stat(conf.GetIDPath()); !os.IsNotExist(err) {
var readError error
id, readError = ioutil.ReadFile(conf.GetIDPath())
if readError != nil {
log.Fatal("Cannot read file ", conf.GetIDPath(), ": ", readError)
}
if len(id) > 0 {
id = id[:len(id)-1]
}
}
return id
}
func startContainer(conf *ImageConfiguration) {
if len(readContainerID(conf)) > 0 {
fmt.Println("The container is already started")
return
}
if conf.Gui {
xhostError := exec.Command("xhost", []string{"+"}...).Run()
if xhostError != nil {
log.Fatal("Cannot disable xhost acces control: ", xhostError)
}
}
var params = conf.GenerateStartCommand()
cmd := exec.Command(params[0], params[1:]...)
fmt.Println("Starting the container " + conf.Image + ":" + conf.Tag)
runOutput, runError := cmd.CombinedOutput()
if runError != nil {
log.Fatal("Unable to start the container: ", runError, ". ", string(runOutput))
}
out, psError := exec.Command("docker", []string{"ps", "-q", "-l"}...).Output()
if psError != nil {
log.Fatal("Unable to get the container ID: ", psError)
}
writeError := ioutil.WriteFile(conf.GetIDPath(), out, 0644)
if writeError != nil {
log.Fatal("Unable to save the container ID: ", writeError)
}
}
func connectToContainer(conf *ImageConfiguration) {
id := readContainerID(conf)
if len(id) == 0 {
startContainer(conf)
id = readContainerID(conf)
}
dockerPath, lookError := exec.LookPath("docker")
if lookError != nil {
log.Fatal("The docker executable cannot be found in PATH")
}
runError := syscall.Exec(dockerPath, []string{"docker", "exec", "-ti", string(id), conf.Shell}, os.Environ())
if runError != nil {
log.Fatal("Unable to connect to the container: ", runError)
}
}
func saveContainer(conf *ImageConfiguration) {
id := readContainerID(conf)
if len(id) == 0 {
fmt.Println("The container is not running")
return
}
commitError := exec.Command("docker", []string{"commit", string(id), conf.GetImageWithSaveTag()}...).Run()
if commitError != nil {
log.Fatal("Unable to save the container state: ", commitError)
}
}
func stopContainer(conf *ImageConfiguration) {
id := readContainerID(conf)
if len(id) == 0 {
fmt.Println("The container is not running")
return
}
if conf.Autosave {
saveContainer(conf)
}
stopError := exec.Command("docker", []string{"kill", string(id)}...).Run()
if stopError != nil {
log.Fatal("Unable to stop the container: ", stopError)
}
writeError := ioutil.WriteFile(conf.GetIDPath(), []byte{}, 0644)
if writeError != nil {
log.Fatal("Unable to reset the container ID: ", writeError)
}
if conf.Gui {
xhostError := exec.Command("xhost", []string{"-"}...).Run()
if xhostError != nil {
log.Fatal("Cannot enable xhost acces control: ", xhostError)
}
}
}