forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
106 lines (78 loc) · 2.09 KB
/
misc.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
package virtualbox
import (
"bufio"
"math/rand"
"os"
"time"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
)
// B2DUpdater describes the interactions with b2d.
type B2DUpdater interface {
UpdateISOCache(storePath, isoURL string) error
CopyIsoToMachineDir(storePath, machineName, isoURL string) error
}
func NewB2DUpdater() B2DUpdater {
return &b2dUtilsUpdater{}
}
type b2dUtilsUpdater struct{}
func (u *b2dUtilsUpdater) CopyIsoToMachineDir(storePath, machineName, isoURL string) error {
return mcnutils.NewB2dUtils(storePath).CopyIsoToMachineDir(isoURL, machineName)
}
func (u *b2dUtilsUpdater) UpdateISOCache(storePath, isoURL string) error {
return mcnutils.NewB2dUtils(storePath).UpdateISOCache(isoURL)
}
// SSHKeyGenerator describes the generation of ssh keys.
type SSHKeyGenerator interface {
Generate(path string) error
}
func NewSSHKeyGenerator() SSHKeyGenerator {
return &defaultSSHKeyGenerator{}
}
type defaultSSHKeyGenerator struct{}
func (g *defaultSSHKeyGenerator) Generate(path string) error {
return ssh.GenerateSSHKey(path)
}
// LogsReader describes the reading of VBox.log
type LogsReader interface {
Read(path string) ([]string, error)
}
func NewLogsReader() LogsReader {
return &vBoxLogsReader{}
}
type vBoxLogsReader struct{}
func (c *vBoxLogsReader) Read(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return []string{}, err
}
defer file.Close()
lines := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
// RandomInter returns random int values.
type RandomInter interface {
RandomInt(n int) int
}
func NewRandomInter() RandomInter {
return &defaultRandomInter{}
}
type defaultRandomInter struct{}
func (d *defaultRandomInter) RandomInt(n int) int {
return rand.Intn(n)
}
// Sleeper sleeps for given duration.
type Sleeper interface {
Sleep(d time.Duration)
}
func NewSleeper() Sleeper {
return &defaultSleeper{}
}
type defaultSleeper struct{}
func (s *defaultSleeper) Sleep(d time.Duration) {
time.Sleep(d)
}