-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtui-opts.go
348 lines (296 loc) · 8.28 KB
/
btui-opts.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
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"github.com/pterm/pterm"
"log"
"os/exec"
"os/user"
"io"
"io/ioutil"
"os"
"encoding/json"
)
type Server struct {
Ipaddr string `json:"ipaddr"`
Iface string `json:"iface"`
Available string `json:"available"`
Running string `json:"running"`
Name string `json:"name"`
Owner string `json:"owner"`
Type string `json:"type"`
}
type VM struct {
Servers []Server `json:"servers"`
}
func printHeader(h string) {
print("\033[H\033[2J")
header := pterm.DefaultHeader
header.BackgroundStyle = pterm.NewStyle(pterm.BgBlue)
header.WithFullWidth().Println(h)
pterm.Println() // Blank line
}
func goodbye() {
pterm.Println()
e := pterm.Success
e.Prefix.Text = "GoodBye!"
e.Printf("Please Log out of the system\n")
os.Exit(0)
}
func checkerr(err error) {
if err != nil {
log.Fatal(err)
}
}
func continueText() {
confirm := pterm.DefaultInteractiveContinue
confirm.DefaultText = "press <enter> to continue"
confirm.Options = []string{"ok"}
result, _ := confirm.Show()
pterm.Println(result)
}
func showContainers(vms VM) {
ptable := [][]string{}
// pterm.DefaultHeader.WithFullWidth().Println("Available containers:")
pterm.Println(pterm.BgBlue.Sprint("Available containers:"))
pterm.Println(pterm.Bold.Sprint("============================="))
// list json table
ptable = append(ptable, []string{"IP Address", "Name", "Owner", "Running", "Available", "Type"})
for _,v := range vms.Servers {
ptable = append(ptable, []string{v.Ipaddr, v.Name, v.Owner, v.Running, v.Available, v.Type})
}
pterm.DefaultTable.WithHasHeader().WithData(ptable).Render()
}
func serverAction(vms VM, a string) {
h := pterm.Sprintf("%s Server\n", a)
printHeader(h)
showContainers(vms)
servers := []string{}
cuser, err := user.Current()
checkerr(err)
for _, v := range vms.Servers {
if v.Available == "no" && v.Owner == cuser.Username {
servers = append(servers, v.Name)
}
}
if len(servers) < 1 {
pterm.Println("no servers for", cuser.Username)
continueText()
return
}
newselect := pterm.DefaultInteractiveSelect
newselect.MaxHeight = 7
newselect.DefaultText = pterm.Sprintf("Please select server for %s to %s", pterm.Green(cuser.Username), a)
servOpt, _ := newselect.WithOptions(servers).Show()
pterm.Info.Printfln("Do you want to %s %s? ", a, servOpt)
confirmB, _ := pterm.DefaultInteractiveConfirm.Show()
if !confirmB {
return
}
for i, v := range vms.Servers {
if v.Name == servOpt {
switch a {
case "start":
if v.Running == "yes" {
pterm.Warning.Println("Server is already running.")
continueText()
return
}
vms.Servers[i].Running = "yes"
case "stop":
if v.Running == "no" {
pterm.Warning.Println("Server is not running.")
continueText()
return
}
vms.Servers[i].Running = "no"
case "destroy":
if v.Running == "yes" {
pterm.Warning.Println("Server must be stopped before destroy")
continueText()
return
}
vms.Servers[i].Name = "none"
vms.Servers[i].Available = "yes"
vms.Servers[i].Type = "none"
vms.Servers[i].Owner = "none"
}
}
}
cmd := exec.Command("doas", "/root/bastille-scripts/action.csh", servOpt, a)
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
slurp, _ := io.ReadAll(stderr)
pterm.Info.Printf("%s\n", slurp)
if err := cmd.Wait(); err != nil {
pterm.Fatal.Println("Something went wrong...")
log.Fatal(err)
}
jsonOut, err := json.MarshalIndent(vms," "," ")
checkerr(err)
ioutil.WriteFile("/usr/local/etc/vms.json", jsonOut, 0666)
continueText()
}
func bastilleList() {
cmd := exec.Command("doas", "/root/bastille-scripts/action.csh", "all", "list")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
slurp, _ := io.ReadAll(stdout)
pterm.Info.Printf("%s\n", slurp)
if err := cmd.Wait(); err != nil {
pterm.Fatal.Println("Something went wrong...")
log.Fatal(err)
}
continueText()
}
func buildServer(vms VM) {
printHeader("Build Server")
showContainers(vms)
pterm.DefaultBasicText.Println("Please choose a server type to build")
// select options
var options []string
options = append(options, "Golang Server")
options = append(options, "R Server")
options = append(options, "Nginx Server")
options = append(options, "Python Dev")
options = append(options, "Rust Dev")
options = append(options, "Ruby Dev")
options = append(options, "Node Dev")
options = append(options, "Apache Server")
options = append(options, "Iperf Server")
selectedOption, _ := pterm.DefaultInteractiveSelect.WithOptions(options).Show()
pterm.Info.Printfln("Selected option: %s", pterm.Green(selectedOption))
sOpt := "none"
switch selectedOption {
case "Golang Server":
sOpt = "gdev"
case "R Server":
sOpt = "R-stat"
case "Nginx Server":
sOpt = "nginx"
case "Python Dev":
sOpt = "python"
case "Rust Dev":
sOpt = "rust"
case "Ruby Dev":
sOpt = "ruby"
case "Node Dev":
sOpt = "node"
case "Apache Server":
sOpt = "apache"
case "Iperf Server":
sOpt = "iperf"
}
// get ssh key
pterm.Println() // Blank line
pterm.DefaultBasicText.Println("Please paste in your ssh pub key")
result, _ := pterm.DefaultInteractiveTextInput.WithMultiLine(false).Show()
pterm.Println() // Blank line
pterm.Info.Printfln("You answered: %s", result)
result = result + "\n"
ioutil.WriteFile("/tmp/authkey", []byte(result), 0600)
// get name
pterm.Println() // Blank line
pterm.DefaultBasicText.Println("Enter name of your container")
cName, _ := pterm.DefaultInteractiveTextInput.WithMultiLine(false).Show()
pterm.Println() // Blank line
pterm.Info.Printfln("Continue building %s? \n", cName)
confirmB, _ := pterm.DefaultInteractiveConfirm.Show()
if !confirmB {
return
}
spinnerInfo, _ := pterm.DefaultSpinner.Start("Starting build. Please wait...")
cuser, err := user.Current()
checkerr(err)
// get available server
availContainer := Server{}
for i,v := range vms.Servers {
if v.Available == "yes" {
availContainer = Server{
Ipaddr: v.Ipaddr,
Name: cName,
Iface: v.Iface,
Running: "yes",
Available: "no",
Owner: cuser.Username,
Type: sOpt,
}
vms.Servers[i] = availContainer
break
}
}
// cmd example
// echo usage create.csh <jid> <int> <ipaddr> <type>
cmd := exec.Command("doas", "/root/bastille-scripts/create-jail.csh", availContainer.Name, availContainer.Iface, availContainer.Ipaddr, availContainer.Type)
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
slurp, _ := io.ReadAll(stderr)
pterm.Info.Printf("%s\n", slurp)
if err := cmd.Wait(); err != nil {
spinnerInfo.Fail("Something went wrong")
log.Fatal(err)
}
jsonOut, err := json.MarshalIndent(vms," "," ")
checkerr(err)
ioutil.WriteFile("/usr/local/etc/vms.json", jsonOut, 0666)
spinnerInfo.Success("Congratulations! You can now log in to your new servers as root to ", availContainer.Ipaddr)
continueText()
}
func main() {
for {
file, err := os.Open("/usr/local/etc/vms.json")
checkerr(err)
defer file.Close()
var vms VM
b, err := io.ReadAll(file)
checkerr(err)
err = json.Unmarshal(b, &vms)
checkerr(err)
printHeader("Welcome to Bastille interactive container tool")
// select action
messText := pterm.DefaultBasicText
messText.Println("Build and manage your own FreeBSD container from software templates.")
// select options
var actopts []string
actopts = append(actopts, "Build Server")
actopts = append(actopts, "Start Server")
actopts = append(actopts, "Stop Server")
actopts = append(actopts, "Destroy Server")
actopts = append(actopts, "List Servers")
actopts = append(actopts, "Bastille List Servers")
actopts = append(actopts, "Quit")
myselect := pterm.DefaultInteractiveSelect
myselect.MaxHeight = 7
selActopt, _ := myselect.WithOptions(actopts).Show()
switch selActopt {
case "Build Server":
buildServer(vms)
case "Start Server":
serverAction(vms, "start")
case "Stop Server":
serverAction(vms, "stop")
case "Destroy Server":
serverAction(vms, "destroy")
case "Bastille List Servers":
bastilleList()
case "List Servers":
showContainers(vms)
continueText()
case "Quit":
goodbye()
}
}
}