-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.go
90 lines (78 loc) · 2.81 KB
/
start.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
package start
import (
"errors"
"fmt"
"io/ioutil"
"naksu/log"
"naksu/mebroutines"
"naksu/ui/progress"
"naksu/xlate"
"os"
"path"
"regexp"
"strings"
)
// Server starts exam server by running vagrant
func Server() {
cleanUpTrashVMDirectories()
// chdir ~/ktp
if !mebroutines.ChdirVagrantDirectory() {
mebroutines.ShowErrorMessage("Could not change to vagrant directory ~/ktp")
return
}
// Start VM
progress.TranslateAndSetMessage("Starting Exam server. This takes a while.")
upRunParams := []string{"up"}
err := mebroutines.RunVagrant(upRunParams)
if err != nil {
if err.Error() == "macaddress/rtgetopt" {
mebroutines.ShowInfoMessage(xlate.Get("Server failed to start. This is typical in Windows after an update. Please try again to start the server."))
} else {
mebroutines.ShowErrorMessage(fmt.Sprintf(xlate.Get("Failed to execute %s: %v"), "vagrant up", err))
return
}
}
}
// cleanUpTrashVMDirectories tries to find and delete leftover VM directories that only contain one .vbox file and nothing else
func cleanUpTrashVMDirectories() {
defaultVMDirectory, err := virtualBoxDefaultVMDirectory()
if err != nil {
log.Debug(fmt.Sprintf("Error searching for trash VM directories (get default vm dir): %v", err))
return
}
entriesInDefaultVMDir, err := ioutil.ReadDir(defaultVMDirectory)
if err != nil {
log.Debug(fmt.Sprintf("Error searching for trash VM directories (list default vm dir %s): %v", defaultVMDirectory, err))
return
}
for _, entryInDefaultVMDirRoot := range entriesInDefaultVMDir {
if !entryInDefaultVMDirRoot.IsDir() {
continue
}
fullPathToPotentialTrashVMDir := path.Join(defaultVMDirectory, entryInDefaultVMDirRoot.Name())
entriesInSubDir, err := ioutil.ReadDir(fullPathToPotentialTrashVMDir)
if err != nil {
log.Debug(fmt.Sprintf("Error searching for trash VM directories (listing '%s'): %v", fullPathToPotentialTrashVMDir, err))
return
}
if len(entriesInSubDir) == 1 && !entriesInSubDir[0].IsDir() && strings.HasSuffix(entriesInSubDir[0].Name(), ".vbox") {
log.Debug(fmt.Sprintf("Removing trash VM dir %s", fullPathToPotentialTrashVMDir))
err := os.RemoveAll(fullPathToPotentialTrashVMDir)
if err != nil {
log.Debug(fmt.Sprintf("Error removing trash VM dir %s", fullPathToPotentialTrashVMDir))
}
}
}
}
func virtualBoxDefaultVMDirectory() (string, error) {
systemProperties, err := mebroutines.RunVBoxManage([]string{"list", "systemproperties"})
if err != nil {
log.Debug("Failing to list system properties is not a fatal error, continuing normally")
}
defaultVMDirectoryRE := regexp.MustCompile(`Default machine folder:\s+(\S.*)`)
result := defaultVMDirectoryRE.FindStringSubmatch(systemProperties)
if len(result) > 1 {
return strings.TrimSpace(result[1]), nil
}
return "", errors.New("failed to get defaultVMDirectory: no regex matches")
}