This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathpreflight.go
91 lines (84 loc) · 1.92 KB
/
preflight.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
package main
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/alecthomas/units"
"github.com/coreos/go-semver/semver"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
)
func preflightEnsureDockerVersion() error {
if *skipDockerVersion {
return nil
}
version, err := dockerVersion()
if err != nil {
return err
}
vA := semver.New(MinDockerVersion)
vB := semver.New(strings.TrimSpace(version))
if vB.Compare(*vA) == -1 {
return fmt.Errorf("Installed docker version %s is no longer supported", vB)
}
return nil
}
func preflightInHomePath(dir string) error {
// do not check if the directory is empty
if dir == "" {
return nil
}
homePath, err := homedir.Dir()
if err != nil {
return err
}
dir, err = filepath.Abs(dir)
if err != nil {
return err
}
if !strings.HasPrefix(dir, homePath) {
return fmt.Errorf("work directory %s should be below your home directory %s;\nthis is required to be accessible by Docker", dir, homePath)
}
return nil
}
// Preflight perform preflight checks
func Preflight(dir string) (string, error) {
info, err := dockerInfo()
if err != nil {
return "", err
}
err = preflightDockerMemory(info)
if err != nil {
return "", err
}
err = preflightEnsureDockerVersion()
if err != nil {
return "", err
}
err = preflightInHomePath(dir)
if err != nil {
return "", err
}
return info, nil
}
// preflightDockerMemory checks docker memory
func preflightDockerMemory(info string) error {
var search = regexp.MustCompile(`Total Memory: (.*)`)
result := search.FindString(string(info))
if result == "" {
return fmt.Errorf("Docker is not running")
}
mem := strings.Split(result, ":")
memory := strings.TrimSpace(mem[1])
n, err := units.ParseStrictBytes(memory)
if err != nil {
return err
}
log.Debug("mem:", n)
//fmt.Println(n)
if n <= int64(MinDockerMem) {
return fmt.Errorf("IOSDK needs 4GB memory allocatable on docker")
}
return nil
}